Atomic boost::filesystem::rename
Hello, What is the most "atomic" way to move two files from a local disk to a server location? The meaning of atomic in this context means that either both files are moved or both files stay in original location. No intermediate files should be present once operation is finished. The two files are located in different directories; /root/file_a /root/data/file_b The solution must support both Windows and Linux environment. It appears boost::filesystem::rename(..) uses Win32 API MoveFileExW for the Windows implementation. The documentation for MoveFileExW states; "If the file is to be moved to a different volume, the function simulates the move by using the CopyFile and DeleteFile functions. If the file is successfully copied to a different volume and the original file is unable to be deleted, the function succeeds leaving the source file intact." This means boost::filesystem::rename(..) could potentially just perform a file copy which is undesirable behaviour. Our understanding is that Boost filesystem does not contain ACL permission support [1] which make it difficult to check privileges prior to performing moves. Appreciate any input on the matter. Using C++14 and Boost. [1] - https://lists.boost.org/boost-users/2018/12/89232.php
On 16/01/2019 09:02, Lars wrote:
What is the most "atomic" way to move two files from a local disk to a server location? The meaning of atomic in this context means that either both files are moved or both files stay in original location. No intermediate files should be present once operation is finished.
When you detect that the source and target are on different filesystems, you should first perform a regular copy of the file to the target filesystem (using a different filename). Then perform the rename from this temporary file to the actual target filename. Usually you can put the temporary file in the same target directory but with a different file pattern that you know will not be observed by consuming directory watchers. If watchers might be looking for all files, then you may need to put temporary files in a different directory, as long as it's on the same filesystem. Using the same target directory also reduces the risk of permissions problems, although whatever installation process you use can require that the target directory and the temp directory have the same permissions, along with ensuring that they're suitably configured so your app can find them in the first place.
participants (2)
-
Gavin Lambert
-
Lars