/** * Return a list of all files and folders in given path. * * def listdir(path) */ static mp_obj_t os_listdir(mp_uint_t n_args, const mp_obj_t *args_p) { struct fs_dir_t dir; struct fs_dir_entry_t entry; mp_obj_t entries; mp_obj_t name; const char *path_p; if (n_args == 0) { path_p = ""; } else { path_p = mp_obj_str_get_str(args_p[0]); } if (fs_dir_open(&dir, path_p, O_READ) != 0) { nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "No such file or directory: '%s'", path_p)); } entries = mp_obj_new_list(0, NULL); while (fs_dir_read(&dir, &entry) == 1) { name = mp_obj_new_str(&entry.name[0], strlen(entry.name), false); mp_obj_list_append(entries, name); } fs_dir_close(&dir); return (entries); }
static bool pak_insert_all(pak_file_t *pak, const char *dir) { DIR *dp; struct dirent *dirp; if (!(pak->insert)) return false; if (!(dp = fs_dir_open(dir))) return false; while ((dirp = fs_dir_read(dp))) { if (!(pak_insert_one(pak, dirp->d_name))) { fs_dir_close(dp); return false; } } fs_dir_close(dp); return true; }
/* * Task precleanup removes any existing temporary files or log files * left behind from a previous invoke of the test-suite. */ static void task_precleanup(const char *curdir) { fs_dir_t *dir; fs_dirent_t *files; char buffer[4096]; dir = fs_dir_open(curdir); while ((files = fs_dir_read(dir))) { if (strstr(files->d_name, "TMP") || strstr(files->d_name, ".stdout") || strstr(files->d_name, ".stderr") || strstr(files->d_name, ".dat")) { util_snprintf(buffer, sizeof(buffer), "%s/%s", curdir, files->d_name); if (remove(buffer)) con_err("error removing temporary file: %s\n", buffer); } } fs_dir_close(dir); }
/* * Read a directory and searches for all template files in it * which is later used to run all tests. */ static bool task_propagate(const char *curdir, size_t *pad, const char *defs) { bool success = true; fs_dir_t *dir; fs_dirent_t *files; struct stat directory; char buffer[4096]; size_t found = 0; char **directories = NULL; char *claim = util_strdup(curdir); size_t i; vec_push(directories, claim); dir = fs_dir_open(claim); /* * Generate a list of subdirectories since we'll be checking them too * for tmpl files. */ while ((files = fs_dir_read(dir))) { util_asprintf(&claim, "%s/%s", curdir, files->d_name); if (stat(claim, &directory) == -1) { fs_dir_close(dir); mem_d(claim); return false; } if (S_ISDIR(directory.st_mode) && files->d_name[0] != '.') { vec_push(directories, claim); } else { mem_d(claim); claim = NULL; } } fs_dir_close(dir); /* * Now do all the work, by touching all the directories inside * test as well and compile the task templates into data we can * use to run the tests. */ for (i = 0; i < vec_size(directories); i++) { dir = fs_dir_open(directories[i]); while ((files = fs_dir_read(dir))) { util_snprintf(buffer, sizeof(buffer), "%s/%s", directories[i], files->d_name); if (stat(buffer, &directory) == -1) { con_err("internal error: stat failed, aborting\n"); abort(); } if (S_ISDIR(directory.st_mode)) continue; /* * We made it here, which concludes the file/directory is not * actually a directory, so it must be a file :) */ if (strcmp(files->d_name + strlen(files->d_name) - 5, ".tmpl") == 0) { task_template_t *tmpl = task_template_compile(files->d_name, directories[i], pad); char buf[4096]; /* one page should be enough */ const char *qcflags = NULL; task_t task; found ++; if (!tmpl) { con_err("error compiling task template: %s\n", files->d_name); success = false; continue; } /* * Generate a temportary file name for the output binary * so we don't trample over an existing one. */ tmpl->tempfilename = NULL; util_asprintf(&tmpl->tempfilename, "%s/TMPDAT.%s.dat", directories[i], files->d_name); /* * Additional QCFLAGS enviroment variable may be used * to test compile flags for all tests. This needs to be * BEFORE other flags (so that the .tmpl can override them) */ qcflags = platform_getenv("QCFLAGS"); /* * Generate the command required to open a pipe to a process * which will be refered to with a handle in the task for * reading the data from the pipe. */ if (strcmp(tmpl->proceduretype, "-pp")) { if (qcflags) { if (tmpl->testflags && !strcmp(tmpl->testflags, "-no-defs")) { util_snprintf(buf, sizeof(buf), "%s %s/%s %s %s -o %s", task_bins[TASK_COMPILE], directories[i], tmpl->sourcefile, qcflags, tmpl->compileflags, tmpl->tempfilename ); } else { util_snprintf(buf, sizeof(buf), "%s %s/%s %s/%s %s %s -o %s", task_bins[TASK_COMPILE], curdir, defs, directories[i], tmpl->sourcefile, qcflags, tmpl->compileflags, tmpl->tempfilename ); } } else { if (tmpl->testflags && !strcmp(tmpl->testflags, "-no-defs")) { util_snprintf(buf, sizeof(buf), "%s %s/%s %s -o %s", task_bins[TASK_COMPILE], directories[i], tmpl->sourcefile, tmpl->compileflags, tmpl->tempfilename ); } else { util_snprintf(buf, sizeof(buf), "%s %s/%s %s/%s %s -o %s", task_bins[TASK_COMPILE], curdir, defs, directories[i], tmpl->sourcefile, tmpl->compileflags, tmpl->tempfilename ); } } } else { /* Preprocessing (qcflags mean shit all here we don't allow them) */ if (tmpl->testflags && !strcmp(tmpl->testflags, "-no-defs")) { util_snprintf(buf, sizeof(buf), "%s -E %s/%s -o %s", task_bins[TASK_COMPILE], directories[i], tmpl->sourcefile, tmpl->tempfilename ); } else { util_snprintf(buf, sizeof(buf), "%s -E %s/%s %s/%s -o %s", task_bins[TASK_COMPILE], curdir, defs, directories[i], tmpl->sourcefile, tmpl->tempfilename ); } } /* * The task template was compiled, now lets create a task from * the template data which has now been propagated. */ task.tmpl = tmpl; if (!(task.runhandles = task_popen(buf, "r"))) { con_err("error opening pipe to process for test: %s\n", tmpl->description); success = false; continue; } /* * Open up some file desciptors for logging the stdout/stderr * to our own. */ util_snprintf(buf, sizeof(buf), "%s.stdout", tmpl->tempfilename); task.stdoutlogfile = util_strdup(buf); if (!(task.stdoutlog = fs_file_open(buf, "w"))) { con_err("error opening %s for stdout\n", buf); continue; } util_snprintf(buf, sizeof(buf), "%s.stderr", tmpl->tempfilename); task.stderrlogfile = util_strdup(buf); if (!(task.stderrlog = fs_file_open(buf, "w"))) { con_err("error opening %s for stderr\n", buf); continue; } vec_push(task_tasks, task); } } fs_dir_close(dir); mem_d(directories[i]); /* free claimed memory */ } vec_free(directories); return success; }