* Truncates the file synchronously. * @param {number} [len] The new length
(len = 0)
| 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. |
no test coverage detected