Saving Atomic file on Linux without loss of metadata

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?

+7
linux file atomic perl macos
source share
2 answers

The only approach that I see here is to read the metadata from the file you are replacing, apply it to the temporary file, and then rename the temporary file on top of the old file. ( rename obviously preserves the attributes of the source file.)

+6
source share

Specifically for the file system, but ...

XFS_IOC_SWAPEXT ioctl replaces the extents of two file descriptors with XFS .

 #include <xfs/xfs.h> #include <xfs/xfs_dfrag.h> xfs_swapext_t sx = { ..., .sx_fdtarget = fd1, .sx_fdtmp = fd2, ... }; xfs_swapext(fd1, &sx); 

See xfs_fsr sources for example usage.

+4
source share

All Articles