VFS: Make functions vfs_canon_and_translate() and vfs_canon() as static in lib/vfs/path.c

Signed-off-by: Slava Zanko <slavazanko@gmail.com>
This commit is contained in:
Slava Zanko 2011-05-01 13:30:58 +03:00
parent 8ea49095c3
commit 405cc98cea
9 changed files with 94 additions and 68 deletions

View File

@ -34,6 +34,7 @@
#include "lib/global.h" #include "lib/global.h"
#include "lib/strutil.h" #include "lib/strutil.h"
#include "lib/util.h" /* concat_dir_and_file */
#include "vfs.h" #include "vfs.h"
#include "utilvfs.h" #include "utilvfs.h"
@ -120,6 +121,64 @@ _vfs_split_with_semi_skip_count (char *path, const char **inpath, const char **o
return ret; return ret;
} }
/* --------------------------------------------------------------------------------------------- */
/**
* remove //, /./ and /../
*
* @return newly allocated string
*/
static char *
vfs_canon (const char *path)
{
if (!path)
vfs_die ("Cannot canonicalize NULL");
/* Relative to current directory */
if (*path != PATH_SEP)
{
char *local, *result, *curr_dir;
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);
return result;
}
/*
* So we have path of following form:
* /p1/p2#op/.././././p3#op/p4. Good luck.
*/
{
char *result = g_strdup (path);
canonicalize_pathname (result);
return result;
}
}
/* --------------------------------------------------------------------------------------------- */
/** canonize and translate path
*
* @return new string
*/
static char *
vfs_canon_and_translate (const char *path)
{
char *canon;
char *result;
if (path == NULL)
canon = g_strdup ("");
else
canon = vfs_canon (path);
result = vfs_translate_path_n (canon);
g_free (canon);
return result;
}
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/ /*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
@ -185,7 +244,7 @@ vfs_path_to_str (const vfs_path_t * vpath)
* *
* @param path_str VFS-path * @param path_str VFS-path
* *
* @return pointer to newly created vfs_path_t object with filled elements array. * @return pointer to newly created vfs_path_t object with filled path elements array.
*/ */
vfs_path_t * vfs_path_t *
@ -202,9 +261,7 @@ vfs_path_from_str (const char *path_str)
vpath = vfs_path_new (); vpath = vfs_path_new ();
vpath->unparsed_encoding = g_strdup (vfs_get_encoding (path_str)); vpath->unparsed_encoding = g_strdup (vfs_get_encoding (path_str));
vpath->unparsed = vfs_canon_and_translate (path_str); vpath->unparsed = vfs_canon_and_translate (path_str);
if (vpath->unparsed == NULL) if (vpath->unparsed == NULL)
{ {
vfs_path_free (vpath); vfs_path_free (vpath);

View File

@ -471,22 +471,6 @@ vfs_translate_path_n (const char *path)
return (result != NULL) ? g_strdup (result) : NULL; return (result != NULL) ? g_strdup (result) : NULL;
} }
/* --------------------------------------------------------------------------------------------- */
char *
vfs_canon_and_translate (const char *path)
{
char *canon;
char *result;
if (path == NULL)
canon = g_strdup ("");
else
canon = vfs_canon (path);
result = vfs_translate_path_n (canon);
g_free (canon);
return result;
}
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
/** /**
* Get current directory without any OS calls. * Get current directory without any OS calls.
@ -527,42 +511,6 @@ vfs_set_raw_current_dir (const vfs_path_t * vpath)
current_path = (vfs_path_t *) vpath; current_path = (vfs_path_t *) vpath;
} }
/* --------------------------------------------------------------------------------------------- */
/**
* remove //, /./ and /../
*/
char *
vfs_canon (const char *path)
{
if (!path)
vfs_die ("Cannot canonicalize NULL");
/* Relative to current directory */
if (*path != PATH_SEP)
{
char *local, *result, *curr_dir;
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);
return result;
}
/*
* So we have path of following form:
* /p1/p2#op/.././././p3#op/p4. Good luck.
*/
{
char *result = g_strdup (path);
canonicalize_pathname (result);
return result;
}
}
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
/* Return TRUE is the current VFS class is local */ /* Return TRUE is the current VFS class is local */

View File

