Skip to content

Commit 008de22

Browse files
committed
Add SyncAccessHandle truncate/getSize
1 parent 91fbb6d commit 008de22

File tree

2 files changed

+468
-246
lines changed

2 files changed

+468
-246
lines changed

index.bs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,7 +1258,7 @@ interface FileSystemSyncAccessHandle {
12581258
unsigned long long write([AllowShared] BufferSource buffer,
12591259
FilesystemReadWriteOptions options);
12601260

1261-
Promise<undefined> truncate([EnforceRange] unsigned long long size);
1261+
Promise<undefined> truncate([EnforceRange] unsigned long long newSize);
12621262
Promise<unsigned long long> getSize();
12631263
Promise<undefined> flush();
12641264
Promise<undefined> close();
@@ -1370,16 +1370,31 @@ these steps:
13701370
### The {{FileSystemSyncAccessHandle/truncate()}} method ### {#api-filesystemsyncaccesshandle-truncate}
13711371

13721372
<div class="note domintro">
1373-
: |handle| . {{FileSystemSyncAccessHandle/truncate()|truncate}}(|size|)
1374-
:: Resizes the file associated with stream to be size bytes long. If size is larger than the current file size this pads the file with null bytes, otherwise it truncates the file.
1373+
: |handle| . {{FileSystemSyncAccessHandle/truncate()|truncate}}(|newSize|)
1374+
:: Resizes the file associated with stream to be |newSize| bytes long. If size is larger than the current file size this pads the file with null bytes, otherwise it truncates the file.
13751375
</div>
13761376

1377-
//TODO(fivedots):Make sure negative lengths are not allowed.
13781377
<div algorithm>
1379-
The <dfn method for=FileSystemSyncAccessHandle>truncate(|size|)</dfn> method, when invoked, must run
1378+
The <dfn method for=FileSystemSyncAccessHandle>truncate(|newSize|)</dfn> method, when invoked, must run
13801379
these steps:
13811380

1382-
// TODO(fivedots): fill in.
1381+
1. If [=this=].[=[[state]]=] is "closed", throw a {{InvalidStateError}}.
1382+
1. If |newSize| is less than 0, throw a {{InvalidStateError}}.
1383+
1. Let |fileContents| be a copy of [=this=].[=[[file]]=]'s [=file entry/binary data=].
1384+
1. Let |p| be [=a new promise=].
1385+
1. Run the following steps [=in parallel=]:
1386+
1. Let |oldSize| be the [=byte sequence/length=] of [=this=].[=[[file]]=]'s [=file entry/binary data=].
1387+
1. If |newSize| is larger than |oldSize|:
1388+
1. If |newSize| - |oldSize| exceeds the available [=storage quota=], [=/reject=] |p|
1389+
with a {{QuotaExceededError}} and abort.
1390+
1. Set [=this=].[=[[file]]=]'s to a [=byte sequence=] formed by concating
1391+
|fileContents| with a [=byte sequence=]
1392+
containing |newSize|-|oldSize| `0x00` bytes.
1393+
1. Else if |newSize| is smaller than |oldSize|:
1394+
1. Set [=this=].[=[[file]]=]'s to a [=byte sequence=] containing the first |newSize| bytes
1395+
in |fileContents|.
1396+
1. [=/Resolve=] |p|.
1397+
1. Return |p|
13831398

13841399
</div>
13851400

@@ -1394,7 +1409,13 @@ these steps:
13941409
The <dfn method for=FileSystemSyncAccessHandle>getSize()</dfn> method, when invoked, must run
13951410
these steps:
13961411

1397-
// TODO(fivedots): fill in.
1412+
1. If [=this=].[=[[state]]=] is "closed", throw a {{InvalidStateError}}.
1413+
1. Let |p| be [=a new promise=].
1414+
1. Run the following steps [=in parallel=]:
1415+
1. Let |size| be the [=byte sequence/length=] of [=this=].[=[[file]]=]'s [=file entry/binary data=].
1416+
1. [=/Resolve=] |p| with |size|.
1417+
1. Return |p|
1418+
13981419

13991420
</div>
14001421

@@ -1409,7 +1430,7 @@ these steps:
14091430
The <dfn method for=FileSystemSyncAccessHandle>flush()</dfn> method, when invoked, must run
14101431
these steps:
14111432

1412-
// TODO(fivedots): fill in.
1433+
// TODO(fivedots): Fill in, after figuring out language to describe flushing at the OS level.
14131434

14141435
</div>
14151436

0 commit comments

Comments
 (0)