blob: a4c7ecfc95dbb260930da6fc37d8d73e470aa393 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
/* ISC license. */
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <skalibs/posixplz.h>
#include <skalibs/unix-transactional.h>
int atomic_symlink (char const *target, char const *name, char const *suffix)
{
{
int e = errno ;
if (symlink(target, name) == 0) return 1 ;
if (errno != EEXIST) return 0 ;
errno = e ;
}
{
size_t namelen = strlen(name) ;
size_t suffixlen = suffix ? strlen(suffix) + 1 : 0 ;
char tmp[namelen + suffixlen + 8] ;
memcpy(tmp, name, namelen) ;
if (suffix)
{
tmp[namelen] = ':' ;
memcpy(tmp + namelen + 1, suffix, suffixlen - 1) ;
}
memcpy(tmp + namelen + suffixlen, ":XXXXXX", 7) ;
tmp[namelen + suffixlen + 7] = 0 ;
if (mkltemp(target, tmp) == -1) return 0 ;
if (rename(tmp, name) < 0)
{
unlink_void(tmp) ;
return 0 ;
}
}
return 1 ;
}
|