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:
- Get handle of a file (it can be from IndexedDB storage, a dialog picker...)
- Prepare data (run long computation / algorithm, e.g. compress data, serialize data...)
- 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:
- Get handle of a directory
- Prepare structure of a project
- list directories
- list files
- prepare data of files
- Create prepared directories in (1)
- 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.
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
FileSystemFileHandleandFileSystemDirectoryHandleare the only way to test if a file or directory entry exists on user's device. I've been explained there is noexists()like-method on these interfaces due to theTOCTTOUprinciple. 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:
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:
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.