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