c - Can hardlinks be overwritten without using a temporary file? -
i have hardlink must exist on filesystem. inode hardlink points not constant. want update hardlink without adding temporary entry directory.
(creating file without directory entry can done using open(2)
temp flag.)
the issue i'm facing replacing/updating hardlink. documentation on relevant system calls, seems have 2 options, , neither avoids temporary file:
using
renameat
, possible insure hardlink exists. however, must consume hardlink , hence necessitating temporary files (not mention inability dereference symbolic links).using
linkat
, possible produce hardlink without sacrificing file. cannot overwrite existing files; requiring deletion of original hard link.
is @ possible create link inode replaces older link same name?
you need have file switch link. rename
, renameat
not need inode linked in same directory; require inode exist on same filesystem, or more on same mount point; otherwise linux rename
fails exdev
:
exdev
oldpath
,newpath
not on same mounted filesystem. (linux permits filesystem mounted @ multiple points, rename() not work across different mount points, if same filesystem mounted on both.)
since linux 3.11 there way make new file without linking filesystem: open(2) has new flag o_tmpfile
:
o_tmpfile
(since linux 3.11)create unnamed temporary file. pathname argument specifies directory; unnamed inode created in directory's filesystem. written resulting file lost when last file descriptor closed, unless file given name.
o_tmpfile
must specified 1 ofo_rdwr
oro_wronly
and, optionally,o_excl
. ifo_excl
not specified,linkat
(2) can used link temporary file filesystem, making permanent, using code following:char path[path_max]; fd = open("/path/to/dir", o_tmpfile | o_rdwr, s_irusr | s_iwusr); /* file i/o on 'fd'... */ snprintf(path, path_max, "/proc/self/fd/%d", fd); linkat(at_fdcwd, path, at_fdcwd, "/path/for/file", at_symlink_follow);
in case,
open()
mode argument determines file permission mode,o_creat
.
the manual tells 1 of 2 common use cases o_tmpfile
is
creating file invisible, populated data , adjusted have appropriate filesystem attributes (chown(2), chmod(2), fsetxattr(2), etc.) before being atomically linked filesystem in formed state (using linkat(2) described above).
there many downsides this, apart being quite new: file system must support o_tmpfile
; ext[234] support it, , xfs in 3.15; btrfs in 3.16; furthermore might still not match case, linkat
requires at_symlink_follow
not available renameat
; if target name exists, `linkat not replace the target.
Comments
Post a Comment