I am working on a Perl-based file synchronization tool. It downloads files to a temporary directory (which is guaranteed to be on the same file system as the real file), and then moves the temporary files to a place on top of the old ones, preserving metadata such as permissions, ownership, and ACLs. I am wondering how to achieve this last step in Linux.
On Mac OS X, at least in C, I would use the exchangedata function. This takes two file names as arguments and swaps their contents, leaving all metadata (except mtime) intact. This ensures that the operation is atomic - all readers will see either the old file or the new one, never between them. Unfortunately, I do not think it is available on Linux.
I know that rename moves atomically, but does not save metadata. On the other hand, I could open the file and overwrite the data with the contents of the new one, which would retain all the metadata, but would not become an atomic operation. Any suggestions for resolving this issue?
linux file atomic perl macos
Brent royal-gordon
source share