Skip to content

Commit

Permalink
Editorial review: Add information on file system access locking modes (
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdavidmills authored Aug 9, 2024
1 parent 84ae777 commit 2cba64f
Show file tree
Hide file tree
Showing 3 changed files with 276 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ size = accessHandle.getSize();
const dataView = new DataView(new ArrayBuffer(size));

// Read the entire file into the data view.
accessHandle.read(dataView);
accessHandle.read(dataView, { at: 0 });
// Logs `"Some textMore content"`.
console.log(textDecoder.decode(dataView));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,24 @@ Creating a {{domxref('FileSystemSyncAccessHandle')}} takes an exclusive lock on

```js-nolint
createSyncAccessHandle()
createSyncAccessHandle(options)
```

### Parameters

None.
- `options` {{optional_inline}}

- : An object with the following properties:

- `mode` {{optional_inline}} {{non-standard_inline}}
- : A string specifying the locking mode for the access handle. The default value is `"readwrite"`.
Possible values are:
- `"read-only"`
- : Multiple `FileSystemSyncAccessHandle` objects can be opened simultaneously on a file (for example when using the same app in multiple tabs), provided they are all opened in `"read-only"` mode. Once opened, read-like methods can be called on the handles — {{domxref("FileSystemSyncAccessHandle.read", "read()")}}, {{domxref("FileSystemSyncAccessHandle.getSize", "getSize()")}}, and {{domxref("FileSystemSyncAccessHandle.close", "close()")}}.
- `"readwrite"`
- : Only one `FileSystemSyncAccessHandle` object can be opened on a file. Attempting to open subsequent handles before the first handle is closed results in a `NoModificationAllowedError` exception being thrown. Once opened, any available method can be called on the handle.
- `"readwrite-unsafe"`
- : Multiple `FileSystemSyncAccessHandle` objects can be opened simultaneously on a file, provided they are all opened in `"readwrite-unsafe"` mode. Once opened, any available method can be called on the handles.

### Return value

Expand All @@ -38,10 +51,12 @@ A {{jsxref('Promise')}} which resolves to a {{domxref('FileSystemSyncAccessHandl
- `NotFoundError` {{domxref("DOMException")}}
- : Thrown if current entry is not found.
- `NoModificationAllowedError` {{domxref("DOMException")}}
- : Thrown if the browser is not able to acquire a lock on the file associated with the file handle.
- : Thrown if the browser is not able to acquire a lock on the file associated with the file handle. This could be because `mode` is set to `readwrite` and an attempt is made to open multiple handles simultaneously.

## Examples

### Basic usage

The following asynchronous event handler function is contained inside a Web Worker. The snippet inside it creates a synchronous file access handle.

```js
Expand All @@ -62,6 +77,139 @@ onmessage = async (e) => {
};
```

### Complete example with `mode` option

Our [`createSyncAccessHandle()` mode test](https://createsyncaccesshandle-mode-test.glitch.me/) example provides an {{htmlelement("input")}} field to enter text into, and two buttons — one to write entered text to the end of a file in the origin private file system, and one to empty the file when it becomes too full.

Try exploring the demo above, with the browser developer console open so you can see what is happening. If you try opening the demo in multiple browser tabs, you will find that multiple handles can be opened at once to write to the file at the same time. This is because `mode: "readwrite-unsafe"` is set on the `createSyncAccessHandle()` calls.

Below we'll explore the code.

#### HTML

The two {{htmlelement("button")}} elements and text {{htmlelement("input")}} field look like this:

```html
<ol>
<li>
<label for="filetext">Enter text to write to the file:</label>
<input type="text" id="filetext" name="filetext" />
</li>
<li>
Write your text to the file: <button class="write">Write text</button>
</li>
<li>
Empty the file if it gets too full:
<button class="empty">Empty file</button>
</li>
</ol>
```

#### Main JavaScript

The main thread JavaScript inside the HTML file is shown below. We grab references to the write text button, empty file button, and text input field, then we create a new web worker using the {{domxref("Worker.Worker", "Worker()")}} constructor. We then define two functions and set them as event handlers on the buttons:

- `writeToOPFS()` is run when the write text button is clicked. This function posts the entered value of the text field to the worker inside an object using the {{domxref("Worker.postMessage()")}} method, then empties the text field, ready for the next addition. Note how the passed object also includes a `command: "write"` property to specify that we want to trigger a write action with this message.
- `emptyOPFS()` is run when the empty file button is clicked. This posts an object containing a `command: "empty"` property to the worker specifying that the file is to be emptied.

```js
const writeBtn = document.querySelector(".write");
const emptyBtn = document.querySelector(".empty");
const fileText = document.querySelector("#filetext");

const opfsWorker = new Worker("worker.js");

function writeToOPFS() {
opfsWorker.postMessage({
command: "write",
content: fileText.value,
});
console.log("Main script: Text posted to worker");
fileText.value = "";
}

function emptyOPFS() {
opfsWorker.postMessage({
command: "empty",
});
}

writeBtn.addEventListener("click", writeToOPFS);
emptyBtn.addEventListener("click", emptyOPFS);
```

#### Worker JavaScript

The worker JavaScript is shown below.

First, we run a function called `initOPFS()` that gets a reference to the OPFS root using {{domxref("StorageManager.getDirectory()")}}, creates a file and returns its handle using {{domxref("FileSystemDirectoryHandle.getFileHandle()")}}, and then returns a {{domxref("FileSystemSyncAccessHandle")}} using `createSyncAccessHandle()`. This call includes the `mode: "readwrite-unsafe"` property, allowing multiple handles to access the same file simultaneously.

```js
let accessHandle;

async function initOPFS() {
const opfsRoot = await navigator.storage.getDirectory();
const fileHandle = await opfsRoot.getFileHandle("file.txt", { create: true });
accessHandle = await fileHandle.createSyncAccessHandle({
mode: "readwrite-unsafe",
});
}

initOPFS();
```

Inside the worker's [message event](/en-US/docs/Web/API/Worker/message_event) handler function, we first get the size of the file using {{domxref("FileSystemSyncAccessHandle.getSize", "getSize()")}}. We then check to see whether the data sent in the message includes a `command` property value of `"empty"`. If so, we empty the file using {{domxref("FileSystemSyncAccessHandle.truncate", "truncate()")}} with a value of `0`, and update the file size contained in the `size` variable.

If the message data is something else, we:

- Create a new {{domxref("TextEncoder")}} and {{domxref("TextDecoder")}} to handle encoding and decoding the text content later on.
- Encode the message data and write the result to the end of the file using {{domxref("FileSystemSyncAccessHandle.write", "write()")}}, then update the file size contained in the `size` variable.
- Create a {{domxref("DataView")}} to contain the file contents, and read the content into it using {{domxref("FileSystemSyncAccessHandle.read", "read()")}}.
- Decode the `DataView` contents and log it to the console.

```js
onmessage = function (e) {
console.log("Worker: Message received from main script");

// Get the current size of the file
let size = accessHandle.getSize();

if (e.data.command === "empty") {
// Truncate the file to 0 bytes
accessHandle.truncate(0);

// Get the current size of the file
size = accessHandle.getSize();
} else {
const textEncoder = new TextEncoder();
const textDecoder = new TextDecoder();

// Encode content to write to the file
const content = textEncoder.encode(e.data.content);
// Write the content at the end of the file
accessHandle.write(content, { at: size });

// Get the current size of the file
size = accessHandle.getSize();

// Prepare a data view of the length of the file
const dataView = new DataView(new ArrayBuffer(size));

// Read the entire file into the data view
accessHandle.read(dataView, { at: 0 });

// Log the current file contents to the console
console.log("File contents: " + textDecoder.decode(dataView));

// Flush the changes
accessHandle.flush();
}

// Log the size of the file to the console
console.log("Size: " + size);
};
```

## Specifications

{{Specifications}}
Expand Down
126 changes: 125 additions & 1 deletion files/en-us/web/api/filesystemfilehandle/createwritable/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ createWritable(options)
- : A {{jsxref('Boolean')}}. Default `false`.
When set to `true` if the file exists, the existing file is first copied to the temporary file.
Otherwise the temporary file starts out empty.
- `mode` {{optional_inline}} {{non-standard_inline}}
- : A string specifying the locking mode for the writable file stream. The default value is `"siloed"`.
Possible values are:
- `"exclusive"`
- : Only one `FileSystemWritableFileStream` writer can be opened. Attempting to open subsequent writers before the first writer is closed results in a `NoModificationAllowedError` exception being thrown.
- `"siloed"`
- : Multiple `FileSystemWritableFileStream` writers can be opened at the same time, each with its own swap file, for example when using the same app in multiple tabs. The last writer opened has its data written, as the data gets flushed when each writer is closed.

### Return value

Expand All @@ -43,12 +50,14 @@ A {{jsxref('Promise')}} which resolves to a {{domxref('FileSystemWritableFileStr
- `NotFoundError` {{domxref("DOMException")}}
- : Thrown if current entry is not found.
- `NoModificationAllowedError` {{domxref("DOMException")}}
- : Thrown if the browser is not able to acquire a lock on the file associated with the file handle.
- : Thrown if the browser is not able to acquire a lock on the file associated with the file handle. This could be because `mode` is set to `exclusive` and an attempt is made to open multiple writers simultaneously.
- `AbortError` {{domxref("DOMException")}}
- : Thrown if implementation-defined malware scans and safe-browsing checks fails.

## Examples

### Basic usage

The following asynchronous function writes the given contents to the file handle, and thus to disk.

```js
Expand All @@ -64,6 +73,121 @@ async function writeFile(fileHandle, contents) {
}
```

### Expanded usage with options

Our [`createWritable()` mode test](https://createwritable-mode-test.glitch.me/) example provides a {{htmlelement("button")}} to select a file to write to, a text {{htmlelement("input")}} field into which you can enter some text to write to the file, and a second `<button>` to write the text to the file.

In the demo above, try selecting a text file on your file system (or entering a new file name), entering some text into the input field, and writing the text to the file. Open the file on your file system to check whether the write was successful.

Also, try opening the page in two browser tabs simultaneously. Select a file to write to in the first tab, and then immediately try selecting the same file to write to in the second tab. You should get an error message because we set `mode: "exclusive"` in the `createWritable()` call.

Below we'll explore the code.

#### HTML

The two {{htmlelement("button")}} elements and text {{htmlelement("input")}} field look like this:

```html
<ol>
<li>
Select a file to write to: <button class="select">Select file</button>
</li>
<li>
<label for="filetext">Enter text to write to the file:</label>
<input type="text" id="filetext" name="filetext" disabled />
</li>
<li>
Write your text to the file:
<button class="write" disabled>Write text</button>
</li>
</ol>
```

The text input field and the write text button are set to be disabled initially via the [`disabled`](/en-US/docs/Web/HTML/Attributes/disabled) attribute — they shouldn't be used until the user has selected a file to write to.

```css hidden
li {
margin-bottom: 10px;
}
```

#### JavaScript

We start by grabbing references to the select file button, the write text button, and the text input field. We also declare a global variable `writableStream`, which will store a reference to the writeable stream for writing the text to the file, once created. We initially set it to `null`.

```js
const selectBtn = document.querySelector(".select");
const writeBtn = document.querySelector(".write");
const fileText = document.querySelector("#filetext");

let writableStream = null;
```

Next, we create an async function called `selectFile()`, which we'll invoke when the select button is pressed. This uses the {{domxref("Window.showSaveFilePicker()")}} method to show the user a file picker dialog and create a file handle to the file they choose. On that handle, we invoke the `createWritable()` method to create a stream to write the text to the selected file. If the call fails, we log an error to the console.

We pass `createWritable()` an options object containing the following options:

- `keepExistingData: true`: If the selected file already exists, and data contained within it is copied to the temporary file before writing commences.
- `mode: "exclusive"`: States that only one writer can be open on the file handle simultneously. If a second user loads the example and tries to select a file, they will get an error.

Last of all, we enable the input field and the write text button, as they are needed for the next step, and disable the select file button (this is not currently needed).

```js
async function selectFile() {
// Create a new handle
const handle = await window.showSaveFilePicker();

// Create a FileSystemWritableFileStream to write to
try {
writableStream = await handle.createWritable({
keepExistingData: true,
mode: "exclusive",
});
} catch (e) {
if (e.name === "NoModificationAllowedError") {
console.log(
`You can't access that file right now; someone else is trying to modify it. Try again later.`,
);
} else {
console.log(e.message);
}
}

// Enable text field and write button, disable select button
fileText.disabled = false;
writeBtn.disabled = false;
selectBtn.disabled = true;
}
```

Our next function, `writeFile()`, writes the text entered into the input field to the chosen file using {{domxref("FileSystemWritableFileStream.write()")}}, then empties the input field. We then close the writable stream using {{domxref("WritableStream.close()")}}, and reset the demo so it can be run again — the `disabled` states of the controls are toggled back to their original states, and the `writableStream` variable is set back to `null`.

```js
async function writeFile() {
// Write text to our file and empty out the text field
await writableStream.write(fileText.value);
fileText.value = "";

// Close the file and write the contents to disk.
await writableStream.close();

// Disable text field and write button, enable select button
fileText.disabled = true;
writeBtn.disabled = true;
selectBtn.disabled = false;

// Set writableStream back to null
writableStream = null;
}
```

To get the demo running, we set event listeners on the buttons so that the relevant function is run when each one is clicked.

```js
selectBtn.addEventListener("click", selectFile);
writeBtn.addEventListener("click", writeFile);
```

## Specifications

{{Specifications}}
Expand Down

0 comments on commit 2cba64f

Please sign in to comment.