static void test_yes(void) { assert_true(path_starts_with("/home/trash", "/home/trash")); assert_true(path_starts_with("/home/trash/", "/home/trash")); assert_true(path_starts_with("/home/trash/", "/home/trash/")); assert_true(path_starts_with("/home/trash", "/home/trash/")); }
/* Removes entries that belong to specified trash directory. Removes all if * trash_dir is NULL. */ static void remove_trash_entries(const char trash_dir[]) { int i; int j = 0; for(i = 0; i < nentries; ++i) { if(trash_dir == NULL || path_starts_with(trash_list[i].trash_name, trash_dir)) { free(trash_list[i].path); free(trash_list[i].trash_name); continue; } trash_list[j++] = trash_list[i]; } nentries = j; if(nentries == 0) { free(trash_list); trash_list = NULL; } }
/* Moves file/directory overwriting destination files if requested. Returns * non-zero on error, otherwise zero is returned. */ static int op_mv(void *data, const char src[], const char dst[], int overwrite) { #ifndef _WIN32 struct stat st; char *escaped_src, *escaped_dst; char cmd[6 + PATH_MAX*2 + 1]; int result; if(lstat(dst, &st) == 0) return -1; escaped_src = escape_filename(src, 0); escaped_dst = escape_filename(dst, 0); if(escaped_src == NULL || escaped_dst == NULL) { free(escaped_dst); free(escaped_src); return -1; } snprintf(cmd, sizeof(cmd), "mv %s %s %s", overwrite ? "" : NO_CLOBBER, escaped_src, escaped_dst); free(escaped_dst); free(escaped_src); LOG_INFO_MSG("Running mv command: \"%s\"", cmd); if((result = background_and_wait_for_errors(cmd)) != 0) return result; if(path_starts_with(dst, cfg.trash_dir)) add_to_trash(src, strrchr(dst, '/') + 1); else if(path_starts_with(src, cfg.trash_dir)) remove_from_trash(strrchr(src, '/') + 1); return 0; #else BOOL ret = MoveFile(src, dst); if(!ret && GetLastError() == 5) { int r = op_cp(data, src, dst, overwrite); if(r != 0) return r; return op_removesl(data, src, NULL); } return ret == 0; #endif }
void test_path_starts_with() { assert(path_starts_with("/", "/bar/11") > 0); assert(path_starts_with("/", "/") > 0); assert(path_starts_with("/bar", "/bar/11") > 0); assert(path_starts_with("/bar", "/bar/") > 0); assert(path_starts_with("/bar", "/bar") > 0); assert(path_starts_with("/bar", "/bar1") == 0); assert(path_starts_with("/bar/", "/bar/11") > 0); }
/* Counts number of pane tabs from the collection that are inside specified * path. When view parameter isn't NULL, it's a possible replacement view to * use instead of the stored one. Returns the count. */ static int count_pane_visitors(const pane_tabs_t *ptabs, const char path[], const view_t *view) { int count = 0; int i; for(i = 0; i < (int)DA_SIZE(ptabs->tabs); ++i) { const pane_tab_t *const ptab = &ptabs->tabs[i]; const view_t *const v = (view != NULL && i == ptabs->current) ? view : &ptab->view; if(path_starts_with(v->curr_dir, path)) { ++count; } } return count; }
static void test_no(void) { assert_false(path_starts_with("/home/tras", "/home/trash")); assert_false(path_starts_with("/home/trash_", "/home/trash")); }
int is_on_slow_fs(const char full_path[]) { char fs_name[PATH_MAX]; get_mount_point_traverser_state state = { .type = MI_FS_TYPE, .path = full_path, .buf_len = sizeof(fs_name), .buf = fs_name, .curr_len = 0UL, }; /* Empty list optimization. */ if(cfg.slow_fs_list[0] == '\0') { return 0; } if(traverse_mount_points(&get_mount_info_traverser, &state) == 0) { if(state.curr_len > 0) { return begins_with_list_item(fs_name, cfg.slow_fs_list); } } return 0; } int get_mount_point(const char path[], size_t buf_len, char buf[]) { get_mount_point_traverser_state state = { .type = MI_MOUNT_POINT, .path = path, .buf_len = buf_len, .buf = buf, .curr_len = 0UL, }; return traverse_mount_points(&get_mount_info_traverser, &state); } /* traverse_mount_points client that gets mount point info for a given path. */ static int get_mount_info_traverser(struct mntent *entry, void *arg) { get_mount_point_traverser_state *const state = arg; if(path_starts_with(state->path, entry->mnt_dir)) { const size_t new_len = strlen(entry->mnt_dir); if(new_len > state->curr_len) { state->curr_len = new_len; switch(state->type) { case MI_MOUNT_POINT: copy_str(state->buf, state->buf_len, entry->mnt_dir); break; case MI_FS_TYPE: copy_str(state->buf, state->buf_len, entry->mnt_type); break; default: assert(0 && "Unknown mount information type."); break; } } } return 0; }