-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Request to check if FileHandle/DirectoryHandle exists before executing read/write operations #80
Comments
I can see the argument that This might be relevant to FWIW, currently |
This is unspecified (see #11 - this is one of the primary use cases that needs to be specified) but methods which create a child ( Consider Of course attempting to create a new file isn't a good way to check if a directory exists. Tying this to a |
I'm guessing
With this in mind, I believe an |
Correct, it probably makes sense for @annevk any thoughts from a web ergonomics perspective? Related to this issue, if we were to assert that a handle is no longer usable after it's been removed (see #39 (comment)) then an |
For use case A: why is there the assumption that the file would go invalid between 1 and 2 and not 2 and 3? Why not hold a lock? Same questions for use case B. Getting a handle is pretty quick and it seems unlikely-if-not-impossible it would invalidate itself by the time you start preparing data if that happens in the same task as suggested. Now I suppose you could be concerned that it invalidates while preparing the data, but that really suggests you need to use a lock of some kind. |
@a-sully: From what I understand of issue #39 and its implication,
@annevk: No reason actually, assuming wrong I admit. If tacking a lock could be a solution, then could const handle; // from picker / IndexedDB
try {
const stream = handle.createWritable({exclusiveLock: true}); // tack an exclusive lock or throw NotFoundError.
const rawData = compute(data); // computation-heavy.
await stream.write(rawData);
await stream.close(); // release lock.
} catch (error) {
console.log("File not found");
} This is only an example. Currently and according to If it were to tack an exclusive lock, writing content could directly happen in file without the use of a temporary file? |
#39 was addressed by #96, which clarified that a Also related is #126. As noted on that issue, it would be nice if a
https://github.com/whatwg/fs/blob/main/proposals/MultipleReadersWriters.md proposes allowing |
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
andFileSystemDirectoryHandle
are 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 theTOCTTOU
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:
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.The text was updated successfully, but these errors were encountered: