How to use the lstatSync function from graceful-fs

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

graceful-fs.lstatSync is a method that retrieves the metadata of a file system object in a synchronous manner.

523
524
525
526
527
528
529
530
531
532
533
exports.stat = Promise.promisify(fs.stat);
exports.statSync = fs.statSync;
exports.fstat = Promise.promisify(fs.fstat);
exports.fstatSync = fs.fstatSync;
exports.lstat = Promise.promisify(fs.lstat);
exports.lstatSync = fs.lstatSync;


// truncate
exports.truncate = Promise.promisify(fs.truncate);
exports.truncateSync = fs.truncateSync;
fork icon9
star icon48
watch icon0

+ 7 other calls in file

1110
1111
1112
1113
1114
1115
1116
1117
1118

if (item) {
  let stats = null;

  try {
    stats = fs.lstatSync(fs.realpathSync(item)).isFile();
  } catch (error) {
    // Ignore error
  }
fork icon0
star icon0
watch icon1

+ 5 other calls in file

How does graceful-fs.lstatSync work?

The graceful-fs.lstatSync function is used to retrieve the metadata of a file system object in a synchronous manner. It takes a single argument, the path to the file system object. The returned metadata includes information such as the file type (file, directory, etc.), size, creation and modification times, and ownership and permission information. The function throws an error if the specified path is invalid or if the user does not have permission to access the file system object. Unlike the fs.lstatSync function, graceful-fs.lstatSync implements a backoff strategy to avoid EMFILE errors that can occur when too many file descriptors are open at once.

38
39
40
41
42
43
44
45
46
47
48
function createLinkSync (srcpath, dstpath) {
  const destinationExists = fs.existsSync(dstpath)
  if (destinationExists) return undefined


  try {
    fs.lstatSync(srcpath)
  } catch (err) {
    err.message = err.message.replace('lstat', 'ensureLink')
    throw err
  }
fork icon0
star icon0
watch icon1

+ 5 other calls in file

11
12
13
14
15
16
17
18
19
20
21
let needChown = typeof process.getuid === 'function'


const getCacheOwner = () => {
  let st
  try {
    st = fs.lstatSync(cache)
  } catch (er) {
    if (er.code !== 'ENOENT') {
      throw er
    }
fork icon0
star icon0
watch icon0

+ 3 other calls in file

Ai Example

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

const stats = fs.lstatSync("/path/to/file");

console.log(stats.isFile()); // outputs true if the path points to a file, false otherwise
console.log(stats.isDirectory()); // outputs true if the path points to a directory, false otherwise
console.log(stats.isSymbolicLink()); // outputs true if the path points to a symbolic link, false otherwise
console.log(stats.size); // outputs the size of the file or directory in bytes
console.log(stats.mtime); // outputs the date and time the file or directory was last modified

In this example, graceful-fs.lstatSync() is used to retrieve information about a file or directory at the specified path. The returned stats object contains information such as whether the path points to a file, directory, or symbolic link, as well as the size of the file or directory and the date and time it was last modified.