]>
Commit | Line | Data |
---|---|---|
bb742ede | 1 | /* |
5e0de328 | 2 | * Copyright (C) 2009-2012 the libgit2 contributors |
bb742ede VM |
3 | * |
4 | * This file is part of libgit2, distributed under the GNU GPL v2 with | |
5 | * a Linking Exception. For full terms see the included COPYING file. | |
6 | */ | |
5ee2fe77 | 7 | #include "common.h" |
ec250c6e | 8 | #include "fileops.h" |
2e29957a | 9 | #include <ctype.h> |
997579be SS |
10 | #if GIT_WIN32 |
11 | #include "win32/findfile.h" | |
12 | #endif | |
ec250c6e | 13 | |
ce8cd006 | 14 | int git_futils_mkpath2file(const char *file_path, const mode_t mode) |
55ffebe3 | 15 | { |
ca1b6e54 RB |
16 | return git_futils_mkdir( |
17 | file_path, NULL, mode, GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST); | |
55ffebe3 VM |
18 | } |
19 | ||
97769280 | 20 | int git_futils_mktmp(git_buf *path_out, const char *filename) |
72a3fe42 VM |
21 | { |
22 | int fd; | |
23 | ||
97769280 RB |
24 | git_buf_sets(path_out, filename); |
25 | git_buf_puts(path_out, "_git2_XXXXXX"); | |
26 | ||
27 | if (git_buf_oom(path_out)) | |
1a481123 | 28 | return -1; |
72a3fe42 | 29 | |
1a481123 VM |
30 | if ((fd = p_mkstemp(path_out->ptr)) < 0) { |
31 | giterr_set(GITERR_OS, | |
ae9e29fd | 32 | "Failed to create temporary file '%s'", path_out->ptr); |
1a481123 VM |
33 | return -1; |
34 | } | |
72a3fe42 | 35 | |
f978b748 | 36 | return fd; |
72a3fe42 VM |
37 | } |
38 | ||
ce8cd006 | 39 | int git_futils_creat_withpath(const char *path, const mode_t dirmode, const mode_t mode) |
7dd8a9f7 | 40 | { |
1a481123 | 41 | int fd; |
55ffebe3 | 42 | |
1a481123 VM |
43 | if (git_futils_mkpath2file(path, dirmode) < 0) |
44 | return -1; | |
45 | ||
46 | fd = p_creat(path, mode); | |
47 | if (fd < 0) { | |
ae9e29fd | 48 | giterr_set(GITERR_OS, "Failed to create file '%s'", path); |
1a481123 VM |
49 | return -1; |
50 | } | |
51 | ||
52 | return fd; | |
55ffebe3 VM |
53 | } |
54 | ||
33127043 | 55 | int git_futils_creat_locked(const char *path, const mode_t mode) |
1549cba9 | 56 | { |
09719c50 | 57 | int fd; |
58 | ||
59 | #ifdef GIT_WIN32 | |
6813169a | 60 | wchar_t buf[GIT_WIN_PATH]; |
09719c50 | 61 | |
0f4c6175 | 62 | git__utf8_to_16(buf, GIT_WIN_PATH, path); |
09719c50 | 63 | fd = _wopen(buf, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_EXCL, mode); |
09719c50 | 64 | #else |
65 | fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_EXCL, mode); | |
66 | #endif | |
67 | ||
1a481123 | 68 | if (fd < 0) { |
ae9e29fd | 69 | giterr_set(GITERR_OS, "Failed to create locked file '%s'", path); |
1a481123 VM |
70 | return -1; |
71 | } | |
72 | ||
73 | return fd; | |
1549cba9 RG |
74 | } |
75 | ||
ce8cd006 | 76 | int git_futils_creat_locked_withpath(const char *path, const mode_t dirmode, const mode_t mode) |
1549cba9 | 77 | { |
1a481123 VM |
78 | if (git_futils_mkpath2file(path, dirmode) < 0) |
79 | return -1; | |
1549cba9 | 80 | |
f79026b4 | 81 | return git_futils_creat_locked(path, mode); |
ec250c6e AE |
82 | } |
83 | ||
deafee7b RB |
84 | int git_futils_open_ro(const char *path) |
85 | { | |
86 | int fd = p_open(path, O_RDONLY); | |
87 | if (fd < 0) { | |
d0a920a6 | 88 | if (errno == ENOENT || errno == ENOTDIR) |
deafee7b RB |
89 | fd = GIT_ENOTFOUND; |
90 | giterr_set(GITERR_OS, "Failed to open '%s'", path); | |
91 | } | |
92 | return fd; | |
93 | } | |
94 | ||
f79026b4 | 95 | git_off_t git_futils_filesize(git_file fd) |
ec250c6e | 96 | { |
7dd8a9f7 | 97 | struct stat sb; |
deafee7b RB |
98 | |
99 | if (p_fstat(fd, &sb)) { | |
100 | giterr_set(GITERR_OS, "Failed to stat file descriptor"); | |
101 | return -1; | |
102 | } | |
5ad739e8 | 103 | |
ec250c6e AE |
104 | return sb.st_size; |
105 | } | |
4188d28f | 106 | |
b6c93aef RB |
107 | mode_t git_futils_canonical_mode(mode_t raw_mode) |
108 | { | |
109 | if (S_ISREG(raw_mode)) | |
110 | return S_IFREG | GIT_CANONICAL_PERMS(raw_mode); | |
111 | else if (S_ISLNK(raw_mode)) | |
112 | return S_IFLNK; | |
b6c93aef RB |
113 | else if (S_ISGITLINK(raw_mode)) |
114 | return S_IFGITLINK; | |
7c7ff7d1 RB |
115 | else if (S_ISDIR(raw_mode)) |
116 | return S_IFDIR; | |
b6c93aef RB |
117 | else |
118 | return 0; | |
119 | } | |
120 | ||
60b9d3fc RB |
121 | int git_futils_readbuffer_fd(git_buf *buf, git_file fd, size_t len) |
122 | { | |
1f35e89d | 123 | ssize_t read_size; |
60b9d3fc RB |
124 | |
125 | git_buf_clear(buf); | |
126 | ||
127 | if (git_buf_grow(buf, len + 1) < 0) | |
128 | return -1; | |
129 | ||
1f35e89d RB |
130 | /* p_read loops internally to read len bytes */ |
131 | read_size = p_read(fd, buf->ptr, len); | |
60b9d3fc | 132 | |
c859184b | 133 | if (read_size != (ssize_t)len) { |
1f35e89d RB |
134 | giterr_set(GITERR_OS, "Failed to read descriptor"); |
135 | return -1; | |
60b9d3fc RB |
136 | } |
137 | ||
1f35e89d RB |
138 | buf->ptr[read_size] = '\0'; |
139 | buf->size = read_size; | |
140 | ||
60b9d3fc RB |
141 | return 0; |
142 | } | |
143 | ||
144 | int git_futils_readbuffer_updated( | |
145 | git_buf *buf, const char *path, time_t *mtime, int *updated) | |
75d58430 RJ |
146 | { |
147 | git_file fd; | |
c3da9f06 | 148 | struct stat st; |
75d58430 | 149 | |
13224ea4 | 150 | assert(buf && path && *path); |
75d58430 | 151 | |
c3da9f06 CMN |
152 | if (updated != NULL) |
153 | *updated = 0; | |
75d58430 | 154 | |
ae9e29fd RB |
155 | if ((fd = git_futils_open_ro(path)) < 0) |
156 | return fd; | |
c3da9f06 | 157 | |
13224ea4 | 158 | if (p_fstat(fd, &st) < 0 || S_ISDIR(st.st_mode) || !git__is_sizet(st.st_size+1)) { |
e1de726c | 159 | p_close(fd); |
1a481123 VM |
160 | giterr_set(GITERR_OS, "Invalid regular file stat for '%s'", path); |
161 | return -1; | |
13224ea4 | 162 | } |
c3da9f06 CMN |
163 | |
164 | /* | |
165 | * If we were given a time, we only want to read the file if it | |
166 | * has been modified. | |
167 | */ | |
13224ea4 | 168 | if (mtime != NULL && *mtime >= st.st_mtime) { |
e1de726c | 169 | p_close(fd); |
13224ea4 VM |
170 | return 0; |
171 | } | |
c3da9f06 CMN |
172 | |
173 | if (mtime != NULL) | |
174 | *mtime = st.st_mtime; | |
c3da9f06 | 175 | |
60b9d3fc | 176 | if (git_futils_readbuffer_fd(buf, fd, (size_t)st.st_size) < 0) { |
e1de726c RB |
177 | p_close(fd); |
178 | return -1; | |
75d58430 RJ |
179 | } |
180 | ||
f79026b4 | 181 | p_close(fd); |
75d58430 | 182 | |
c3da9f06 CMN |
183 | if (updated != NULL) |
184 | *updated = 1; | |
185 | ||
13224ea4 | 186 | return 0; |
c3da9f06 CMN |
187 | } |
188 | ||
13224ea4 | 189 | int git_futils_readbuffer(git_buf *buf, const char *path) |
97769280 | 190 | { |
13224ea4 | 191 | return git_futils_readbuffer_updated(buf, path, NULL, NULL); |
97769280 RB |
192 | } |
193 | ||
ce8cd006 | 194 | int git_futils_mv_withpath(const char *from, const char *to, const mode_t dirmode) |
19a30a3f | 195 | { |
deafee7b RB |
196 | if (git_futils_mkpath2file(to, dirmode) < 0) |
197 | return -1; | |
19a30a3f | 198 | |
deafee7b RB |
199 | if (p_rename(from, to) < 0) { |
200 | giterr_set(GITERR_OS, "Failed to rename '%s' to '%s'", from, to); | |
201 | return -1; | |
202 | } | |
203 | ||
204 | return 0; | |
19a30a3f VM |
205 | } |
206 | ||
f79026b4 | 207 | int git_futils_mmap_ro(git_map *out, git_file fd, git_off_t begin, size_t len) |
20e7f426 | 208 | { |
f79026b4 | 209 | return p_mmap(out, len, GIT_PROT_READ, GIT_MAP_SHARED, fd, begin); |
20e7f426 SP |
210 | } |
211 | ||
74fa4bfa RB |
212 | int git_futils_mmap_ro_file(git_map *out, const char *path) |
213 | { | |
0d0fa7c3 RB |
214 | git_file fd = git_futils_open_ro(path); |
215 | git_off_t len; | |
da9abdd6 | 216 | int result; |
0d0fa7c3 RB |
217 | |
218 | if (fd < 0) | |
219 | return fd; | |
220 | ||
221 | len = git_futils_filesize(fd); | |
deafee7b RB |
222 | if (!git__is_sizet(len)) { |
223 | giterr_set(GITERR_OS, "File `%s` too large to mmap", path); | |
224 | return -1; | |
225 | } | |
0d0fa7c3 | 226 | |
da9abdd6 | 227 | result = git_futils_mmap_ro(out, fd, 0, (size_t)len); |
74fa4bfa RB |
228 | p_close(fd); |
229 | return result; | |
230 | } | |
231 | ||
f79026b4 | 232 | void git_futils_mmap_free(git_map *out) |
20e7f426 | 233 | { |
f79026b4 | 234 | p_munmap(out); |
20e7f426 SP |
235 | } |
236 | ||
ca1b6e54 RB |
237 | int git_futils_mkdir( |
238 | const char *path, | |
239 | const char *base, | |
240 | mode_t mode, | |
241 | uint32_t flags) | |
1a5204a7 | 242 | { |
97769280 | 243 | git_buf make_path = GIT_BUF_INIT; |
ca1b6e54 RB |
244 | ssize_t root = 0; |
245 | char lastch, *tail; | |
dc07184f | 246 | |
ca1b6e54 RB |
247 | /* build path and find "root" where we should start calling mkdir */ |
248 | if (git_path_join_unrooted(&make_path, path, base, &root) < 0) | |
249 | return -1; | |
dc07184f | 250 | |
ca1b6e54 RB |
251 | if (make_path.size == 0) { |
252 | giterr_set(GITERR_OS, "Attempt to create empty path"); | |
253 | goto fail; | |
97769280 | 254 | } |
f0b2bfe5 | 255 | |
ca1b6e54 RB |
256 | /* remove trailing slashes on path */ |
257 | while (make_path.ptr[make_path.size - 1] == '/') { | |
258 | make_path.size--; | |
259 | make_path.ptr[make_path.size] = '\0'; | |
260 | } | |
412de9a6 | 261 | |
ca1b6e54 RB |
262 | /* if we are not supposed to made the last element, truncate it */ |
263 | if ((flags & GIT_MKDIR_SKIP_LAST) != 0) | |
264 | git_buf_rtruncate_at_char(&make_path, '/'); | |
265 | ||
266 | /* if we are not supposed to make the whole path, reset root */ | |
267 | if ((flags & GIT_MKDIR_PATH) == 0) | |
268 | root = git_buf_rfind(&make_path, '/'); | |
269 | ||
270 | /* clip root to make_path length */ | |
271 | if (root >= (ssize_t)make_path.size) | |
272 | root = (ssize_t)make_path.size - 1; | |
0e26202c RB |
273 | if (root < 0) |
274 | root = 0; | |
ca1b6e54 RB |
275 | |
276 | tail = & make_path.ptr[root]; | |
277 | ||
278 | while (*tail) { | |
279 | /* advance tail to include next path component */ | |
280 | while (*tail == '/') | |
281 | tail++; | |
282 | while (*tail && *tail != '/') | |
283 | tail++; | |
284 | ||
285 | /* truncate path at next component */ | |
286 | lastch = *tail; | |
287 | *tail = '\0'; | |
288 | ||
289 | /* make directory */ | |
290 | if (p_mkdir(make_path.ptr, mode) < 0 && | |
291 | (errno != EEXIST || (flags & GIT_MKDIR_EXCL) != 0)) | |
292 | { | |
293 | giterr_set(GITERR_OS, "Failed to make directory '%s'", | |
294 | make_path.ptr); | |
295 | goto fail; | |
d5f25204 | 296 | } |
40c44d2f | 297 | |
ca1b6e54 RB |
298 | /* chmod if requested */ |
299 | if ((flags & GIT_MKDIR_CHMOD_PATH) != 0 || | |
300 | ((flags & GIT_MKDIR_CHMOD) != 0 && lastch == '\0')) | |
301 | { | |
302 | if (p_chmod(make_path.ptr, mode) < 0) { | |
303 | giterr_set(GITERR_OS, "Failed to set permissions on '%s'", | |
304 | make_path.ptr); | |
305 | goto fail; | |
306 | } | |
307 | } | |
f0b2bfe5 | 308 | |
ca1b6e54 | 309 | *tail = lastch; |
a993e4fe | 310 | } |
40c44d2f | 311 | |
97769280 | 312 | git_buf_free(&make_path); |
ca1b6e54 | 313 | return 0; |
77c3999c | 314 | |
ca1b6e54 RB |
315 | fail: |
316 | git_buf_free(&make_path); | |
317 | return -1; | |
318 | } | |
77c3999c | 319 | |
ca1b6e54 RB |
320 | int git_futils_mkdir_r(const char *path, const char *base, const mode_t mode) |
321 | { | |
322 | return git_futils_mkdir(path, base, mode, GIT_MKDIR_PATH); | |
170d3f2f | 323 | } |
324 | ||
97769280 | 325 | static int _rmdir_recurs_foreach(void *opaque, git_buf *path) |
42b3a460 | 326 | { |
1a2b8725 | 327 | git_directory_removal_type removal_type = *(git_directory_removal_type *)opaque; |
555aa453 | 328 | |
1a481123 | 329 | if (git_path_isdir(path->ptr) == true) { |
deafee7b RB |
330 | if (git_path_direach(path, _rmdir_recurs_foreach, opaque) < 0) |
331 | return -1; | |
332 | ||
333 | if (p_rmdir(path->ptr) < 0) { | |
54bdc64a | 334 | if (removal_type == GIT_DIRREMOVAL_ONLY_EMPTY_DIRS && (errno == ENOTEMPTY || errno == EEXIST)) |
555aa453 | 335 | return 0; |
336 | ||
deafee7b RB |
337 | giterr_set(GITERR_OS, "Could not remove directory '%s'", path->ptr); |
338 | return -1; | |
339 | } | |
42b3a460 | 340 | |
deafee7b | 341 | return 0; |
858dba58 VM |
342 | } |
343 | ||
555aa453 | 344 | if (removal_type == GIT_DIRREMOVAL_FILES_AND_DIRS) { |
deafee7b RB |
345 | if (p_unlink(path->ptr) < 0) { |
346 | giterr_set(GITERR_OS, "Could not remove directory. File '%s' cannot be removed", path->ptr); | |
347 | return -1; | |
348 | } | |
349 | ||
350 | return 0; | |
351 | } | |
352 | ||
555aa453 | 353 | if (removal_type == GIT_DIRREMOVAL_EMPTY_HIERARCHY) { |
354 | giterr_set(GITERR_OS, "Could not remove directory. File '%s' still present", path->ptr); | |
355 | return -1; | |
356 | } | |
357 | ||
358 | return 0; | |
42b3a460 MS |
359 | } |
360 | ||
0d64bef9 RB |
361 | int git_futils_rmdir_r( |
362 | const char *path, const char *base, git_directory_removal_type removal_type) | |
42b3a460 | 363 | { |
97769280 | 364 | int error; |
0d64bef9 RB |
365 | git_buf fullpath = GIT_BUF_INIT; |
366 | ||
367 | assert(removal_type == GIT_DIRREMOVAL_EMPTY_HIERARCHY | |
368 | || removal_type == GIT_DIRREMOVAL_FILES_AND_DIRS | |
369 | || removal_type == GIT_DIRREMOVAL_ONLY_EMPTY_DIRS); | |
370 | ||
371 | /* build path and find "root" where we should start calling mkdir */ | |
372 | if (git_path_join_unrooted(&fullpath, path, base, NULL) < 0) | |
373 | return -1; | |
374 | ||
375 | error = _rmdir_recurs_foreach(&removal_type, &fullpath); | |
376 | ||
377 | git_buf_free(&fullpath); | |
97769280 | 378 | |
97769280 | 379 | return error; |
42b3a460 MS |
380 | } |
381 | ||
32a4e3b7 SS |
382 | int git_futils_find_system_file(git_buf *path, const char *filename) |
383 | { | |
384 | #ifdef GIT_WIN32 | |
385 | // try to find git.exe/git.cmd on path | |
dee18b82 | 386 | if (!win32_find_system_file_using_path(path, filename)) |
32a4e3b7 SS |
387 | return 0; |
388 | ||
389 | // try to find msysgit installation path using registry | |
dee18b82 | 390 | if (!win32_find_system_file_using_registry(path, filename)) |
32a4e3b7 | 391 | return 0; |
349fb6d7 | 392 | #else |
cb8a7961 VM |
393 | if (git_buf_joinpath(path, "/etc", filename) < 0) |
394 | return -1; | |
73b51450 | 395 | |
1a481123 | 396 | if (git_path_exists(path->ptr) == true) |
deafee7b | 397 | return 0; |
32a4e3b7 | 398 | #endif |
73b51450 RB |
399 | |
400 | git_buf_clear(path); | |
0b956819 | 401 | giterr_set(GITERR_OS, "The system file '%s' doesn't exist", filename); |
349fb6d7 | 402 | return GIT_ENOTFOUND; |
349fb6d7 | 403 | } |
73b51450 | 404 | |
349fb6d7 VM |
405 | int git_futils_find_global_file(git_buf *path, const char *filename) |
406 | { | |
aed8f8a1 SS |
407 | const char *home = getenv("HOME"); |
408 | ||
73b51450 | 409 | #ifdef GIT_WIN32 |
349fb6d7 VM |
410 | struct win32_path root; |
411 | ||
aed8f8a1 SS |
412 | if (home != NULL) { |
413 | if (git_buf_joinpath(path, home, filename) < 0) | |
414 | return -1; | |
415 | ||
416 | if (git_path_exists(path->ptr)) { | |
417 | return 0; | |
418 | } | |
419 | } | |
420 | ||
f2b126c7 SS |
421 | if (getenv("HOMEPATH") != NULL) { |
422 | if (win32_expand_path(&root, L"%HOMEDRIVE%%HOMEPATH%\\") < 0 || | |
423 | root.path[0] == L'%') /* i.e. no expansion happened */ | |
424 | { | |
425 | giterr_set(GITERR_OS, "Cannot locate the user's profile directory"); | |
18217e7e | 426 | return GIT_ENOTFOUND; |
f2b126c7 SS |
427 | } |
428 | } else { | |
429 | if (win32_expand_path(&root, L"%USERPROFILE%\\") < 0 || | |
430 | root.path[0] == L'%') /* i.e. no expansion happened */ | |
431 | { | |
432 | giterr_set(GITERR_OS, "Cannot locate the user's profile directory"); | |
18217e7e | 433 | return GIT_ENOTFOUND; |
f2b126c7 | 434 | } |
349fb6d7 VM |
435 | } |
436 | ||
29ef309e | 437 | if (win32_find_file(path, &root, filename) < 0) { |
0b956819 | 438 | giterr_set(GITERR_OS, "The global file '%s' doesn't exist", filename); |
29ef309e RB |
439 | git_buf_clear(path); |
440 | return GIT_ENOTFOUND; | |
441 | } | |
442 | ||
349fb6d7 | 443 | return 0; |
73b51450 | 444 | #else |
349fb6d7 VM |
445 | if (home == NULL) { |
446 | giterr_set(GITERR_OS, "Global file lookup failed. " | |
447 | "Cannot locate the user's home directory"); | |
18217e7e | 448 | return GIT_ENOTFOUND; |
349fb6d7 VM |
449 | } |
450 | ||
451 | if (git_buf_joinpath(path, home, filename) < 0) | |
452 | return -1; | |
453 | ||
454 | if (git_path_exists(path->ptr) == false) { | |
0b956819 | 455 | giterr_set(GITERR_OS, "The global file '%s' doesn't exist", filename); |
349fb6d7 VM |
456 | git_buf_clear(path); |
457 | return GIT_ENOTFOUND; | |
458 | } | |
459 | ||
460 | return 0; | |
73b51450 RB |
461 | #endif |
462 | } | |
8651c10f BS |
463 | |
464 | int git_futils_fake_symlink(const char *old, const char *new) | |
465 | { | |
466 | int retcode = GIT_ERROR; | |
467 | int fd = git_futils_creat_withpath(new, 0755, 0644); | |
468 | if (fd >= 0) { | |
469 | retcode = p_write(fd, old, strlen(old)); | |
470 | p_close(fd); | |
471 | } | |
472 | return retcode; | |
473 | } | |
ca1b6e54 | 474 | |
85bd1746 | 475 | static int cp_by_fd(int ifd, int ofd, bool close_fd_when_done) |
ca1b6e54 RB |
476 | { |
477 | int error = 0; | |
478 | char buffer[4096]; | |
479 | ssize_t len = 0; | |
480 | ||
481 | while (!error && (len = p_read(ifd, buffer, sizeof(buffer))) > 0) | |
482 | /* p_write() does not have the same semantics as write(). It loops | |
483 | * internally and will return 0 when it has completed writing. | |
484 | */ | |
485 | error = p_write(ofd, buffer, len); | |
486 | ||
487 | if (len < 0) { | |
488 | giterr_set(GITERR_OS, "Read error while copying file"); | |
489 | error = (int)len; | |
490 | } | |
491 | ||
85bd1746 | 492 | if (close_fd_when_done) { |
ca1b6e54 RB |
493 | p_close(ifd); |
494 | p_close(ofd); | |
495 | } | |
496 | ||
497 | return error; | |
498 | } | |
499 | ||
85bd1746 | 500 | int git_futils_cp(const char *from, const char *to, mode_t filemode) |
ca1b6e54 RB |
501 | { |
502 | int ifd, ofd; | |
503 | ||
ca1b6e54 RB |
504 | if ((ifd = git_futils_open_ro(from)) < 0) |
505 | return ifd; | |
506 | ||
507 | if ((ofd = p_open(to, O_WRONLY | O_CREAT | O_EXCL, filemode)) < 0) { | |
508 | if (errno == ENOENT || errno == ENOTDIR) | |
509 | ofd = GIT_ENOTFOUND; | |
510 | giterr_set(GITERR_OS, "Failed to open '%s' for writing", to); | |
511 | p_close(ifd); | |
512 | return ofd; | |
513 | } | |
514 | ||
85bd1746 | 515 | return cp_by_fd(ifd, ofd, true); |
ca1b6e54 RB |
516 | } |
517 | ||
85bd1746 | 518 | static int cp_link(const char *from, const char *to, size_t link_size) |
ca1b6e54 RB |
519 | { |
520 | int error = 0; | |
521 | ssize_t read_len; | |
85bd1746 | 522 | char *link_data = git__malloc(link_size + 1); |
ca1b6e54 RB |
523 | GITERR_CHECK_ALLOC(link_data); |
524 | ||
85bd1746 RB |
525 | read_len = p_readlink(from, link_data, link_size); |
526 | if (read_len != (ssize_t)link_size) { | |
ca1b6e54 RB |
527 | giterr_set(GITERR_OS, "Failed to read symlink data for '%s'", from); |
528 | error = -1; | |
529 | } | |
530 | else { | |
531 | link_data[read_len] = '\0'; | |
532 | ||
533 | if (p_symlink(link_data, to) < 0) { | |
534 | giterr_set(GITERR_OS, "Could not symlink '%s' as '%s'", | |
535 | link_data, to); | |
536 | error = -1; | |
537 | } | |
538 | } | |
539 | ||
540 | git__free(link_data); | |
541 | return error; | |
542 | } | |
543 | ||
544 | typedef struct { | |
545 | const char *to_root; | |
546 | git_buf to; | |
547 | ssize_t from_prefix; | |
548 | uint32_t flags; | |
549 | uint32_t mkdir_flags; | |
550 | mode_t dirmode; | |
551 | } cp_r_info; | |
552 | ||
553 | static int _cp_r_callback(void *ref, git_buf *from) | |
554 | { | |
555 | cp_r_info *info = ref; | |
556 | struct stat from_st, to_st; | |
557 | bool exists = false; | |
558 | ||
559 | if ((info->flags & GIT_CPDIR_COPY_DOTFILES) == 0 && | |
560 | from->ptr[git_path_basename_offset(from)] == '.') | |
561 | return 0; | |
562 | ||
563 | if (git_buf_joinpath( | |
564 | &info->to, info->to_root, from->ptr + info->from_prefix) < 0) | |
565 | return -1; | |
566 | ||
567 | if (p_lstat(info->to.ptr, &to_st) < 0) { | |
2eb4edf5 | 568 | if (errno != ENOENT && errno != ENOTDIR) { |
ca1b6e54 RB |
569 | giterr_set(GITERR_OS, |
570 | "Could not access %s while copying files", info->to.ptr); | |
571 | return -1; | |
572 | } | |
573 | } else | |
574 | exists = true; | |
575 | ||
576 | if (git_path_lstat(from->ptr, &from_st) < 0) | |
577 | return -1; | |
578 | ||
579 | if (S_ISDIR(from_st.st_mode)) { | |
580 | int error = 0; | |
581 | mode_t oldmode = info->dirmode; | |
582 | ||
583 | /* if we are not chmod'ing, then overwrite dirmode */ | |
584 | if ((info->flags & GIT_CPDIR_CHMOD) == 0) | |
585 | info->dirmode = from_st.st_mode; | |
586 | ||
587 | /* make directory now if CREATE_EMPTY_DIRS is requested and needed */ | |
588 | if (!exists && (info->flags & GIT_CPDIR_CREATE_EMPTY_DIRS) != 0) | |
589 | error = git_futils_mkdir( | |
590 | info->to.ptr, NULL, info->dirmode, info->mkdir_flags); | |
591 | ||
592 | /* recurse onto target directory */ | |
593 | if (!exists || S_ISDIR(to_st.st_mode)) | |
594 | error = git_path_direach(from, _cp_r_callback, info); | |
595 | ||
596 | if (oldmode != 0) | |
597 | info->dirmode = oldmode; | |
598 | ||
599 | return error; | |
600 | } | |
601 | ||
602 | if (exists) { | |
603 | if ((info->flags & GIT_CPDIR_OVERWRITE) == 0) | |
604 | return 0; | |
605 | ||
606 | if (p_unlink(info->to.ptr) < 0) { | |
607 | giterr_set(GITERR_OS, "Cannot overwrite existing file '%s'", | |
608 | info->to.ptr); | |
609 | return -1; | |
610 | } | |
611 | } | |
612 | ||
613 | /* Done if this isn't a regular file or a symlink */ | |
614 | if (!S_ISREG(from_st.st_mode) && | |
615 | (!S_ISLNK(from_st.st_mode) || | |
616 | (info->flags & GIT_CPDIR_COPY_SYMLINKS) == 0)) | |
617 | return 0; | |
618 | ||
619 | /* Make container directory on demand if needed */ | |
620 | if ((info->flags & GIT_CPDIR_CREATE_EMPTY_DIRS) == 0 && | |
621 | git_futils_mkdir( | |
622 | info->to.ptr, NULL, info->dirmode, info->mkdir_flags) < 0) | |
623 | return -1; | |
624 | ||
625 | /* make symlink or regular file */ | |
626 | if (S_ISLNK(from_st.st_mode)) | |
85bd1746 | 627 | return cp_link(from->ptr, info->to.ptr, (size_t)from_st.st_size); |
ca1b6e54 | 628 | else |
85bd1746 | 629 | return git_futils_cp(from->ptr, info->to.ptr, from_st.st_mode); |
ca1b6e54 RB |
630 | } |
631 | ||
632 | int git_futils_cp_r( | |
633 | const char *from, | |
634 | const char *to, | |
635 | uint32_t flags, | |
636 | mode_t dirmode) | |
637 | { | |
638 | int error; | |
639 | git_buf path = GIT_BUF_INIT; | |
640 | cp_r_info info; | |
641 | ||
642 | if (git_buf_sets(&path, from) < 0) | |
643 | return -1; | |
644 | ||
645 | info.to_root = to; | |
646 | info.flags = flags; | |
647 | info.dirmode = dirmode; | |
648 | info.from_prefix = path.size; | |
649 | git_buf_init(&info.to, 0); | |
650 | ||
651 | /* precalculate mkdir flags */ | |
652 | if ((flags & GIT_CPDIR_CREATE_EMPTY_DIRS) == 0) { | |
653 | info.mkdir_flags = GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST; | |
654 | if ((flags & GIT_CPDIR_CHMOD) != 0) | |
655 | info.mkdir_flags |= GIT_MKDIR_CHMOD_PATH; | |
656 | } else { | |
657 | info.mkdir_flags = | |
658 | ((flags & GIT_CPDIR_CHMOD) != 0) ? GIT_MKDIR_CHMOD : 0; | |
659 | } | |
660 | ||
661 | error = _cp_r_callback(&info, &path); | |
662 | ||
663 | git_buf_free(&path); | |
07c06f7a | 664 | git_buf_free(&info.to); |
ca1b6e54 RB |
665 | |
666 | return error; | |
667 | } |