Skip to content

Commit

Permalink
fix: extend with null bytes when calling truncate with a len grea…
Browse files Browse the repository at this point in the history
…ter than the current file size (#875)

* fix: truncate to larger size

* chore: add missing semicolon
  • Loading branch information
toyobayashi committed Oct 28, 2022
1 parent 0233301 commit 25027fb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/__tests__/promises.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ describe('Promises API', () => {
const fileHandle = await promises.open('/foo', 'r+');
await fileHandle.truncate(5);
expect(vol.readFileSync('/foo').toString()).toEqual('01234');
await fileHandle.truncate(7);
expect(vol.readFileSync('/foo').toString()).toEqual('01234\0\0');
await fileHandle.close();
});
it('Reject when the file handle was closed', async () => {
Expand Down Expand Up @@ -592,6 +594,8 @@ describe('Promises API', () => {
it('Truncate an existing file', async () => {
await promises.truncate('/foo', 5);
expect(vol.readFileSync('/foo').toString()).toEqual('01234');
await promises.truncate('/foo', 7);
expect(vol.readFileSync('/foo').toString()).toEqual('01234\0\0');
});
it('Reject when file does not exist', () => {
return expect(promises.truncate('/bar')).rejects.toBeInstanceOf(Error);
Expand Down
7 changes: 7 additions & 0 deletions src/__tests__/volume.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,13 @@ describe('volume', () => {
vol.truncateSync('/1', 2);
expect(vol.readFileSync('/1', 'utf8')).toBe('12');
});
it('Larger truncate', () => {
const fd = vol.openSync('/2', 'w');
vol.writeFileSync(fd, '12345');
expect(vol.readFileSync('/2', 'utf8')).toBe('12345');
vol.truncateSync('/2', 10);
expect(vol.readFileSync('/2', 'utf8')).toBe('12345\0\0\0\0\0');
});
});
describe('.truncate(path[, len], callback)', () => {
xit('...', () => {});
Expand Down
5 changes: 3 additions & 2 deletions src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,10 @@ export class Node extends EventEmitter {
if (len <= this.buf.length) {
this.buf = this.buf.slice(0, len);
} else {
const buf = bufferAllocUnsafe(0);
const buf = bufferAllocUnsafe(len);
this.buf.copy(buf);
buf.fill(0, len);
buf.fill(0, this.buf.length);
this.buf = buf;
}
}

Expand Down

0 comments on commit 25027fb

Please sign in to comment.