2 Copyright (C) 2005 Juliusz Chroboczek
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include <sys/types.h>
33 maybe_relink(const char *src
, const char *dst
, int careful
)
40 /* Tries to link src to dst if both files exist and have the same
41 contents. If careful is false only the file sizes are compared; if
42 it is true, the full contents are compared.
44 This code assumes that dst cannot change behind our back -- the
45 caller is supposed to protect it by a lock. On the other hand, it
46 does handle simultaneous access to src, but only if src is never
47 modified in place. It should also be safe over NFS.
49 Assumes that rename cannot fail mid-way on a single filesystem.
51 Returns 1 on success, 0 if the files are already linked, -1 for an
52 error in errno, -2 if the files cannot be linked because they are not
53 the same, on different devices, or on a filesystem with no support for
54 hard links, -3 if there was a race condition, -4 if something unexpected
58 maybe_relink(char *src
, char *dst
, int careful
)
60 #define RELINK_BUFFER_SIZE 8192
62 int len
, rc
, saved_errno
;
64 struct stat srcstat
, dststat
, tempstat
;
67 rc
= stat(src
, &srcstat
);
75 rc
= stat(dst
, &dststat
);
78 if(!S_ISREG(srcstat
.st_mode
) || !S_ISREG(dststat
.st_mode
)) {
82 if(srcstat
.st_dev
!= dststat
.st_dev
) {
86 if(srcstat
.st_ino
== dststat
.st_ino
)
87 /* Files are already linked */
90 if(srcstat
.st_size
!= dststat
.st_size
)
93 /* link is atomic even on NFS, we will fail gracefully if the name
95 gettimeofday(&now
, NULL
);
97 tempname
= malloc(rc
);
98 if(tempname
== NULL
) return -1;
99 len
= snprintf(tempname
, rc
, "%s-%04x", dst
,
100 ((unsigned)(now
.tv_usec
^ (now
.tv_usec
>> 16))) & 0xFFFF);
101 if(len
< 0 || len
>= rc
) {
106 rc
= link(src
, tempname
);
108 /* We need to try to remove the link in case this was a
109 problem with NFS over an unreliable transport. */
113 rc
= stat(tempname
, &tempstat
);
114 if(rc
< 0) goto fail
;
116 /* Check for a race condition. The size and mtime checks are
117 gratuitious, but they don't cost much, and might save your data
118 if you're on a filesystem without i-nodes. */
119 if(tempstat
.st_ino
!= srcstat
.st_ino
||
120 tempstat
.st_size
!= srcstat
.st_size
||
121 tempstat
.st_mtime
!= srcstat
.st_mtime
) {
127 int fd1
, fd2
, i
, rc1
, rc2
;
128 char buf1
[RELINK_BUFFER_SIZE
], buf2
[RELINK_BUFFER_SIZE
];
130 fd1
= open(tempname
, O_RDONLY
);
131 if(fd1
< 0) goto fail
;
132 fd2
= open(dst
, O_RDONLY
);
133 if(fd2
< 0) { close(fd1
); goto fail
; }
136 /* This comparison is approximate: it doesn't deal with short
137 reads and EINTR. It's okay, as these cases are rare and if
138 they happen, we're still safe. */
139 while(i
< tempstat
.st_size
) {
140 rc1
= read(fd1
, buf1
, RELINK_BUFFER_SIZE
);
141 if(rc1
< 0) { close(fd1
); close(fd2
); goto fail
; }
142 rc2
= read(fd2
, buf2
, RELINK_BUFFER_SIZE
);
143 if(rc2
< 0) { close(fd1
); close(fd2
); goto fail
; }
144 if(rc1
== 0 || rc1
!= rc2
|| memcmp(buf1
, buf2
, rc1
) != 0) {
145 close(fd1
); close(fd2
);
152 close(fd1
); close(fd2
);
155 rc
= rename(tempname
, dst
);
156 if(rc
< 0) goto fail
;
166 if(errno
== EPERM
|| errno
== EOPNOTSUPP
)
170 #undef RELINK_BUFFER_SIZE