1

I want to get the file states of a specific .js file. so i used the below posted code. but the first log statement returned

33204

while the second one returned:

144

Output:

Stats {
dev: 2049,
mode: 33204,
 nlink: 2,
 uid: 1000,
}

but I want to interpret these number to know if the file is accessible for (read, write) or not

code:

const fs = require('fs');
var mode = fs.statSync('../../var/opt/personal/guest/op/op_12201/data/persGuesOapDataFolder00/test0.js').mode;
var writePermissions = mode & 0x92; // 010010010

console.log(mode);
console.log(writePermissions);
Amrmsmb
  • 1
  • 27
  • 104
  • 226

2 Answers2

1

this mode indicates some info regarding the file like:

  • Permissions (for user, group and other)
  • type of file

To filter a bit this data, there are some masks defined at OS to extract the info we need. These masks are:

* For filter permissions (user, group and other)

    The following mask values are defined for the file mode component of the st_mode field:

    S_ISUID     04000   set-user-ID bit
    S_ISGID     02000   set-group-ID bit (see below)
    S_ISVTX     01000   sticky bit (see below)

    S_IRWXU     00700   owner has read, write, and execute permission
    S_IRUSR     00400   owner has read permission
    S_IWUSR     00200   owner has write permission
    S_IXUSR     00100   owner has execute permission

    S_IRWXG     00070   group has read, write, and execute permission
    S_IRGRP     00040   group has read permission
    S_IWGRP     00020   group has write permission
    S_IXGRP     00010   group has execute permission

    S_IRWXO     00007   others (not in group) have read, write, and
                       execute permission
    S_IROTH     00004   others have read permission
    S_IWOTH     00002   others have write permission
    S_IXOTH     00001   others have execute permission

* For detect type of file

The following mask values are defined for the file type:
  S_IFMT     0170000   bit mask for the file type bit field
  S_IFSOCK   0140000   socket
  S_IFLNK    0120000   symbolic link
  S_IFREG    0100000   regular file
  S_IFBLK    0060000   block device
  S_IFDIR    0040000   directory
  S_IFCHR    0020000   character device
  S_IFIFO    0010000   FIFO

Scenario 1: The file has read access (for everyone)

const fs = require('fs');

const mode = fs.statSync('./yourfile.txt').mode;

if (mode & (fs.constants.S_IRUSR | fs.constants.S_IRGRP | fs.constants.S_IROTH)) {
  console.log('file has read permissions');
}

Scenario 2: Is a symbolic link

if (mode & fs.constants.S_IFLNK) {
  console.log("It's a symbolic link");
} else {
  console.log("It's not a symbolic link");
}

Hope this helps you to understand how OS works (unix systems). More info: http://man7.org/linux/man-pages/man7/inode.7.html

Jose Mato
  • 2,709
  • 1
  • 17
  • 18
0

check if a stat object is readable or writable

useful to check if a pipe is readable or writable

const fs = require('fs');
const os = require('os');

function checkAccess(s, check="r") {
  // check if s=fs.statSync(path) is readable or writable
  const { uid: u, gid: g } = os.userInfo();
  const m = s.mode;
  if (check == "r") {
    return (
      ((s.uid == u) && (m & fs.constants.S_IRUSR)) ||
      ((s.gid == g) && (m & fs.constants.S_IRGRP)) ||
      (m & fs.constants.S_IROTH)
    ) != 0;
  }
  if (check == "w") {
    return (
      ((s.uid == u) && (m & fs.constants.S_IWUSR)) ||
      ((s.gid == g) && (m & fs.constants.S_IWGRP)) ||
      (m & fs.constants.S_IWOTH)
    ) != 0;
  }
  throw Exception("check must be r or w");
}

var s = fs.fstatSync(0); // fd 0 == stdin
console.log("fd 0 is readable?", checkAccess(s, "r"));

var s = fs.fstatSync(1); // fd 1 == stdout
console.log("fd 1 is writable?", checkAccess(s, "w"));

fs.writeSync(1, "hello\n");

see also: same thing in python

probably breaks on windows

milahu
  • 2,447
  • 1
  • 18
  • 25