node-file-utils icon indicating copy to clipboard operation
node-file-utils copied to clipboard

`~foobar` maps to `/home/foobar/foobar`

Open amagura opened this issue 11 years ago • 0 comments

file.path.abspath('~foobar') incorrectly maps ~foobar to $HOME/foobar instead of the expected /home/foobar.

One suggestion would be to check to see if an unescaped ~ is present in provided path and then check if there are no filesystem path delimiters:

function abspath(path) {
   if (path.match(/\//) === null && path.match(/\\~/) === null) {
     /* path probably refers to a user's home directory */
   } else {
    /* path probably doesn't refer to a user's home directory */
   }
});

Or you could try something like this, assuming you can find a reliable means of getting user x's $HOME:

a = '~<USER>';
b = '\\~<USER>';

a.replace(/~.*/, <USERS HOME>) === <USERS HOME>
// true

b.replace(/~.*/, <USERS HOME>) === <USERS HOME>
// false

/* only works if USER is `foobar` */
a = '~foobar';
b = '\\~foobar';

a.replace(/~.*/, process.env.HOME) === process.env.HOME
// true

b.replace(/~.*/, process.env.HOME) === process.env.HOME
// false

or something like that.

amagura avatar Apr 30 '14 23:04 amagura