Find git executable at run time
[git-darcs-import.git] / src / maybe_relink.c
blob50b0383fbe0edd4ae2f340cfa79d87cebff5a354
1 /*
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)
7 any later version.
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>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <stdlib.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <sys/time.h>
31 #ifdef _WIN32
32 int
33 maybe_relink(const char *src, const char *dst, int careful)
35 return 0;
38 #else
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
55 happened. */
57 int
58 maybe_relink(char *src, char *dst, int careful)
60 #define RELINK_BUFFER_SIZE 8192
62 int len, rc, saved_errno;
63 char *tempname;
64 struct stat srcstat, dststat, tempstat;
65 struct timeval now;
67 rc = stat(src, &srcstat);
68 if(rc < 0) {
69 if(errno == ENOENT)
70 return -2;
71 else
72 return -1;
75 rc = stat(dst, &dststat);
76 if(rc < 0) return -1;
78 if(!S_ISREG(srcstat.st_mode) || !S_ISREG(dststat.st_mode)) {
79 return -4;
82 if(srcstat.st_dev != dststat.st_dev) {
83 return -2;
86 if(srcstat.st_ino == dststat.st_ino)
87 /* Files are already linked */
88 return 0;
90 if(srcstat.st_size != dststat.st_size)
91 return -2;
93 /* link is atomic even on NFS, we will fail gracefully if the name
94 is not unique. */
95 gettimeofday(&now, NULL);
96 rc = strlen(dst) + 6;
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) {
102 free(tempname);
103 return -4;
106 rc = link(src, tempname);
107 if(rc < 0) {
108 /* We need to try to remove the link in case this was a
109 problem with NFS over an unreliable transport. */
110 goto fail;
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) {
122 unlink(tempname);
123 free(tempname);
124 return -3;
126 if(careful) {
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; }
135 i = 0;
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);
146 unlink(tempname);
147 free(tempname);
148 return -2;
150 i += rc1;
152 close(fd1); close(fd2);
155 rc = rename(tempname, dst);
156 if(rc < 0) goto fail;
158 free(tempname);
159 return 1;
161 fail:
162 saved_errno = errno;
163 unlink(tempname);
164 free(tempname);
165 errno = saved_errno;
166 if(errno == EPERM || errno == EOPNOTSUPP)
167 return -2;
168 return -1;
170 #undef RELINK_BUFFER_SIZE
173 #endif