How to use the chmod function from graceful-fs

Find comprehensive JavaScript graceful-fs.chmod code examples handpicked from public code repositorys.

graceful-fs.chmod is a function used in Node.js that changes the permissions (mode) of a file or directory to the specified mode.

143
144
145
146
147
148
149
150
151
152
function mkDir (dir, target) {
  fs.mkdir(target, dir.mode, function (err) {
    if (err) return onError(err)
    // despite setting mode in fs.mkdir, doesn't seem to work
    // so we set it here.
    fs.chmod(target, dir.mode, function (err) {
      if (err) return onError(err)
      copyDir(dir.name)
    })
  })
fork icon1
star icon0
watch icon0

473
474
475
476
477
478
479
480
481
482
483
484


function addMode (cachedRemote, mode, cb) {
  fs.stat(cachedRemote, function (er, stats) {
    if (er) return cb(er)
    mode = stats.mode | mode
    fs.chmod(cachedRemote, mode, cb)
  })
}


// taken from https://github.com/isaacs/chmodr/blob/master/chmodr.js
fork icon0
star icon1
watch icon1

How does graceful-fs.chmod work?

graceful-fs.chmod is a function provided by the Node.js module 'graceful-fs', which is used for handling file system operations. This function allows you to change the mode (i.e., permissions) of a file or directory specified by its path. To use it, you pass the file path and the desired mode as arguments to the function. The mode can be specified as an octal number or a string of the form 'rwxrwxrwx'. The function then sets the mode of the file or directory to the specified value. Under the hood, the implementation of graceful-fs.chmod varies depending on the platform and the version of Node.js being used. On POSIX-compliant systems (e.g., Linux, macOS), the function calls the 'chmod' system call to change the mode of the file or directory. On Windows systems, the function uses the 'icacls' command to change the mode. In either case, the function checks for errors and throws an exception if the operation fails. Overall, graceful-fs.chmod is a convenient way to change the mode of a file or directory in a platform-independent manner, without having to worry about the specific implementation details of the underlying file system.

52
53
54
55
56
57
58
59
60
61

    if (!customMode) {
      return callback();
    }

    fs.chmod(dirpath, mode, callback);
  }
}

function onRecurse(recurseErr) {
fork icon0
star icon0
watch icon1

141
142
143
144
145
146
147
148
149
150
    })
  }
}).then(function chmod () {
  if (options.mode) {
    return new Promise(function (resolve, reject) {
      fs.chmod(tmpfile, options.mode, function (err) {
        if (err) reject(err)
        else resolve()
      })
    })
fork icon0
star icon0
watch icon0

+ 3 other calls in file

Ai Example

1
2
3
4
5
6
7
const fs = require("graceful-fs");

// Set the file mode to 644 (owner can read and write, others can read only)
fs.chmod("/path/to/myfile.txt", 0o644, (err) => {
  if (err) throw err;
  console.log("File mode changed successfully!");
});

In this example, fs.chmod is used to change the mode of the file /path/to/myfile.txt to 0o644 (the octal representation of rw-r--r--). The function takes a callback function that is called when the operation completes. If an error occurs during the operation, the function throws an error that can be caught using a try-catch block or handled in some other way. Note that fs.chmod is an asynchronous function, meaning that it returns immediately and the actual file mode change is performed in the background. If you need to perform multiple file system operations in a specific order, you may want to use the fs.promises API or a third-party module like 'async' to manage the callbacks and ensure that the operations are performed in the correct order.