From d2eac2f5af5a156fe99e8f84f554f11e04cd648d Mon Sep 17 00:00:00 2001 From: Slava Zanko Date: Thu, 20 Jun 2013 14:24:34 +0300 Subject: [PATCH] Code reorganization and fixes around the vfs_path_as_str() function Signed-off-by: Slava Zanko --- lib/vfs/gc.c | 6 +- lib/vfs/vfs.h | 2 +- src/filemanager/dir.c | 13 ++- src/filemanager/file.c | 8 +- src/filemanager/layout.c | 8 +- src/filemanager/midnight.c | 43 ++------ src/filemanager/panel.c | 134 +++++++++++++----------- src/filemanager/panel.h | 6 +- src/filemanager/panelize.c | 8 +- tests/lib/vfs/vfs_path_string_convert.c | 2 +- 10 files changed, 116 insertions(+), 114 deletions(-) diff --git a/lib/vfs/gc.c b/lib/vfs/gc.c index c38080aec..cbcb03b7c 100644 --- a/lib/vfs/gc.c +++ b/lib/vfs/gc.c @@ -270,16 +270,12 @@ vfs_timeout_handler (void) /* --------------------------------------------------------------------------------------------- */ void -vfs_release_path (const char *dir) +vfs_release_path (const vfs_path_t * vpath) { - vfs_path_t *vpath; const vfs_path_element_t *path_element; - vpath = vfs_path_from_str (dir); path_element = vfs_path_get_by_index (vpath, -1); - vfs_stamp_create (path_element->class, vfs_getid (vpath)); - vfs_path_free (vpath); } /* --------------------------------------------------------------------------------------------- */ diff --git a/lib/vfs/vfs.h b/lib/vfs/vfs.h index 757549cfa..4260a2813 100644 --- a/lib/vfs/vfs.h +++ b/lib/vfs/vfs.h @@ -253,7 +253,7 @@ char *vfs_translate_path_n (const char *path); void vfs_stamp_path (const char *path); -void vfs_release_path (const char *dir); +void vfs_release_path (const vfs_path_t * vpath); void vfs_fill_names (fill_names_f); diff --git a/src/filemanager/dir.c b/src/filemanager/dir.c index 6f4e3bf2f..db0ba4bcb 100644 --- a/src/filemanager/dir.c +++ b/src/filemanager/dir.c @@ -557,7 +557,6 @@ do_load_dir (const vfs_path_t * vpath, dir_list * list, sortfn * sort, gboolean int status, link_to_dir, stale_link; int next_free = 0; struct stat st; - const char *vpath_str; /* ".." (if any) must be the first entry in the list */ if (!set_zero_dir (list)) @@ -576,10 +575,14 @@ do_load_dir (const vfs_path_t * vpath, dir_list * list, sortfn * sort, gboolean tree_store_start_check (vpath); - vpath_str = vfs_path_as_str (vpath); - /* Do not add a ".." entry to the root directory */ - if ((vpath_str[0] == PATH_SEP) && (vpath_str[1] == '\0')) - next_free--; + { + const char *vpath_str; + + vpath_str = vfs_path_as_str (vpath); + /* Do not add a ".." entry to the root directory */ + if ((vpath_str[0] == PATH_SEP) && (vpath_str[1] == '\0')) + next_free--; + } while ((dp = mc_readdir (dirp)) != NULL) { diff --git a/src/filemanager/file.c b/src/filemanager/file.c index 0d595be4c..941c0884c 100644 --- a/src/filemanager/file.c +++ b/src/filemanager/file.c @@ -197,12 +197,12 @@ static FileProgressStatus transform_error = FILE_CONT; /* --------------------------------------------------------------------------------------------- */ static char * -transform_source (FileOpContext * ctx, const char *source) +transform_source (FileOpContext * ctx, const vfs_path_t * source_vpath) { char *s, *q; char *fnsource; - s = g_strdup (source); + s = g_strdup (vfs_path_as_str (source_vpath)); /* We remove \n from the filename since regex routines would use \n as an anchor */ /* this is just to be allowed to maniupulate file names with \n on it */ @@ -2886,7 +2886,7 @@ panel_operate (void *source_panel, FileOperation operation, gboolean force_singl } else { - temp = transform_source (ctx, vfs_path_as_str (source_with_vpath)); + temp = transform_source (ctx, source_with_vpath); if (temp == NULL) value = transform_error; else @@ -2997,7 +2997,7 @@ panel_operate (void *source_panel, FileOperation operation, gboolean force_singl } else { - temp = transform_source (ctx, vfs_path_as_str (source_with_vpath)); + temp = transform_source (ctx, source_with_vpath); if (temp == NULL) value = transform_error; else diff --git a/src/filemanager/layout.c b/src/filemanager/layout.c index 1d067dd66..eb8a89459 100644 --- a/src/filemanager/layout.c +++ b/src/filemanager/layout.c @@ -608,7 +608,13 @@ restore_into_right_dir_panel (int idx, Widget * from_widget) const char *p_name = get_nth_panel_name (idx); if (last_was_panel) - new_widget = panel_new_with_dir (p_name, saved_dir); + { + vfs_path_t *saved_dir_vpath; + + saved_dir_vpath = vfs_path_from_str (saved_dir); + new_widget = panel_new_with_dir (p_name, saved_dir_vpath); + vfs_path_free (saved_dir_vpath); + } else new_widget = panel_new (p_name); diff --git a/src/filemanager/midnight.c b/src/filemanager/midnight.c index ad9002291..b0551da5e 100644 --- a/src/filemanager/midnight.c +++ b/src/filemanager/midnight.c @@ -683,47 +683,25 @@ create_panels (void) /* --------------------------------------------------------------------------------------------- */ static void -put_current_path (void) +midnight_put_panel_path (WPanel * panel) { vfs_path_t *cwd_vpath; + const char *cwd_vpath_str; if (!command_prompt) return; #ifdef HAVE_CHARSET - cwd_vpath = remove_encoding_from_path (current_panel->cwd_vpath); + cwd_vpath = remove_encoding_from_path (panel->cwd_vpath); #else - cwd_vpath = vfs_path_clone (current_panel->cwd_vpath); + cwd_vpath = vfs_path_clone (panel->cwd_vpath); #endif - command_insert (cmdline, vfs_path_as_str (cwd_vpath), FALSE); - if (cwd_vpath->str[strlen (vfs_path_as_str (cwd_vpath)) - 1] != PATH_SEP) - command_insert (cmdline, PATH_SEP_STR, FALSE); + cwd_vpath_str = vfs_path_as_str (cwd_vpath); - vfs_path_free (cwd_vpath); -} + command_insert (cmdline, cwd_vpath_str, FALSE); -/* --------------------------------------------------------------------------------------------- */ - -static void -put_other_path (void) -{ - vfs_path_t *cwd_vpath; - - if (get_other_type () != view_listing) - return; - - if (!command_prompt) - return; - -#ifdef HAVE_CHARSET - cwd_vpath = remove_encoding_from_path (other_panel->cwd_vpath); -#else - cwd_vpath = vfs_path_clone (other_panel->cwd_vpath); -#endif - - command_insert (cmdline, vfs_path_as_str (cwd_vpath), FALSE); - if (cwd_vpath->str[strlen (vfs_path_as_str (cwd_vpath)) - 1] != PATH_SEP) + if (cwd_vpath_str[strlen (cwd_vpath_str) - 1] != PATH_SEP) command_insert (cmdline, PATH_SEP_STR, FALSE); vfs_path_free (cwd_vpath); @@ -1130,7 +1108,7 @@ midnight_execute_cmd (Widget * sender, unsigned long command) copy_cmd (); break; case CK_PutCurrentPath: - put_current_path (); + midnight_put_panel_path (current_panel); break; case CK_PutCurrentLink: put_current_link (); @@ -1139,7 +1117,8 @@ midnight_execute_cmd (Widget * sender, unsigned long command) put_current_tagged (); break; case CK_PutOtherPath: - put_other_path (); + if (get_other_type () == view_listing) + midnight_put_panel_path (other_panel); break; case CK_PutOtherLink: put_other_link (); @@ -1466,7 +1445,7 @@ midnight_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void /* Ctrl-Shift-Enter */ if (parm == (KEY_M_CTRL | KEY_M_SHIFT | '\n')) { - put_current_path (); + midnight_put_panel_path (current_panel); put_prog_name (); return MSG_HANDLED; } diff --git a/src/filemanager/panel.c b/src/filemanager/panel.c index 18555b72c..be7420d62 100644 --- a/src/filemanager/panel.c +++ b/src/filemanager/panel.c @@ -2972,17 +2972,20 @@ panel_set_sort_type_by_id (WPanel * panel, const char *name) */ static const char * -get_parent_dir_name (const char *cwd, const char *lwd) +get_parent_dir_name (const vfs_path_t * cwd_vpath, const vfs_path_t * lwd_vpath) { size_t llen, clen; - const char *p; + const char *p, *cwd, *lwd; - llen = strlen (lwd); - clen = strlen (cwd); + llen = vfs_path_len (lwd_vpath); + clen = vfs_path_len (cwd_vpath); if (llen <= clen) return NULL; + cwd = vfs_path_as_str (cwd_vpath); + lwd = vfs_path_as_str (lwd_vpath); + p = g_strrstr (lwd, VFS_PATH_URL_DELIMITER); if (p == NULL) @@ -3044,21 +3047,18 @@ _do_panel_cd (WPanel * panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_ } if (mc_chdir (new_dir_vpath) == -1) - { - panel_set_cwd (panel, vfs_path_as_str (panel->cwd_vpath)); return FALSE; - } /* Success: save previous directory, shutdown status of previous dir */ olddir_vpath = vfs_path_clone (panel->cwd_vpath); - panel_set_lwd (panel, vfs_path_as_str (panel->cwd_vpath)); + panel_set_lwd (panel, panel->cwd_vpath); input_free_completions (cmdline); vfs_path_free (panel->cwd_vpath); vfs_setup_cwd (); panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ()); - vfs_release_path (vfs_path_as_str (olddir_vpath)); + vfs_release_path (olddir_vpath); subshell_chdir (panel->cwd_vpath); @@ -3069,9 +3069,7 @@ _do_panel_cd (WPanel * panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_ do_load_dir (panel->cwd_vpath, &panel->dir, panel->sort_info.sort_field->sort_routine, panel->sort_info.reverse, panel->sort_info.case_sensitive, panel->sort_info.exec_first, panel->filter); - try_to_select (panel, - get_parent_dir_name (vfs_path_as_str (panel->cwd_vpath), - vfs_path_as_str (olddir_vpath))); + try_to_select (panel, get_parent_dir_name (panel->cwd_vpath, olddir_vpath)); load_hint (0); panel->dirty = 1; @@ -3926,6 +3924,34 @@ panel_save_curent_file_to_clip_file (const gchar * event_group_name, const gchar return TRUE; } +/* --------------------------------------------------------------------------------------------- */ + +static vfs_path_t * +panel_recursive_cd_to_parent (const vfs_path_t * vpath) +{ + vfs_path_t *cwd_vpath; + + cwd_vpath = vfs_path_clone (vpath); + + while (mc_chdir (cwd_vpath) < 0) + { + const char *panel_cwd_path; + vfs_path_t *tmp_vpath; + + /* check if path contains only '/' */ + panel_cwd_path = vfs_path_as_str (cwd_vpath); + if (panel_cwd_path[0] == PATH_SEP && panel_cwd_path[1] == '\0') + return NULL; + + tmp_vpath = vfs_path_vtokens_get (cwd_vpath, 0, -1); + vfs_path_free (cwd_vpath); + cwd_vpath = vfs_path_build_filename (PATH_SEP_STR, vfs_path_as_str (tmp_vpath), NULL); + vfs_path_free (tmp_vpath); + } + + return cwd_vpath; +} + /* --------------------------------------------------------------------------------------------- */ /*** public functions ****************************************************************************/ /* --------------------------------------------------------------------------------------------- */ @@ -3968,10 +3994,13 @@ panel_clean_dir (WPanel * panel) */ void -panel_set_cwd (WPanel * panel, const char *path_str) +panel_set_cwd (WPanel * panel, const vfs_path_t * vpath) { - vfs_path_free (panel->cwd_vpath); - panel->cwd_vpath = vfs_path_from_str (path_str); + if (vpath != panel->cwd_vpath) /* check if new vpath is not the panel->cwd_vpath object */ + { + vfs_path_free (panel->cwd_vpath); + panel->cwd_vpath = vfs_path_clone (vpath); + } } /* --------------------------------------------------------------------------------------------- */ @@ -3983,10 +4012,13 @@ panel_set_cwd (WPanel * panel, const char *path_str) */ void -panel_set_lwd (WPanel * panel, const char *path_str) +panel_set_lwd (WPanel * panel, const vfs_path_t * vpath) { - vfs_path_free (panel->lwd_vpath); - panel->lwd_vpath = vfs_path_from_str (path_str); + if (vpath != panel->lwd_vpath) /* check if new vpath is not the panel->lwd_vpath object */ + { + vfs_path_free (panel->lwd_vpath); + panel->lwd_vpath = vfs_path_clone (vpath); + } } /* --------------------------------------------------------------------------------------------- */ @@ -4013,7 +4045,7 @@ panel_new (const char *panel_name) */ WPanel * -panel_new_with_dir (const char *panel_name, const char *wpath) +panel_new_with_dir (const char *panel_name, const vfs_path_t * vpath) { WPanel *panel; Widget *w; @@ -4028,10 +4060,10 @@ panel_new_with_dir (const char *panel_name, const char *wpath) /* We do not want the cursor */ widget_want_cursor (w, FALSE); - if (wpath != NULL) + if (vpath != NULL) { curdir = _vfs_get_cwd (); - panel_set_cwd (panel, wpath); + panel_set_cwd (panel, vpath); } else { @@ -4039,7 +4071,7 @@ panel_new_with_dir (const char *panel_name, const char *wpath) panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ()); } - panel_set_lwd (panel, "."); + panel_set_lwd (panel, vfs_get_raw_current_dir ()); panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL); /* directories history will be get later */ @@ -4120,11 +4152,11 @@ panel_new_with_dir (const char *panel_name, const char *wpath) /* Restore old right path */ if (curdir != NULL) { - vfs_path_t *vpath; + vfs_path_t *tmp_vpath; - vpath = vfs_path_from_str (curdir); - err = mc_chdir (vpath); - vfs_path_free (vpath); + tmp_vpath = vfs_path_from_str (curdir); + err = mc_chdir (tmp_vpath); + vfs_path_free (tmp_vpath); } g_free (curdir); @@ -4137,45 +4169,27 @@ void panel_reload (WPanel * panel) { struct stat current_stat; - gboolean ok; + vfs_path_t *cwd_vpath; - ok = (panels_options.fast_reload - && stat (vfs_path_as_str (panel->cwd_vpath), ¤t_stat) == 0 - && current_stat.st_ctime == panel->dir_stat.st_ctime - && current_stat.st_mtime == panel->dir_stat.st_mtime); - - if (ok) + if (panels_options.fast_reload && stat (vfs_path_as_str (panel->cwd_vpath), ¤t_stat) == 0 + && current_stat.st_ctime == panel->dir_stat.st_ctime + && current_stat.st_mtime == panel->dir_stat.st_mtime) return; - do + cwd_vpath = panel_recursive_cd_to_parent (panel->cwd_vpath); + vfs_path_free (panel->cwd_vpath); + + if (cwd_vpath == NULL) { - char *last_slash; - const char *panel_cwd_path; - - if (mc_chdir (panel->cwd_vpath) != -1) - break; - - panel_cwd_path = vfs_path_as_str (panel->cwd_vpath); - - if (panel_cwd_path[0] == PATH_SEP && panel_cwd_path[1] == '\0') - { - panel_clean_dir (panel); - panel->count = set_zero_dir (&panel->dir) ? 1 : 0; - return; - } - last_slash = strrchr (panel_cwd_path, PATH_SEP); - vfs_path_free (panel->cwd_vpath); - if (last_slash == NULL || last_slash == panel_cwd_path) - panel->cwd_vpath = vfs_path_from_str (PATH_SEP_STR); - else - { - *last_slash = '\0'; - panel->cwd_vpath = vfs_path_clone (panel->cwd_vpath); - } - memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat)); - show_dir (panel); + panel->cwd_vpath = vfs_path_from_str (PATH_SEP_STR); + panel_clean_dir (panel); + panel->count = set_zero_dir (&panel->dir) ? 1 : 0; + return; } - while (TRUE); + + panel->cwd_vpath = cwd_vpath; + memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat)); + show_dir (panel); panel->count = do_reload_dir (panel->cwd_vpath, &panel->dir, panel->sort_info.sort_field->sort_routine, diff --git a/src/filemanager/panel.h b/src/filemanager/panel.h index 00ca21071..41641ceea 100644 --- a/src/filemanager/panel.h +++ b/src/filemanager/panel.h @@ -152,7 +152,7 @@ extern mc_fhl_t *mc_filehighlight; /*** declarations of public functions ************************************************************/ WPanel *panel_new (const char *panel_name); -WPanel *panel_new_with_dir (const char *panel_name, const char *dr); +WPanel *panel_new_with_dir (const char *panel_name, const vfs_path_t * vpath); void panel_clean_dir (WPanel * panel); void panel_reload (WPanel * panel); @@ -186,8 +186,8 @@ const panel_field_t *panel_get_field_by_title (const char *); const panel_field_t *panel_get_field_by_title_hotkey (const char *); gsize panel_get_num_of_user_possible_fields (void); const char **panel_get_user_possible_fields (gsize *); -void panel_set_cwd (WPanel * panel, const char *path_str); -void panel_set_lwd (WPanel * panel, const char *path_str); +void panel_set_cwd (WPanel * panel, const vfs_path_t * vpath); +void panel_set_lwd (WPanel * panel, const vfs_path_t * vpath); void panel_init (void); void panel_deinit (void); diff --git a/src/filemanager/panelize.c b/src/filemanager/panelize.c index 50f7a9dd2..ccb12b199 100644 --- a/src/filemanager/panelize.c +++ b/src/filemanager/panelize.c @@ -376,9 +376,13 @@ do_external_panelize (char *command) current_panel->count = next_free; if (list->list[0].fname[0] == PATH_SEP) { + vfs_path_t *vpath_root; int ret; - panel_set_cwd (current_panel, PATH_SEP_STR); - ret = chdir (PATH_SEP_STR); + + vpath_root = vfs_path_from_str (PATH_SEP_STR); + panel_set_cwd (current_panel, vpath_root); + ret = mc_chdir (vpath_root); + vfs_path_free (vpath_root); (void) ret; } } diff --git a/tests/lib/vfs/vfs_path_string_convert.c b/tests/lib/vfs/vfs_path_string_convert.c index 69168e3b9..c920780eb 100644 --- a/tests/lib/vfs/vfs_path_string_convert.c +++ b/tests/lib/vfs/vfs_path_string_convert.c @@ -182,7 +182,7 @@ START_PARAMETRIZED_TEST (test_from_to_string, test_from_to_string_ds) vfs_path_t *vpath; size_t vpath_len; const vfs_path_element_t *path_element; - char *actual_result; + const char *actual_result; vpath = vfs_path_from_str_flags (data->input_string, VPF_USE_DEPRECATED_PARSER);