Skip to content

Commit

Permalink
When handling bookmark, use readlink, not realpath
Browse files Browse the repository at this point in the history
This is to cd to path as it pointed by symlink, not to it's real path. Bookmarked directory may itself contain symlinks in path, which should be respected.

For example: if directory is physically in /mnt/storage/some and it's symlinked to ~/some and directory ~/some/dir added to bookmarks, it's expected that when following bookmark directory will be changed to ~/some/dir (as in bookmark's link) not to /mnt/storage/some/dir (as dir real path).
  • Loading branch information
me committed Nov 22, 2023
1 parent 485079d commit e5a2587
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/nnn.c
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,21 @@ static char *abspath(const char *filepath, char *cwd, char *buf)
return resolved_path;
}

/* finds abspath of link pointed by filepath, taking cwd into account */
static char *bmtarget(const char *filepath, char *cwd, char *buf)
{
char target[PATH_MAX + 1];

int n = readlink(filepath, target, PATH_MAX);
if (n != -1) {
target[n] = '\0';

return abspath(target, cwd, buf);
} else {
return NULL;
}
}

/* wraps the argument in single quotes so it can be safely fed to shell */
static bool shell_escape(char *output, size_t outlen, const char *s)
{
Expand Down Expand Up @@ -7053,7 +7068,7 @@ static bool browse(char *ipath, const char *session, int pkey)

pent = &pdents[cur];
if (!g_state.selbm || !(S_ISLNK(pent->mode) &&
realpath(pent->name, newpath) &&
bmtarget(pent->name, path, newpath) &&
xstrsncpy(path, lastdir, PATH_MAX)))
mkpath(path, pent->name, newpath);
g_state.selbm = 0;
Expand Down

0 comments on commit e5a2587

Please sign in to comment.