]>
Commit | Line | Data |
---|---|---|
bb742ede | 1 | /* |
359fc2d2 | 2 | * Copyright (C) the libgit2 contributors. All rights reserved. |
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 | */ | |
eae0bfdc | 7 | |
22a2d3d5 | 8 | #include "futils.h" |
eae0bfdc | 9 | |
c25aa7cd | 10 | #include "runtime.h" |
500ec543 | 11 | #include "strmap.h" |
c25aa7cd | 12 | #include "hash.h" |
e579e0f7 MB |
13 | #include "rand.h" |
14 | ||
2e29957a | 15 | #include <ctype.h> |
997579be SS |
16 | #if GIT_WIN32 |
17 | #include "win32/findfile.h" | |
18 | #endif | |
ec250c6e | 19 | |
e579e0f7 MB |
20 | #define GIT_FILEMODE_DEFAULT 0100666 |
21 | ||
ce8cd006 | 22 | int git_futils_mkpath2file(const char *file_path, const mode_t mode) |
55ffebe3 | 23 | { |
ca1b6e54 | 24 | return git_futils_mkdir( |
ac2fba0e | 25 | file_path, mode, |
331e7de9 | 26 | GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST | GIT_MKDIR_VERIFY_DIR); |
55ffebe3 VM |
27 | } |
28 | ||
e579e0f7 | 29 | int git_futils_mktmp(git_str *path_out, const char *filename, mode_t mode) |
72a3fe42 | 30 | { |
e579e0f7 MB |
31 | const int open_flags = O_RDWR | O_CREAT | O_EXCL | O_BINARY | O_CLOEXEC; |
32 | unsigned int tries = 32; | |
72a3fe42 VM |
33 | int fd; |
34 | ||
e579e0f7 MB |
35 | while (tries--) { |
36 | uint64_t rand = git_rand_next(); | |
97769280 | 37 | |
e579e0f7 MB |
38 | git_str_sets(path_out, filename); |
39 | git_str_puts(path_out, "_git2_"); | |
40 | git_str_encode_hexstr(path_out, (void *)&rand, sizeof(uint64_t)); | |
72a3fe42 | 41 | |
e579e0f7 MB |
42 | if (git_str_oom(path_out)) |
43 | return -1; | |
72a3fe42 | 44 | |
e579e0f7 MB |
45 | /* Note that we open with O_CREAT | O_EXCL */ |
46 | if ((fd = p_open(path_out->ptr, open_flags, mode)) >= 0) | |
47 | return fd; | |
1d3a8aeb ET |
48 | } |
49 | ||
e579e0f7 MB |
50 | git_error_set(GIT_ERROR_OS, |
51 | "failed to create temporary file '%s'", path_out->ptr); | |
52 | git_str_dispose(path_out); | |
53 | return -1; | |
72a3fe42 VM |
54 | } |
55 | ||
ce8cd006 | 56 | int git_futils_creat_withpath(const char *path, const mode_t dirmode, const mode_t mode) |
7dd8a9f7 | 57 | { |
1a481123 | 58 | int fd; |
55ffebe3 | 59 | |
1a481123 VM |
60 | if (git_futils_mkpath2file(path, dirmode) < 0) |
61 | return -1; | |
62 | ||
63 | fd = p_creat(path, mode); | |
64 | if (fd < 0) { | |
ac3d33df | 65 | git_error_set(GIT_ERROR_OS, "failed to create file '%s'", path); |
1a481123 VM |
66 | return -1; |
67 | } | |
68 | ||
69 | return fd; | |
55ffebe3 VM |
70 | } |
71 | ||
33127043 | 72 | int git_futils_creat_locked(const char *path, const mode_t mode) |
1549cba9 | 73 | { |
4a26915d ET |
74 | int fd = p_open(path, O_WRONLY | O_CREAT | O_EXCL | O_BINARY | O_CLOEXEC, |
75 | mode); | |
09719c50 | 76 | |
1a481123 | 77 | if (fd < 0) { |
f94825c1 | 78 | int error = errno; |
ac3d33df | 79 | git_error_set(GIT_ERROR_OS, "failed to create locked file '%s'", path); |
f94825c1 CMN |
80 | switch (error) { |
81 | case EEXIST: | |
82 | return GIT_ELOCKED; | |
83 | case ENOENT: | |
84 | return GIT_ENOTFOUND; | |
85 | default: | |
86 | return -1; | |
87 | } | |
1a481123 VM |
88 | } |
89 | ||
90 | return fd; | |
1549cba9 RG |
91 | } |
92 | ||
ce8cd006 | 93 | int git_futils_creat_locked_withpath(const char *path, const mode_t dirmode, const mode_t mode) |
1549cba9 | 94 | { |
1a481123 VM |
95 | if (git_futils_mkpath2file(path, dirmode) < 0) |
96 | return -1; | |
1549cba9 | 97 | |
f79026b4 | 98 | return git_futils_creat_locked(path, mode); |
ec250c6e AE |
99 | } |
100 | ||
deafee7b RB |
101 | int git_futils_open_ro(const char *path) |
102 | { | |
103 | int fd = p_open(path, O_RDONLY); | |
14997dc5 | 104 | if (fd < 0) |
e579e0f7 | 105 | return git_fs_path_set_error(errno, path, "open"); |
deafee7b RB |
106 | return fd; |
107 | } | |
108 | ||
eae0bfdc PP |
109 | int git_futils_truncate(const char *path, int mode) |
110 | { | |
111 | int fd = p_open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, mode); | |
112 | if (fd < 0) | |
e579e0f7 | 113 | return git_fs_path_set_error(errno, path, "open"); |
eae0bfdc PP |
114 | |
115 | close(fd); | |
116 | return 0; | |
117 | } | |
118 | ||
22a2d3d5 | 119 | int git_futils_filesize(uint64_t *out, git_file fd) |
ec250c6e | 120 | { |
7dd8a9f7 | 121 | struct stat sb; |
deafee7b RB |
122 | |
123 | if (p_fstat(fd, &sb)) { | |
ac3d33df | 124 | git_error_set(GIT_ERROR_OS, "failed to stat file descriptor"); |
deafee7b RB |
125 | return -1; |
126 | } | |
5ad739e8 | 127 | |
22a2d3d5 UG |
128 | if (sb.st_size < 0) { |
129 | git_error_set(GIT_ERROR_INVALID, "invalid file size"); | |
130 | return -1; | |
131 | } | |
132 | ||
133 | *out = sb.st_size; | |
134 | return 0; | |
ec250c6e | 135 | } |
4188d28f | 136 | |
b6c93aef RB |
137 | mode_t git_futils_canonical_mode(mode_t raw_mode) |
138 | { | |
139 | if (S_ISREG(raw_mode)) | |
f240acce | 140 | return S_IFREG | GIT_PERMS_CANONICAL(raw_mode); |
b6c93aef RB |
141 | else if (S_ISLNK(raw_mode)) |
142 | return S_IFLNK; | |
b6c93aef RB |
143 | else if (S_ISGITLINK(raw_mode)) |
144 | return S_IFGITLINK; | |
7c7ff7d1 RB |
145 | else if (S_ISDIR(raw_mode)) |
146 | return S_IFDIR; | |
b6c93aef RB |
147 | else |
148 | return 0; | |
149 | } | |
150 | ||
e579e0f7 | 151 | int git_futils_readbuffer_fd(git_str *buf, git_file fd, size_t len) |
60b9d3fc | 152 | { |
de590550 | 153 | ssize_t read_size = 0; |
f1453c59 | 154 | size_t alloc_len; |
60b9d3fc | 155 | |
e579e0f7 | 156 | git_str_clear(buf); |
60b9d3fc | 157 | |
8d534b47 | 158 | if (!git__is_ssizet(len)) { |
ac3d33df | 159 | git_error_set(GIT_ERROR_INVALID, "read too large"); |
8d534b47 ET |
160 | return -1; |
161 | } | |
162 | ||
ac3d33df | 163 | GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, len, 1); |
e579e0f7 | 164 | if (git_str_grow(buf, alloc_len) < 0) |
60b9d3fc RB |
165 | return -1; |
166 | ||
1f35e89d RB |
167 | /* p_read loops internally to read len bytes */ |
168 | read_size = p_read(fd, buf->ptr, len); | |
60b9d3fc | 169 | |
c859184b | 170 | if (read_size != (ssize_t)len) { |
ac3d33df | 171 | git_error_set(GIT_ERROR_OS, "failed to read descriptor"); |
e579e0f7 | 172 | git_str_dispose(buf); |
1f35e89d | 173 | return -1; |
60b9d3fc RB |
174 | } |
175 | ||
1f35e89d RB |
176 | buf->ptr[read_size] = '\0'; |
177 | buf->size = read_size; | |
178 | ||
60b9d3fc RB |
179 | return 0; |
180 | } | |
181 | ||
182 | int git_futils_readbuffer_updated( | |
e579e0f7 MB |
183 | git_str *out, |
184 | const char *path, | |
185 | unsigned char checksum[GIT_HASH_SHA1_SIZE], | |
186 | int *updated) | |
75d58430 | 187 | { |
eb597799 | 188 | int error; |
75d58430 | 189 | git_file fd; |
c3da9f06 | 190 | struct stat st; |
e579e0f7 MB |
191 | git_str buf = GIT_STR_INIT; |
192 | unsigned char checksum_new[GIT_HASH_SHA1_SIZE]; | |
75d58430 | 193 | |
c25aa7cd PP |
194 | GIT_ASSERT_ARG(out); |
195 | GIT_ASSERT_ARG(path && *path); | |
75d58430 | 196 | |
c3da9f06 CMN |
197 | if (updated != NULL) |
198 | *updated = 0; | |
75d58430 | 199 | |
14997dc5 | 200 | if (p_stat(path, &st) < 0) |
e579e0f7 | 201 | return git_fs_path_set_error(errno, path, "stat"); |
c3da9f06 | 202 | |
7c9f5bec CMN |
203 | |
204 | if (S_ISDIR(st.st_mode)) { | |
ac3d33df | 205 | git_error_set(GIT_ERROR_INVALID, "requested file is a directory"); |
7c9f5bec CMN |
206 | return GIT_ENOTFOUND; |
207 | } | |
208 | ||
209 | if (!git__is_sizet(st.st_size+1)) { | |
ac3d33df | 210 | git_error_set(GIT_ERROR_OS, "invalid regular file stat for '%s'", path); |
1a481123 | 211 | return -1; |
13224ea4 | 212 | } |
c3da9f06 | 213 | |
9ccdb211 BR |
214 | if ((fd = git_futils_open_ro(path)) < 0) |
215 | return fd; | |
216 | ||
3547b122 | 217 | if (git_futils_readbuffer_fd(&buf, fd, (size_t)st.st_size) < 0) { |
e1de726c RB |
218 | p_close(fd); |
219 | return -1; | |
75d58430 RJ |
220 | } |
221 | ||
f79026b4 | 222 | p_close(fd); |
75d58430 | 223 | |
11c8e756 | 224 | if (checksum) { |
e579e0f7 MB |
225 | if ((error = git_hash_buf(checksum_new, buf.ptr, buf.size, GIT_HASH_ALGORITHM_SHA1)) < 0) { |
226 | git_str_dispose(&buf); | |
11c8e756 ET |
227 | return error; |
228 | } | |
eb597799 | 229 | |
11c8e756 ET |
230 | /* |
231 | * If we were given a checksum, we only want to use it if it's different | |
232 | */ | |
e579e0f7 MB |
233 | if (!memcmp(checksum, checksum_new, GIT_HASH_SHA1_SIZE)) { |
234 | git_str_dispose(&buf); | |
11c8e756 ET |
235 | if (updated) |
236 | *updated = 0; | |
eb597799 | 237 | |
11c8e756 ET |
238 | return 0; |
239 | } | |
240 | ||
e579e0f7 | 241 | memcpy(checksum, checksum_new, GIT_HASH_SHA1_SIZE); |
eb597799 CMN |
242 | } |
243 | ||
244 | /* | |
245 | * If we're here, the file did change, or the user didn't have an old version | |
246 | */ | |
c3da9f06 CMN |
247 | if (updated != NULL) |
248 | *updated = 1; | |
249 | ||
e579e0f7 MB |
250 | git_str_swap(out, &buf); |
251 | git_str_dispose(&buf); | |
3547b122 | 252 | |
13224ea4 | 253 | return 0; |
c3da9f06 CMN |
254 | } |
255 | ||
e579e0f7 | 256 | int git_futils_readbuffer(git_str *buf, const char *path) |
97769280 | 257 | { |
eb597799 | 258 | return git_futils_readbuffer_updated(buf, path, NULL, NULL); |
97769280 RB |
259 | } |
260 | ||
4742148d | 261 | int git_futils_writebuffer( |
e579e0f7 | 262 | const git_str *buf, const char *path, int flags, mode_t mode) |
4742148d | 263 | { |
5312621b | 264 | int fd, do_fsync = 0, error = 0; |
4742148d | 265 | |
5a747e0c | 266 | if (!flags) |
4742148d | 267 | flags = O_CREAT | O_TRUNC | O_WRONLY; |
5a747e0c | 268 | |
5312621b ET |
269 | if ((flags & O_FSYNC) != 0) |
270 | do_fsync = 1; | |
271 | ||
272 | flags &= ~O_FSYNC; | |
4742148d | 273 | |
4742148d | 274 | if (!mode) |
e579e0f7 | 275 | mode = GIT_FILEMODE_DEFAULT; |
4742148d RB |
276 | |
277 | if ((fd = p_open(path, flags, mode)) < 0) { | |
ac3d33df | 278 | git_error_set(GIT_ERROR_OS, "could not open '%s' for writing", path); |
4742148d RB |
279 | return fd; |
280 | } | |
281 | ||
e579e0f7 | 282 | if ((error = p_write(fd, git_str_cstr(buf), git_str_len(buf))) < 0) { |
ac3d33df | 283 | git_error_set(GIT_ERROR_OS, "could not write to '%s'", path); |
4742148d | 284 | (void)p_close(fd); |
f5254d78 | 285 | return error; |
4742148d RB |
286 | } |
287 | ||
5312621b | 288 | if (do_fsync && (error = p_fsync(fd)) < 0) { |
ac3d33df | 289 | git_error_set(GIT_ERROR_OS, "could not fsync '%s'", path); |
5312621b ET |
290 | p_close(fd); |
291 | return error; | |
292 | } | |
293 | ||
1229e1c4 | 294 | if ((error = p_close(fd)) < 0) { |
ac3d33df | 295 | git_error_set(GIT_ERROR_OS, "error while closing '%s'", path); |
1229e1c4 ET |
296 | return error; |
297 | } | |
298 | ||
299 | if (do_fsync && (flags & O_CREAT)) | |
300 | error = git_futils_fsync_parent(path); | |
4742148d RB |
301 | |
302 | return error; | |
303 | } | |
304 | ||
ce8cd006 | 305 | int git_futils_mv_withpath(const char *from, const char *to, const mode_t dirmode) |
19a30a3f | 306 | { |
deafee7b RB |
307 | if (git_futils_mkpath2file(to, dirmode) < 0) |
308 | return -1; | |
19a30a3f | 309 | |
deafee7b | 310 | if (p_rename(from, to) < 0) { |
ac3d33df | 311 | git_error_set(GIT_ERROR_OS, "failed to rename '%s' to '%s'", from, to); |
deafee7b RB |
312 | return -1; |
313 | } | |
314 | ||
315 | return 0; | |
19a30a3f VM |
316 | } |
317 | ||
22a2d3d5 | 318 | int git_futils_mmap_ro(git_map *out, git_file fd, off64_t begin, size_t len) |
20e7f426 | 319 | { |
f79026b4 | 320 | return p_mmap(out, len, GIT_PROT_READ, GIT_MAP_SHARED, fd, begin); |
20e7f426 SP |
321 | } |
322 | ||
74fa4bfa RB |
323 | int git_futils_mmap_ro_file(git_map *out, const char *path) |
324 | { | |
0d0fa7c3 | 325 | git_file fd = git_futils_open_ro(path); |
22a2d3d5 | 326 | uint64_t len; |
da9abdd6 | 327 | int result; |
0d0fa7c3 RB |
328 | |
329 | if (fd < 0) | |
330 | return fd; | |
331 | ||
22a2d3d5 | 332 | if ((result = git_futils_filesize(&len, fd)) < 0) |
38b6e700 | 333 | goto out; |
9daba9f4 | 334 | |
deafee7b | 335 | if (!git__is_sizet(len)) { |
ac3d33df | 336 | git_error_set(GIT_ERROR_OS, "file `%s` too large to mmap", path); |
38b6e700 PS |
337 | result = -1; |
338 | goto out; | |
deafee7b | 339 | } |
0d0fa7c3 | 340 | |
da9abdd6 | 341 | result = git_futils_mmap_ro(out, fd, 0, (size_t)len); |
38b6e700 | 342 | out: |
74fa4bfa RB |
343 | p_close(fd); |
344 | return result; | |
345 | } | |
346 | ||
f79026b4 | 347 | void git_futils_mmap_free(git_map *out) |
20e7f426 | 348 | { |
f79026b4 | 349 | p_munmap(out); |
20e7f426 SP |
350 | } |
351 | ||
81aaf370 ET |
352 | GIT_INLINE(int) mkdir_validate_dir( |
353 | const char *path, | |
e74340b0 ET |
354 | struct stat *st, |
355 | mode_t mode, | |
356 | uint32_t flags, | |
81aaf370 | 357 | struct git_futils_mkdir_options *opts) |
e74340b0 | 358 | { |
81aaf370 ET |
359 | /* with exclusive create, existing dir is an error */ |
360 | if ((flags & GIT_MKDIR_EXCL) != 0) { | |
ac3d33df | 361 | git_error_set(GIT_ERROR_FILESYSTEM, |
909d5494 | 362 | "failed to make directory '%s': directory exists", path); |
81aaf370 ET |
363 | return GIT_EEXISTS; |
364 | } | |
365 | ||
e74340b0 ET |
366 | if ((S_ISREG(st->st_mode) && (flags & GIT_MKDIR_REMOVE_FILES)) || |
367 | (S_ISLNK(st->st_mode) && (flags & GIT_MKDIR_REMOVE_SYMLINKS))) { | |
81aaf370 | 368 | if (p_unlink(path) < 0) { |
ac3d33df | 369 | git_error_set(GIT_ERROR_OS, "failed to remove %s '%s'", |
81aaf370 | 370 | S_ISLNK(st->st_mode) ? "symlink" : "file", path); |
e74340b0 ET |
371 | return GIT_EEXISTS; |
372 | } | |
373 | ||
81aaf370 | 374 | opts->perfdata.mkdir_calls++; |
e74340b0 | 375 | |
81aaf370 | 376 | if (p_mkdir(path, mode) < 0) { |
ac3d33df | 377 | git_error_set(GIT_ERROR_OS, "failed to make directory '%s'", path); |
e74340b0 ET |
378 | return GIT_EEXISTS; |
379 | } | |
380 | } | |
381 | ||
382 | else if (S_ISLNK(st->st_mode)) { | |
383 | /* Re-stat the target, make sure it's a directory */ | |
81aaf370 | 384 | opts->perfdata.stat_calls++; |
e74340b0 | 385 | |
81aaf370 | 386 | if (p_stat(path, st) < 0) { |
ac3d33df | 387 | git_error_set(GIT_ERROR_OS, "failed to make directory '%s'", path); |
e74340b0 ET |
388 | return GIT_EEXISTS; |
389 | } | |
390 | } | |
391 | ||
392 | else if (!S_ISDIR(st->st_mode)) { | |
ac3d33df | 393 | git_error_set(GIT_ERROR_FILESYSTEM, |
909d5494 | 394 | "failed to make directory '%s': directory exists", path); |
e74340b0 ET |
395 | return GIT_EEXISTS; |
396 | } | |
397 | ||
398 | return 0; | |
399 | } | |
400 | ||
81aaf370 ET |
401 | GIT_INLINE(int) mkdir_validate_mode( |
402 | const char *path, | |
403 | struct stat *st, | |
404 | bool terminal_path, | |
405 | mode_t mode, | |
406 | uint32_t flags, | |
407 | struct git_futils_mkdir_options *opts) | |
408 | { | |
409 | if (((terminal_path && (flags & GIT_MKDIR_CHMOD) != 0) || | |
410 | (flags & GIT_MKDIR_CHMOD_PATH) != 0) && st->st_mode != mode) { | |
411 | ||
412 | opts->perfdata.chmod_calls++; | |
413 | ||
414 | if (p_chmod(path, mode) < 0) { | |
ac3d33df | 415 | git_error_set(GIT_ERROR_OS, "failed to set permissions on '%s'", path); |
81aaf370 ET |
416 | return -1; |
417 | } | |
418 | } | |
419 | ||
420 | return 0; | |
421 | } | |
28cdb315 | 422 | |
e24c60db | 423 | GIT_INLINE(int) mkdir_canonicalize( |
e579e0f7 | 424 | git_str *path, |
e24c60db | 425 | uint32_t flags) |
1a5204a7 | 426 | { |
e24c60db | 427 | ssize_t root_len; |
ac2fba0e | 428 | |
e24c60db | 429 | if (path->size == 0) { |
ac3d33df | 430 | git_error_set(GIT_ERROR_OS, "attempt to create empty path"); |
ca1b6e54 | 431 | return -1; |
97769280 | 432 | } |
f0b2bfe5 | 433 | |
9cb5b0f7 | 434 | /* Trim trailing slashes (except the root) */ |
e579e0f7 | 435 | if ((root_len = git_fs_path_root(path->ptr)) < 0) |
9cb5b0f7 ET |
436 | root_len = 0; |
437 | else | |
438 | root_len++; | |
439 | ||
e24c60db ET |
440 | while (path->size > (size_t)root_len && path->ptr[path->size - 1] == '/') |
441 | path->ptr[--path->size] = '\0'; | |
412de9a6 | 442 | |
ca1b6e54 | 443 | /* if we are not supposed to made the last element, truncate it */ |
3c42e4ef | 444 | if ((flags & GIT_MKDIR_SKIP_LAST2) != 0) { |
e579e0f7 | 445 | git_fs_path_dirname_r(path, path->ptr); |
3c42e4ef RB |
446 | flags |= GIT_MKDIR_SKIP_LAST; |
447 | } | |
9cb5b0f7 | 448 | if ((flags & GIT_MKDIR_SKIP_LAST) != 0) { |
e579e0f7 | 449 | git_fs_path_dirname_r(path, path->ptr); |
9cb5b0f7 | 450 | } |
ca1b6e54 | 451 | |
9cb5b0f7 | 452 | /* We were either given the root path (or trimmed it to |
e24c60db ET |
453 | * the root), we don't have anything to do. |
454 | */ | |
455 | if (path->size <= (size_t)root_len) | |
e579e0f7 | 456 | git_str_clear(path); |
e24c60db ET |
457 | |
458 | return 0; | |
459 | } | |
460 | ||
461 | int git_futils_mkdir( | |
462 | const char *path, | |
463 | mode_t mode, | |
464 | uint32_t flags) | |
465 | { | |
e579e0f7 | 466 | git_str make_path = GIT_STR_INIT, parent_path = GIT_STR_INIT; |
e24c60db ET |
467 | const char *relative; |
468 | struct git_futils_mkdir_options opts = { 0 }; | |
469 | struct stat st; | |
470 | size_t depth = 0; | |
9ce2e7b3 | 471 | int len = 0, root_len, error; |
e24c60db | 472 | |
e579e0f7 | 473 | if ((error = git_str_puts(&make_path, path)) < 0 || |
e24c60db | 474 | (error = mkdir_canonicalize(&make_path, flags)) < 0 || |
e579e0f7 | 475 | (error = git_str_puts(&parent_path, make_path.ptr)) < 0 || |
e24c60db ET |
476 | make_path.size == 0) |
477 | goto done; | |
478 | ||
e579e0f7 | 479 | root_len = git_fs_path_root(make_path.ptr); |
9ce2e7b3 | 480 | |
e24c60db ET |
481 | /* find the first parent directory that exists. this will be used |
482 | * as the base to dirname_relative. | |
9cb5b0f7 | 483 | */ |
e24c60db ET |
484 | for (relative = make_path.ptr; parent_path.size; ) { |
485 | error = p_lstat(parent_path.ptr, &st); | |
486 | ||
487 | if (error == 0) { | |
488 | break; | |
489 | } else if (errno != ENOENT) { | |
ac3d33df | 490 | git_error_set(GIT_ERROR_OS, "failed to stat '%s'", parent_path.ptr); |
22a2d3d5 | 491 | error = -1; |
e24c60db ET |
492 | goto done; |
493 | } | |
494 | ||
495 | depth++; | |
496 | ||
497 | /* examine the parent of the current path */ | |
e579e0f7 | 498 | if ((len = git_fs_path_dirname_r(&parent_path, parent_path.ptr)) < 0) { |
e24c60db ET |
499 | error = len; |
500 | goto done; | |
501 | } | |
502 | ||
c25aa7cd | 503 | GIT_ASSERT(len); |
e24c60db | 504 | |
6147f643 PP |
505 | /* |
506 | * We've walked all the given path's parents and it's either relative | |
507 | * (the parent is simply '.') or rooted (the length is less than or | |
508 | * equal to length of the root path). The path may be less than the | |
509 | * root path length on Windows, where `C:` == `C:/`. | |
e24c60db | 510 | */ |
22a2d3d5 UG |
511 | if ((len == 1 && parent_path.ptr[0] == '.') || |
512 | (len == 1 && parent_path.ptr[0] == '/') || | |
513 | len <= root_len) { | |
e24c60db ET |
514 | relative = make_path.ptr; |
515 | break; | |
516 | } | |
517 | ||
518 | relative = make_path.ptr + len + 1; | |
519 | ||
520 | /* not recursive? just make this directory relative to its parent. */ | |
521 | if ((flags & GIT_MKDIR_PATH) == 0) | |
522 | break; | |
523 | } | |
524 | ||
525 | /* we found an item at the location we're trying to create, | |
526 | * validate it. | |
527 | */ | |
528 | if (depth == 0) { | |
81aaf370 | 529 | error = mkdir_validate_dir(make_path.ptr, &st, mode, flags, &opts); |
e24c60db | 530 | |
81aaf370 ET |
531 | if (!error) |
532 | error = mkdir_validate_mode( | |
533 | make_path.ptr, &st, true, mode, flags, &opts); | |
e24c60db | 534 | |
f7e56150 RB |
535 | goto done; |
536 | } | |
537 | ||
e24c60db ET |
538 | /* we already took `SKIP_LAST` and `SKIP_LAST2` into account when |
539 | * canonicalizing `make_path`. | |
540 | */ | |
541 | flags &= ~(GIT_MKDIR_SKIP_LAST2 | GIT_MKDIR_SKIP_LAST); | |
542 | ||
543 | error = git_futils_mkdir_relative(relative, | |
544 | parent_path.size ? parent_path.ptr : NULL, mode, flags, &opts); | |
545 | ||
546 | done: | |
e579e0f7 MB |
547 | git_str_dispose(&make_path); |
548 | git_str_dispose(&parent_path); | |
e24c60db ET |
549 | return error; |
550 | } | |
551 | ||
552 | int git_futils_mkdir_r(const char *path, const mode_t mode) | |
553 | { | |
554 | return git_futils_mkdir(path, mode, GIT_MKDIR_PATH); | |
555 | } | |
556 | ||
557 | int git_futils_mkdir_relative( | |
558 | const char *relative_path, | |
559 | const char *base, | |
560 | mode_t mode, | |
561 | uint32_t flags, | |
562 | struct git_futils_mkdir_options *opts) | |
563 | { | |
e579e0f7 | 564 | git_str make_path = GIT_STR_INIT; |
e24c60db ET |
565 | ssize_t root = 0, min_root_len; |
566 | char lastch = '/', *tail; | |
567 | struct stat st; | |
568 | struct git_futils_mkdir_options empty_opts = {0}; | |
569 | int error; | |
570 | ||
571 | if (!opts) | |
572 | opts = &empty_opts; | |
573 | ||
574 | /* build path and find "root" where we should start calling mkdir */ | |
e579e0f7 | 575 | if (git_fs_path_join_unrooted(&make_path, relative_path, base, &root) < 0) |
e24c60db ET |
576 | return -1; |
577 | ||
578 | if ((error = mkdir_canonicalize(&make_path, flags)) < 0 || | |
579 | make_path.size == 0) | |
580 | goto done; | |
581 | ||
ca1b6e54 RB |
582 | /* if we are not supposed to make the whole path, reset root */ |
583 | if ((flags & GIT_MKDIR_PATH) == 0) | |
e579e0f7 | 584 | root = git_str_rfind(&make_path, '/'); |
ca1b6e54 | 585 | |
f7e56150 | 586 | /* advance root past drive name or network mount prefix */ |
e579e0f7 | 587 | min_root_len = git_fs_path_root(make_path.ptr); |
f7e56150 RB |
588 | if (root < min_root_len) |
589 | root = min_root_len; | |
2da72fb2 | 590 | while (root >= 0 && make_path.ptr[root] == '/') |
f7e56150 RB |
591 | ++root; |
592 | ||
ca1b6e54 | 593 | /* clip root to make_path length */ |
f7e56150 RB |
594 | if (root > (ssize_t)make_path.size) |
595 | root = (ssize_t)make_path.size; /* i.e. NUL byte of string */ | |
0e26202c RB |
596 | if (root < 0) |
597 | root = 0; | |
ca1b6e54 | 598 | |
f7e56150 RB |
599 | /* walk down tail of path making each directory */ |
600 | for (tail = &make_path.ptr[root]; *tail; *tail = lastch) { | |
d88e6e9b | 601 | bool mkdir_attempted = false; |
999d4405 | 602 | |
ca1b6e54 RB |
603 | /* advance tail to include next path component */ |
604 | while (*tail == '/') | |
605 | tail++; | |
606 | while (*tail && *tail != '/') | |
607 | tail++; | |
608 | ||
609 | /* truncate path at next component */ | |
610 | lastch = *tail; | |
611 | *tail = '\0'; | |
999d4405 | 612 | st.st_mode = 0; |
ca1b6e54 | 613 | |
500ec543 ET |
614 | if (opts->dir_map && git_strmap_exists(opts->dir_map, make_path.ptr)) |
615 | continue; | |
616 | ||
fe598f09 | 617 | /* See what's going on with this path component */ |
500ec543 | 618 | opts->perfdata.stat_calls++; |
1d50b364 | 619 | |
d88e6e9b | 620 | retry_lstat: |
fe598f09 | 621 | if (p_lstat(make_path.ptr, &st) < 0) { |
d88e6e9b | 622 | if (mkdir_attempted || errno != ENOENT) { |
ac3d33df | 623 | git_error_set(GIT_ERROR_OS, "cannot access component in path '%s'", make_path.ptr); |
d88e6e9b | 624 | error = -1; |
fe598f09 ET |
625 | goto done; |
626 | } | |
627 | ||
ac3d33df | 628 | git_error_clear(); |
d88e6e9b VM |
629 | opts->perfdata.mkdir_calls++; |
630 | mkdir_attempted = true; | |
631 | if (p_mkdir(make_path.ptr, mode) < 0) { | |
632 | if (errno == EEXIST) | |
633 | goto retry_lstat; | |
ac3d33df | 634 | git_error_set(GIT_ERROR_OS, "failed to make directory '%s'", make_path.ptr); |
d88e6e9b VM |
635 | error = -1; |
636 | goto done; | |
637 | } | |
fe598f09 | 638 | } else { |
81aaf370 ET |
639 | if ((error = mkdir_validate_dir( |
640 | make_path.ptr, &st, mode, flags, opts)) < 0) | |
fe598f09 | 641 | goto done; |
d5f25204 | 642 | } |
40c44d2f | 643 | |
999d4405 | 644 | /* chmod if requested and necessary */ |
81aaf370 ET |
645 | if ((error = mkdir_validate_mode( |
646 | make_path.ptr, &st, (lastch == '\0'), mode, flags, opts)) < 0) | |
647 | goto done; | |
500ec543 ET |
648 | |
649 | if (opts->dir_map && opts->pool) { | |
f1453c59 ET |
650 | char *cache_path; |
651 | size_t alloc_size; | |
652 | ||
ac3d33df | 653 | GIT_ERROR_CHECK_ALLOC_ADD(&alloc_size, make_path.size, 1); |
22a2d3d5 | 654 | cache_path = git_pool_malloc(opts->pool, alloc_size); |
ac3d33df | 655 | GIT_ERROR_CHECK_ALLOC(cache_path); |
500ec543 ET |
656 | |
657 | memcpy(cache_path, make_path.ptr, make_path.size + 1); | |
658 | ||
22a2d3d5 | 659 | if ((error = git_strmap_set(opts->dir_map, cache_path, cache_path)) < 0) |
500ec543 ET |
660 | goto done; |
661 | } | |
a993e4fe | 662 | } |
40c44d2f | 663 | |
999d4405 RB |
664 | error = 0; |
665 | ||
666 | /* check that full path really is a directory if requested & needed */ | |
f7e56150 | 667 | if ((flags & GIT_MKDIR_VERIFY_DIR) != 0 && |
1d50b364 | 668 | lastch != '\0') { |
500ec543 | 669 | opts->perfdata.stat_calls++; |
1d50b364 ET |
670 | |
671 | if (p_stat(make_path.ptr, &st) < 0 || !S_ISDIR(st.st_mode)) { | |
ac3d33df | 672 | git_error_set(GIT_ERROR_OS, "path is not a directory '%s'", |
e74340b0 | 673 | make_path.ptr); |
1d50b364 ET |
674 | error = GIT_ENOTFOUND; |
675 | } | |
999d4405 | 676 | } |
77c3999c | 677 | |
999d4405 | 678 | done: |
e579e0f7 | 679 | git_str_dispose(&make_path); |
331e7de9 | 680 | return error; |
ca1b6e54 | 681 | } |
77c3999c | 682 | |
331e7de9 | 683 | typedef struct { |
ad9a921b | 684 | const char *base; |
7e5c8a5b | 685 | size_t baselen; |
331e7de9 | 686 | uint32_t flags; |
219d3457 | 687 | int depth; |
331e7de9 RB |
688 | } futils__rmdir_data; |
689 | ||
219d3457 RB |
690 | #define FUTILS_MAX_DEPTH 100 |
691 | ||
331e7de9 | 692 | static int futils__error_cannot_rmdir(const char *path, const char *filemsg) |
42b3a460 | 693 | { |
331e7de9 | 694 | if (filemsg) |
ac3d33df | 695 | git_error_set(GIT_ERROR_OS, "could not remove directory '%s': %s", |
331e7de9 RB |
696 | path, filemsg); |
697 | else | |
ac3d33df | 698 | git_error_set(GIT_ERROR_OS, "could not remove directory '%s'", path); |
555aa453 | 699 | |
331e7de9 RB |
700 | return -1; |
701 | } | |
deafee7b | 702 | |
e579e0f7 | 703 | static int futils__rm_first_parent(git_str *path, const char *ceiling) |
ad9a921b RB |
704 | { |
705 | int error = GIT_ENOTFOUND; | |
706 | struct stat st; | |
707 | ||
708 | while (error == GIT_ENOTFOUND) { | |
e579e0f7 | 709 | git_str_rtruncate_at_char(path, '/'); |
ad9a921b RB |
710 | |
711 | if (!path->size || git__prefixcmp(path->ptr, ceiling) != 0) | |
712 | error = 0; | |
cccacac5 | 713 | else if (p_lstat_posixly(path->ptr, &st) == 0) { |
ad9a921b RB |
714 | if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) |
715 | error = p_unlink(path->ptr); | |
716 | else if (!S_ISDIR(st.st_mode)) | |
717 | error = -1; /* fail to remove non-regular file */ | |
718 | } else if (errno != ENOTDIR) | |
719 | error = -1; | |
720 | } | |
721 | ||
722 | if (error) | |
723 | futils__error_cannot_rmdir(path->ptr, "cannot remove parent"); | |
724 | ||
725 | return error; | |
726 | } | |
727 | ||
e579e0f7 | 728 | static int futils__rmdir_recurs_foreach(void *opaque, git_str *path) |
331e7de9 | 729 | { |
96869a4e | 730 | int error = 0; |
331e7de9 | 731 | futils__rmdir_data *data = opaque; |
14997dc5 | 732 | struct stat st; |
555aa453 | 733 | |
14997dc5 RB |
734 | if (data->depth > FUTILS_MAX_DEPTH) |
735 | error = futils__error_cannot_rmdir( | |
736 | path->ptr, "directory nesting too deep"); | |
219d3457 | 737 | |
14997dc5 | 738 | else if ((error = p_lstat_posixly(path->ptr, &st)) < 0) { |
ad9a921b | 739 | if (errno == ENOENT) |
14997dc5 | 740 | error = 0; |
ad9a921b RB |
741 | else if (errno == ENOTDIR) { |
742 | /* asked to remove a/b/c/d/e and a/b is a normal file */ | |
743 | if ((data->flags & GIT_RMDIR_REMOVE_BLOCKERS) != 0) | |
14997dc5 | 744 | error = futils__rm_first_parent(path, data->base); |
ad9a921b RB |
745 | else |
746 | futils__error_cannot_rmdir( | |
747 | path->ptr, "parent is not directory"); | |
748 | } | |
749 | else | |
e579e0f7 | 750 | error = git_fs_path_set_error(errno, path->ptr, "rmdir"); |
ad9a921b RB |
751 | } |
752 | ||
753 | else if (S_ISDIR(st.st_mode)) { | |
219d3457 RB |
754 | data->depth++; |
755 | ||
e579e0f7 | 756 | error = git_fs_path_direach(path, 0, futils__rmdir_recurs_foreach, data); |
219d3457 RB |
757 | |
758 | data->depth--; | |
759 | ||
96869a4e | 760 | if (error < 0) |
25e0b157 | 761 | return error; |
96869a4e | 762 | |
219d3457 | 763 | if (data->depth == 0 && (data->flags & GIT_RMDIR_SKIP_ROOT) != 0) |
25e0b157 | 764 | return error; |
331e7de9 | 765 | |
14997dc5 | 766 | if ((error = p_rmdir(path->ptr)) < 0) { |
331e7de9 | 767 | if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) != 0 && |
e09d18ee | 768 | (errno == ENOTEMPTY || errno == EEXIST || errno == EBUSY)) |
14997dc5 | 769 | error = 0; |
331e7de9 | 770 | else |
e579e0f7 | 771 | error = git_fs_path_set_error(errno, path->ptr, "rmdir"); |
deafee7b | 772 | } |
858dba58 VM |
773 | } |
774 | ||
331e7de9 | 775 | else if ((data->flags & GIT_RMDIR_REMOVE_FILES) != 0) { |
14997dc5 | 776 | if (p_unlink(path->ptr) < 0) |
e579e0f7 | 777 | error = git_fs_path_set_error(errno, path->ptr, "remove"); |
deafee7b RB |
778 | } |
779 | ||
ad9a921b | 780 | else if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) == 0) |
14997dc5 | 781 | error = futils__error_cannot_rmdir(path->ptr, "still present"); |
555aa453 | 782 | |
25e0b157 | 783 | return error; |
331e7de9 RB |
784 | } |
785 | ||
bbb988a5 | 786 | static int futils__rmdir_empty_parent(void *opaque, const char *path) |
331e7de9 | 787 | { |
7e5c8a5b | 788 | futils__rmdir_data *data = opaque; |
96869a4e | 789 | int error = 0; |
7e5c8a5b | 790 | |
bbb988a5 | 791 | if (strlen(path) <= data->baselen) |
25e0b157 | 792 | error = GIT_ITEROVER; |
331e7de9 | 793 | |
bbb988a5 | 794 | else if (p_rmdir(path) < 0) { |
331e7de9 RB |
795 | int en = errno; |
796 | ||
797 | if (en == ENOENT || en == ENOTDIR) { | |
96869a4e | 798 | /* do nothing */ |
4a0df574 ET |
799 | } else if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) == 0 && |
800 | en == EBUSY) { | |
e579e0f7 | 801 | error = git_fs_path_set_error(errno, path, "rmdir"); |
e09d18ee | 802 | } else if (en == ENOTEMPTY || en == EEXIST || en == EBUSY) { |
331e7de9 RB |
803 | error = GIT_ITEROVER; |
804 | } else { | |
e579e0f7 | 805 | error = git_fs_path_set_error(errno, path, "rmdir"); |
331e7de9 RB |
806 | } |
807 | } | |
808 | ||
25e0b157 | 809 | return error; |
42b3a460 MS |
810 | } |
811 | ||
0d64bef9 | 812 | int git_futils_rmdir_r( |
331e7de9 | 813 | const char *path, const char *base, uint32_t flags) |
42b3a460 | 814 | { |
97769280 | 815 | int error; |
e579e0f7 | 816 | git_str fullpath = GIT_STR_INIT; |
96869a4e | 817 | futils__rmdir_data data; |
0d64bef9 RB |
818 | |
819 | /* build path and find "root" where we should start calling mkdir */ | |
e579e0f7 | 820 | if (git_fs_path_join_unrooted(&fullpath, path, base, NULL) < 0) |
0d64bef9 RB |
821 | return -1; |
822 | ||
96869a4e | 823 | memset(&data, 0, sizeof(data)); |
7e5c8a5b RB |
824 | data.base = base ? base : ""; |
825 | data.baselen = base ? strlen(base) : 0; | |
826 | data.flags = flags; | |
331e7de9 RB |
827 | |
828 | error = futils__rmdir_recurs_foreach(&data, &fullpath); | |
829 | ||
830 | /* remove now-empty parents if requested */ | |
96869a4e | 831 | if (!error && (flags & GIT_RMDIR_EMPTY_PARENTS) != 0) |
e579e0f7 | 832 | error = git_fs_path_walk_up( |
331e7de9 RB |
833 | &fullpath, base, futils__rmdir_empty_parent, &data); |
834 | ||
25e0b157 | 835 | if (error == GIT_ITEROVER) { |
ac3d33df | 836 | git_error_clear(); |
96869a4e | 837 | error = 0; |
25e0b157 | 838 | } |
0d64bef9 | 839 | |
e579e0f7 | 840 | git_str_dispose(&fullpath); |
97769280 | 841 | |
97769280 | 842 | return error; |
42b3a460 MS |
843 | } |
844 | ||
22a2d3d5 | 845 | int git_futils_fake_symlink(const char *target, const char *path) |
8651c10f BS |
846 | { |
847 | int retcode = GIT_ERROR; | |
22a2d3d5 | 848 | int fd = git_futils_creat_withpath(path, 0755, 0644); |
8651c10f | 849 | if (fd >= 0) { |
22a2d3d5 | 850 | retcode = p_write(fd, target, strlen(target)); |
8651c10f BS |
851 | p_close(fd); |
852 | } | |
853 | return retcode; | |
854 | } | |
ca1b6e54 | 855 | |
85bd1746 | 856 | static int cp_by_fd(int ifd, int ofd, bool close_fd_when_done) |
ca1b6e54 RB |
857 | { |
858 | int error = 0; | |
7dd22538 | 859 | char buffer[FILEIO_BUFSIZE]; |
ca1b6e54 RB |
860 | ssize_t len = 0; |
861 | ||
862 | while (!error && (len = p_read(ifd, buffer, sizeof(buffer))) > 0) | |
863 | /* p_write() does not have the same semantics as write(). It loops | |
864 | * internally and will return 0 when it has completed writing. | |
865 | */ | |
866 | error = p_write(ofd, buffer, len); | |
867 | ||
868 | if (len < 0) { | |
ac3d33df | 869 | git_error_set(GIT_ERROR_OS, "read error while copying file"); |
ca1b6e54 RB |
870 | error = (int)len; |
871 | } | |
872 | ||
edef91ee | 873 | if (error < 0) |
ac3d33df | 874 | git_error_set(GIT_ERROR_OS, "write error while copying file"); |
edef91ee | 875 | |
85bd1746 | 876 | if (close_fd_when_done) { |
ca1b6e54 RB |
877 | p_close(ifd); |
878 | p_close(ofd); | |
879 | } | |
880 | ||
881 | return error; | |
882 | } | |
883 | ||
85bd1746 | 884 | int git_futils_cp(const char *from, const char *to, mode_t filemode) |
ca1b6e54 RB |
885 | { |
886 | int ifd, ofd; | |
887 | ||
ca1b6e54 RB |
888 | if ((ifd = git_futils_open_ro(from)) < 0) |
889 | return ifd; | |
890 | ||
891 | if ((ofd = p_open(to, O_WRONLY | O_CREAT | O_EXCL, filemode)) < 0) { | |
ca1b6e54 | 892 | p_close(ifd); |
e579e0f7 | 893 | return git_fs_path_set_error(errno, to, "open for writing"); |
ca1b6e54 RB |
894 | } |
895 | ||
85bd1746 | 896 | return cp_by_fd(ifd, ofd, true); |
ca1b6e54 RB |
897 | } |
898 | ||
27051d4e | 899 | int git_futils_touch(const char *path, time_t *when) |
8f09a98e ET |
900 | { |
901 | struct p_timeval times[2]; | |
8f09a98e ET |
902 | int ret; |
903 | ||
27051d4e ET |
904 | times[0].tv_sec = times[1].tv_sec = when ? *when : time(NULL); |
905 | times[0].tv_usec = times[1].tv_usec = 0; | |
8f09a98e ET |
906 | |
907 | ret = p_utimes(path, times); | |
908 | ||
e579e0f7 | 909 | return (ret < 0) ? git_fs_path_set_error(errno, path, "touch") : 0; |
8f09a98e ET |
910 | } |
911 | ||
85bd1746 | 912 | static int cp_link(const char *from, const char *to, size_t link_size) |
ca1b6e54 RB |
913 | { |
914 | int error = 0; | |
915 | ssize_t read_len; | |
392702ee | 916 | char *link_data; |
f1453c59 | 917 | size_t alloc_size; |
392702ee | 918 | |
ac3d33df | 919 | GIT_ERROR_CHECK_ALLOC_ADD(&alloc_size, link_size, 1); |
f1453c59 | 920 | link_data = git__malloc(alloc_size); |
ac3d33df | 921 | GIT_ERROR_CHECK_ALLOC(link_data); |
ca1b6e54 | 922 | |
85bd1746 RB |
923 | read_len = p_readlink(from, link_data, link_size); |
924 | if (read_len != (ssize_t)link_size) { | |
ac3d33df | 925 | git_error_set(GIT_ERROR_OS, "failed to read symlink data for '%s'", from); |
ca1b6e54 RB |
926 | error = -1; |
927 | } | |
928 | else { | |
929 | link_data[read_len] = '\0'; | |
930 | ||
931 | if (p_symlink(link_data, to) < 0) { | |
ac3d33df | 932 | git_error_set(GIT_ERROR_OS, "could not symlink '%s' as '%s'", |
ca1b6e54 RB |
933 | link_data, to); |
934 | error = -1; | |
935 | } | |
936 | } | |
937 | ||
938 | git__free(link_data); | |
939 | return error; | |
940 | } | |
941 | ||
942 | typedef struct { | |
943 | const char *to_root; | |
e579e0f7 | 944 | git_str to; |
ca1b6e54 RB |
945 | ssize_t from_prefix; |
946 | uint32_t flags; | |
947 | uint32_t mkdir_flags; | |
948 | mode_t dirmode; | |
949 | } cp_r_info; | |
950 | ||
3c42e4ef RB |
951 | #define GIT_CPDIR__MKDIR_DONE_FOR_TO_ROOT (1u << 10) |
952 | ||
e579e0f7 | 953 | static int _cp_r_mkdir(cp_r_info *info, git_str *from) |
3c42e4ef RB |
954 | { |
955 | int error = 0; | |
956 | ||
957 | /* create root directory the first time we need to create a directory */ | |
958 | if ((info->flags & GIT_CPDIR__MKDIR_DONE_FOR_TO_ROOT) == 0) { | |
959 | error = git_futils_mkdir( | |
ac2fba0e | 960 | info->to_root, info->dirmode, |
18f08264 | 961 | (info->flags & GIT_CPDIR_CHMOD_DIRS) ? GIT_MKDIR_CHMOD : 0); |
3c42e4ef RB |
962 | |
963 | info->flags |= GIT_CPDIR__MKDIR_DONE_FOR_TO_ROOT; | |
964 | } | |
965 | ||
966 | /* create directory with root as base to prevent excess chmods */ | |
967 | if (!error) | |
ac2fba0e | 968 | error = git_futils_mkdir_relative( |
3c42e4ef | 969 | from->ptr + info->from_prefix, info->to_root, |
ac2fba0e | 970 | info->dirmode, info->mkdir_flags, NULL); |
3c42e4ef RB |
971 | |
972 | return error; | |
973 | } | |
974 | ||
e579e0f7 | 975 | static int _cp_r_callback(void *ref, git_str *from) |
ca1b6e54 | 976 | { |
3c42e4ef | 977 | int error = 0; |
ca1b6e54 RB |
978 | cp_r_info *info = ref; |
979 | struct stat from_st, to_st; | |
980 | bool exists = false; | |
981 | ||
982 | if ((info->flags & GIT_CPDIR_COPY_DOTFILES) == 0 && | |
e579e0f7 | 983 | from->ptr[git_fs_path_basename_offset(from)] == '.') |
ca1b6e54 RB |
984 | return 0; |
985 | ||
e579e0f7 | 986 | if ((error = git_str_joinpath( |
96869a4e | 987 | &info->to, info->to_root, from->ptr + info->from_prefix)) < 0) |
25e0b157 | 988 | return error; |
ca1b6e54 | 989 | |
e579e0f7 | 990 | if (!(error = git_fs_path_lstat(info->to.ptr, &to_st))) |
ca1b6e54 | 991 | exists = true; |
14997dc5 | 992 | else if (error != GIT_ENOTFOUND) |
25e0b157 | 993 | return error; |
14997dc5 | 994 | else { |
ac3d33df | 995 | git_error_clear(); |
14997dc5 RB |
996 | error = 0; |
997 | } | |
ca1b6e54 | 998 | |
e579e0f7 | 999 | if ((error = git_fs_path_lstat(from->ptr, &from_st)) < 0) |
25e0b157 | 1000 | return error; |
ca1b6e54 RB |
1001 | |
1002 | if (S_ISDIR(from_st.st_mode)) { | |
ca1b6e54 RB |
1003 | mode_t oldmode = info->dirmode; |
1004 | ||
1005 | /* if we are not chmod'ing, then overwrite dirmode */ | |
18f08264 | 1006 | if ((info->flags & GIT_CPDIR_CHMOD_DIRS) == 0) |
ca1b6e54 RB |
1007 | info->dirmode = from_st.st_mode; |
1008 | ||
1009 | /* make directory now if CREATE_EMPTY_DIRS is requested and needed */ | |
1010 | if (!exists && (info->flags & GIT_CPDIR_CREATE_EMPTY_DIRS) != 0) | |
3c42e4ef | 1011 | error = _cp_r_mkdir(info, from); |
ca1b6e54 RB |
1012 | |
1013 | /* recurse onto target directory */ | |
25e0b157 | 1014 | if (!error && (!exists || S_ISDIR(to_st.st_mode))) |
e579e0f7 | 1015 | error = git_fs_path_direach(from, 0, _cp_r_callback, info); |
ca1b6e54 RB |
1016 | |
1017 | if (oldmode != 0) | |
1018 | info->dirmode = oldmode; | |
1019 | ||
25e0b157 | 1020 | return error; |
ca1b6e54 RB |
1021 | } |
1022 | ||
1023 | if (exists) { | |
1024 | if ((info->flags & GIT_CPDIR_OVERWRITE) == 0) | |
1025 | return 0; | |
1026 | ||
1027 | if (p_unlink(info->to.ptr) < 0) { | |
ac3d33df | 1028 | git_error_set(GIT_ERROR_OS, "cannot overwrite existing file '%s'", |
ca1b6e54 | 1029 | info->to.ptr); |
25e0b157 | 1030 | return GIT_EEXISTS; |
ca1b6e54 RB |
1031 | } |
1032 | } | |
1033 | ||
1034 | /* Done if this isn't a regular file or a symlink */ | |
1035 | if (!S_ISREG(from_st.st_mode) && | |
1036 | (!S_ISLNK(from_st.st_mode) || | |
1037 | (info->flags & GIT_CPDIR_COPY_SYMLINKS) == 0)) | |
1038 | return 0; | |
1039 | ||
1040 | /* Make container directory on demand if needed */ | |
1041 | if ((info->flags & GIT_CPDIR_CREATE_EMPTY_DIRS) == 0 && | |
3c42e4ef | 1042 | (error = _cp_r_mkdir(info, from)) < 0) |
25e0b157 | 1043 | return error; |
ca1b6e54 RB |
1044 | |
1045 | /* make symlink or regular file */ | |
94f742ba | 1046 | if (info->flags & GIT_CPDIR_LINK_FILES) { |
54738368 | 1047 | if ((error = p_link(from->ptr, info->to.ptr)) < 0) |
ac3d33df | 1048 | git_error_set(GIT_ERROR_OS, "failed to link '%s'", from->ptr); |
94f742ba | 1049 | } else if (S_ISLNK(from_st.st_mode)) { |
3c42e4ef | 1050 | error = cp_link(from->ptr, info->to.ptr, (size_t)from_st.st_size); |
94f742ba | 1051 | } else { |
3c42e4ef RB |
1052 | mode_t usemode = from_st.st_mode; |
1053 | ||
18f08264 | 1054 | if ((info->flags & GIT_CPDIR_SIMPLE_TO_MODE) != 0) |
f240acce | 1055 | usemode = GIT_PERMS_FOR_WRITE(usemode); |
3c42e4ef RB |
1056 | |
1057 | error = git_futils_cp(from->ptr, info->to.ptr, usemode); | |
3c42e4ef RB |
1058 | } |
1059 | ||
25e0b157 | 1060 | return error; |
ca1b6e54 RB |
1061 | } |
1062 | ||
1063 | int git_futils_cp_r( | |
1064 | const char *from, | |
1065 | const char *to, | |
1066 | uint32_t flags, | |
1067 | mode_t dirmode) | |
1068 | { | |
1069 | int error; | |
e579e0f7 | 1070 | git_str path = GIT_STR_INIT; |
ca1b6e54 RB |
1071 | cp_r_info info; |
1072 | ||
e579e0f7 | 1073 | if (git_str_joinpath(&path, from, "") < 0) /* ensure trailing slash */ |
ca1b6e54 RB |
1074 | return -1; |
1075 | ||
96869a4e | 1076 | memset(&info, 0, sizeof(info)); |
ca1b6e54 RB |
1077 | info.to_root = to; |
1078 | info.flags = flags; | |
1079 | info.dirmode = dirmode; | |
1080 | info.from_prefix = path.size; | |
e579e0f7 | 1081 | git_str_init(&info.to, 0); |
ca1b6e54 RB |
1082 | |
1083 | /* precalculate mkdir flags */ | |
1084 | if ((flags & GIT_CPDIR_CREATE_EMPTY_DIRS) == 0) { | |
3c42e4ef RB |
1085 | /* if not creating empty dirs, then use mkdir to create the path on |
1086 | * demand right before files are copied. | |
1087 | */ | |
ca1b6e54 | 1088 | info.mkdir_flags = GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST; |
18f08264 | 1089 | if ((flags & GIT_CPDIR_CHMOD_DIRS) != 0) |
ca1b6e54 RB |
1090 | info.mkdir_flags |= GIT_MKDIR_CHMOD_PATH; |
1091 | } else { | |
3c42e4ef | 1092 | /* otherwise, we will do simple mkdir as directories are encountered */ |
ca1b6e54 | 1093 | info.mkdir_flags = |
18f08264 | 1094 | ((flags & GIT_CPDIR_CHMOD_DIRS) != 0) ? GIT_MKDIR_CHMOD : 0; |
ca1b6e54 RB |
1095 | } |
1096 | ||
1097 | error = _cp_r_callback(&info, &path); | |
1098 | ||
e579e0f7 MB |
1099 | git_str_dispose(&path); |
1100 | git_str_dispose(&info.to); | |
ca1b6e54 RB |
1101 | |
1102 | return error; | |
1103 | } | |
744cc03e | 1104 | |
c1f61af6 VM |
1105 | int git_futils_filestamp_check( |
1106 | git_futils_filestamp *stamp, const char *path) | |
744cc03e RB |
1107 | { |
1108 | struct stat st; | |
1109 | ||
c8b511f3 RB |
1110 | /* if the stamp is NULL, then always reload */ |
1111 | if (stamp == NULL) | |
744cc03e RB |
1112 | return 1; |
1113 | ||
7d490872 | 1114 | if (p_stat(path, &st) < 0) |
744cc03e RB |
1115 | return GIT_ENOTFOUND; |
1116 | ||
3d6a42d1 | 1117 | if (stamp->mtime.tv_sec == st.st_mtime && |
0226f7dd | 1118 | #if defined(GIT_USE_NSEC) |
3d6a42d1 | 1119 | stamp->mtime.tv_nsec == st.st_mtime_nsec && |
0226f7dd | 1120 | #endif |
22a2d3d5 | 1121 | stamp->size == (uint64_t)st.st_size && |
c8b511f3 | 1122 | stamp->ino == (unsigned int)st.st_ino) |
744cc03e RB |
1123 | return 0; |
1124 | ||
3d6a42d1 | 1125 | stamp->mtime.tv_sec = st.st_mtime; |
0226f7dd | 1126 | #if defined(GIT_USE_NSEC) |
3d6a42d1 | 1127 | stamp->mtime.tv_nsec = st.st_mtime_nsec; |
0226f7dd | 1128 | #endif |
22a2d3d5 | 1129 | stamp->size = (uint64_t)st.st_size; |
c8b511f3 | 1130 | stamp->ino = (unsigned int)st.st_ino; |
744cc03e RB |
1131 | |
1132 | return 1; | |
1133 | } | |
1134 | ||
c1f61af6 VM |
1135 | void git_futils_filestamp_set( |
1136 | git_futils_filestamp *target, const git_futils_filestamp *source) | |
c8b511f3 | 1137 | { |
c8b511f3 RB |
1138 | if (source) |
1139 | memcpy(target, source, sizeof(*target)); | |
1140 | else | |
1141 | memset(target, 0, sizeof(*target)); | |
1142 | } | |
823c0e9c RB |
1143 | |
1144 | ||
1145 | void git_futils_filestamp_set_from_stat( | |
1146 | git_futils_filestamp *stamp, struct stat *st) | |
1147 | { | |
1148 | if (st) { | |
3d6a42d1 ET |
1149 | stamp->mtime.tv_sec = st->st_mtime; |
1150 | #if defined(GIT_USE_NSEC) | |
1151 | stamp->mtime.tv_nsec = st->st_mtime_nsec; | |
1152 | #else | |
973a09a4 AR |
1153 | stamp->mtime.tv_nsec = 0; |
1154 | #endif | |
22a2d3d5 | 1155 | stamp->size = (uint64_t)st->st_size; |
823c0e9c RB |
1156 | stamp->ino = (unsigned int)st->st_ino; |
1157 | } else { | |
1158 | memset(stamp, 0, sizeof(*stamp)); | |
1159 | } | |
1160 | } | |
1229e1c4 ET |
1161 | |
1162 | int git_futils_fsync_dir(const char *path) | |
1163 | { | |
3ac05d11 ET |
1164 | #ifdef GIT_WIN32 |
1165 | GIT_UNUSED(path); | |
1166 | return 0; | |
1167 | #else | |
1229e1c4 ET |
1168 | int fd, error = -1; |
1169 | ||
1170 | if ((fd = p_open(path, O_RDONLY)) < 0) { | |
ac3d33df | 1171 | git_error_set(GIT_ERROR_OS, "failed to open directory '%s' for fsync", path); |
1229e1c4 ET |
1172 | return -1; |
1173 | } | |
1174 | ||
1175 | if ((error = p_fsync(fd)) < 0) | |
ac3d33df | 1176 | git_error_set(GIT_ERROR_OS, "failed to fsync directory '%s'", path); |
1229e1c4 ET |
1177 | |
1178 | p_close(fd); | |
1179 | return error; | |
3ac05d11 | 1180 | #endif |
1229e1c4 ET |
1181 | } |
1182 | ||
1183 | int git_futils_fsync_parent(const char *path) | |
1184 | { | |
0c28c72d PS |
1185 | char *parent; |
1186 | int error; | |
1187 | ||
e579e0f7 | 1188 | if ((parent = git_fs_path_dirname(path)) == NULL) |
0c28c72d | 1189 | return -1; |
1229e1c4 | 1190 | |
0c28c72d | 1191 | error = git_futils_fsync_dir(parent); |
1229e1c4 ET |
1192 | git__free(parent); |
1193 | return error; | |
1194 | } |