@ -236,7 +236,6 @@ void vfs_set_raw_current_dir (const vfs_path_t * vpath);
gboolean vfs_current_is_local (void); gboolean vfs_current_is_local (void);
gboolean vfs_file_is_local (const vfs_path_t * vpath); gboolean vfs_file_is_local (const vfs_path_t * vpath);
char *vfs_canon (const char *path);
char *vfs_strip_suffix_from_filename (const char *filename); char *vfs_strip_suffix_from_filename (const char *filename);
char *vfs_translate_url (const char *url); char *vfs_translate_url (const char *url);
@ -250,8 +249,6 @@ vfs_class_flags_t vfs_file_class_flags (const vfs_path_t * vpath);
* return static buffer */ * return static buffer */
const char *vfs_get_encoding (const char *path); const char *vfs_get_encoding (const char *path);
/* canonize and translate path, return new string */
char *vfs_canon_and_translate (const char *path);
/* translate path back to terminal encoding, remove all #enc: /* translate path back to terminal encoding, remove all #enc:
* every invalid character is replaced with question mark * every invalid character is replaced with question mark
* return static buffer */ * return static buffer */

View File

@ -499,12 +499,15 @@ edit_load_position (WEdit * edit)
char *filename; char *filename;
long line, column; long line, column;
off_t offset; off_t offset;
vfs_path_t *vpath;
if (!edit->filename || !*edit->filename) if (!edit->filename || !*edit->filename)
return; return;
filename = vfs_canon (edit->filename); vpath = vfs_path_from_str (edit->filename);
filename = vfs_path_to_str (vpath);
load_file_position (filename, &line, &column, &offset, &edit->serialized_bookmarks); load_file_position (filename, &line, &column, &offset, &edit->serialized_bookmarks);
vfs_path_free (vpath);
g_free (filename); g_free (filename);
if (line > 0) if (line > 0)
@ -532,11 +535,13 @@ static void
edit_save_position (WEdit * edit) edit_save_position (WEdit * edit)
{ {
char *filename; char *filename;
vfs_path_t *vpath;
if (edit->filename == NULL || *edit->filename == '\0') if (edit->filename == NULL || *edit->filename == '\0')
return; return;
filename = vfs_canon (edit->filename); vpath = vfs_path_from_str (edit->filename);
filename = vfs_path_to_str (vpath);
book_mark_serialize (edit, BOOK_MARK_COLOR); book_mark_serialize (edit, BOOK_MARK_COLOR);
save_file_position (filename, edit->curs_line + 1, edit->curs_col, edit->curs1, save_file_position (filename, edit->curs_line + 1, edit->curs_col, edit->curs1,
@ -544,6 +549,7 @@ edit_save_position (WEdit * edit)
edit->serialized_bookmarks = NULL; edit->serialized_bookmarks = NULL;
g_free (filename); g_free (filename);
vfs_path_free (vpath);
} }
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */

View File

@ -437,7 +437,7 @@ execute_with_vfs_arg (const char *command, const char *filename)
/* Simplest case, this file is local */ /* Simplest case, this file is local */
if (!filename || vfs_file_is_local (vpath)) if (!filename || vfs_file_is_local (vpath))
{ {
fn = vfs_canon_and_translate (filename); fn = vfs_path_to_str (vpath);
do_execute (command, fn, EXECUTE_INTERNAL); do_execute (command, fn, EXECUTE_INTERNAL);
g_free (fn); g_free (fn);
vfs_path_free (vpath); vfs_path_free (vpath);

View File

@ -119,7 +119,6 @@ exec_extension (const char *filename, const char *lc_data, int *move_dir, int st
else else
do_local_copy = 0; do_local_copy = 0;
vfs_path_free (vpath);
/* /*
* All commands should be run in /bin/sh regardless of user shell. * All commands should be run in /bin/sh regardless of user shell.
* To do that, create temporary shell script and run it. * To do that, create temporary shell script and run it.
@ -160,6 +159,7 @@ exec_extension (const char *filename, const char *lc_data, int *move_dir, int st
g_free (localcopy); g_free (localcopy);
} }
g_free (file_name); g_free (file_name);
vfs_path_free (vpath);
return; return;
} }
fputs (parameter, cmd_file); fputs (parameter, cmd_file);
@ -229,6 +229,7 @@ exec_extension (const char *filename, const char *lc_data, int *move_dir, int st
fclose (cmd_file); fclose (cmd_file);
unlink (file_name); unlink (file_name);
g_free (file_name); g_free (file_name);
vfs_path_free (vpath);
return; return;
} }
mc_stat (localcopy, &mystat); mc_stat (localcopy, &mystat);
@ -237,7 +238,7 @@ exec_extension (const char *filename, const char *lc_data, int *move_dir, int st
} }
else else
{ {
fn = vfs_canon_and_translate (filename); fn = vfs_path_to_str (vpath);
text = quote_func (fn, 0); text = quote_func (fn, 0);
g_free (fn); g_free (fn);
} }
@ -375,6 +376,7 @@ exec_extension (const char *filename, const char *lc_data, int *move_dir, int st
mc_ungetlocalcopy (filename, localcopy, localmtime != mystat.st_mtime); mc_ungetlocalcopy (filename, localcopy, localmtime != mystat.st_mtime);
g_free (localcopy); g_free (localcopy);
} }
vfs_path_free (vpath);
} }
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */

View File

@ -92,7 +92,11 @@ my_mkdir_rec (char *s, mode_t mode)
} }
p = concat_dir_and_file (s, ".."); p = concat_dir_and_file (s, "..");
q = vfs_canon (p); {
vfs_path_t *vpath = vfs_path_from_str (p);
q = vfs_path_to_str (vpath);
vfs_path_free (vpath);
}
g_free (p); g_free (p);
result = my_mkdir_rec (q, mode); result = my_mkdir_rec (q, mode);
@ -116,9 +120,13 @@ my_mkdir (const char *s, mode_t mode)
result = mc_mkdir (s, mode); result = mc_mkdir (s, mode);
if (result) if (result)
{ {
char *p = vfs_canon (s); vfs_path_t *vpath;
char *p;
vpath = vfs_path_from_str (s);
p = vfs_path_to_str (vpath);
result = my_mkdir_rec (p, mode); result = my_mkdir_rec (p, mode);
vfs_path_free (vpath);
g_free (p); g_free (p);
} }
if (result == 0) if (result == 0)

View File

@ -236,10 +236,13 @@ mcview_done (mcview_t * view)
if (mcview_remember_file_position && view->filename != NULL) if (mcview_remember_file_position && view->filename != NULL)
{ {
char *canon_fname; char *canon_fname;
canon_fname = vfs_canon (view->filename); vfs_path_t *vpath;
vpath = vfs_path_from_str (view->filename);
canon_fname = vfs_path_to_str (vpath);
save_file_position (canon_fname, -1, 0, view->dpy_start, view->saved_bookmarks); save_file_position (canon_fname, -1, 0, view->dpy_start, view->saved_bookmarks);
view->saved_bookmarks = NULL; view->saved_bookmarks = NULL;
g_free (canon_fname); g_free (canon_fname);
vfs_path_free (vpath);
} }
/* Write back the global viewer mode */ /* Write back the global viewer mode */
@ -290,7 +293,9 @@ mcview_set_codeset (mcview_t * view)
const char *cp_id = NULL; const char *cp_id = NULL;
view->utf8 = TRUE; view->utf8 = TRUE;
cp_id = get_codepage_id (mc_global.source_codepage >= 0 ? mc_global.source_codepage : mc_global.display_codepage); cp_id =
get_codepage_id (mc_global.source_codepage >=
0 ? mc_global.source_codepage : mc_global.display_codepage);
if (cp_id != NULL) if (cp_id != NULL)
{ {
GIConv conv; GIConv conv;

View File

@ -387,12 +387,15 @@ mcview_load (mcview_t * view, const char *command, const char *file, int start_l
char *canon_fname; char *canon_fname;
long line, col; long line, col;
off_t new_offset; off_t new_offset;
vfs_path_t *vpath;
canon_fname = vfs_canon (view->filename); vpath = vfs_path_from_str (view->filename);
canon_fname = vfs_path_to_str (vpath);
load_file_position (canon_fname, &line, &col, &new_offset, &view->saved_bookmarks); load_file_position (canon_fname, &line, &col, &new_offset, &view->saved_bookmarks);
new_offset = min (new_offset, mcview_get_filesize (view)); new_offset = min (new_offset, mcview_get_filesize (view));
view->dpy_start = mcview_bol (view, new_offset, 0); view->dpy_start = mcview_bol (view, new_offset, 0);
g_free (canon_fname); g_free (canon_fname);
vfs_path_free (vpath);
} }
else if (start_line > 0) else if (start_line > 0)
mcview_moveto (view, start_line - 1, 0); mcview_moveto (view, start_line - 1, 0);