Search code examples
node.jsntfsfsfile-attributes

nodejs fs mode manipulation in windows read-only attribute


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]);
           }
       }
   }
});

Solution

  • 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);
    };