VFS: Current directory handler now have type vfs_path_t

Signed-off-by: Slava Zanko <slavazanko@gmail.com>
This commit is contained in:
Slava Zanko 2011-04-29 14:13:29 +03:00
parent c188153507
commit 093128c532
11 changed files with 141 additions and 63 deletions

View File

@ -93,6 +93,33 @@ START_TEST (test_vfs_path_from_to_string)
}
END_TEST
/* --------------------------------------------------------------------------------------------- */
START_TEST (test_vfs_path_from_to_string2)
{
vfs_path_t *vpath;
size_t vpath_len;
char *result;
vfs_path_element_t *path_element;
vpath = vfs_path_from_str ("/");
vpath_len = vfs_path_elements_count(vpath);
fail_unless(vpath_len == 1, "vpath length should be 1 (actial: %d)",vpath_len);
result = vfs_path_to_str(vpath);
fail_unless(strcmp("/", result) == 0, "expected(%s) doesn't equal to actual(%s)", "/", result);
g_free(result);
path_element = vfs_path_get_by_index (vpath, -1);
fail_unless(strcmp("/", path_element->path) == 0, "expected(%s) doesn't equal to actual(%s)", "/", path_element->path);
fail_unless(path_element->class == &vfs_local_ops , "actual vfs-class doesn't equal to localfs");
vfs_path_free(vpath);
}
END_TEST
/* --------------------------------------------------------------------------------------------- */
START_TEST (test_vfs_path_from_to_partial_string_by_class)
{
@ -177,6 +204,7 @@ main (void)
/* Add new tests here: *************** */
tcase_add_test (tc_core, test_vfs_path_from_to_string);
tcase_add_test (tc_core, test_vfs_path_from_to_string2);
tcase_add_test (tc_core, test_vfs_path_from_to_partial_string_by_class);
/* *********************************** */

View File

@ -187,7 +187,7 @@ vfs_stamp_create (struct vfs_class *vclass, vfsid id)
if (!mc_event_present (MCEVENT_GROUP_CORE, "vfs_timestamp"))
return;
vpath = vfs_path_from_str (vfs_get_current_dir ());
vpath = vfs_get_raw_current_dir ();
path_element = vfs_path_get_by_index (vpath, -1);
nvfsid = vfs_getid (vpath);
@ -201,7 +201,6 @@ vfs_stamp_create (struct vfs_class *vclass, vfsid id)
&& vclass->nothingisopen (id) != 0)
vfs_addstamp (vclass, id);
}
vfs_path_free (vpath);
}
/* --------------------------------------------------------------------------------------------- */

View File

@ -57,7 +57,6 @@
extern GString *vfs_str_buffer;
extern struct vfs_class *current_vfs;
extern char *current_dir;
/*** global variables ****************************************************************************/
@ -637,9 +636,11 @@ mc_fstat (int handle, struct stat *buf)
char *
mc_get_current_wd (char *buffer, size_t size)
{
const char *cwd = _vfs_get_cwd ();
char *cwd = _vfs_get_cwd ();
g_strlcpy (buffer, cwd, size);
g_free (cwd);
return buffer;
}
@ -702,8 +703,6 @@ mc_ungetlocalcopy (const char *pathname, const char *local, int has_changed)
int
mc_chdir (const char *path)
{
// char *new_dir;
// char *trans_dir;
struct vfs_class *old_vfs;
vfsid old_vfsid;
int result;
@ -728,27 +727,23 @@ mc_chdir (const char *path)
return -1;
}
{
vfs_path_t *current_vpath = vfs_path_from_str (current_dir);
old_vfsid = vfs_getid (current_vpath);
old_vfsid = vfs_getid (vfs_get_raw_current_dir ());
old_vfs = current_vfs;
vfs_path_free (current_vpath);
}
/* Actually change directory */
g_free (current_dir);
current_dir = vpath->unparsed;
vfs_set_raw_current_dir (vpath);
current_vfs = path_element->class;
/* This function uses the new current_dir implicitly */
vfs_stamp_create (old_vfs, old_vfsid);
/* Sometimes we assume no trailing slash on cwd */
if (*current_dir)
path_element = vfs_path_get_by_index (vfs_get_raw_current_dir (), -1);
if ((path_element != NULL) && (*path_element->path != '\0'))
{
char *p;
p = strchr (current_dir, 0) - 1;
if (*p == PATH_SEP && p > current_dir)
p = strchr (path_element->path, 0) - 1;
if (*p == PATH_SEP && p > path_element->path)
*p = 0;
}
return 0;

