Skip to content

Request to check if FileHandle/DirectoryHandle exists before executing read/write operations #80

@poirierlouis

Description

@poirierlouis

After a small talk on Matrix chat (@annevk), I'd like to present a problem and use-case regarding handles when they no longer exists on user's device.

Currently, read/write access operations on FileSystemFileHandle and FileSystemDirectoryHandle are the only way to test if a file or directory entry exists on user's device. I've been explained there is no exists() like-method on these interfaces due to the TOCTTOU principle. Considering it, here is a problem where I believe current state of API prevent any chance at green code practice:

A. Use-case on file:

  1. Get handle of a file (it can be from IndexedDB storage, a dialog picker...)
  2. Prepare data (run long computation / algorithm, e.g. compress data, serialize data...)
  3. Write data in file

Access to underlying file will happen at step (3), and when file does not exist, it rejects with a NotFoundError.
In such case, there is no way to save time and device's resources. It will run, depending on the use-case, a long computation / algorithm to prepare data. Only once data is ready, it will try to write in file, which may or may not exist.

B. Use-case on directory:

  1. Get handle of a directory
  2. Prepare structure of a project
    • list directories
    • list files
    • prepare data of files
  3. Create prepared directories in (1)
  4. Create prepared files in (1)

In the same situation, step (2) could be prevented if directory entry is already known to not exist.

Problem:

There is no way nor "chance" given at the end-user to prevent execution of step (2). This seems to be a waste regarding the device power/resources, and regarding the time of the user of the application.

Let me know if anything is not clear.
What do you think?

Aside: is TOCTTOU like a general no-go rule when writing a specification? Or can it be argued on depending on use-case, the technicality of such, I think, edge-cases? While not recommended, for example Node.js expose an exists() like-method.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions