]> git.proxmox.com Git - libgit2.git/blob - src/fileops.c
Merge pull request #3303 from libgit2/cmn/index-add-submodule
[libgit2.git] / src / fileops.c
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
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 */
7 #include "common.h"
8 #include "fileops.h"
9 #include "global.h"
10 #include "strmap.h"
11 #include <ctype.h>
12 #if GIT_WIN32
13 #include "win32/findfile.h"
14 #endif
15
16 GIT__USE_STRMAP
17
18 int git_futils_mkpath2file(const char *file_path, const mode_t mode)
19 {
20 return git_futils_mkdir(
21 file_path, NULL, mode,
22 GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST | GIT_MKDIR_VERIFY_DIR);
23 }
24
25 int git_futils_mktmp(git_buf *path_out, const char *filename, mode_t mode)
26 {
27 int fd;
28 mode_t mask;
29
30 p_umask(mask = p_umask(0));
31
32 git_buf_sets(path_out, filename);
33 git_buf_puts(path_out, "_git2_XXXXXX");
34
35 if (git_buf_oom(path_out))
36 return -1;
37
38 if ((fd = p_mkstemp(path_out->ptr)) < 0) {
39 giterr_set(GITERR_OS,
40 "Failed to create temporary file '%s'", path_out->ptr);
41 return -1;
42 }
43
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
50 return fd;
51 }
52
53 int git_futils_creat_withpath(const char *path, const mode_t dirmode, const mode_t mode)
54 {
55 int fd;
56
57 if (git_futils_mkpath2file(path, dirmode) < 0)
58 return -1;
59
60 fd = p_creat(path, mode);
61 if (fd < 0) {
62 giterr_set(GITERR_OS, "Failed to create file '%s'", path);
63 return -1;
64 }
65
66 return fd;
67 }
68
69 int git_futils_creat_locked(const char *path, const mode_t mode)
70 {
71 int fd = p_open(path, O_WRONLY | O_CREAT | O_TRUNC |
72 O_EXCL | O_BINARY | O_CLOEXEC, mode);
73
74 if (fd < 0) {
75 giterr_set(GITERR_OS, "Failed to create locked file '%s'", path);
76 return errno == EEXIST ? GIT_ELOCKED : -1;
77 }
78
79 return fd;
80 }
81
82 int git_futils_creat_locked_withpath(const char *path, const mode_t dirmode, const mode_t mode)
83 {
84 if (git_futils_mkpath2file(path, dirmode) < 0)
85 return -1;
86
87 return git_futils_creat_locked(path, mode);
88 }
89
90 int git_futils_open_ro(const char *path)
91 {
92 int fd = p_open(path, O_RDONLY);
93 if (fd < 0)
94 return git_path_set_error(errno, path, "open");
95 return fd;
96 }
97
98 git_off_t git_futils_filesize(git_file fd)
99 {
100 struct stat sb;
101
102 if (p_fstat(fd, &sb)) {
103 giterr_set(GITERR_OS, "Failed to stat file descriptor");
104 return -1;
105 }
106
107 return sb.st_size;
108 }
109
110 mode_t git_futils_canonical_mode(mode_t raw_mode)
111 {
112 if (S_ISREG(raw_mode))
113 return S_IFREG | GIT_PERMS_CANONICAL(raw_mode);
114 else if (S_ISLNK(raw_mode))
115 return S_IFLNK;
116 else if (S_ISGITLINK(raw_mode))
117 return S_IFGITLINK;
118 else if (S_ISDIR(raw_mode))
119 return S_IFDIR;
120 else
121 return 0;
122 }
123
124 int git_futils_readbuffer_fd(git_buf *buf, git_file fd, size_t len)
125 {
126 ssize_t read_size = 0;
127 size_t alloc_len;
128
129 git_buf_clear(buf);
130
131 if (!git__is_ssizet(len)) {
132 giterr_set(GITERR_INVALID, "Read too large.");
133 return -1;
134 }
135
136 GITERR_CHECK_ALLOC_ADD(&alloc_len, len, 1);
137 if (git_buf_grow(buf, alloc_len) < 0)
138 return -1;
139
140 /* p_read loops internally to read len bytes */
141 read_size = p_read(fd, buf->ptr, len);
142
143 if (read_size != (ssize_t)len) {
144 giterr_set(GITERR_OS, "Failed to read descriptor");
145 git_buf_free(buf);
146 return -1;
147 }
148
149 buf->ptr[read_size] = '\0';
150 buf->size = read_size;
151
152 return 0;
153 }
154
155 int git_futils_readbuffer_updated(
156 git_buf *buf, const char *path, time_t *mtime, size_t *size, int *updated)
157 {
158 git_file fd;
159 struct stat st;
160 bool changed = false;
161
162 assert(buf && path && *path);
163
164 if (updated != NULL)
165 *updated = 0;
166
167 if (p_stat(path, &st) < 0)
168 return git_path_set_error(errno, path, "stat");
169
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)) {
177 giterr_set(GITERR_OS, "Invalid regular file stat for '%s'", path);
178 return -1;
179 }
180
181 /*
182 * If we were given a time and/or a size, we only want to read the file
183 * if it has been modified.
184 */
185 if (size && *size != (size_t)st.st_size)
186 changed = true;
187 if (mtime && *mtime != (time_t)st.st_mtime)
188 changed = true;
189 if (!size && !mtime)
190 changed = true;
191
192 if (!changed) {
193 return 0;
194 }
195
196 if (mtime != NULL)
197 *mtime = st.st_mtime;
198 if (size != NULL)
199 *size = (size_t)st.st_size;
200
201 if ((fd = git_futils_open_ro(path)) < 0)
202 return fd;
203
204 if (git_futils_readbuffer_fd(buf, fd, (size_t)st.st_size) < 0) {
205 p_close(fd);
206 return -1;
207 }
208
209 p_close(fd);
210
211 if (updated != NULL)
212 *updated = 1;
213
214 return 0;
215 }
216
217 int git_futils_readbuffer(git_buf *buf, const char *path)
218 {
219 return git_futils_readbuffer_updated(buf, path, NULL, NULL, NULL);
220 }
221
222 int 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);
240 return error;
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
249 int git_futils_mv_withpath(const char *from, const char *to, const mode_t dirmode)
250 {
251 if (git_futils_mkpath2file(to, dirmode) < 0)
252 return -1;
253
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;
260 }
261
262 int git_futils_mmap_ro(git_map *out, git_file fd, git_off_t begin, size_t len)
263 {
264 return p_mmap(out, len, GIT_PROT_READ, GIT_MAP_SHARED, fd, begin);
265 }
266
267 int git_futils_mmap_ro_file(git_map *out, const char *path)
268 {
269 git_file fd = git_futils_open_ro(path);
270 git_off_t len;
271 int result;
272
273 if (fd < 0)
274 return fd;
275
276 len = git_futils_filesize(fd);
277 if (!git__is_sizet(len)) {
278 giterr_set(GITERR_OS, "File `%s` too large to mmap", path);
279 return -1;
280 }
281
282 result = git_futils_mmap_ro(out, fd, 0, (size_t)len);
283 p_close(fd);
284 return result;
285 }
286
287 void git_futils_mmap_free(git_map *out)
288 {
289 p_munmap(out);
290 }
291
292 GIT_INLINE(int) validate_existing(
293 const char *make_path,
294 struct stat *st,
295 mode_t mode,
296 uint32_t flags,
297 struct git_futils_mkdir_perfdata *perfdata)
298 {
299 if ((S_ISREG(st->st_mode) && (flags & GIT_MKDIR_REMOVE_FILES)) ||
300 (S_ISLNK(st->st_mode) && (flags & GIT_MKDIR_REMOVE_SYMLINKS))) {
301 if (p_unlink(make_path) < 0) {
302 giterr_set(GITERR_OS, "Failed to remove %s '%s'",
303 S_ISLNK(st->st_mode) ? "symlink" : "file", make_path);
304 return GIT_EEXISTS;
305 }
306
307 perfdata->mkdir_calls++;
308
309 if (p_mkdir(make_path, mode) < 0) {
310 giterr_set(GITERR_OS, "Failed to make directory '%s'", make_path);
311 return GIT_EEXISTS;
312 }
313 }
314
315 else if (S_ISLNK(st->st_mode)) {
316 /* Re-stat the target, make sure it's a directory */
317 perfdata->stat_calls++;
318
319 if (p_stat(make_path, st) < 0) {
320 giterr_set(GITERR_OS, "Failed to make directory '%s'", make_path);
321 return GIT_EEXISTS;
322 }
323 }
324
325 else if (!S_ISDIR(st->st_mode)) {
326 giterr_set(GITERR_FILESYSTEM,
327 "Failed to make directory '%s': directory exists", make_path);
328 return GIT_EEXISTS;
329 }
330
331 return 0;
332 }
333
334 int git_futils_mkdir_ext(
335 const char *path,
336 const char *base,
337 mode_t mode,
338 uint32_t flags,
339 struct git_futils_mkdir_options *opts)
340 {
341 int error = -1;
342 git_buf make_path = GIT_BUF_INIT;
343 ssize_t root = 0, min_root_len, root_len;
344 char lastch = '/', *tail;
345 struct stat st;
346
347 /* build path and find "root" where we should start calling mkdir */
348 if (git_path_join_unrooted(&make_path, path, base, &root) < 0)
349 return -1;
350
351 if (make_path.size == 0) {
352 giterr_set(GITERR_OS, "Attempt to create empty path");
353 goto done;
354 }
355
356 /* Trim trailing slashes (except the root) */
357 if ((root_len = git_path_root(make_path.ptr)) < 0)
358 root_len = 0;
359 else
360 root_len++;
361
362 while (make_path.size > (size_t)root_len &&
363 make_path.ptr[make_path.size - 1] == '/')
364 make_path.ptr[--make_path.size] = '\0';
365
366 /* if we are not supposed to made the last element, truncate it */
367 if ((flags & GIT_MKDIR_SKIP_LAST2) != 0) {
368 git_path_dirname_r(&make_path, make_path.ptr);
369 flags |= GIT_MKDIR_SKIP_LAST;
370 }
371 if ((flags & GIT_MKDIR_SKIP_LAST) != 0) {
372 git_path_dirname_r(&make_path, make_path.ptr);
373 }
374
375 /* We were either given the root path (or trimmed it to
376 * the root), we don't have anything to do.
377 */
378 if (make_path.size <= (size_t)root_len) {
379 error = 0;
380 goto done;
381 }
382
383 /* if we are not supposed to make the whole path, reset root */
384 if ((flags & GIT_MKDIR_PATH) == 0)
385 root = git_buf_rfind(&make_path, '/');
386
387 /* advance root past drive name or network mount prefix */
388 min_root_len = git_path_root(make_path.ptr);
389 if (root < min_root_len)
390 root = min_root_len;
391 while (root >= 0 && make_path.ptr[root] == '/')
392 ++root;
393
394 /* clip root to make_path length */
395 if (root > (ssize_t)make_path.size)
396 root = (ssize_t)make_path.size; /* i.e. NUL byte of string */
397 if (root < 0)
398 root = 0;
399
400 /* walk down tail of path making each directory */
401 for (tail = &make_path.ptr[root]; *tail; *tail = lastch) {
402 bool mkdir_attempted = false;
403
404 /* advance tail to include next path component */
405 while (*tail == '/')
406 tail++;
407 while (*tail && *tail != '/')
408 tail++;
409
410 /* truncate path at next component */
411 lastch = *tail;
412 *tail = '\0';
413 st.st_mode = 0;
414
415 if (opts->dir_map && git_strmap_exists(opts->dir_map, make_path.ptr))
416 continue;
417
418 /* See what's going on with this path component */
419 opts->perfdata.stat_calls++;
420
421 retry_lstat:
422 if (p_lstat(make_path.ptr, &st) < 0) {
423 if (mkdir_attempted || errno != ENOENT) {
424 giterr_set(GITERR_OS, "Cannot access component in path '%s'", make_path.ptr);
425 error = -1;
426 goto done;
427 }
428
429 giterr_clear();
430 opts->perfdata.mkdir_calls++;
431 mkdir_attempted = true;
432 if (p_mkdir(make_path.ptr, mode) < 0) {
433 if (errno == EEXIST)
434 goto retry_lstat;
435 giterr_set(GITERR_OS, "Failed to make directory '%s'", make_path.ptr);
436 error = -1;
437 goto done;
438 }
439 } else {
440 /* with exclusive create, existing dir is an error */
441 if ((flags & GIT_MKDIR_EXCL) != 0) {
442 giterr_set(GITERR_FILESYSTEM, "Failed to make directory '%s': directory exists", make_path.ptr);
443 error = GIT_EEXISTS;
444 goto done;
445 }
446
447 if ((error = validate_existing(
448 make_path.ptr, &st, mode, flags, &opts->perfdata)) < 0)
449 goto done;
450 }
451
452 /* chmod if requested and necessary */
453 if (((flags & GIT_MKDIR_CHMOD_PATH) != 0 ||
454 (lastch == '\0' && (flags & GIT_MKDIR_CHMOD) != 0)) &&
455 st.st_mode != mode) {
456
457 opts->perfdata.chmod_calls++;
458
459 if ((error = p_chmod(make_path.ptr, mode)) < 0 &&
460 lastch == '\0') {
461 giterr_set(GITERR_OS, "Failed to set permissions on '%s'",
462 make_path.ptr);
463 goto done;
464 }
465 }
466
467 if (opts->dir_map && opts->pool) {
468 char *cache_path;
469 size_t alloc_size;
470
471 GITERR_CHECK_ALLOC_ADD(&alloc_size, make_path.size, 1);
472 if (!git__is_uint32(alloc_size))
473 return -1;
474 cache_path = git_pool_malloc(opts->pool, (uint32_t)alloc_size);
475 GITERR_CHECK_ALLOC(cache_path);
476
477 memcpy(cache_path, make_path.ptr, make_path.size + 1);
478
479 git_strmap_insert(opts->dir_map, cache_path, cache_path, error);
480 if (error < 0)
481 goto done;
482 }
483 }
484
485 error = 0;
486
487 /* check that full path really is a directory if requested & needed */
488 if ((flags & GIT_MKDIR_VERIFY_DIR) != 0 &&
489 lastch != '\0') {
490 opts->perfdata.stat_calls++;
491
492 if (p_stat(make_path.ptr, &st) < 0 || !S_ISDIR(st.st_mode)) {
493 giterr_set(GITERR_OS, "Path is not a directory '%s'",
494 make_path.ptr);
495 error = GIT_ENOTFOUND;
496 }
497 }
498
499 done:
500 git_buf_free(&make_path);
501 return error;
502 }
503
504 int git_futils_mkdir(
505 const char *path,
506 const char *base,
507 mode_t mode,
508 uint32_t flags)
509 {
510 struct git_futils_mkdir_options options = {0};
511 return git_futils_mkdir_ext(path, base, mode, flags, &options);
512 }
513
514 int git_futils_mkdir_r(const char *path, const char *base, const mode_t mode)
515 {
516 return git_futils_mkdir(path, base, mode, GIT_MKDIR_PATH);
517 }
518
519 typedef struct {
520 const char *base;
521 size_t baselen;
522 uint32_t flags;
523 int depth;
524 } futils__rmdir_data;
525
526 #define FUTILS_MAX_DEPTH 100
527
528 static int futils__error_cannot_rmdir(const char *path, const char *filemsg)
529 {
530 if (filemsg)
531 giterr_set(GITERR_OS, "Could not remove directory. File '%s' %s",
532 path, filemsg);
533 else
534 giterr_set(GITERR_OS, "Could not remove directory '%s'", path);
535
536 return -1;
537 }
538
539 static int futils__rm_first_parent(git_buf *path, const char *ceiling)
540 {
541 int error = GIT_ENOTFOUND;
542 struct stat st;
543
544 while (error == GIT_ENOTFOUND) {
545 git_buf_rtruncate_at_char(path, '/');
546
547 if (!path->size || git__prefixcmp(path->ptr, ceiling) != 0)
548 error = 0;
549 else if (p_lstat_posixly(path->ptr, &st) == 0) {
550 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
551 error = p_unlink(path->ptr);
552 else if (!S_ISDIR(st.st_mode))
553 error = -1; /* fail to remove non-regular file */
554 } else if (errno != ENOTDIR)
555 error = -1;
556 }
557
558 if (error)
559 futils__error_cannot_rmdir(path->ptr, "cannot remove parent");
560
561 return error;
562 }
563
564 static int futils__rmdir_recurs_foreach(void *opaque, git_buf *path)
565 {
566 int error = 0;
567 futils__rmdir_data *data = opaque;
568 struct stat st;
569
570 if (data->depth > FUTILS_MAX_DEPTH)
571 error = futils__error_cannot_rmdir(
572 path->ptr, "directory nesting too deep");
573
574 else if ((error = p_lstat_posixly(path->ptr, &st)) < 0) {
575 if (errno == ENOENT)
576 error = 0;
577 else if (errno == ENOTDIR) {
578 /* asked to remove a/b/c/d/e and a/b is a normal file */
579 if ((data->flags & GIT_RMDIR_REMOVE_BLOCKERS) != 0)
580 error = futils__rm_first_parent(path, data->base);
581 else
582 futils__error_cannot_rmdir(
583 path->ptr, "parent is not directory");
584 }
585 else
586 error = git_path_set_error(errno, path->ptr, "rmdir");
587 }
588
589 else if (S_ISDIR(st.st_mode)) {
590 data->depth++;
591
592 error = git_path_direach(path, 0, futils__rmdir_recurs_foreach, data);
593
594 data->depth--;
595
596 if (error < 0)
597 return error;
598
599 if (data->depth == 0 && (data->flags & GIT_RMDIR_SKIP_ROOT) != 0)
600 return error;
601
602 if ((error = p_rmdir(path->ptr)) < 0) {
603 if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) != 0 &&
604 (errno == ENOTEMPTY || errno == EEXIST || errno == EBUSY))
605 error = 0;
606 else
607 error = git_path_set_error(errno, path->ptr, "rmdir");
608 }
609 }
610
611 else if ((data->flags & GIT_RMDIR_REMOVE_FILES) != 0) {
612 if (p_unlink(path->ptr) < 0)
613 error = git_path_set_error(errno, path->ptr, "remove");
614 }
615
616 else if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) == 0)
617 error = futils__error_cannot_rmdir(path->ptr, "still present");
618
619 return error;
620 }
621
622 static int futils__rmdir_empty_parent(void *opaque, const char *path)
623 {
624 futils__rmdir_data *data = opaque;
625 int error = 0;
626
627 if (strlen(path) <= data->baselen)
628 error = GIT_ITEROVER;
629
630 else if (p_rmdir(path) < 0) {
631 int en = errno;
632
633 if (en == ENOENT || en == ENOTDIR) {
634 /* do nothing */
635 } else if (en == ENOTEMPTY || en == EEXIST || en == EBUSY) {
636 error = GIT_ITEROVER;
637 } else {
638 error = git_path_set_error(errno, path, "rmdir");
639 }
640 }
641
642 return error;
643 }
644
645 int git_futils_rmdir_r(
646 const char *path, const char *base, uint32_t flags)
647 {
648 int error;
649 git_buf fullpath = GIT_BUF_INIT;
650 futils__rmdir_data data;
651
652 /* build path and find "root" where we should start calling mkdir */
653 if (git_path_join_unrooted(&fullpath, path, base, NULL) < 0)
654 return -1;
655
656 memset(&data, 0, sizeof(data));
657 data.base = base ? base : "";
658 data.baselen = base ? strlen(base) : 0;
659 data.flags = flags;
660
661 error = futils__rmdir_recurs_foreach(&data, &fullpath);
662
663 /* remove now-empty parents if requested */
664 if (!error && (flags & GIT_RMDIR_EMPTY_PARENTS) != 0)
665 error = git_path_walk_up(
666 &fullpath, base, futils__rmdir_empty_parent, &data);
667
668 if (error == GIT_ITEROVER) {
669 giterr_clear();
670 error = 0;
671 }
672
673 git_buf_free(&fullpath);
674
675 return error;
676 }
677
678 int git_futils_fake_symlink(const char *old, const char *new)
679 {
680 int retcode = GIT_ERROR;
681 int fd = git_futils_creat_withpath(new, 0755, 0644);
682 if (fd >= 0) {
683 retcode = p_write(fd, old, strlen(old));
684 p_close(fd);
685 }
686 return retcode;
687 }
688
689 static int cp_by_fd(int ifd, int ofd, bool close_fd_when_done)
690 {
691 int error = 0;
692 char buffer[FILEIO_BUFSIZE];
693 ssize_t len = 0;
694
695 while (!error && (len = p_read(ifd, buffer, sizeof(buffer))) > 0)
696 /* p_write() does not have the same semantics as write(). It loops
697 * internally and will return 0 when it has completed writing.
698 */
699 error = p_write(ofd, buffer, len);
700
701 if (len < 0) {
702 giterr_set(GITERR_OS, "Read error while copying file");
703 error = (int)len;
704 }
705
706 if (error < 0)
707 giterr_set(GITERR_OS, "write error while copying file");
708
709 if (close_fd_when_done) {
710 p_close(ifd);
711 p_close(ofd);
712 }
713
714 return error;
715 }
716
717 int git_futils_cp(const char *from, const char *to, mode_t filemode)
718 {
719 int ifd, ofd;
720
721 if ((ifd = git_futils_open_ro(from)) < 0)
722 return ifd;
723
724 if ((ofd = p_open(to, O_WRONLY | O_CREAT | O_EXCL, filemode)) < 0) {
725 p_close(ifd);
726 return git_path_set_error(errno, to, "open for writing");
727 }
728
729 return cp_by_fd(ifd, ofd, true);
730 }
731
732 static int cp_link(const char *from, const char *to, size_t link_size)
733 {
734 int error = 0;
735 ssize_t read_len;
736 char *link_data;
737 size_t alloc_size;
738
739 GITERR_CHECK_ALLOC_ADD(&alloc_size, link_size, 1);
740 link_data = git__malloc(alloc_size);
741 GITERR_CHECK_ALLOC(link_data);
742
743 read_len = p_readlink(from, link_data, link_size);
744 if (read_len != (ssize_t)link_size) {
745 giterr_set(GITERR_OS, "Failed to read symlink data for '%s'", from);
746 error = -1;
747 }
748 else {
749 link_data[read_len] = '\0';
750
751 if (p_symlink(link_data, to) < 0) {
752 giterr_set(GITERR_OS, "Could not symlink '%s' as '%s'",
753 link_data, to);
754 error = -1;
755 }
756 }
757
758 git__free(link_data);
759 return error;
760 }
761
762 typedef struct {
763 const char *to_root;
764 git_buf to;
765 ssize_t from_prefix;
766 uint32_t flags;
767 uint32_t mkdir_flags;
768 mode_t dirmode;
769 } cp_r_info;
770
771 #define GIT_CPDIR__MKDIR_DONE_FOR_TO_ROOT (1u << 10)
772
773 static int _cp_r_mkdir(cp_r_info *info, git_buf *from)
774 {
775 int error = 0;
776
777 /* create root directory the first time we need to create a directory */
778 if ((info->flags & GIT_CPDIR__MKDIR_DONE_FOR_TO_ROOT) == 0) {
779 error = git_futils_mkdir(
780 info->to_root, NULL, info->dirmode,
781 (info->flags & GIT_CPDIR_CHMOD_DIRS) ? GIT_MKDIR_CHMOD : 0);
782
783 info->flags |= GIT_CPDIR__MKDIR_DONE_FOR_TO_ROOT;
784 }
785
786 /* create directory with root as base to prevent excess chmods */
787 if (!error)
788 error = git_futils_mkdir(
789 from->ptr + info->from_prefix, info->to_root,
790 info->dirmode, info->mkdir_flags);
791
792 return error;
793 }
794
795 static int _cp_r_callback(void *ref, git_buf *from)
796 {
797 int error = 0;
798 cp_r_info *info = ref;
799 struct stat from_st, to_st;
800 bool exists = false;
801
802 if ((info->flags & GIT_CPDIR_COPY_DOTFILES) == 0 &&
803 from->ptr[git_path_basename_offset(from)] == '.')
804 return 0;
805
806 if ((error = git_buf_joinpath(
807 &info->to, info->to_root, from->ptr + info->from_prefix)) < 0)
808 return error;
809
810 if (!(error = git_path_lstat(info->to.ptr, &to_st)))
811 exists = true;
812 else if (error != GIT_ENOTFOUND)
813 return error;
814 else {
815 giterr_clear();
816 error = 0;
817 }
818
819 if ((error = git_path_lstat(from->ptr, &from_st)) < 0)
820 return error;
821
822 if (S_ISDIR(from_st.st_mode)) {
823 mode_t oldmode = info->dirmode;
824
825 /* if we are not chmod'ing, then overwrite dirmode */
826 if ((info->flags & GIT_CPDIR_CHMOD_DIRS) == 0)
827 info->dirmode = from_st.st_mode;
828
829 /* make directory now if CREATE_EMPTY_DIRS is requested and needed */
830 if (!exists && (info->flags & GIT_CPDIR_CREATE_EMPTY_DIRS) != 0)
831 error = _cp_r_mkdir(info, from);
832
833 /* recurse onto target directory */
834 if (!error && (!exists || S_ISDIR(to_st.st_mode)))
835 error = git_path_direach(from, 0, _cp_r_callback, info);
836
837 if (oldmode != 0)
838 info->dirmode = oldmode;
839
840 return error;
841 }
842
843 if (exists) {
844 if ((info->flags & GIT_CPDIR_OVERWRITE) == 0)
845 return 0;
846
847 if (p_unlink(info->to.ptr) < 0) {
848 giterr_set(GITERR_OS, "Cannot overwrite existing file '%s'",
849 info->to.ptr);
850 return GIT_EEXISTS;
851 }
852 }
853
854 /* Done if this isn't a regular file or a symlink */
855 if (!S_ISREG(from_st.st_mode) &&
856 (!S_ISLNK(from_st.st_mode) ||
857 (info->flags & GIT_CPDIR_COPY_SYMLINKS) == 0))
858 return 0;
859
860 /* Make container directory on demand if needed */
861 if ((info->flags & GIT_CPDIR_CREATE_EMPTY_DIRS) == 0 &&
862 (error = _cp_r_mkdir(info, from)) < 0)
863 return error;
864
865 /* make symlink or regular file */
866 if (info->flags & GIT_CPDIR_LINK_FILES) {
867 if ((error = p_link(from->ptr, info->to.ptr)) < 0)
868 giterr_set(GITERR_OS, "failed to link '%s'", from->ptr);
869 } else if (S_ISLNK(from_st.st_mode)) {
870 error = cp_link(from->ptr, info->to.ptr, (size_t)from_st.st_size);
871 } else {
872 mode_t usemode = from_st.st_mode;
873
874 if ((info->flags & GIT_CPDIR_SIMPLE_TO_MODE) != 0)
875 usemode = GIT_PERMS_FOR_WRITE(usemode);
876
877 error = git_futils_cp(from->ptr, info->to.ptr, usemode);
878 }
879
880 return error;
881 }
882
883 int git_futils_cp_r(
884 const char *from,
885 const char *to,
886 uint32_t flags,
887 mode_t dirmode)
888 {
889 int error;
890 git_buf path = GIT_BUF_INIT;
891 cp_r_info info;
892
893 if (git_buf_joinpath(&path, from, "") < 0) /* ensure trailing slash */
894 return -1;
895
896 memset(&info, 0, sizeof(info));
897 info.to_root = to;
898 info.flags = flags;
899 info.dirmode = dirmode;
900 info.from_prefix = path.size;
901 git_buf_init(&info.to, 0);
902
903 /* precalculate mkdir flags */
904 if ((flags & GIT_CPDIR_CREATE_EMPTY_DIRS) == 0) {
905 /* if not creating empty dirs, then use mkdir to create the path on
906 * demand right before files are copied.
907 */
908 info.mkdir_flags = GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST;
909 if ((flags & GIT_CPDIR_CHMOD_DIRS) != 0)
910 info.mkdir_flags |= GIT_MKDIR_CHMOD_PATH;
911 } else {
912 /* otherwise, we will do simple mkdir as directories are encountered */
913 info.mkdir_flags =
914 ((flags & GIT_CPDIR_CHMOD_DIRS) != 0) ? GIT_MKDIR_CHMOD : 0;
915 }
916
917 error = _cp_r_callback(&info, &path);
918
919 git_buf_free(&path);
920 git_buf_free(&info.to);
921
922 return error;
923 }
924
925 int git_futils_filestamp_check(
926 git_futils_filestamp *stamp, const char *path)
927 {
928 struct stat st;
929
930 /* if the stamp is NULL, then always reload */
931 if (stamp == NULL)
932 return 1;
933
934 if (p_stat(path, &st) < 0)
935 return GIT_ENOTFOUND;
936
937 if (stamp->mtime == (git_time_t)st.st_mtime &&
938 stamp->size == (git_off_t)st.st_size &&
939 stamp->ino == (unsigned int)st.st_ino)
940 return 0;
941
942 stamp->mtime = (git_time_t)st.st_mtime;
943 stamp->size = (git_off_t)st.st_size;
944 stamp->ino = (unsigned int)st.st_ino;
945
946 return 1;
947 }
948
949 void git_futils_filestamp_set(
950 git_futils_filestamp *target, const git_futils_filestamp *source)
951 {
952 assert(target);
953
954 if (source)
955 memcpy(target, source, sizeof(*target));
956 else
957 memset(target, 0, sizeof(*target));
958 }
959
960
961 void git_futils_filestamp_set_from_stat(
962 git_futils_filestamp *stamp, struct stat *st)
963 {
964 if (st) {
965 stamp->mtime = (git_time_t)st->st_mtime;
966 stamp->size = (git_off_t)st->st_size;
967 stamp->ino = (unsigned int)st->st_ino;
968 } else {
969 memset(stamp, 0, sizeof(*stamp));
970 }
971 }