Skip to content

Commit

Permalink
FSA: Implement a cursor for SyncAccessHandles
Browse files Browse the repository at this point in the history
Specified at whatwg/fs#76

This only makes sense if the "at" option is optional, so that was later
specified here: whatwg/fs#82

Fixed: 1394790
Change-Id: I51e7343d221c1b89dfb10e22434d9d213e152b2d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4137091
Auto-Submit: Austin Sullivan <[email protected]>
Commit-Queue: Austin Sullivan <[email protected]>
Reviewed-by: Daseul Lee <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1093907}
  • Loading branch information
a-sully authored and chromium-wpt-export-bot committed Jan 18, 2023
1 parent f4656d2 commit ad88d36
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
58 changes: 56 additions & 2 deletions fs/FileSystemSyncAccessHandle-read-write.https.tentative.worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,7 @@ sync_access_handle_test((t, handle) => {

const bufferLength = expected.length;
const readBuffer = new Uint8Array(expected.length);
// No options parameter provided, should read at offset 0.
const readBytes = handle.read(readBuffer);
const readBytes = handle.read(readBuffer, {at: 0});
assert_equals(expected.length, readBytes, 'Check that all bytes were read');
const actual = new TextDecoder().decode(readBuffer);
assert_equals(
Expand Down Expand Up @@ -242,4 +241,59 @@ sync_access_handle_test((t, handle) => {
assert_equals(0, readBytes, 'Check that no bytes were written');
}, 'Test writing at a negative offset fails.');

sync_access_handle_test((t, handle) => {
const text = 'foobar';
const writeBuffer = new TextEncoder().encode(text);
let writtenBytes = handle.write(writeBuffer);
assert_equals(
writeBuffer.byteLength, writtenBytes,
'Check that all bytes were written.');

// This should double the file size, not overwrite the existing bytes.
writtenBytes = handle.write(writeBuffer);
assert_equals(
writeBuffer.byteLength, writtenBytes,
'Check that all bytes were written.');

// Reading at the cursor will read zero bytes at the end of the file.
const expectedFileSize = text.length * 2;
const readBuffer = new Uint8Array(expectedFileSize);
let readBytes = handle.read(readBuffer);
assert_equals(0, readBytes, 'Check that no bytes were read');

// Reading from the start of the file.
readBytes = handle.read(readBuffer, {at: 0});
assert_equals(expectedFileSize, readBytes, 'Check that all bytes were read');
}, 'Test that writing moves the file position cursor');

sync_access_handle_test((t, handle) => {
const decoder = new TextDecoder();

const text = 'foobar';
const writeBuffer = new TextEncoder().encode(text);
let writtenBytes = handle.write(writeBuffer);
assert_equals(
writeBuffer.byteLength, writtenBytes,
'Check that all bytes were written.');

// Read the first half of the file.
let expected = 'foo';
const readBuffer = new Uint8Array(3);
let readBytes = handle.read(readBuffer, {at: 0});
assert_equals(3, readBytes, 'Check that all bytes were read');
let actual = decoder.decode(readBuffer);
assert_equals(
expected, actual,
`Expected to read ${expected} but the actual value was ${actual}.`);

// Read the second half of the file, without specifying an offset.
expected = 'bar';
readBytes = handle.read(readBuffer);
assert_equals(3, readBytes, 'Check that all bytes were read');
actual = decoder.decode(readBuffer);
assert_equals(
expected, actual,
`Expected to read ${expected} but the actual value was ${actual}.`);
}, 'Test that reading moves the file position cursor');

done();
19 changes: 19 additions & 0 deletions fs/FileSystemSyncAccessHandle-truncate.https.tentative.worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,23 @@ sync_access_handle_test((t, handle) => {
assert_array_equals(expected, readBuffer);
}, 'test SyncAccessHandle.truncate after SyncAccessHandle.write');

sync_access_handle_test((t, handle) => {
// The cursor will be at the end of the file after this write.
const writeBuffer = new Uint8Array(4);
writeBuffer.set([0, 1, 2, 3]);
handle.write(writeBuffer);

// Extending the file should not move the cursor.
handle.truncate(6);
let readBuffer = new Uint8Array(2);
let expected = new Uint8Array(2);
expected.set([0, 0]);
assert_equals(2, handle.read(readBuffer));
assert_array_equals(expected, readBuffer);

// Shortening the file should move the cursor to the new end.
handle.truncate(2);
assert_equals(0, handle.read(readBuffer));
}, 'test SyncAccessHandle.truncate resets the file position cursor');

done();

0 comments on commit ad88d36

Please sign in to comment.