Inspecting file permission in nodejs

There's function to show file stat in nodejs, but to access file permission is a bit tricky. Keep this trick here would save my time someday.

I took hours to find this trick, yep my bad. fs.Stats object contain this information

   {
       dev: 2114,
       ino: 48064969,
       mode: 33188,
       nlink: 1,
       uid: 85,
       gid: 100,
       rdev: 0,
       size: 527,
       blksize: 4096,
       blocks: 8,
       atime: Mon, 10 Oct 2011 23:24:11 GMT,
       mtime: Mon, 10 Oct 2011 23:24:11 GMT,
       ctime: Mon, 10 Oct 2011 23:24:11 GMT
   }

There's permission information in line 3 mode: 33188 it's different with unix permission information. Unix file system information use octal number instead of decimal as fs.Stats.mode did . Thank's daniel for this, i won't know it if he don't say that.

Later than i found this question and answer by simo that help me a lot. Here is the detail.

    var stat = fs.statSync('file.txt');
    var permission = parseInt(stat.mode.toString(8), 10);
    console.log(permission); // 100664

From the code above, we can use parseInt and toString function to convert decimal number to octal.

Comments