/* Skip first line - should be #!/bin/bash Shebang */ if (io__get_char(&io) != '#') goto err_out; if (io__get_char(&io) != '!') goto err_out; do {
ch = io__get_char(&io); if (ch < 0) goto err_out;
} while (ch != '\n');
do {
ch = io__get_char(&io); if (ch < 0) goto err_out;
} while (ch == '#' || isspace(ch)); while (ch > 0 && ch != '\n') {
desc[pos++] = ch; if (pos >= (int)sizeof(desc) - 1) break;
ch = io__get_char(&io);
} while (pos > 0 && isspace(desc[--pos]))
;
desc[++pos] = '\0';
close(io.fd); return strdup(desc);
err_out:
close(io.fd); return NULL;
}
/* Is this full file path a shell script */ staticbool is_shell_script(int dir_fd, constchar *path)
{ constchar *ext;
ext = strrchr(path, '.'); if (!ext) returnfalse; if (!strcmp(ext, ".sh")) { /* Has .sh extension */ if (faccessat(dir_fd, path, R_OK | X_OK, 0) == 0) /* Is executable */ returntrue;
} returnfalse;
}
/* Is this file in this dir a shell script (for test purposes) */ staticbool is_test_script(int dir_fd, constchar *name)
{ return is_shell_script(dir_fd, name);
}
/* Duplicate a string and fall over and die if we run out of memory */ staticchar *strdup_check(constchar *str)
{ char *newstr;
newstr = strdup(str); if (!newstr) {
pr_err("Out of memory while duplicating test script string\n");
abort();
} return newstr;
}
staticint shell_test__run(struct test_suite *test, int subtest __maybe_unused)
{ constchar *file = test->priv; int err; char *cmd = NULL;
snprintf(link, sizeof(link), "/proc/%d/fd/%d", getpid(), dir_fd);
len = readlink(link, filename, sizeof(filename)); if (len < 0) {
pr_err("Failed to readlink %s", link); return;
}
filename[len++] = '/';
strcpy(&filename[len], name);
tests = calloc(2, sizeof(*tests)); if (!tests) {
pr_err("Out of memory while building script test suite list\n"); return;
}
tests[0].name = strdup_check(name);
exclusive = strstr(desc, " (exclusive)"); if (exclusive != NULL) {
tests[0].exclusive = true;
exclusive[0] = '\0';
}
tests[0].desc = strdup_check(desc);
tests[0].run_case = shell_test__run;
test_suite = zalloc(sizeof(*test_suite)); if (!test_suite) {
pr_err("Out of memory while building script test suite list\n");
free(tests); return;
}
test_suite->desc = desc;
test_suite->test_cases = tests;
test_suite->priv = strdup_check(filename); /* Realloc is good enough, though we could realloc by chunks, not that
* anyone will ever measure performance here */
result_tmp = realloc(*result, (*result_sz + 1) * sizeof(*result_tmp)); if (result_tmp == NULL) {
pr_err("Out of memory while building script test suite list\n");
free(tests);
free(test_suite); return;
} /* Add file to end and NULL terminate the struct array */
*result = result_tmp;
(*result)[*result_sz] = test_suite;
(*result_sz)++;
}
/* List files, sorted by alpha */
n_dirs = scandirat(dir_fd, ".", &entlist, NULL, alphasort); if (n_dirs == -1) return; for (i = 0; i < n_dirs && (ent = entlist[i]); i++) { int fd;
if (ent->d_name[0] == '.') continue; /* Skip hidden files */ if (is_test_script(dir_fd, ent->d_name)) { /* It's a test */ char *desc = shell_test__description(dir_fd, ent->d_name);
if (desc) /* It has a desc line - valid script */
append_script(dir_fd, ent->d_name, desc, result, result_sz); continue;
} if (ent->d_type != DT_DIR) { struct stat st;
if (ent->d_type != DT_UNKNOWN) continue;
fstatat(dir_fd, ent->d_name, &st, 0); if (!S_ISDIR(st.st_mode)) continue;
} if (strncmp(ent->d_name, "base_", 5) == 0) continue; /* Skip scripts that have a separate driver. */
fd = openat(dir_fd, ent->d_name, O_PATH);
append_scripts_in_dir(fd, result, result_sz);
close(fd);
} for (i = 0; i < n_dirs; i++) /* Clean up */
zfree(&entlist[i]);
free(entlist);
}
struct test_suite **create_script_test_suites(void)
{ struct test_suite **result = NULL, **result_tmp;
size_t result_sz = 0; int dir_fd = shell_tests__dir_fd(); /* Walk dir */
/* * Append scripts if fd is good, otherwise return a NULL terminated zero * length array.
*/ if (dir_fd >= 0)
append_scripts_in_dir(dir_fd, &result, &result_sz);
result_tmp = realloc(result, (result_sz + 1) * sizeof(*result_tmp)); if (result_tmp == NULL) {
pr_err("Out of memory while building script test suite list\n");
abort();
} /* NULL terminate the test suite array. */
result = result_tmp;
result[result_sz] = NULL; if (dir_fd >= 0)
close(dir_fd); return result;
}
Messung V0.5
¤ Dauer der Verarbeitung: 0.22 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.