How to use the chown function from graceful-fs

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

graceful-fs.chown is a method used to change the ownership of a file or directory in a graceful manner, which means it falls back to an alternate implementation when encountering known errors.

386
387
388
389
390
391
392
393
394
395
mkdir(templates, function (er) {
  if (er) return cb(er)

  // Ensure that both the template and remotes directories have the correct
  // permissions.
  fs.chown(templates, stats.uid, stats.gid, function (er) {
    if (er) return cb(er)

    fs.chown(remotes, stats.uid, stats.gid, function (er) {
      cb(er, stats)
fork icon0
star icon1
watch icon1

+ 3 other calls in file

79
80
81
82
83
84
85
86
87
88
  if (writeStream.__atomicClosed) return
  writeStream.__atomicClosed = true
  if (writeStream.__atomicChown) {
    var uid = writeStream.__atomicChown.uid
    var gid = writeStream.__atomicChown.gid
    return fs.chown(writeStream.__atomicTmp, uid, gid, iferr(cleanup, moveIntoPlace))
  } else {
    moveIntoPlace()
  }
}
fork icon0
star icon0
watch icon0

How does graceful-fs.chown work?

The graceful-fs.chown method is used to change the owner and/or group of a file or directory asynchronously by delegating to the underlying fs.chown method while handling errors and retrying on failure in a graceful way.

132
133
134
135
136
137
138
139
140
141
  })
}).then(function chown () {
  fd = null
  if (options.chown) {
    return new Promise(function (resolve, reject) {
      fs.chown(tmpfile, options.chown.uid, options.chown.gid, function (err) {
        if (err) reject(err)
        else resolve()
      })
    })
fork icon0
star icon0
watch icon0

+ 3 other calls in file

-1
fork icon0
star icon0
watch icon1

+ 3 other calls in file

Ai Example

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

fs.chown("/path/to/file", 1001, 1001, (err) => {
  if (err) throw err;
  console.log("File ownership changed successfully");
});

In this example, chown changes the owner and group of the file located at /path/to/file to the user and group with the IDs 1001. If the operation is successful, it logs a message to the console.