On Sat, Apr 29, 2017 at 6:45 PM, Tim Chase <python.l...@tim.thechases.com> wrote: > Working on some deduplication code, I want do my my best at > performing an atomic re-hard-linking atop an existing file, akin to > "ln -f source.txt dest.txt" > > However, when I issue > > os.link("source.txt", "dest.txt") > > it fails with an OSError (EEXISTS). This isn't surprising as it's > documented. Unfortunately, os.link doesn't support something like > > os.link("source.txt", "dest.txt", force=True)
FYI, on Windows this is possible if you use the NTAPI functions NtOpenFile and NtSetInformationFile instead of WinAPI CreateHardLink. Using the NT API can also support src_dir_fd, dst_dir_fd, and follow_symlinks=True [1]. I have a prototype that uses ctypes. I named this parameter "replace_existing". It's atomic, but it will fail if the destination is open. An open file can't be unlinked on Windows. [1]: MSDN claims that "if the path points to a symbolic link, the function [CreateHardLink] creates a hard link to the target". Unless I'm misreading, this statement is wrong because it actually links to the symlink, i.e. the reparse point. -- https://mail.python.org/mailman/listinfo/python-list