1
1

(resolve_symlinks): use (vfs_path_t *) as argument.

Minor optimization of resolve_symlinks() and diff_two_paths().

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
Этот коммит содержится в:
Andrew Borodin 2012-03-31 13:07:22 +04:00 коммит произвёл Slava Zanko
родитель 2fe6ab5e9f
Коммит b1bcf6b01f

Просмотреть файл

@ -110,25 +110,22 @@ is_8bit_printable (unsigned char c)
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
static char * static char *
resolve_symlinks (const char *path) resolve_symlinks (const vfs_path_t *vpath)
{ {
char *p;
char *buf, *buf2, *q, *r, c; char *buf, *buf2, *q, *r, c;
int len;
struct stat mybuf; struct stat mybuf;
const char *p;
vfs_path_t *vpath;
if (*path != PATH_SEP) if (vpath->relative)
return NULL; return NULL;
vpath = vfs_path_from_str (path); p = vfs_path_to_str (vpath);
r = buf = g_malloc (MC_MAXPATHLEN); r = buf = g_malloc (MC_MAXPATHLEN);
buf2 = g_malloc (MC_MAXPATHLEN); buf2 = g_malloc (MC_MAXPATHLEN);
*r++ = PATH_SEP; *r++ = PATH_SEP;
*r = 0; *r = 0;
p = path;
for (;;) do
{ {
q = strchr (p + 1, PATH_SEP); q = strchr (p + 1, PATH_SEP);
if (!q) if (!q)
@ -143,19 +140,19 @@ resolve_symlinks (const char *path)
{ {
g_free (buf); g_free (buf);
buf = NULL; buf = NULL;
*q = c;
goto ret; goto ret;
} }
if (!S_ISLNK (mybuf.st_mode)) if (!S_ISLNK (mybuf.st_mode))
strcpy (r, p + 1); strcpy (r, p + 1);
else else
{ {
int len;
len = mc_readlink (vpath, buf2, MC_MAXPATHLEN - 1); len = mc_readlink (vpath, buf2, MC_MAXPATHLEN - 1);
if (len < 0) if (len < 0)
{ {
g_free (buf); g_free (buf);
buf = NULL; buf = NULL;
*q = c;
goto ret; goto ret;
} }
buf2[len] = 0; buf2[len] = 0;
@ -167,15 +164,16 @@ resolve_symlinks (const char *path)
canonicalize_pathname (buf); canonicalize_pathname (buf);
r = strchr (buf, 0); r = strchr (buf, 0);
if (!*r || *(r - 1) != PATH_SEP) if (!*r || *(r - 1) != PATH_SEP)
/* FIXME: this condition is always true because r points to the EOL */
{ {
*r++ = PATH_SEP; *r++ = PATH_SEP;
*r = 0; *r = 0;
} }
*q = c; *q = c;
p = q; p = q;
if (!c)
break;
} }
while (c != '\0');
if (!*buf) if (!*buf)
strcpy (buf, PATH_SEP_STR); strcpy (buf, PATH_SEP_STR);
else if (*(r - 1) == PATH_SEP && r != buf + 1) else if (*(r - 1) == PATH_SEP && r != buf + 1)
@ -183,7 +181,7 @@ resolve_symlinks (const char *path)
ret: ret:
g_free (buf2); g_free (buf2);
vfs_path_free (vpath); g_free (p);
return buf; return buf;
} }
@ -977,22 +975,14 @@ diff_two_paths (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
char *p, *q, *r, *s, *buf = NULL; char *p, *q, *r, *s, *buf = NULL;
int i, j, prevlen = -1, currlen; int i, j, prevlen = -1, currlen;
char *my_first = NULL, *my_second = NULL; char *my_first = NULL, *my_second = NULL;
char *path_str;
path_str = vfs_path_to_str (vpath1); my_first = resolve_symlinks (vpath1);
my_first = resolve_symlinks (path_str);
g_free (path_str);
if (my_first == NULL) if (my_first == NULL)
return NULL; goto ret;
path_str = vfs_path_to_str (vpath2); my_second = resolve_symlinks (vpath2);
my_second = resolve_symlinks (path_str);
g_free (path_str);
if (my_second == NULL) if (my_second == NULL)
{ goto ret;
g_free (my_first);
return NULL;
}
for (j = 0; j < 2; j++) for (j = 0; j < 2; j++)
{ {
@ -1028,11 +1018,7 @@ diff_two_paths (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
if (currlen < prevlen) if (currlen < prevlen)
g_free (buf); g_free (buf);
else else
{ goto ret;
g_free (my_first);
g_free (my_second);
return buf;
}
} }
p = buf = g_malloc (currlen); p = buf = g_malloc (currlen);
prevlen = currlen; prevlen = currlen;
@ -1040,6 +1026,8 @@ diff_two_paths (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
strcpy (p, "../"); strcpy (p, "../");
strcpy (p, q); strcpy (p, q);
} }
ret:
g_free (my_first); g_free (my_first);
g_free (my_second); g_free (my_second);
return buf; return buf;