I need to sync data with WebDAV and other similar backend that do not have a concept of atomic operations. In some cases, I might need to read a file, change its content and write it back. And of course, I want to make sure that no other client read and write the same file in the meantime.
I'm wondering if there's any general technique to handle this?
I thought about having a lock file and if that file is present, no other client can write to the backend. The problem is that there could also be race conditions when creating the lock file: first I need to check if it exists and, if it doesn't, create it. But of course, another client might be doing the same operatons at the same time.
So any idea how this can be handled in general? (It doesn't have to language or OS specific as the backend could be anything, like ftp, scp, etc.)