EXCHANGEDATA(2) | System Calls Manual | EXCHANGEDATA(2) |
exchangedata
—
atomically exchange data between two files
#include
<unistd.h>
#include <sys/attr.h>
int
exchangedata
(const
char * path1, const char
* path2, unsigned int
options);
The
exchangedata
()
function swaps the contents of the files referenced by
path1 and path2 in an atomic
fashion. That is, all concurrent processes will either see the pre-exchanged
state or the post-exchanged state; they can never see the files in an
inconsistent state. The data in all forks is swapped in this way. The
options parameter lets you control specific aspects of
the function's behaviour.
Open file descriptors follow the swapped data. Thus, a descriptor that previously referenced path1 will now reference the data that's accessible via path2, and vice versa.
In general, the file attributes (metadata) are not exchanged.
Specifically, the object identifier attributes (that is, the
ATTR_CMN_OBJID
and
ATTR_CMN_OBJPERMANENTID
attributes as defined by the
getattrlist(2) function) are not
swapped. An exception to this general rule is that the modification time
attribute ( ATTR_CMN_MODTIME
) is swapped.
When combined, these features allow you to implement a 'safe save' function that does not break references to the file (for example, aliases). You first save the new contents to a temporary file and then exchange the data of the original file and the temporary. Programs that reference the file via an object identifier will continue to reference the original file, but now it has the new data.
WARNING: This system call is largely supported only by
HFS and AFP file systems. Many other file systems, including APFS, do not
support it. Further, it is not supported on iOS, tvOS, or watchOS. It is
recommended that callers refer instead to
rename
() or
renamex_np
()
to conduct safe-save operations instead.
The path1 and path2 parameters must both reference valid files. All directories listed in the path names leading to these files must be searchable. You must have write access to the files.
The options parameter is
a bit set that controls the behaviour of
exchangedata
().
The following option bits are defined.
exchangedata
() will not follow
a symlink if it occurs as the last component of
path1 or path2.Upon successful completion a value of 0 is returned. Otherwise, a value of -1 is returned and errno is set to indicate the error.
Not all volumes support exchangedata
().
This includes APFS volumes. You can test whether a volume supports
exchangedata
() by using
getattrlist(2) to get the volume
capabilities attribute ATTR_VOL_CAPABILITIES
, and
then testing the VOL_CAP_INT_EXCHANGEDATA
flag.
exchangedata
() will fail if:
ENOTSUP
]exchangedata
().ENOTDIR
]ENAMETOOLONG
]NAME_MAX
characters, or an entire path name exceeded
PATH_MAX
characters.ENOENT
]EACCES
]ELOOP
]EFAULT
]EXDEV
]EINVAL
]EINVAL
]EIO
]A exchangedata
() function call appeared in
Darwin 1.3.1 (Mac OS X version 10.0). It was deprecated in macOS 10.13.
December 15, 2003 | Darwin |