]> git.proxmox.com Git - libgit2.git/blob - src/fileops.c
Merge pull request #1359 from martinwoodward/remove-sample-hooks
[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 <ctype.h>
10 #if GIT_WIN32
11 #include "win32/findfile.h"
12 #endif
13
14 int git_futils_mkpath2file(const char *file_path, const mode_t mode)
15 {
16 return git_futils_mkdir(
17 file_path, NULL, mode,
18 GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST | GIT_MKDIR_VERIFY_DIR);
19 }
20
21 int git_futils_mktmp(git_buf *path_out, const char *filename)
22 {
23 int fd;
24
25 git_buf_sets(path_out, filename);
26 git_buf_puts(path_out, "_git2_XXXXXX");
27
28 if (git_buf_oom(path_out))
29 return -1;
30
31 if ((fd = p_mkstemp(path_out->ptr)) < 0) {
32 giterr_set(GITERR_OS,
33 "Failed to create temporary file '%s'", path_out->ptr);
34 return -1;
35 }
36
37 return fd;
38 }
39
40 int git_futils_creat_withpath(const char *path, const mode_t dirmode, const mode_t mode)
41 {
42 int fd;
43
44 if (git_futils_mkpath2file(path, dirmode) < 0)
45 return -1;
46
47 fd = p_creat(path, mode);
48 if (fd < 0) {
49 giterr_set(GITERR_OS, "Failed to create file '%s'", path);
50 return -1;
51 }
52
53 return fd;
54 }
55
56 int git_futils_creat_locked(const char *path, const mode_t mode)
57 {
58 int fd;
59
60 #ifdef GIT_WIN32
61 wchar_t buf[GIT_WIN_PATH];
62
63 git__utf8_to_16(buf, GIT_WIN_PATH, path);
64 fd = _wopen(buf, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_EXCL, mode);
65 #else
66 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_EXCL, mode);
67 #endif
68
69 if (fd < 0) {
70 giterr_set(GITERR_OS, "Failed to create locked file '%s'", path);
71 return -1;
72 }
73
74 return fd;
75 }
76
77 int git_futils_creat_locked_withpath(const char *path, const mode_t dirmode, const mode_t mode)
78 {
79 if (git_futils_mkpath2file(path, dirmode) < 0)
80 return -1;
81
82 return git_futils_creat_locked(path, mode);
83 }
84
85 int git_futils_open_ro(const char *path)
86 {
87 int fd = p_open(path, O_RDONLY);
88 if (fd < 0) {
89 if (errno == ENOENT || errno == ENOTDIR)
90 fd = GIT_ENOTFOUND;
91 giterr_set(GITERR_OS, "Failed to open '%s'", path);
92 }
93 return fd;
94 }
95
96 git_off_t git_futils_filesize(git_file fd)
97 {
98 struct stat sb;
99
100 if (p_fstat(fd, &sb)) {
101 giterr_set(GITERR_OS, "Failed to stat file descriptor");
102 return -1;
103 }
104
105 return sb.st_size;
106 }
107
108 mode_t git_futils_canonical_mode(mode_t raw_mode)
109 {
110 if (S_ISREG(raw_mode))
111 return S_IFREG | GIT_CANONICAL_PERMS(raw_mode);
112 else if (S_ISLNK(raw_mode))
113 return S_IFLNK;
114 else if (S_ISGITLINK(raw_mode))
115 return S_IFGITLINK;
116 else if (S_ISDIR(raw_mode))
117 return S_IFDIR;
118 else
119 return 0;
120 }
121
122 int git_futils_readbuffer_fd(git_buf *buf, git_file fd, size_t len)
123 {
124 ssize_t read_size = 0;
125
126 git_buf_clear(buf);
127
128 if (git_buf_grow(buf, len + 1) < 0)
129 return -1;
130
131 /* p_read loops internally to read len bytes */
132 read_size = p_read(fd, buf->ptr, len);
133
134 if (read_size != (ssize_t)len) {
135 giterr_set(GITERR_OS, "Failed to read descriptor");
136 return -1;
137 }
138
139 buf->ptr[read_size] = '\0';
140 buf->size = read_size;
141
142 return 0;
143 }
144
145 int git_futils_readbuffer_updated(
146 git_buf *buf, const char *path, time_t *mtime, size_t *size, int *updated)
147 {
148 git_file fd;
149 struct stat st;
150 bool changed = false;
151
152 assert(buf && path && *path);
153
154 if (updated != NULL)
155 *updated = 0;
156
157 if ((fd = git_futils_open_ro(path)) < 0)
158 return fd;
159
160 if (p_fstat(fd, &st) < 0 || S_ISDIR(st.st_mode) || !git__is_sizet(st.st_size+1)) {
161 p_close(fd);
162 giterr_set(GITERR_OS, "Invalid regular file stat for '%s'", path);
163 return -1;
164 }
165
166 /*
167 * If we were given a time and/or a size, we only want to read the file
168 * if it has been modified.
169 */
170 if (size && *size != (size_t)st.st_size)
171 changed = true;
172 if (mtime && *mtime != st.st_mtime)
173 changed = true;
174 if (!size && !mtime)
175 changed = true;
176
177 if (!changed) {
178 p_close(fd);
179 return 0;
180 }
181
182 if (mtime != NULL)
183 *mtime = st.st_mtime;
184 if (size != NULL)
185 *size = (size_t)st.st_size;
186
187 if (git_futils_readbuffer_fd(buf, fd, (size_t)st.st_size) < 0) {
188 p_close(fd);
189 return -1;
190 }
191
192 p_close(fd);
193
194 if (updated != NULL)
195 *updated = 1;
196
197 return 0;
198 }
199
200 int git_futils_readbuffer(git_buf *buf, const char *path)
201 {
202 return git_futils_readbuffer_updated(buf, path, NULL, NULL, NULL);
203 }
204
205 int git_futils_mv_withpath(const char *from, const char *to, const mode_t dirmode)
206 {
207 if (git_futils_mkpath2file(to, dirmode) < 0)
208 return -1;
209
210 if (p_rename(from, to) < 0) {
211 giterr_set(GITERR_OS, "Failed to rename '%s' to '%s'", from, to);
212 return -1;
213 }
214
215 return 0;
216 }
217
218 int git_futils_mmap_ro(git_map *out, git_file fd, git_off_t begin, size_t len)
219 {
220 return p_mmap(out, len, GIT_PROT_READ, GIT_MAP_SHARED, fd, begin);
221 }
222
223 int git_futils_mmap_ro_file(git_map *out, const char *path)
224 {
225 git_file fd = git_futils_open_ro(path);
226 git_off_t len;
227 int result;
228
229 if (fd < 0)
230 return fd;
231
232 len = git_futils_filesize(fd);
233 if (!git__is_sizet(len)) {
234 giterr_set(GITERR_OS, "File `%s` too large to mmap", path);
235 return -1;
236 }
237
238 result = git_futils_mmap_ro(out, fd, 0, (size_t)len);
239 p_close(fd);
240 return result;
241 }
242
243 void git_futils_mmap_free(git_map *out)
244 {
245 p_munmap(out);
246 }
247
248 int git_futils_mkdir(
249 const char *path,
250 const char *base,
251 mode_t mode,
252 uint32_t flags)
253 {
254 int error = -1;
255 git_buf make_path = GIT_BUF_INIT;
256 ssize_t root = 0;
257 char lastch, *tail;
258
259 /* build path and find "root" where we should start calling mkdir */
260 if (git_path_join_unrooted(&make_path, path, base, &root) < 0)
261 return -1;
262
263 if (make_path.size == 0) {
264 giterr_set(GITERR_OS, "Attempt to create empty path");
265 goto fail;
266 }
267
268 /* remove trailing slashes on path */
269 while (make_path.ptr[make_path.size - 1] == '/') {
270 make_path.size--;
271 make_path.ptr[make_path.size] = '\0';
272 }
273
274 /* if we are not supposed to made the last element, truncate it */
275 if ((flags & GIT_MKDIR_SKIP_LAST) != 0)
276 git_buf_rtruncate_at_char(&make_path, '/');
277
278 /* if we are not supposed to make the whole path, reset root */
279 if ((flags & GIT_MKDIR_PATH) == 0)
280 root = git_buf_rfind(&make_path, '/');
281
282 /* clip root to make_path length */
283 if (root >= (ssize_t)make_path.size)
284 root = (ssize_t)make_path.size - 1;
285 if (root < 0)
286 root = 0;
287
288 tail = & make_path.ptr[root];
289
290 while (*tail) {
291 /* advance tail to include next path component */
292 while (*tail == '/')
293 tail++;
294 while (*tail && *tail != '/')
295 tail++;
296
297 /* truncate path at next component */
298 lastch = *tail;
299 *tail = '\0';
300
301 /* make directory */
302 if (p_mkdir(make_path.ptr, mode) < 0) {
303 int already_exists = 0;
304
305 switch (errno) {
306 case EEXIST:
307 if (!lastch && (flags & GIT_MKDIR_VERIFY_DIR) != 0 &&
308 !git_path_isdir(make_path.ptr)) {
309 giterr_set(
310 GITERR_OS, "Existing path is not a directory '%s'",
311 make_path.ptr);
312 error = GIT_ENOTFOUND;
313 goto fail;
314 }
315
316 already_exists = 1;
317 break;
318 case ENOSYS:
319 /* Solaris can generate this error if you try to mkdir
320 * a path which is already a mount point. In that case,
321 * the path does already exist; but it's not implied by
322 * the definition of the error, so let's recheck */
323 if (git_path_isdir(make_path.ptr)) {
324 already_exists = 1;
325 break;
326 }
327
328 /* Fall through */
329 errno = ENOSYS;
330 default:
331 giterr_set(GITERR_OS, "Failed to make directory '%s'",
332 make_path.ptr);
333 goto fail;
334 }
335
336 if (already_exists && (flags & GIT_MKDIR_EXCL) != 0) {
337 giterr_set(GITERR_OS, "Directory already exists '%s'",
338 make_path.ptr);
339 error = GIT_EEXISTS;
340 goto fail;
341 }
342 }
343
344 /* chmod if requested */
345 if ((flags & GIT_MKDIR_CHMOD_PATH) != 0 ||
346 ((flags & GIT_MKDIR_CHMOD) != 0 && lastch == '\0'))
347 {
348 if (p_chmod(make_path.ptr, mode) < 0) {
349 giterr_set(GITERR_OS, "Failed to set permissions on '%s'",
350 make_path.ptr);
351 goto fail;
352 }
353 }
354
355 *tail = lastch;
356 }
357
358 git_buf_free(&make_path);
359 return 0;
360
361 fail:
362 git_buf_free(&make_path);
363 return error;
364 }
365
366 int git_futils_mkdir_r(const char *path, const char *base, const mode_t mode)
367 {
368 return git_futils_mkdir(path, base, mode, GIT_MKDIR_PATH);
369 }
370
371 typedef struct {
372 const char *base;
373 size_t baselen;
374 uint32_t flags;
375 int error;
376 } futils__rmdir_data;
377
378 static int futils__error_cannot_rmdir(const char *path, const char *filemsg)
379 {
380 if (filemsg)
381 giterr_set(GITERR_OS, "Could not remove directory. File '%s' %s",
382 path, filemsg);
383 else
384 giterr_set(GITERR_OS, "Could not remove directory '%s'", path);
385
386 return -1;
387 }
388
389 static int futils__rm_first_parent(git_buf *path, const char *ceiling)
390 {
391 int error = GIT_ENOTFOUND;
392 struct stat st;
393
394 while (error == GIT_ENOTFOUND) {
395 git_buf_rtruncate_at_char(path, '/');
396
397 if (!path->size || git__prefixcmp(path->ptr, ceiling) != 0)
398 error = 0;
399 else if (p_lstat_posixly(path->ptr, &st) == 0) {
400 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
401 error = p_unlink(path->ptr);
402 else if (!S_ISDIR(st.st_mode))
403 error = -1; /* fail to remove non-regular file */
404 } else if (errno != ENOTDIR)
405 error = -1;
406 }
407
408 if (error)
409 futils__error_cannot_rmdir(path->ptr, "cannot remove parent");
410
411 return error;
412 }
413
414 static int futils__rmdir_recurs_foreach(void *opaque, git_buf *path)
415 {
416 struct stat st;
417 futils__rmdir_data *data = opaque;
418
419 if ((data->error = p_lstat_posixly(path->ptr, &st)) < 0) {
420 if (errno == ENOENT)
421 data->error = 0;
422 else if (errno == ENOTDIR) {
423 /* asked to remove a/b/c/d/e and a/b is a normal file */
424 if ((data->flags & GIT_RMDIR_REMOVE_BLOCKERS) != 0)
425 data->error = futils__rm_first_parent(path, data->base);
426 else
427 futils__error_cannot_rmdir(
428 path->ptr, "parent is not directory");
429 }
430 else
431 futils__error_cannot_rmdir(path->ptr, "cannot access");
432 }
433
434 else if (S_ISDIR(st.st_mode)) {
435 int error = git_path_direach(path, futils__rmdir_recurs_foreach, data);
436 if (error < 0)
437 return (error == GIT_EUSER) ? data->error : error;
438
439 data->error = p_rmdir(path->ptr);
440
441 if (data->error < 0) {
442 if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) != 0 &&
443 (errno == ENOTEMPTY || errno == EEXIST))
444 data->error = 0;
445 else
446 futils__error_cannot_rmdir(path->ptr, NULL);
447 }
448 }
449
450 else if ((data->flags & GIT_RMDIR_REMOVE_FILES) != 0) {
451 data->error = p_unlink(path->ptr);
452
453 if (data->error < 0)
454 futils__error_cannot_rmdir(path->ptr, "cannot be removed");
455 }
456
457 else if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) == 0)
458 data->error = futils__error_cannot_rmdir(path->ptr, "still present");
459
460 return data->error;
461 }
462
463 static int futils__rmdir_empty_parent(void *opaque, git_buf *path)
464 {
465 futils__rmdir_data *data = opaque;
466 int error;
467
468 if (git_buf_len(path) <= data->baselen)
469 return GIT_ITEROVER;
470
471 error = p_rmdir(git_buf_cstr(path));
472
473 if (error) {
474 int en = errno;
475
476 if (en == ENOENT || en == ENOTDIR) {
477 giterr_clear();
478 error = 0;
479 } else if (en == ENOTEMPTY || en == EEXIST) {
480 giterr_clear();
481 error = GIT_ITEROVER;
482 } else {
483 futils__error_cannot_rmdir(git_buf_cstr(path), NULL);
484 }
485 }
486
487 return error;
488 }
489
490 int git_futils_rmdir_r(
491 const char *path, const char *base, uint32_t flags)
492 {
493 int error;
494 git_buf fullpath = GIT_BUF_INIT;
495 futils__rmdir_data data;
496
497 /* build path and find "root" where we should start calling mkdir */
498 if (git_path_join_unrooted(&fullpath, path, base, NULL) < 0)
499 return -1;
500
501 data.base = base ? base : "";
502 data.baselen = base ? strlen(base) : 0;
503 data.flags = flags;
504 data.error = 0;
505
506 error = futils__rmdir_recurs_foreach(&data, &fullpath);
507
508 /* remove now-empty parents if requested */
509 if (!error && (flags & GIT_RMDIR_EMPTY_PARENTS) != 0) {
510 error = git_path_walk_up(
511 &fullpath, base, futils__rmdir_empty_parent, &data);
512
513 if (error == GIT_ITEROVER)
514 error = 0;
515 }
516
517 git_buf_free(&fullpath);
518
519 return error;
520 }
521
522 int git_futils_find_system_file(git_buf *path, const char *filename)
523 {
524 #ifdef GIT_WIN32
525 // try to find git.exe/git.cmd on path
526 if (!win32_find_system_file_using_path(path, filename))
527 return 0;
528
529 // try to find msysgit installation path using registry
530 if (!win32_find_system_file_using_registry(path, filename))
531 return 0;
532 #else
533 if (git_buf_joinpath(path, "/etc", filename) < 0)
534 return -1;
535
536 if (git_path_exists(path->ptr) == true)
537 return 0;
538 #endif
539
540 git_buf_clear(path);
541 giterr_set(GITERR_OS, "The system file '%s' doesn't exist", filename);
542 return GIT_ENOTFOUND;
543 }
544
545 int git_futils_find_global_file(git_buf *path, const char *filename)
546 {
547 #ifdef GIT_WIN32
548 struct win32_path root;
549 static const wchar_t *tmpls[4] = {
550 L"%HOME%\\",
551 L"%HOMEDRIVE%%HOMEPATH%\\",
552 L"%USERPROFILE%\\",
553 NULL,
554 };
555 const wchar_t **tmpl;
556
557 for (tmpl = tmpls; *tmpl != NULL; tmpl++) {
558 /* try to expand environment variable, skipping if not set */
559 if (win32_expand_path(&root, *tmpl) != 0 || root.path[0] == L'%')
560 continue;
561
562 /* try to look up file under path */
563 if (!win32_find_file(path, &root, filename))
564 return 0;
565 }
566
567 giterr_set(GITERR_OS, "The global file '%s' doesn't exist", filename);
568 git_buf_clear(path);
569
570 return GIT_ENOTFOUND;
571 #else
572 const char *home = getenv("HOME");
573
574 if (home == NULL) {
575 giterr_set(GITERR_OS, "Global file lookup failed. "
576 "Cannot locate the user's home directory");
577 return GIT_ENOTFOUND;
578 }
579
580 if (git_buf_joinpath(path, home, filename) < 0)
581 return -1;
582
583 if (git_path_exists(path->ptr) == false) {
584 giterr_set(GITERR_OS, "The global file '%s' doesn't exist", filename);
585 git_buf_clear(path);
586 return GIT_ENOTFOUND;
587 }
588
589 return 0;
590 #endif
591 }
592
593 int git_futils_fake_symlink(const char *old, const char *new)
594 {
595 int retcode = GIT_ERROR;
596 int fd = git_futils_creat_withpath(new, 0755, 0644);
597 if (fd >= 0) {
598 retcode = p_write(fd, old, strlen(old));
599 p_close(fd);
600 }
601 return retcode;
602 }
603
604 static int cp_by_fd(int ifd, int ofd, bool close_fd_when_done)
605 {
606 int error = 0;
607 char buffer[4096];
608 ssize_t len = 0;
609
610 while (!error && (len = p_read(ifd, buffer, sizeof(buffer))) > 0)
611 /* p_write() does not have the same semantics as write(). It loops
612 * internally and will return 0 when it has completed writing.
613 */
614 error = p_write(ofd, buffer, len);
615
616 if (len < 0) {
617 giterr_set(GITERR_OS, "Read error while copying file");
618 error = (int)len;
619 }
620
621 if (close_fd_when_done) {
622 p_close(ifd);
623 p_close(ofd);
624 }
625
626 return error;
627 }
628
629 int git_futils_cp(const char *from, const char *to, mode_t filemode)
630 {
631 int ifd, ofd;
632
633 if ((ifd = git_futils_open_ro(from)) < 0)
634 return ifd;
635
636 if ((ofd = p_open(to, O_WRONLY | O_CREAT | O_EXCL, filemode)) < 0) {
637 if (errno == ENOENT || errno == ENOTDIR)
638 ofd = GIT_ENOTFOUND;
639 giterr_set(GITERR_OS, "Failed to open '%s' for writing", to);
640 p_close(ifd);
641 return ofd;
642 }
643
644 return cp_by_fd(ifd, ofd, true);
645 }
646
647 static int cp_link(const char *from, const char *to, size_t link_size)
648 {
649 int error = 0;
650 ssize_t read_len;
651 char *link_data = git__malloc(link_size + 1);
652 GITERR_CHECK_ALLOC(link_data);
653
654 read_len = p_readlink(from, link_data, link_size);
655 if (read_len != (ssize_t)link_size) {
656 giterr_set(GITERR_OS, "Failed to read symlink data for '%s'", from);
657 error = -1;
658 }
659 else {
660 link_data[read_len] = '\0';
661
662 if (p_symlink(link_data, to) < 0) {
663 giterr_set(GITERR_OS, "Could not symlink '%s' as '%s'",
664 link_data, to);
665 error = -1;
666 }
667 }
668
669 git__free(link_data);
670 return error;
671 }
672
673 typedef struct {
674 const char *to_root;
675 git_buf to;
676 ssize_t from_prefix;
677 uint32_t flags;
678 uint32_t mkdir_flags;
679 mode_t dirmode;
680 } cp_r_info;
681
682 static int _cp_r_callback(void *ref, git_buf *from)
683 {
684 cp_r_info *info = ref;
685 struct stat from_st, to_st;
686 bool exists = false;
687
688 if ((info->flags & GIT_CPDIR_COPY_DOTFILES) == 0 &&
689 from->ptr[git_path_basename_offset(from)] == '.')
690 return 0;
691
692 if (git_buf_joinpath(
693 &info->to, info->to_root, from->ptr + info->from_prefix) < 0)
694 return -1;
695
696 if (p_lstat(info->to.ptr, &to_st) < 0) {
697 if (errno != ENOENT && errno != ENOTDIR) {
698 giterr_set(GITERR_OS,
699 "Could not access %s while copying files", info->to.ptr);
700 return -1;
701 }
702 } else
703 exists = true;
704
705 if (git_path_lstat(from->ptr, &from_st) < 0)
706 return -1;
707
708 if (S_ISDIR(from_st.st_mode)) {
709 int error = 0;
710 mode_t oldmode = info->dirmode;
711
712 /* if we are not chmod'ing, then overwrite dirmode */
713 if ((info->flags & GIT_CPDIR_CHMOD) == 0)
714 info->dirmode = from_st.st_mode;
715
716 /* make directory now if CREATE_EMPTY_DIRS is requested and needed */
717 if (!exists && (info->flags & GIT_CPDIR_CREATE_EMPTY_DIRS) != 0)
718 error = git_futils_mkdir(
719 info->to.ptr, NULL, info->dirmode, info->mkdir_flags);
720
721 /* recurse onto target directory */
722 if (!exists || S_ISDIR(to_st.st_mode))
723 error = git_path_direach(from, _cp_r_callback, info);
724
725 if (oldmode != 0)
726 info->dirmode = oldmode;
727
728 return error;
729 }
730
731 if (exists) {
732 if ((info->flags & GIT_CPDIR_OVERWRITE) == 0)
733 return 0;
734
735 if (p_unlink(info->to.ptr) < 0) {
736 giterr_set(GITERR_OS, "Cannot overwrite existing file '%s'",
737 info->to.ptr);
738 return -1;
739 }
740 }
741
742 /* Done if this isn't a regular file or a symlink */
743 if (!S_ISREG(from_st.st_mode) &&
744 (!S_ISLNK(from_st.st_mode) ||
745 (info->flags & GIT_CPDIR_COPY_SYMLINKS) == 0))
746 return 0;
747
748 /* Make container directory on demand if needed */
749 if ((info->flags & GIT_CPDIR_CREATE_EMPTY_DIRS) == 0 &&
750 git_futils_mkdir(
751 info->to.ptr, NULL, info->dirmode, info->mkdir_flags) < 0)
752 return -1;
753
754 /* make symlink or regular file */
755 if (S_ISLNK(from_st.st_mode))
756 return cp_link(from->ptr, info->to.ptr, (size_t)from_st.st_size);
757 else
758 return git_futils_cp(from->ptr, info->to.ptr, from_st.st_mode);
759 }
760
761 int git_futils_cp_r(
762 const char *from,
763 const char *to,
764 uint32_t flags,
765 mode_t dirmode)
766 {
767 int error;
768 git_buf path = GIT_BUF_INIT;
769 cp_r_info info;
770
771 if (git_buf_sets(&path, from) < 0)
772 return -1;
773
774 info.to_root = to;
775 info.flags = flags;
776 info.dirmode = dirmode;
777 info.from_prefix = path.size;
778 git_buf_init(&info.to, 0);
779
780 /* precalculate mkdir flags */
781 if ((flags & GIT_CPDIR_CREATE_EMPTY_DIRS) == 0) {
782 info.mkdir_flags = GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST;
783 if ((flags & GIT_CPDIR_CHMOD) != 0)
784 info.mkdir_flags |= GIT_MKDIR_CHMOD_PATH;
785 } else {
786 info.mkdir_flags =
787 ((flags & GIT_CPDIR_CHMOD) != 0) ? GIT_MKDIR_CHMOD : 0;
788 }
789
790 error = _cp_r_callback(&info, &path);
791
792 git_buf_free(&path);
793 git_buf_free(&info.to);
794
795 return error;
796 }
797
798 int git_futils_filestamp_check(
799 git_futils_filestamp *stamp, const char *path)
800 {
801 struct stat st;
802
803 /* if the stamp is NULL, then always reload */
804 if (stamp == NULL)
805 return 1;
806
807 if (p_stat(path, &st) < 0)
808 return GIT_ENOTFOUND;
809
810 if (stamp->mtime == (git_time_t)st.st_mtime &&
811 stamp->size == (git_off_t)st.st_size &&
812 stamp->ino == (unsigned int)st.st_ino)
813 return 0;
814
815 stamp->mtime = (git_time_t)st.st_mtime;
816 stamp->size = (git_off_t)st.st_size;
817 stamp->ino = (unsigned int)st.st_ino;
818
819 return 1;
820 }
821
822 void git_futils_filestamp_set(
823 git_futils_filestamp *target, const git_futils_filestamp *source)
824 {
825 assert(target);
826
827 if (source)
828 memcpy(target, source, sizeof(*target));
829 else
830 memset(target, 0, sizeof(*target));
831 }