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

Method truncateSync

lib/internal/vfs/file_handle.js:678–705  ·  view source on GitHub ↗

* Truncates the file synchronously. * @param {number} [len] The new length

(len = 0)

Source from the content-addressed store, hash-verified

676 * @param {number} [len] The new length
677 */
678 truncateSync(len = 0) {
679 this.#checkClosed('ftruncate');
680 this.#checkWritable();
681
682 if (len < this.#size) {
683 // Zero out truncated region to avoid stale data
684 this.#content.fill(0, len, this.#size);
685 this.#size = len;
686 } else if (len > this.#size) {
687 if (len > this.#content.length) {
688 const newContent = Buffer.alloc(len);
689 this.#content.copy(newContent, 0, 0, this.#size);
690 this.#content = newContent;
691 } else {
692 // Buffer has enough capacity, just zero-fill the extension
693 this.#content.fill(0, this.#size, len);
694 }
695 this.#size = len;
696 }
697
698 // Update the entry's content, mtime, and ctime
699 if (this.#entry) {
700 const now = DateNow();
701 this.#entry.content = this.#content.subarray(0, this.#size);
702 this.#entry.mtime = now;
703 this.#entry.ctime = now;
704 }
705 }
706
707 /**
708 * Truncates the file.

Calls 5

#checkClosedMethod · 0.95
#checkWritableMethod · 0.95
fillMethod · 0.80
allocMethod · 0.80
copyMethod · 0.65

Tested by

no test coverage detected