3 const call = require('./call');
8 * Retrieves the {@link fs.Stats} for the given path. If the path is a symbolic link,
9 * then the Stats of the symlink's target are returned instead. If the symlink is broken,
10 * then the Stats of the symlink itself are returned.
12 * @param {object} fs - Synchronous or Asynchronouse facade for the "fs" module
13 * @param {string} path - The path to return stats for
14 * @param {function} callback
16 function stat (fs, path, callback) {
17 let isSymLink = false;
19 call.safe(fs.lstat, path, (err, lstats) => {
21 // fs.lstat threw an eror
26 isSymLink = lstats.isSymbolicLink();
29 // lstats.isSymbolicLink() threw an error
30 // (probably because fs.lstat returned an invalid result)
31 return callback(err2);
35 // Try to resolve the symlink
36 symlinkStat(fs, path, lstats, callback);
39 // It's not a symlink, so return the stats as-is
40 callback(null, lstats);
46 * Retrieves the {@link fs.Stats} for the target of the given symlink.
47 * If the symlink is broken, then the Stats of the symlink itself are returned.
49 * @param {object} fs - Synchronous or Asynchronouse facade for the "fs" module
50 * @param {string} path - The path of the symlink to return stats for
51 * @param {object} lstats - The stats of the symlink
52 * @param {function} callback
54 function symlinkStat (fs, path, lstats, callback) {
55 call.safe(fs.stat, path, (err, stats) => {
57 // The symlink is broken, so return the stats for the link itself
58 return callback(null, lstats);
62 // Return the stats for the resolved symlink target,
63 // and override the `isSymbolicLink` method to indicate that it's a symlink
64 stats.isSymbolicLink = () => true;
67 // Setting stats.isSymbolicLink threw an error
68 // (probably because fs.stat returned an invalid result)
69 return callback(err2);
72 callback(null, stats);