int caching_list_directory(const char *path, dir_entry **list) { pthread_mutex_lock(&dmut); if (!strcmp(path, "/")) path = ""; dir_cache *cw; for (cw = dcache; cw; cw = cw->next) if (!strcmp(cw->path, path)) break; if (!cw) { if (!list_directory(path, list)) return 0; cw = new_cache(path); } else if (cache_timeout > 0 && (time(NULL) - cw->cached > cache_timeout)) { if (!list_directory(path, list)) return 0; free_dir_list(cw->entries); cw->cached = time(NULL); } else *list = cw->entries; cw->entries = *list; pthread_mutex_unlock(&dmut); return 1; }
int main(void) { atexit(cleanup); system("rm -fr DIR"); /* simple case */ xmkdir("DIR"); xmkdir("DIR/a"); xmkdir("DIR/a/b"); xmkdir("DIR/c"); xmkdir("DIR/d"); touch("DIR/0"); touch("DIR/a/1"); touch("DIR/a/b/2"); touch("DIR/c/3"); touch("DIR/c/4"); /* DIR/d is empty */ struct Expected expected[] = { {0, "c/4"}, {0, "c/3"}, {0, "a/b/2"}, {0, "a/1"}, {0, "0"}, }; struct DirListEntry * list = make_recursive_dir_list("DIR"); verify(expected, list, sizeof(expected) / sizeof(expected[0])); free_dir_list(list); exit(EXIT_SUCCESS); }
static void dir_decache(const char *path) { dir_cache *cw; pthread_mutex_lock(&dmut); dir_entry *de, *tmpde; char dir[MAX_PATH_SIZE]; dir_for(path, dir); for (cw = dcache; cw; cw = cw->next) { if (!strcmp(cw->path, path)) { if (cw == dcache) dcache = cw->next; if (cw->prev) cw->prev->next = cw->next; if (cw->next) cw->next->prev = cw->prev; free_dir_list(cw->entries); free(cw->path); free(cw); } else if (cw->entries && !strcmp(dir, cw->path)) { if (!strcmp(cw->entries->full_name, path)) { de = cw->entries; cw->entries = de->next; de->next = NULL; free_dir_list(de); } else for (de = cw->entries; de->next; de = de->next) { if (!strcmp(de->next->full_name, path)) { tmpde = de->next; de->next = de->next->next; tmpde->next = NULL; free_dir_list(tmpde); break; } } } } pthread_mutex_unlock(&dmut); }
int heap_dispose (void) { mem_strfree (&path); if (file_list) { free_dir_list (file_list); file_list = NULL; file_info = NULL; } return HEAP_OK; }
int curses_ui() { int err; err = ui_init(); if (err == -1) { return (-1); } file_list.dir_name = malloc(MAXPATHLEN + 1); if (!file_list.dir_name) { mvwprintw(main_win, 0, 1, "ERROR: Can't initialize dir_name."); wrefresh(main_win); return (-1); } err = first_run_file_list(main_win); if (err == -1) { mvwprintw(main_win, 0, 1, "ERROR: Can't initialize file list."); wrefresh(main_win); return (-1); } sock_fd = get_client_socket(); err = pthread_create(&receiver_thread, rcv_attr, ui_socket_receiver, rcv_arg); if (err != 0) { mvwprintw(main_win, 0, 1, "ERROR: receiver thread"); wrefresh(main_win); return (-1); } show_files(main_win); curses_loop(); // wait for receiver_thread if alive if (pthread_kill(receiver_thread, 0) == 0) { printw("UI: waiting for receiver_thread.."); refresh(); pthread_join(receiver_thread, NULL); } free_dir_list(); free(file_list.dir_name); ui_cleanup(); return (0); }
int heap_recover ( const char *heappath) { int rc = HEAP_OK; if (file_exists (heappath)) { if (!file_is_directory (heappath)) rc = HEAP_INVALID_PATH; } else { if (make_dir (heappath) != 0) rc = HEAP_CANNOT_CREATE_PATH; } if (! rc) { path = mem_strdup (heappath); if (!path) rc = HEAP_MEMORY_ERROR; } if (! rc) { file_list = load_dir_list (path, "t"); if (!file_list) rc = HEAP_MEMORY_ERROR; } for (file_info = (FILEINFO *) file_list-> next; file_info != (FILEINFO *) file_list; file_info = file_info-> next) { rc = file_recover (file_info); if (rc) break; } free_dir_list (file_list); return rc; }
int change_directory(char *dir) { int err, y, x; if (chdir(dir) == -1) { mvwprintw(status_win, 3, 1, "can't change dir: %s", dir); return (-1); } if (getcwd(file_list.dir_name, MAXPATHLEN) == 0) { mvwprintw(status_win, 3, 1, "can't get current directory"); return (-1); } // reset cursor/file position after changing directory file_list.cur_idx = 0; // clean up old list free_dir_list(); // allocate new list init_list_for_dir(); //populate list with file/directory names err = scan_dir(file_list.contents, false, false); if (err == -1) { mvwprintw(status_win, 1, 1, "ERROR in change_directory()"); wrefresh(status_win); return (-1); } getmaxyx(main_win, y, x); file_list.head_idx = 0; file_list.tail_idx = y - 3; file_list.cur_idx = 0; show_files(main_win); return (0); }
int heap_next (char **key, DESCR **data) { int rc = HEAP_OK; char *file_name = NULL, *temp_key; ASSERT (key); if ((! file_list) || (! file_info)) return HEAP_DATA_NOT_FOUND; if (file_info == (FILEINFO *) file_list) { free_dir_list (file_list); file_list = NULL; file_info = NULL; return HEAP_DATA_NOT_FOUND; } file_name = file_info-> dir. file_name; file_info = file_info-> next; temp_key = mem_strdup (file_name); rc = heap_get (temp_key, data); if (rc) mem_free (temp_key); else *key = temp_key; return rc; }
void jscoverage_instrument(const char * source, const char * destination, int verbose, char ** exclude, int num_exclude, char ** no_instrument, int num_no_instrument) { assert(source != NULL); assert(destination != NULL); g_verbose = verbose; /* check if they are the same */ check_same_file(source, destination); /* check if source directory is an ancestor of destination directory */ check_contains_file(source, destination); /* check that the source exists and is a directory */ struct stat buf; xstat(source, &buf); if (! S_ISDIR(buf.st_mode)) { fatal("not a directory: %s", source); } /* if the destination directory exists, check that it is a jscoverage directory */ if (stat(destination, &buf) == 0) { /* it exists */ if (! S_ISDIR(buf.st_mode)) { fatal("not a directory: %s", destination); } if (! directory_is_empty(destination)) { char * expected_file = NULL; if (jscoverage_mode == JSCOVERAGE_MOZILLA) { char * modules_directory = make_path(destination, "modules"); expected_file = make_path(modules_directory, "jscoverage.jsm"); free(modules_directory); } else { expected_file = make_path(destination, "jscoverage.html"); } if (stat(expected_file, &buf) == -1) { fatal("refusing to overwrite directory: %s", destination); } free(expected_file); } } else if (errno == ENOENT) { xmkdir(destination); } else { fatal("cannot stat directory: %s", destination); } /* copy the resources */ if (jscoverage_mode == JSCOVERAGE_MOZILLA) { char * chrome_directory = make_path(destination, "chrome"); char * jscoverage_chrome_directory = make_path(chrome_directory, "jscoverage"); mkdirs(jscoverage_chrome_directory); copy_resource("jscoverage.manifest", chrome_directory); copy_resource("jscoverage.html", jscoverage_chrome_directory); copy_resource("jscoverage.css", jscoverage_chrome_directory); copy_resource("jscoverage.js", jscoverage_chrome_directory); copy_resource("jscoverage-throbber.gif", jscoverage_chrome_directory); copy_resource("jscoverage-highlight.css", jscoverage_chrome_directory); copy_resource("jscoverage.xul", jscoverage_chrome_directory); copy_resource("jscoverage-overlay.js", jscoverage_chrome_directory); free(jscoverage_chrome_directory); free(chrome_directory); char * modules_directory = make_path(destination, "modules"); mkdirs(modules_directory); copy_resource("jscoverage.jsm", modules_directory); free(modules_directory); } else { jscoverage_copy_resources(destination); } /* finally: copy the directory */ struct DirListEntry * list = make_recursive_dir_list(source); for (struct DirListEntry * p = list; p != NULL; p = p->next) { char * s = make_path(source, p->name); char * d = make_path(destination, p->name); /* check if it's on the exclude list */ for (int i = 0; i < num_exclude; i++) { char * x = make_path(source, exclude[i]); if (is_same_file(x, s) || contains_file(x, s)) { free(x); goto cleanup; } free(x); } char * dd = make_dirname(d); mkdirs(dd); free(dd); int instrument_this = 1; /* check if it's on the no-instrument list */ for (int i = 0; i < num_no_instrument; i++) { char * ni = make_path(source, no_instrument[i]); if (is_same_file(ni, s) || contains_file(ni, s)) { instrument_this = 0; } free(ni); } instrument_file(s, d, p->name, instrument_this); cleanup: free(s); free(d); } free_dir_list(list); }