Browse Source

Changed interface of function mc_opendir()

...to handle vfs_path_t object as parameter.

Signed-off-by: Slava Zanko <slavazanko@gmail.com>
Slava Zanko 13 years ago
parent
commit
034252a71a

+ 1 - 7
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;
 }
 

+ 25 - 0
lib/vfs/path.c

@@ -1061,3 +1061,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;
+
+}
+
+/* --------------------------------------------------------------------------------------------- */

+ 1 - 0
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);

+ 1 - 1
lib/vfs/vfs.h

@@ -279,7 +279,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);

+ 10 - 5
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);

+ 17 - 8
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;
 }
 

+ 9 - 4
src/filemanager/file.c

@@ -1013,7 +1013,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)
     {
@@ -1085,10 +1085,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))
     {
@@ -1100,6 +1104,7 @@ check_dir_is_empty (const char *path)
     }
 
     mc_closedir (dir);
+    vfs_path_free (vpath);
     return i;
 }
 
@@ -2064,7 +2069,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;
 
@@ -2477,7 +2482,7 @@ compute_dir_size (const char *dirname, const void *ui,
         }
     }
 
-    dir = mc_opendir (dirname);
+    dir = mc_opendir (vpath);
 
     if (dir == NULL)
         goto ret;

+ 1 - 1
src/filemanager/find.c

@@ -1247,7 +1247,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) */
 

+ 7 - 8
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);
 }
 
 /* --------------------------------------------------------------------------------------------- */

+ 12 - 5
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;
 }

Some files were not shown because too many files changed in this diff