3

I am new to NodeJS and I'm having a hard time decrypting the mode value and figuring out how to change the mode to remove an attribute like read-only. Here is how I found this folder to have a mode of 16822. How do I determine what 16822 means in windows and then how do I change mode so that it does not have a read only attr?

fs.mkdir('./build');
fs.stat('./build', function(err, stats){
   if(stats.isDirectory()){
       console.log('Its a dir');

       for(var i in stats){
           if('function' !== typeof stats[i]){
               console.log(i + '\t= ' + stats[i]);
           }
       }
   }
});
ndsmyter
  • 6,535
  • 3
  • 22
  • 37
Csharpfunbag
  • 395
  • 3
  • 19

3 Answers3

3

I found the solution. Basically the bit difference between a file with readonly and without was a value of 146. Checking the bits I found this difference matched up in the mode value (128, 16, and 2). This was my solution: github issue with readonly

Also made an improvement where I pass the stream to a function that allows me to transform the file then pass it along using event-stream:

function removeReadonly(){
  function transform(file, cb){
    if((file.stat.mode & 146) == 0){
      file.stat.mode = file.stat.mode | 146;
    }
    cb(null,file);
  };

  return require('event-stream').map(transform);
};
Csharpfunbag
  • 395
  • 3
  • 19
3

I believe 0x92 (146) is incorrect. 0x92 is inspecting the 'other' write bit and the 'group' execute bit. What it should be is 0x222 (546).

File Access Settings of various files are :

4000 : Hidden File

2000 : System File

1000 : Archive bit

0400 : Individual read

0200 : Individual write

0100 : Individual execute

0040 : Group read

0020 : Group write

0010 : Group execute

0004 : Other read

0002 : Other write

0001 : Other execute

Where 1 represents execute, 2 represents write and 4 represents read

see http://www.codingdefined.com/2014/10/alter-file-permissions-in-nodejs.html

  • I had tried 222 originally but it did not work in windows. I think that is the difference here. – Csharpfunbag Jun 24 '15 at 19:47
  • 2
    File attributes are expressed in octal, not hexadecimal. 146 is 222 in octal (correct). 546 is 1042 in octal (incorrect). – error Jul 13 '15 at 17:42
2

I did it on Windows by setting "666" mode

setReadAndWritePermissions(filePath) {
    let mode = fs.statSync(filePath).mode;
    let newMode = mode | 0o666;
    this.nodeFs.chmodSync(filePath, newMode);
}

Mentioned 146 is the same as octal number 0o222