aconbere / node-file-utils

A collection of async node file system utilities
MIT License
43 stars 4 forks source link

`~foobar` maps to `/home/foobar/foobar` #12

Open amagura opened 10 years ago

amagura commented 10 years ago

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.