file-system-access icon indicating copy to clipboard operation
file-system-access copied to clipboard

Preventing write access by multiple users at a time?

Open anaestheticsapp opened this issue 3 years ago • 5 comments

There is currently no way to prevent more than one user from writing to a file that is currently being worked on with the Native File System API. This causes a problem when multiple users are editing the same file on a shared network drive. If User B opens a file after user A, user B can save changes whilst user A is still working on the same document. Once user A saves the file, changes made by user B are overwritten, causing data loss.

This is not possible in Excel, for example, where changes to a file are prevented whilst open by another process.

Is there a way to work around this issue?

anaestheticsapp avatar Mar 16 '21 21:03 anaestheticsapp

Is there a way to work around this issue?

for now maybe try to compare the lastModifiedDate from getFile() before saving?

jimmywarting avatar Mar 24 '21 17:03 jimmywarting

I'm not sure this is a good idea since locking files is not always possible. For example, a network drive or FUSE drive might not provide the ability to lock files.

slaymaker1907 avatar May 04 '21 20:05 slaymaker1907

It would be nice to have an event or some way of checking that a file was edited via an external editor... rather than having to try to read the file and getting error and hoping a retry will work... like #72

ftreesmilo avatar Jul 18 '21 19:07 ftreesmilo

My hope is that we'll be able to expose the "locking" part of the AccessHandle proposal in #310 for arbitrary files, even if the "in-place" behavior of that API will be limited to files on the origin private file system. There is of course the problem as mentioned that actually locking files might not always be possible, but that might be better than nothing?

mkruisselbrink avatar Jul 20 '21 20:07 mkruisselbrink

For anyone looking at this, I've come up with a few ways to implement this at the application level (obviously not ideal since it doesn't prevent external applications from violating the lock guarantees):

  1. Just use the Web Locks API which works across multiple tabs/windows (but not cross browser or with private windows).
  2. Use a consensus protocol like raft to elect a leader thread of execution which manages the lock(s). Care must be taken with this option to handle non-graceful exits (i.e. what happens when you kill one browser process). This option has the advantage of working where web locks work as well as with NFS and other tabs. You could even handle something like Google Drive/OneDrive which normally mess up read/write ordering depending on implementation.

slaymaker1907 avatar Mar 14 '22 00:03 slaymaker1907