How to use the ftruncateSync function from fs

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

50
51
52
53
54
55
56
57
58
59
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;
exports.futimes = co.promisify(fs.futimes);
exports.futimesSync = fs.futimesSync;
exports.lchmod = co.promisify(fs.lchmod);
exports.lchmodSync = fs.lchmodSync;
fork icon22
star icon45
watch icon26

541
542
543
544
545
546
547
548
549
550
551
552


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


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

144
145
146
147
148
149
150
151
152
153
154
{
  const file2 = path.resolve(tmp, 'truncate-file-2.txt');
  fs.writeFileSync(file2, 'Hi');
  const fd = fs.openSync(file2, 'r+');
  process.on('beforeExit', () => fs.closeSync(fd));
  fs.ftruncateSync(fd, 4);
  assert(fs.readFileSync(file2).equals(Buffer.from('Hi\u0000\u0000')));
}


{
fork icon0
star icon0
watch icon0

+ 2 other calls in file

218
219
220
221
222
223
224
225
226
227
228
229
  });
}


function createZeroFilledFile(filename) {
  const fd = fs.openSync(filename, 'w');
  fs.ftruncateSync(fd, 10 * 1024 * 1024);
  fs.closeSync(fd);
}



fork icon0
star icon0
watch icon0

33
34
35
36
37
38
39
40
41
42
const filepath = path.join(tmpdir.path, 'large.txt');
const fd = fs.openSync(filepath, 'w+');
const offset = 5 * 1024 * 1024 * 1024; // 5GB
const message = 'Large File';

fs.ftruncateSync(fd, offset);
assert.strictEqual(fs.statSync(filepath).size, offset);
const writeBuf = Buffer.from(message);
fs.writeSync(fd, writeBuf, 0, writeBuf.length, offset);
const readBuf = Buffer.allocUnsafe(writeBuf.length);
fork icon0
star icon0
watch icon0