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