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