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