How to use the fstatSync function from fs

Find comprehensive JavaScript fs.fstatSync code examples handpicked from public code repositorys.

46
47
48
49
50
51
52
53
54
55
exports.fchown = co.promisify(fs.fchown);
exports.fchownSync = fs.fchownSync;
exports.fdatasync = co.promisify(fs.fdatasync);
exports.fdatasyncSync = fs.fdatasyncSync;
exports.fstat = co.promisify(fs.fstat);
exports.fstatSync = fs.fstatSync;
exports.fsync = co.promisify(fs.fsync);
exports.fsyncSync = fs.fsyncSync;
exports.ftruncate = co.promisify(fs.ftruncate);
exports.ftruncateSync = fs.ftruncateSync;
fork icon22
star icon45
watch icon26

102
103
104
105
106
107
108
109
110
111
assert.ifError(err);

if (common.isWindows) {
  assert.ok((fs.fstatSync(fd).mode & 0o777) & mode_async);
} else {
  assert.strictEqual(mode_async, fs.fstatSync(fd).mode & 0o777);
}

fs.fchmodSync(fd, mode_sync);
if (common.isWindows) {
fork icon16
star icon65
watch icon0

33
34
35
36
37
38
39
40
41
42
43


function stat_resource(resource, statSync = fs.statSync) {
  if (typeof resource === 'string') {
    return statSync(resource);
  }
  const stats = fs.fstatSync(resource);
  // Ensure mtime has been written to disk
  // except for directories on AIX where it cannot be synced
  if (common.isAIX && stats.isDirectory())
    return stats;
fork icon42
star icon19
watch icon0

+ 3 other calls in file

118
119
120
121
122
123
124
125
126
127
128
129


  common.runWithInvalidFD((fd) => {
    fs.fstat(fd, common.mustCall(validateError));


    assert.throws(
      () => fs.fstatSync(fd),
      validateError
    );
  });
}
fork icon42
star icon19
watch icon0

578
579
580
581
582
583
584
585
586
587
588
589
}


class ScrollCli {
  CommandFnDecoratorSuffix = "Command"


  executeUsersInstructionsFromShell(args = [], userIsPipingInput = fs.fstatSync(0).isFIFO()) {
    const command = args[0] // Note: we don't take any parameters on purpose. Simpler UX.
    const commandName = `${command}${this.CommandFnDecoratorSuffix}`
    if (this[commandName]) return userIsPipingInput ? this._runCommandOnPipedStdIn(commandName) : this[commandName](process.cwd())
    else if (command) this.log(`No command '${command}'. Running help command.`)
fork icon12
star icon314
watch icon8

28
29
30
31
32
33
34
35
36
37
38
39
40


const dataExpected = fs.readFileSync(__filename, 'utf8');


// sometimes stat returns size=0, but it's a lie.
fs._fstat = fs.fstat;
fs._fstatSync = fs.fstatSync;


fs.fstat = (fd, cb) => {
  fs._fstat(fd, (er, st) => {
    if (er) return cb(er);
fork icon0
star icon1
watch icon0

52
53
54
55
56
57
58
59
60
61
  } else {
    throw er
  }
}

const st = fs.fstatSync(fd)
const headBuf = Buffer.alloc(512)

POSITION: for (position = 0; position < st.size; position += 512) {
  for (let bufPos = 0, bytes = 0; bufPos < 512; bufPos += bytes) {
fork icon0
star icon0
watch icon0

17
18
19
20
21
22
23
24
25
26
  console.log('读取文件失败:', err);
}

// 读取到文件信息
try {
  const fileInfo = fs.fstatSync(fd);
  console.log('文件信息:', fileInfo);
} catch (err) {
  console.log('读取文件信息失败!');
}
fork icon0
star icon0
watch icon0

21
22
23
24
25
26
27
28
29
30
31
32
  }));
  return;
}


if (process.argv[2] === 'child') {
  [0, 1, 2].forEach((i) => fs.fstatSync(i));
  return;
}


// Run the script in a shell but close stdout and stderr.
fork icon0
star icon0
watch icon0

142
143
144
145
146
147
148
149
150
151
152
{
  assert.throws(
    () => fs.fstatSync(9999),
    { code: 'EBADF' });
  assert.throws(
    () => fs.fstatSync(9999, { throwIfNoEntry: false }),
    { code: 'EBADF' });
}


const runCallbackTest = (func, arg, done) => {
fork icon0
star icon0
watch icon0

20
21
22
23
24
25
26
27
28
29
  const w = new Worker(`${preamble}
  parentPort.postMessage(fs.openSync(__filename));
  `, { eval: true, trackUnmanagedFds: false });
  const [ [ fd ] ] = await Promise.all([once(w, 'message'), once(w, 'exit')]);
  assert(fd > 2);
  fs.fstatSync(fd); // Does not throw.
  fs.closeSync(fd);
}

// With trackUnmanagedFds, FDs are closed automatically.
fork icon0
star icon0
watch icon0

66
67
68
69
70
71
72
73
74
75
76


// fstatSync
fs.open('.', 'r', undefined, function(err, fd) {
  var stats;
  try {
    stats = fs.fstatSync(fd);
  } catch (err) {
    got_error = true;
  }
  if (stats) {
fork icon0
star icon0
watch icon0

109
110
111
112
113
114
115
116
117
118
console.log(fs.fstatSync(fd).mode);

if (is_windows) {
  assert.ok((fs.fstatSync(fd).mode & 0777) & mode_async);
} else {
  assert.equal(mode_async, fs.fstatSync(fd).mode & 0777);
}

fs.fchmodSync(fd, mode_sync);
if (is_windows) {
fork icon0
star icon0
watch icon0

12
13
14
15
16
17
18
19
20
21
console.info();
console.info('fs.fstatSync() Done.');
fs.fchownSync(fd, 1001, 1001); // TODO: 更改文件所有权(同步方法)
console.info('fs.fchownSync() Done.');
console.info();
var fstatSync_suf = fs.fstatSync(fd); // TODO: 获取文件信息(同步方法)
console.info('txt/fchownSync.txt file info: ');
console.info(fstatSync_suf); // TODO: 打印输出文件信息
console.info();
console.info('fs.fstatSync() Done.');
fork icon0
star icon0
watch icon0

+ 3 other calls in file

219
220
221
222
223
224
225
226
227
228

const fd = fs.openSync('/some/file', 'r');

const server = http2.createServer();
server.on('stream', (stream) => {
  const stat = fs.fstatSync(fd);
  const headers = {
    'content-length': stat.size,
    'last-modified': stat.mtime.toUTCString(),
    'content-type': 'text/plain'
fork icon0
star icon0
watch icon1

+ 11 other calls in file