* Truncates the file. * @param {string | Buffer | URL} path * @param {number} [len] * @param {(err?: Error) => any} callback * @returns {void}
(path, len, callback)
| 1259 | * @returns {void} |
| 1260 | */ |
| 1261 | function 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. |
nothing calls this directly
no test coverage detected
searching dependent graphs…