maerics
maerics

Reputation: 156534

How to test file permissions using node.js?

How can I check to see the permissions (read/write/execute) that a running node.js process has on a given file?

I was hoping that the fs.Stats object had some information about permissions but I don't see any. Is there some built-in function that will allow me to do such checks? For example:

var filename = '/path/to/some/file';
if (fs.canRead(filename)) // OK...
if (fs.canWrite(filename)) // OK...
if (fs.canExecute(filename)) // OK...

Surely I don't have to attempt to open the file in each of those modes and handle an error as the negative affirmation, right? There's got to be a simpler way...

Upvotes: 15

Views: 20974

Answers (5)

Franco
Franco

Reputation: 868

I Implemented a helper function which you can await.

The code:

function checkFolderPermission (path) {
    return new Promise((resolve) => {
        fs.access(path, fs.constants.X_OK || fs.constants.R_OK || fs.constants.W_OK, (err) => {
            if (err) {
                resolve(false);
            } else {
                resolve(true);
              }
        })
    })
}

This checks for Read, Write and Execute permission.

Upvotes: 0

Mamdouh Saeed
Mamdouh Saeed

Reputation: 2324

There is fs.accessSync(path[, mode]) nicely mentioned:

Synchronously tests a user's permissions for the file or directory specified by path. The mode argument is an optional integer that specifies the accessibility checks to be performed. Check File Access Constants for possible values of mode. It is possible to create a mask consisting of the bitwise OR of two or more values (e.g. fs.constants.W_OK | fs.constants.R_OK).

If any of the accessibility checks fail, an Error will be thrown. Otherwise, the method will return undefined.

Embeded example:

try {
  fs.accessSync('etc/passwd', fs.constants.R_OK | fs.constants.W_OK);
  console.log('can read/write');
} catch (err) {
  console.error('no access!');
}

Upvotes: 8

user405398
user405398

Reputation:

I am late, but, I was looking for same reasons as yours and learnt about this.

fs.access is the one you need. It is available from node v0.11.15.

function canWrite(path, callback) {
  fs.access(path, fs.W_OK, function(err) {
    callback(null, !err);
  });
}

canWrite('/some/file/or/folder', function(err, isWritable) {
  console.log(isWritable); // true or false
});

Upvotes: 21

zemirco
zemirco

Reputation: 16395

How about using a child process?

var cp = require('child_process');

cp.exec('ls -l', function(e, stdout, stderr) {
  if(!e) {
    console.log(stdout);
    console.log(stderr);
    // process the resulting string and check for permission
  }
});

Not sure though if process and *child_process* share the same permissions.

Upvotes: -3

lanzz
lanzz

Reputation: 43178

Checking readability is not so straightforward as languages like PHP make it look by abstracting it in a single library function. A file might be readable to everyone, or only to its group, or only to its owner; if it is not readble to everybody, you will need to check if you are actually a member of the group, or if you are the owner of the file. It is usually much easier and faster (not only to write the code, but also to execute the checks) to try to open the file and handle the error.

Upvotes: 1

Related Questions