View File

@ -262,7 +262,10 @@ vfs_path_new (void)
int
vfs_path_elements_count (const vfs_path_t * vpath)
{
return g_list_length (vpath->path);
if (vpath == NULL)
return 0;
return (vpath->path != NULL) ? g_list_length (vpath->path) : 0;
}
/* --------------------------------------------------------------------------------------------- */
@ -278,6 +281,9 @@ vfs_path_elements_count (const vfs_path_t * vpath)
vfs_path_element_t *
vfs_path_get_by_index (const vfs_path_t * vpath, int element_index)
{
if (vpath == NULL)
return NULL;
if (element_index >= 0)
return g_list_nth_data (vpath->path, element_index);

View File

@ -58,11 +58,9 @@ extern struct dirent *mc_readdir_result;
GPtrArray *vfs__classes_list = NULL;
GString *vfs_str_buffer;
struct vfs_class *current_vfs;
GString *vfs_str_buffer = NULL;
struct vfs_class *current_vfs = NULL;
/** They keep track of the current directory */
char *current_dir;
/*** file scope macro definitions ****************************************************************/
@ -85,6 +83,8 @@ struct vfs_openfile
/*** file scope variables ************************************************************************/
/** They keep track of the current directory */
static vfs_path_t *current_path = NULL;
static GPtrArray *vfs_openfiles;
static long vfs_free_handle_list = -1;
@ -488,15 +488,43 @@ vfs_canon_and_translate (const char *path)
}
/* --------------------------------------------------------------------------------------------- */
/**
* Return current directory without any OS calls.
* Get current directory without any OS calls.
*
* @return string contain current path
*/
char *
vfs_get_current_dir (void)
{
return current_dir;
return vfs_path_to_str (current_path);
}
/* --------------------------------------------------------------------------------------------- */
/**
* Get raw current directory object without any OS calls.
*
* @return object contain current path
*/
vfs_path_t *
vfs_get_raw_current_dir (void)
{
return current_path;
}
/* --------------------------------------------------------------------------------------------- */
/**
* Set current directory object.
*
* @param vpath new path
*/
void
vfs_set_raw_current_dir (const vfs_path_t * vpath)
{
if (current_path != NULL)
vfs_path_free (current_path);
current_path = (vfs_path_t *) vpath;
}
/* --------------------------------------------------------------------------------------------- */
@ -513,9 +541,11 @@ vfs_canon (const char *path)
/* Relative to current directory */
if (*path != PATH_SEP)
{
char *local, *result;
char *local, *result, *curr_dir;
local = concat_dir_and_file (current_dir, path);
curr_dir = vfs_get_current_dir ();
local = concat_dir_and_file (curr_dir, path);
g_free (curr_dir);
result = vfs_canon (local);
g_free (local);
@ -580,13 +610,18 @@ vfs_init (void)
void
vfs_setup_work_dir (void)
{
current_dir = g_strdup (PATH_SEP_STR);
_vfs_get_cwd ();
vfs_path_element_t *path_element;
g_free (_vfs_get_cwd ());
/* FIXME: is we really need for this check? */
/*
if (strlen (current_dir) > MC_MAXPATHLEN - 2)
vfs_die ("Current dir too long.\n");
*/
current_vfs = vfs_get_class (current_dir);
path_element = vfs_path_get_by_index (current_path, -1);
current_vfs = path_element->class;
}
/* --------------------------------------------------------------------------------------------- */
@ -598,7 +633,7 @@ vfs_shut (void)
vfs_gc_done ();
g_free (current_dir);
vfs_set_raw_current_dir (NULL);
for (i = 0; i < vfs__classes_list->len; i++)
{
@ -681,16 +716,25 @@ vfs_print_message (const char *msg, ...)
* from the OS. You must g_strdup() whatever this function returns.
*/
const char *
char *
_vfs_get_cwd (void)
{
char *trans;
char *trans, *curr_dir;
trans = vfs_translate_path_n (current_dir);
if (vfs_get_raw_current_dir() == NULL)
{
char *tmp;
tmp = g_get_current_dir ();
vfs_set_raw_current_dir (vfs_path_from_str (tmp));
g_free(tmp);
}
curr_dir = vfs_get_current_dir ();
trans = vfs_translate_path_n (curr_dir);
if (_vfs_get_class (trans) == NULL)
{
const char *encoding = vfs_get_encoding (current_dir);
const char *encoding = vfs_get_encoding (curr_dir);
if (encoding == NULL)
{
@ -700,26 +744,22 @@ _vfs_get_cwd (void)
if (tmp != NULL)
{ /* One of the directories in the path is not readable */
estr_t state;
char *sys_cwd;
g_string_set_size (vfs_str_buffer, 0);
state = str_vfs_convert_from (str_cnv_from_term, tmp, vfs_str_buffer);
g_free (tmp);
sys_cwd = (state == ESTR_SUCCESS) ? g_strdup (vfs_str_buffer->str) : NULL;
if (sys_cwd != NULL)
if (state == ESTR_SUCCESS)
{
struct stat my_stat, my_stat2;
/* Check if it is O.K. to use the current_dir */
if (mc_global.vfs.cd_symlinks
&& mc_stat (sys_cwd, &my_stat) == 0
&& mc_stat (current_dir, &my_stat2) == 0
&& my_stat.st_ino == my_stat2.st_ino && my_stat.st_dev == my_stat2.st_dev)
g_free (sys_cwd);
else
if (!(mc_global.vfs.cd_symlinks
&& mc_stat (vfs_str_buffer->str, &my_stat) == 0
&& mc_stat (curr_dir, &my_stat2) == 0
&& my_stat.st_ino == my_stat2.st_ino
&& my_stat.st_dev == my_stat2.st_dev))
{
g_free (current_dir);
current_dir = sys_cwd;
vfs_set_raw_current_dir (vfs_path_from_str (vfs_str_buffer->str));
}
}
}
@ -727,7 +767,7 @@ _vfs_get_cwd (void)
}
g_free (trans);
return current_dir;
return curr_dir;
}
/* --------------------------------------------------------------------------------------------- */

View File

@ -230,6 +230,10 @@ int vfs_timeouts (void);
void vfs_expire (int now);
char *vfs_get_current_dir (void);
vfs_path_t *vfs_get_raw_current_dir (void);
void vfs_set_raw_current_dir (const vfs_path_t * vpath);
gboolean vfs_current_is_local (void);
gboolean vfs_file_is_local (const char *filename);
@ -274,7 +278,7 @@ void *vfs_class_data_find_by_handle (int handle);
void vfs_free_handle (int handle);
const char *_vfs_get_cwd (void);
char *_vfs_get_cwd (void);
/*** inline functions ****************************************************************************/
#endif /* MC_VFS_VFS_H */

View File

@ -1259,7 +1259,7 @@ edit_set_filename (WEdit * edit, const char *name)
edit->filename = tilde_expand (name);
if (edit->dir == NULL && !g_path_is_absolute (name))
edit->dir = g_strdup (vfs_get_current_dir ());
edit->dir = vfs_get_current_dir ();
}
/* --------------------------------------------------------------------------------------------- */
@ -1382,7 +1382,7 @@ edit_save_as_cmd (WEdit * edit)
/* --------------------------------------------------------------------------------------------- */
static int
edit_macro_comparator (gconstpointer *macro1, gconstpointer *macro2)
edit_macro_comparator (gconstpointer * macro1, gconstpointer * macro2)
{
const macros_t *m1 = (const macros_t *) macro1;
const macros_t *m2 = (const macros_t *) macro2;
@ -1402,7 +1402,7 @@ edit_macro_sort_by_hotkey (void)
/* --------------------------------------------------------------------------------------------- */
static gboolean
edit_get_macro (WEdit * edit, int hotkey, const macros_t **macros, guint *indx)
edit_get_macro (WEdit * edit, int hotkey, const macros_t ** macros, guint * indx)
{
const macros_t *array_start = &g_array_index (macros_list, struct macros_t, 0);
macros_t *result;
@ -1535,8 +1535,7 @@ edit_store_macro_cmd (WEdit * edit)
/* return FALSE if try assign macro into restricted hotkeys */
if (tmp_act == CK_MacroStartRecord
|| tmp_act == CK_MacroStopRecord
|| tmp_act == CK_MacroStartStopRecord)
|| tmp_act == CK_MacroStopRecord || tmp_act == CK_MacroStartStopRecord)
return FALSE;
edit_delete_macro (edit, hotkey);
@ -1569,7 +1568,8 @@ edit_store_macro_cmd (WEdit * edit)
m_act.ch = record_macro_buf[i].ch;
g_array_append_val (macros, m_act);
have_macro = TRUE;
g_string_append_printf (marcros_string, "%s:%i;", action_name, (int) record_macro_buf[i].ch);
g_string_append_printf (marcros_string, "%s:%i;", action_name,
(int) record_macro_buf[i].ch);
}
if (have_macro)
{

View File

@ -130,7 +130,7 @@ do_execute (const char *lc_shell, const char *command, int flags)
char *old_vfs_dir = 0;
if (!vfs_current_is_local ())
old_vfs_dir = g_strdup (vfs_get_current_dir ());
old_vfs_dir = vfs_get_current_dir ();
if (mc_global.mc_run_mode == MC_RUN_FULL)
save_cwds_stat ();

View File

@ -954,7 +954,11 @@ done_mc (void)
save_setup (auto_save_setup, panels_options.auto_save_setup);
done_screen ();
vfs_stamp_path (vfs_get_current_dir ());
{
char *curr_dir = vfs_get_current_dir ();
vfs_stamp_path (curr_dir);
g_free (curr_dir);
}
if ((current_panel != NULL) && (get_current_type () == view_listing))
vfs_stamp_path (current_panel->cwd);

View File

@ -2288,10 +2288,12 @@ do_enter_on_file_entry (file_entry * fe)
if (!vfs_current_is_local ())
{
char *tmp;
char *tmp, *tmp_curr_dir;
int ret;
tmp = concat_dir_and_file (vfs_get_current_dir (), fe->fname);
tmp_curr_dir = vfs_get_current_dir ();
tmp = concat_dir_and_file (tmp_curr_dir, fe->fname);
g_free (tmp_curr_dir);
ret = mc_setctl (tmp, VFS_SETCTL_RUN, NULL);
g_free (tmp);
/* We took action only if the dialog was shown or the execution

View File

@ -282,7 +282,7 @@ mcview_load (mcview_t * view, const char *command, const char *file, int start_l
if ((view->workdir == NULL) && (file != NULL))
{
if (!g_path_is_absolute (file))
view->workdir = g_strdup (vfs_get_current_dir ());
view->workdir = vfs_get_current_dir ();
else
{
/* try extract path form filename */
@ -294,7 +294,7 @@ mcview_load (mcview_t * view, const char *command, const char *file, int start_l
else
{
g_free (dirname);
view->workdir = g_strdup (vfs_get_current_dir ());
view->workdir = vfs_get_current_dir ();
}
}
}