How to use the stat function from graceful-fs

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

graceful-fs.stat is a method that retrieves file system stats for a given file path, similar to the built-in fs.stat method but with added robustness for handling errors and race conditions.

123
124
125
126
127
128
129
130
131
132
133
}


exports.safeStat_p = safeStat_p;
function safeStat_p(path) {
  var deferred = Q.defer();
  fs.stat(path, function(err, stat) {
    if (err)
      deferred.resolve(null);
    else
      deferred.resolve(stat);
fork icon291
star icon672
watch icon0

352
353
354
355
356
357
358
359
360
361
  }
})

var todo = 3
var errState = null
fs.stat(p, function (er, current) {
  if (er) return cb(errState = er)
  endChmod(self, dirProps, current, p, next)
  endChown(self, dirProps, current, p, next)
  endUtimes(self, dirProps, current, p, next)
fork icon0
star icon2
watch icon0

How does graceful-fs.stat work?

graceful-fs.stat works similarly to the built-in fs.stat method, but with added robustness to handle various errors and race conditions that can occur when performing file system operations. When called, graceful-fs.stat first checks if the file path is cached in its internal cache. If it is, it returns the cached stats. Otherwise, it attempts to read the file stats using fs.stat. If an error occurs during the file system operation, graceful-fs.stat retries the operation with a delay and limited number of retries to prevent the operation from being retried indefinitely and potentially causing the program to hang. Additionally, graceful-fs.stat uses a global lock to prevent multiple threads from accessing the file system simultaneously, which can lead to race conditions and file system corruption. Overall, graceful-fs.stat provides a more robust and resilient way to retrieve file system stats compared to the built-in fs.stat method.

29
30
31
32
33
34
35
36
37
38
if (err) return callback(err)
fs.futimes(fd, d, d, err => {
  if (err) return callback(err)
  fs.close(fd, err => {
    if (err) return callback(err)
    fs.stat(tmpfile, (err, stats) => {
      if (err) return callback(err)
      callback(null, stats.mtime > 1435410243000)
    })
  })
fork icon1
star icon0
watch icon0

91
92
93
94
95
96
97
98
99

var versioned = common.registry + "/underscore/1.3.3"
npm.registry.get(versioned, PARAMS, function (er, data) {
  t.ifError(er, "loaded specified version underscore data")
  t.equal(data.version, "1.3.3")
  fs.stat(getCachePath(versioned), function (er) {
    t.ifError(er, "underscore 1.3.3 cache data written")
  })
})
fork icon0
star icon1
watch icon1

+ 5 other calls in file

Ai Example

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

fs.stat("/path/to/file", (err, stats) => {
  if (err) {
    console.error(err);
    return;
  }

  console.log(`File size: ${stats.size}`);
  console.log(`File last modified: ${stats.mtime}`);
});

This code uses graceful-fs.stat to retrieve the stats for the file located at /path/to/file. The stats object returned by graceful-fs.stat contains information such as the file size and the last modified timestamp. This information is then logged to the console. Note that this code is very similar to the equivalent code using the built-in fs.stat method. The main difference is that graceful-fs.stat provides additional robustness and resiliency when accessing the file system.

470
471
472
473
474
475
476
477
478
479
480
    }
  })
}


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)
  })
fork icon0
star icon1
watch icon1

+ 3 other calls in file

77
78
79
80
81
82
83
84
85
if (params.skipCache) {
  return get_.call(this, uri, cachePath, params, cb)
}

var client = this
fs.stat(cachePath, function (er, stat) {
  if (!er) {
    fs.readFile(cachePath, function (er, data) {
      data = parseJSON.noExceptions(data)
fork icon0
star icon1
watch icon1

+ 14 other calls in file

252
253
254
255
256
257
258
259
260

// this logic ported from the old `gyp_addon` python file
var gypScript = path.resolve(__dirname, '..', 'gyp', 'gyp_main.py')
var addonGypi = path.resolve(__dirname, '..', 'addon.gypi')
var commonGypi = path.resolve(nodeDir, 'include/node/common.gypi')
fs.stat(commonGypi, function (err) {
  if (err) {
    commonGypi = path.resolve(nodeDir, 'common.gypi')
  }
fork icon0
star icon0
watch icon1

+ 3 other calls in file

79
80
81
82
83
84
85
86
87
return new Promise(function stat (resolve) {
  if (options.mode && options.chown) resolve()
  else {
    // Either mode or chown is not explicitly set
    // Default behavior is to copy it from original file
    fs.stat(truename, function (err, stats) {
      if (err || !stats) resolve()
      else {
        options = Object.assign({}, options)
fork icon0
star icon0
watch icon0

211
212
213
214
215
216
217
218
219
220
if (err) return callback(err)
var name = configNames.shift()
if (!name) return runGyp()
var fullPath = path.resolve(name)
log.verbose(name, 'checking for gypi file: %s', fullPath)
fs.stat(fullPath, function (err, stat) {
  if (err) {
    if (err.code == 'ENOENT') {
      findConfigs() // check next gypi filename
    } else {
fork icon0
star icon0
watch icon0

+ 3 other calls in file

162
163
164
165
166
167
168
169
170
171
172
173
  return true;
}


function reflectStat(path, file, callback) {
  // Set file.stat to the reflect current state on disk
  fs.stat(path, onStat);


  function onStat(statErr, stat) {
    if (statErr) {
      return callback(statErr);
fork icon0
star icon0
watch icon1

17
18
19
20
21
22
23
24
25
26

fs.mkdir(dirpath, mode, onMkdir);

function onMkdir(mkdirErr) {
  if (!mkdirErr) {
    return fs.stat(dirpath, onStat);
  }

  switch (mkdirErr.code) {
    case 'ENOENT': {
fork icon0
star icon0
watch icon1

137
138
139
140
141
142
143
144
145
146
147
exports.normalizeWinPath = process.platform === 'win32' ? replaceWinPath : _.identity


exports.mkdirIfNotExists = function mkdir (directory, done) {
  // TODO(vojta): handle if it's a file
  /* eslint-disable handle-callback-err */
  fs.stat(directory, function (err, stat) {
    if (stat && stat.isDirectory()) {
      done()
    } else {
      mkdir(path.dirname(directory), function () {
fork icon0
star icon0
watch icon1

+ 13 other calls in file

32
33
34
35
36
37
38
39
40
41
if (options.mode && options.chown) {
  return thenWriteFile()
} else {
  // Either mode or chown is not explicitly set
  // Default behavior is to copy it from original file
  return fs.stat(filename, function (err, stats) {
    if (err || !stats) return thenWriteFile()

    options = extend({}, options)
    if (!options.mode) {
fork icon0
star icon0
watch icon0

+ 2 other calls in file