MCPcopy Index your code
hub / github.com/nodejs/node / truncate

Function truncate

lib/fs.js:1261–1286  ·  view source on GitHub ↗

* Truncates the file. * @param {string | Buffer | URL} path * @param {number} [len] * @param {(err?: Error) => any} callback * @returns {void}

(path, len, callback)

Source from the content-addressed store, hash-verified

1259 * @returns {void}
1260 */
1261function truncate(path, len, callback) {
1262 if (typeof len === 'function') {
1263 callback = len;
1264 len = 0;
1265 } else if (len === undefined) {
1266 len = 0;
1267 }
1268
1269 validateInteger(len, 'len');
1270 len = MathMax(0, len);
1271 validateFunction(callback, 'cb');
1272
1273 const h = vfsState.handlers;
1274 if (h !== null && vfsVoid(h.truncate(path, len), callback)) return;
1275
1276 fs.open(path, 'r+', (er, fd) => {
1277 if (er) return callback(er);
1278 const req = new FSReqCallback();
1279 req.oncomplete = function oncomplete(er) {
1280 fs.close(fd, (er2) => {
1281 callback(aggregateTwoErrors(er2, er));
1282 });
1283 };
1284 binding.ftruncate(fd, len, req);
1285 });
1286}
1287
1288/**
1289 * Synchronously truncates the file.

Callers

nothing calls this directly

Calls 7

vfsVoidFunction · 0.85
aggregateTwoErrorsFunction · 0.85
ftruncateMethod · 0.80
openMethod · 0.65
closeMethod · 0.65
callbackFunction · 0.50
truncateMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…