From e51c9e50fe0a99b9f89bab32cdb4efe279e3f6ae Mon Sep 17 00:00:00 2001 From: Slava Zanko Date: Mon, 18 Jul 2011 21:07:39 +0300 Subject: [PATCH] Changed interface of function mc_opendir() ...to handle vfs_path_t object as parameter. Signed-off-by: Slava Zanko --- lib/vfs/interface.c | 8 +------- lib/vfs/path.c | 25 +++++++++++++++++++++++++ lib/vfs/path.h | 1 + lib/vfs/vfs.h | 2 +- lib/widget/input_complete.c | 15 ++++++++++----- src/filemanager/dir.c | 25 +++++++++++++++++-------- src/filemanager/file.c | 13 +++++++++---- src/filemanager/find.c | 2 +- src/filemanager/tree.c | 15 +++++++-------- src/filemanager/treestore.c | 17 ++++++++++++----- src/filemanager/treestore.h | 2 +- 11 files changed, 85 insertions(+), 40 deletions(-) diff --git a/lib/vfs/interface.c b/lib/vfs/interface.c index c41cf29e8..5a1fdd501 100644 --- a/lib/vfs/interface.c +++ b/lib/vfs/interface.c @@ -428,15 +428,12 @@ mc_close (int handle) /* --------------------------------------------------------------------------------------------- */ DIR * -mc_opendir (const char *dirname) +mc_opendir (const vfs_path_t * vpath) { int handle, *handlep; void *info; - vfs_path_t *vpath; vfs_path_element_t *path_element; - vpath = vfs_path_from_str (dirname); - if (vpath == NULL) return NULL; @@ -445,7 +442,6 @@ mc_opendir (const char *dirname) if (!vfs_path_element_valid (path_element)) { errno = E_NOTSUPP; - vfs_path_free (vpath); return NULL; } @@ -454,7 +450,6 @@ mc_opendir (const char *dirname) if (info == NULL) { errno = path_element->class->opendir ? vfs_ferrno (path_element->class) : E_NOTSUPP; - vfs_path_free (vpath); return NULL; } @@ -470,7 +465,6 @@ mc_opendir (const char *dirname) handlep = g_new (int, 1); *handlep = handle; - vfs_path_free (vpath); return (DIR *) handlep; } diff --git a/lib/vfs/path.c b/lib/vfs/path.c index f5c3b438f..3e9fd9d38 100644 --- a/lib/vfs/path.c +++ b/lib/vfs/path.c @@ -1070,3 +1070,28 @@ vfs_path_build_filename (const char *first_element, ...) } /* --------------------------------------------------------------------------------------------- */ + +vfs_path_t * +vfs_path_append_new (const vfs_path_t * vpath, const char *first_element, ...) +{ + va_list args; + char *str_path, *result_str; + vfs_path_t *ret_vpath; + + if (vpath == NULL || first_element == NULL) + return NULL; + + va_start (args, first_element); + str_path = mc_build_filenamev (first_element, args); + va_end (args); + + result_str = vfs_path_to_str (vpath); + ret_vpath = vfs_path_build_filename (result_str, str_path, NULL); + g_free (result_str); + g_free (str_path); + + return ret_vpath; + +} + +/* --------------------------------------------------------------------------------------------- */ diff --git a/lib/vfs/path.h b/lib/vfs/path.h index e911849f4..d7efb6124 100644 --- a/lib/vfs/path.h +++ b/lib/vfs/path.h @@ -58,6 +58,7 @@ char *vfs_path_to_str_elements_count (const vfs_path_t * path, int elements_coun vfs_path_t *vfs_path_from_str (const char *path_str); vfs_path_t *vfs_path_from_str_flags (const char *path_str, vfs_path_flag_t flags); vfs_path_t *vfs_path_build_filename (const char *first_element, ...); +vfs_path_t *vfs_path_append_new (const vfs_path_t *, const char *first_element, ...); vfs_path_element_t *vfs_path_get_by_index (const vfs_path_t * path, int element_index); vfs_path_element_t *vfs_path_element_clone (const vfs_path_element_t * element); diff --git a/lib/vfs/vfs.h b/lib/vfs/vfs.h index fb6801f54..08a499187 100644 --- a/lib/vfs/vfs.h +++ b/lib/vfs/vfs.h @@ -281,7 +281,7 @@ int mc_utime (const char *path, struct utimbuf *times); int mc_readlink (const char *path, char *buf, size_t bufsiz); int mc_close (int handle); off_t mc_lseek (int fd, off_t offset, int whence); -DIR *mc_opendir (const char *dirname); +DIR *mc_opendir (const vfs_path_t * vpath); struct dirent *mc_readdir (DIR * dirp); int mc_closedir (DIR * dir); int mc_stat (const vfs_path_t * vpath, struct stat *buf); diff --git a/lib/widget/input_complete.c b/lib/widget/input_complete.c index ffbeafb74..12043c200 100644 --- a/lib/widget/input_complete.c +++ b/lib/widget/input_complete.c @@ -126,6 +126,7 @@ filename_completion_function (const char *text, int state, input_complete_t flag static char *users_dirname = NULL; static size_t filename_len; int isdir = 1, isexec = 0; + static vfs_path_t *dirname_vpath = NULL; struct dirent *entry = NULL; @@ -156,6 +157,7 @@ filename_completion_function (const char *text, int state, input_complete_t flag g_free (dirname); g_free (filename); g_free (users_dirname); + vfs_path_free (dirname_vpath); if ((*text != '\0') && (temp = strrchr (text, PATH_SEP)) != NULL) { @@ -167,6 +169,7 @@ filename_completion_function (const char *text, int state, input_complete_t flag dirname = g_strdup ("."); filename = g_strdup (text); } + dirname_vpath = vfs_path_from_str (dirname); /* We aren't done yet. We also support the "~user" syntax. */ @@ -179,7 +182,7 @@ filename_completion_function (const char *text, int state, input_complete_t flag and `command`. Maybe a dream - UNIMPLEMENTED yet. */ - directory = mc_opendir (dirname); + directory = mc_opendir (dirname_vpath); filename_len = strlen (filename); } @@ -209,13 +212,13 @@ filename_completion_function (const char *text, int state, input_complete_t flag isdir = 1; isexec = 0; { - vfs_path_t *vpath; struct stat tempstat; + vfs_path_t *tmp_vpath; - vpath = vfs_path_build_filename (dirname, entry->d_name, (char *) NULL); + tmp_vpath = vfs_path_build_filename (dirname, entry->d_name, (char *) NULL); /* Unix version */ - if (mc_stat (vpath, &tempstat) == 0) + if (mc_stat (tmp_vpath, &tempstat) == 0) { uid_t my_uid = getuid (); gid_t my_gid = getgid (); @@ -235,7 +238,7 @@ filename_completion_function (const char *text, int state, input_complete_t flag /* stat failed, strange. not a dir in any case */ isdir = 0; } - vfs_path_free (vpath); + vfs_path_free (tmp_vpath); } if ((flags & INPUT_COMPLETE_COMMANDS) && (isexec || isdir)) break; @@ -254,6 +257,8 @@ filename_completion_function (const char *text, int state, input_complete_t flag } g_free (dirname); dirname = NULL; + vfs_path_free (dirname_vpath); + dirname_vpath = NULL; g_free (filename); filename = NULL; g_free (users_dirname); diff --git a/src/filemanager/dir.c b/src/filemanager/dir.c index 89cededc2..b63c5c48e 100644 --- a/src/filemanager/dir.c +++ b/src/filemanager/dir.c @@ -535,20 +535,22 @@ do_load_dir (const char *path, dir_list * list, sortfn * sort, gboolean lc_rever int status, link_to_dir, stale_link; int next_free = 0; struct stat st; + vfs_path_t *vpath; /* ".." (if any) must be the first entry in the list */ if (!set_zero_dir (list)) return next_free; + vpath = vfs_path_from_str (path); if (get_dotdot_dir_stat (path, &st)) list->list[next_free].st = st; next_free++; - dirp = mc_opendir (path); - if (!dirp) + dirp = mc_opendir (vpath); + if (dirp == NULL) { message (D_ERROR, MSG_ERROR, _("Cannot read directory contents")); - return next_free; + goto ret; } tree_store_start_check (path); @@ -566,7 +568,7 @@ do_load_dir (const char *path, dir_list * list, sortfn * sort, gboolean lc_rever { tree_store_end_check (); mc_closedir (dirp); - return next_free; + goto ret; } list->list[next_free].fnamelen = NLENGTH (dp); list->list[next_free].fname = g_strndup (dp->d_name, list->list[next_free].fnamelen); @@ -588,6 +590,8 @@ do_load_dir (const char *path, dir_list * list, sortfn * sort, gboolean lc_rever mc_closedir (dirp); tree_store_end_check (); + ret: + vfs_path_free (vpath); return next_free; } @@ -623,12 +627,15 @@ do_reload_dir (const char *path, dir_list * list, sortfn * sort, int count, struct stat st; int marked_cnt; GHashTable *marked_files; + vfs_path_t *vpath; - dirp = mc_opendir (path); - if (!dirp) + vpath = vfs_path_from_str (path); + dirp = mc_opendir (vpath); + if (dirp == NULL) { message (D_ERROR, MSG_ERROR, _("Cannot read directory contents")); clean_dir (list, count); + vfs_path_free (vpath); return set_zero_dir (list) ? 1 : 0; } @@ -660,7 +667,7 @@ do_reload_dir (const char *path, dir_list * list, sortfn * sort, int count, { clean_dir (list, count); clean_dir (&dir_copy, count); - return next_free; + goto ret; } if (get_dotdot_dir_stat (path, &st)) @@ -689,7 +696,7 @@ do_reload_dir (const char *path, dir_list * list, sortfn * sort, int count, */ tree_store_end_check (); g_hash_table_destroy (marked_files); - return next_free; + goto ret; } list->list[next_free].f.marked = 0; @@ -728,6 +735,8 @@ do_reload_dir (const char *path, dir_list * list, sortfn * sort, int count, do_sort (list, sort, next_free - 1, lc_reverse, lc_case_sensitive, exec_ff); } clean_dir (&dir_copy, count); + ret: + vfs_path_free (vpath); return next_free; } diff --git a/src/filemanager/file.c b/src/filemanager/file.c index fd868fdbf..72c71a5ac 100644 --- a/src/filemanager/file.c +++ b/src/filemanager/file.c @@ -1015,7 +1015,7 @@ recursive_erase (FileOpTotalContext * tctx, FileOpContext * ctx, const char *s) return FILE_RETRY; vpath = vfs_path_from_str (s); - reading = mc_opendir (s); + reading = mc_opendir (vpath); if (reading == NULL) { @@ -1087,10 +1087,14 @@ check_dir_is_empty (const char *path) DIR *dir; struct dirent *d; int i; + vfs_path_t *vpath = vfs_path_from_str (path); - dir = mc_opendir (path); + dir = mc_opendir (vpath); if (!dir) + { + vfs_path_free (vpath); return -1; + } for (i = 1, d = mc_readdir (dir); d; d = mc_readdir (dir)) { @@ -1102,6 +1106,7 @@ check_dir_is_empty (const char *path) } mc_closedir (dir); + vfs_path_free (vpath); return i; } @@ -2066,7 +2071,7 @@ copy_dir_dir (FileOpTotalContext * tctx, FileOpContext * ctx, const char *s, con dont_mkdir: /* open the source dir for reading */ - reading = mc_opendir (s); + reading = mc_opendir (src_vpath); if (reading == NULL) goto ret; @@ -2479,7 +2484,7 @@ compute_dir_size (const char *dirname, const void *ui, } } - dir = mc_opendir (dirname); + dir = mc_opendir (vpath); if (dir == NULL) goto ret; diff --git a/src/filemanager/find.c b/src/filemanager/find.c index 96ac490e4..640ac2be8 100644 --- a/src/filemanager/find.c +++ b/src/filemanager/find.c @@ -1233,7 +1233,7 @@ do_search (Dlg_head * h) else subdirs_left = 0; - dirp = mc_opendir (directory); + dirp = mc_opendir (tmp_vpath); vfs_path_free (tmp_vpath); } /* while (!dirp) */ diff --git a/src/filemanager/tree.c b/src/filemanager/tree.c index a5cadd79b..a613ba0b4 100644 --- a/src/filemanager/tree.c +++ b/src/filemanager/tree.c @@ -715,23 +715,22 @@ tree_rescan (void *data) WTree *tree = data; char old_dir[MC_MAXPATHLEN]; vfs_path_t *vpath; - gboolean ok; int ret; if (tree->selected_ptr == NULL || mc_get_current_wd (old_dir, MC_MAXPATHLEN) == NULL) return; vpath = vfs_path_from_str (tree->selected_ptr->name); - ok = (mc_chdir (vpath) == 0); - vfs_path_free (vpath); - - if (ok) + if (mc_chdir (vpath) != 0) { - tree_store_rescan (tree->selected_ptr->name); - vpath = vfs_path_from_str (old_dir); - ret = mc_chdir (vpath); vfs_path_free (vpath); + return; } + + tree_store_rescan (vpath); + vpath = vfs_path_from_str (old_dir); + ret = mc_chdir (vpath); + vfs_path_free (vpath); } /* --------------------------------------------------------------------------------------------- */ diff --git a/src/filemanager/treestore.c b/src/filemanager/treestore.c index 419b2b575..7bd96a1bb 100644 --- a/src/filemanager/treestore.c +++ b/src/filemanager/treestore.c @@ -280,8 +280,10 @@ tree_store_load_from (char *name) /* Nothing loaded, we add some standard directories */ if (!ts.tree_first) { + vfs_path_t *tmp_vpath = vfs_path_from_str (PATH_SEP_STR); tree_store_add_entry (PATH_SEP_STR); - tree_store_rescan (PATH_SEP_STR); + tree_store_rescan (tmp_vpath); + vfs_path_free (tmp_vpath); ts.loaded = TRUE; } @@ -889,27 +891,31 @@ tree_store_end_check (void) /* --------------------------------------------------------------------------------------------- */ tree_entry * -tree_store_rescan (const char *dir) +tree_store_rescan (const vfs_path_t * vpath) { DIR *dirp; struct dirent *dp; struct stat buf; tree_entry *entry; + char *dir = vfs_path_to_str (vpath); if (should_skip_directory (dir)) { entry = tree_store_add_entry (dir); entry->scanned = 1; - + g_free (dir); return entry; } entry = tree_store_start_check (dir); if (!entry) + { + g_free (dir); return NULL; + } - dirp = mc_opendir (dir); + dirp = mc_opendir (vpath); if (dirp) { for (dp = mc_readdir (dirp); dp; dp = mc_readdir (dirp)) @@ -922,7 +928,7 @@ tree_store_rescan (const char *dir) continue; } - tmp_vpath = vfs_path_build_filename (dir, dp->d_name, NULL); + tmp_vpath = vfs_path_append_new (vpath, dp->d_name, NULL); if (mc_lstat (tmp_vpath, &buf) != -1) { if (S_ISDIR (buf.st_mode)) @@ -934,6 +940,7 @@ tree_store_rescan (const char *dir) } tree_store_end_check (); entry->scanned = 1; + g_free (dir); return entry; } diff --git a/src/filemanager/treestore.h b/src/filemanager/treestore.h index 9cdf16038..afa15e85a 100644 --- a/src/filemanager/treestore.h +++ b/src/filemanager/treestore.h @@ -54,7 +54,7 @@ tree_entry *tree_store_start_check (const char *path); void tree_store_mark_checked (const char *subname); void tree_store_end_check (void); tree_entry *tree_store_whereis (const char *name); -tree_entry *tree_store_rescan (const char *dir); +tree_entry *tree_store_rescan (const vfs_path_t * vpath); void tree_store_add_entry_remove_hook (tree_store_remove_fn callback, void *data); void tree_store_remove_entry_remove_hook (tree_store_remove_fn callback); -- 2.11.4.GIT