]> git.proxmox.com Git - libgit2.git/blob - src/fileops.c
Merge pull request #1204 from arrbee/diff-blob-to-buffer
[libgit2.git] / src / fileops.c
1 /*
2 * Copyright (C) 2009-2012 the libgit2 contributors
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;
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 if (errno == EEXIST) {
304 if (!lastch && (flags & GIT_MKDIR_VERIFY_DIR) != 0) {
305 if (!git_path_isdir(make_path.ptr)) {
306 giterr_set(
307 GITERR_OS, "Existing path is not a directory '%s'",
308 make_path.ptr);
309 error = GIT_ENOTFOUND;
310 goto fail;
311 }
312 }
313 if ((flags & GIT_MKDIR_EXCL) != 0) {
314 giterr_set(GITERR_OS, "Directory already exists '%s'",
315 make_path.ptr);
316 error = GIT_EEXISTS;
317 goto fail;
318 }
319 } else {
320 giterr_set(GITERR_OS, "Failed to make directory '%s'",
321 make_path.ptr);
322 goto fail;
323 }
324 }
325
326 /* chmod if requested */
327 if ((flags & GIT_MKDIR_CHMOD_PATH) != 0 ||
328 ((flags & GIT_MKDIR_CHMOD) != 0 && lastch == '\0'))
329 {
330 if (p_chmod(make_path.ptr, mode) < 0) {
331 giterr_set(GITERR_OS, "Failed to set permissions on '%s'",
332 make_path.ptr);
333 goto fail;
334 }
335 }
336
337 *tail = lastch;
338 }
339
340 git_buf_free(&make_path);
341 return 0;
342
343 fail:
344 git_buf_free(&make_path);
345 return error;
346 }
347
348 int git_futils_mkdir_r(const char *path, const char *base, const mode_t mode)
349 {
350 return git_futils_mkdir(path, base, mode, GIT_MKDIR_PATH);
351 }
352
353 typedef struct {
354 const char *base;
355 size_t baselen;
356 uint32_t flags;
357 int error;
358 } futils__rmdir_data;
359
360 static int futils__error_cannot_rmdir(const char *path, const char *filemsg)
361 {
362 if (filemsg)
363 giterr_set(GITERR_OS, "Could not remove directory. File '%s' %s",
364 path, filemsg);
365 else
366 giterr_set(GITERR_OS, "Could not remove directory '%s'", path);
367
368 return -1;
369 }
370
371 static int futils__rm_first_parent(git_buf *path, const char *ceiling)
372 {
373 int error = GIT_ENOTFOUND;
374 struct stat st;
375
376 while (error == GIT_ENOTFOUND) {
377 git_buf_rtruncate_at_char(path, '/');
378
379 if (!path->size || git__prefixcmp(path->ptr, ceiling) != 0)
380 error = 0;
381 else if (p_lstat_posixly(path->ptr, &st) == 0) {
382 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
383 error = p_unlink(path->ptr);
384 else if (!S_ISDIR(st.st_mode))
385 error = -1; /* fail to remove non-regular file */
386 } else if (errno != ENOTDIR)
387 error = -1;
388 }
389
390 if (error)
391 futils__error_cannot_rmdir(path->ptr, "cannot remove parent");
392
393 return error;
394 }
395
396 static int futils__rmdir_recurs_foreach(void *opaque, git_buf *path)
397 {
398 struct stat st;
399 futils__rmdir_data *data = opaque;
400
401 if ((data->error = p_lstat_posixly(path->ptr, &st)) < 0) {
402 if (errno == ENOENT)
403 data->error = 0;
404 else if (errno == ENOTDIR) {
405 /* asked to remove a/b/c/d/e and a/b is a normal file */
406 if ((data->flags & GIT_RMDIR_REMOVE_BLOCKERS) != 0)
407 data->error = futils__rm_first_parent(path, data->base);
408 else
409 futils__error_cannot_rmdir(
410 path->ptr, "parent is not directory");
411 }
412 else
413 futils__error_cannot_rmdir(path->ptr, "cannot access");
414 }
415
416 else if (S_ISDIR(st.st_mode)) {
417 int error = git_path_direach(path, futils__rmdir_recurs_foreach, data);
418 if (error < 0)
419 return (error == GIT_EUSER) ? data->error : error;
420
421 data->error = p_rmdir(path->ptr);
422
423 if (data->error < 0) {
424 if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) != 0 &&
425 (errno == ENOTEMPTY || errno == EEXIST))
426 data->error = 0;
427 else
428 futils__error_cannot_rmdir(path->ptr, NULL);
429 }
430 }
431
432 else if ((data->flags & GIT_RMDIR_REMOVE_FILES) != 0) {
433 data->error = p_unlink(path->ptr);
434
435 if (data->error < 0)
436 futils__error_cannot_rmdir(path->ptr, "cannot be removed");
437 }
438
439 else if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) == 0)
440 data->error = futils__error_cannot_rmdir(path->ptr, "still present");
441
442 return data->error;
443 }
444
445 static int futils__rmdir_empty_parent(void *opaque, git_buf *path)
446 {
447 futils__rmdir_data *data = opaque;
448 int error;
449
450 if (git_buf_len(path) <= data->baselen)
451 return GIT_ITEROVER;
452
453 error = p_rmdir(git_buf_cstr(path));
454
455 if (error) {
456 int en = errno;
457
458 if (en == ENOENT || en == ENOTDIR) {
459 giterr_clear();
460 error = 0;
461 } else if (en == ENOTEMPTY || en == EEXIST) {
462 giterr_clear();
463 error = GIT_ITEROVER;
464 } else {
465 futils__error_cannot_rmdir(git_buf_cstr(path), NULL);
466 }
467 }
468
469 return error;
470 }
471
472 int git_futils_rmdir_r(
473 const char *path, const char *base, uint32_t flags)
474 {
475 int error;
476 git_buf fullpath = GIT_BUF_INIT;
477 futils__rmdir_data data;
478
479 /* build path and find "root" where we should start calling mkdir */
480 if (git_path_join_unrooted(&fullpath, path, base, NULL) < 0)
481 return -1;
482
483 data.base = base ? base : "";
484 data.baselen = base ? strlen(base) : 0;
485 data.flags = flags;
486 data.error = 0;
487
488 error = futils__rmdir_recurs_foreach(&data, &fullpath);
489
490 /* remove now-empty parents if requested */
491 if (!error && (flags & GIT_RMDIR_EMPTY_PARENTS) != 0) {
492 error = git_path_walk_up(
493 &fullpath, base, futils__rmdir_empty_parent, &data);
494
495 if (error == GIT_ITEROVER)
496 error = 0;
497 }
498
499 git_buf_free(&fullpath);
500
501 return error;
502 }
503
504 int git_futils_find_system_file(git_buf *path, const char *filename)
505 {
506 #ifdef GIT_WIN32
507 // try to find git.exe/git.cmd on path
508 if (!win32_find_system_file_using_path(path, filename))
509 return 0;
510
511 // try to find msysgit installation path using registry
512 if (!win32_find_system_file_using_registry(path, filename))
513 return 0;
514 #else
515 if (git_buf_joinpath(path, "/etc", filename) < 0)
516 return -1;
517
518 if (git_path_exists(path->ptr) == true)
519 return 0;
520 #endif
521
522 git_buf_clear(path);
523 giterr_set(GITERR_OS, "The system file '%s' doesn't exist", filename);
524 return GIT_ENOTFOUND;
525 }
526
527 int git_futils_find_global_file(git_buf *path, const char *filename)
528 {
529 #ifdef GIT_WIN32
530 struct win32_path root;
531 static const wchar_t *tmpls[4] = {
532 L"%HOME%\\",
533 L"%HOMEDRIVE%%HOMEPATH%\\",
534 L"%USERPROFILE%\\",
535 NULL,
536 };
537 const wchar_t **tmpl;
538
539 for (tmpl = tmpls; *tmpl != NULL; tmpl++) {
540 /* try to expand environment variable, skipping if not set */
541 if (win32_expand_path(&root, *tmpl) != 0 || root.path[0] == L'%')
542 continue;
543
544 /* try to look up file under path */
545 if (!win32_find_file(path, &root, filename))
546 return 0;
547
548 /* No error if file not found under %HOME%, b/c we don't trust it,
549 * but do error if another var is set and yet file is not found.
550 */
551 if (tmpl != tmpls)
552 break;
553 }
554
555 giterr_set(GITERR_OS, "The global file '%s' doesn't exist", filename);
556 git_buf_clear(path);
557
558 return GIT_ENOTFOUND;
559 #else
560 const char *home = getenv("HOME");
561
562 if (home == NULL) {
563 giterr_set(GITERR_OS, "Global file lookup failed. "
564 "Cannot locate the user's home directory");
565 return GIT_ENOTFOUND;
566 }
567
568 if (git_buf_joinpath(path, home, filename) < 0)
569 return -1;
570
571 if (git_path_exists(path->ptr) == false) {
572 giterr_set(GITERR_OS, "The global file '%s' doesn't exist", filename);
573 git_buf_clear(path);
574 return GIT_ENOTFOUND;
575 }
576
577 return 0;
578 #endif
579 }
580
581 int git_futils_fake_symlink(const char *old, const char *new)
582 {
583 int retcode = GIT_ERROR;
584 int fd = git_futils_creat_withpath(new, 0755, 0644);
585 if (fd >= 0) {
586 retcode = p_write(fd, old, strlen(old));
587 p_close(fd);
588 }
589 return retcode;
590 }
591
592 static int cp_by_fd(int ifd, int ofd, bool close_fd_when_done)
593 {
594 int error = 0;
595 char buffer[4096];
596 ssize_t len = 0;
597
598 while (!error && (len = p_read(ifd, buffer, sizeof(buffer))) > 0)
599 /* p_write() does not have the same semantics as write(). It loops
600 * internally and will return 0 when it has completed writing.
601 */
602 error = p_write(ofd, buffer, len);
603
604 if (len < 0) {
605 giterr_set(GITERR_OS, "Read error while copying file");
606 error = (int)len;
607 }
608
609 if (close_fd_when_done) {
610 p_close(ifd);
611 p_close(ofd);
612 }
613
614 return error;
615 }
616
617 int git_futils_cp(const char *from, const char *to, mode_t filemode)
618 {
619 int ifd, ofd;
620
621 if ((ifd = git_futils_open_ro(from)) < 0)
622 return ifd;
623
624 if ((ofd = p_open(to, O_WRONLY | O_CREAT | O_EXCL, filemode)) < 0) {
625 if (errno == ENOENT || errno == ENOTDIR)
626 ofd = GIT_ENOTFOUND;
627 giterr_set(GITERR_OS, "Failed to open '%s' for writing", to);
628 p_close(ifd);
629 return ofd;
630 }
631
632 return cp_by_fd(ifd, ofd, true);
633 }
634
635 static int cp_link(const char *from, const char *to, size_t link_size)
636 {
637 int error = 0;
638 ssize_t read_len;
639 char *link_data = git__malloc(link_size + 1);
640 GITERR_CHECK_ALLOC(link_data);
641
642 read_len = p_readlink(from, link_data, link_size);
643 if (read_len != (ssize_t)link_size) {
644 giterr_set(GITERR_OS, "Failed to read symlink data for '%s'", from);
645 error = -1;
646 }
647 else {
648 link_data[read_len] = '\0';
649
650 if (p_symlink(link_data, to) < 0) {
651 giterr_set(GITERR_OS, "Could not symlink '%s' as '%s'",
652 link_data, to);
653 error = -1;
654 }
655 }
656
657 git__free(link_data);
658 return error;
659 }
660
661 typedef struct {
662 const char *to_root;
663 git_buf to;
664 ssize_t from_prefix;
665 uint32_t flags;
666 uint32_t mkdir_flags;
667 mode_t dirmode;
668 } cp_r_info;
669
670 static int _cp_r_callback(void *ref, git_buf *from)
671 {
672 cp_r_info *info = ref;
673 struct stat from_st, to_st;
674 bool exists = false;
675
676 if ((info->flags & GIT_CPDIR_COPY_DOTFILES) == 0 &&
677 from->ptr[git_path_basename_offset(from)] == '.')
678 return 0;
679
680 if (git_buf_joinpath(
681 &info->to, info->to_root, from->ptr + info->from_prefix) < 0)
682 return -1;
683
684 if (p_lstat(info->to.ptr, &to_st) < 0) {
685 if (errno != ENOENT && errno != ENOTDIR) {
686 giterr_set(GITERR_OS,
687 "Could not access %s while copying files", info->to.ptr);
688 return -1;
689 }
690 } else
691 exists = true;
692
693 if (git_path_lstat(from->ptr, &from_st) < 0)
694 return -1;
695
696 if (S_ISDIR(from_st.st_mode)) {
697 int error = 0;
698 mode_t oldmode = info->dirmode;
699
700 /* if we are not chmod'ing, then overwrite dirmode */
701 if ((info->flags & GIT_CPDIR_CHMOD) == 0)
702 info->dirmode = from_st.st_mode;
703
704 /* make directory now if CREATE_EMPTY_DIRS is requested and needed */
705 if (!exists && (info->flags & GIT_CPDIR_CREATE_EMPTY_DIRS) != 0)
706 error = git_futils_mkdir(
707 info->to.ptr, NULL, info->dirmode, info->mkdir_flags);
708
709 /* recurse onto target directory */
710 if (!exists || S_ISDIR(to_st.st_mode))
711 error = git_path_direach(from, _cp_r_callback, info);
712
713 if (oldmode != 0)
714 info->dirmode = oldmode;
715
716 return error;
717 }
718
719 if (exists) {
720 if ((info->flags & GIT_CPDIR_OVERWRITE) == 0)
721 return 0;
722
723 if (p_unlink(info->to.ptr) < 0) {
724 giterr_set(GITERR_OS, "Cannot overwrite existing file '%s'",
725 info->to.ptr);
726 return -1;
727 }
728 }
729
730 /* Done if this isn't a regular file or a symlink */
731 if (!S_ISREG(from_st.st_mode) &&
732 (!S_ISLNK(from_st.st_mode) ||
733 (info->flags & GIT_CPDIR_COPY_SYMLINKS) == 0))
734 return 0;
735
736 /* Make container directory on demand if needed */
737 if ((info->flags & GIT_CPDIR_CREATE_EMPTY_DIRS) == 0 &&
738 git_futils_mkdir(
739 info->to.ptr, NULL, info->dirmode, info->mkdir_flags) < 0)
740 return -1;
741
742 /* make symlink or regular file */
743 if (S_ISLNK(from_st.st_mode))
744 return cp_link(from->ptr, info->to.ptr, (size_t)from_st.st_size);
745 else
746 return git_futils_cp(from->ptr, info->to.ptr, from_st.st_mode);
747 }
748
749 int git_futils_cp_r(
750 const char *from,
751 const char *to,
752 uint32_t flags,
753 mode_t dirmode)
754 {
755 int error;
756 git_buf path = GIT_BUF_INIT;
757 cp_r_info info;
758
759 if (git_buf_sets(&path, from) < 0)
760 return -1;
761
762 info.to_root = to;
763 info.flags = flags;
764 info.dirmode = dirmode;
765 info.from_prefix = path.size;
766 git_buf_init(&info.to, 0);
767
768 /* precalculate mkdir flags */
769 if ((flags & GIT_CPDIR_CREATE_EMPTY_DIRS) == 0) {
770 info.mkdir_flags = GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST;
771 if ((flags & GIT_CPDIR_CHMOD) != 0)
772 info.mkdir_flags |= GIT_MKDIR_CHMOD_PATH;
773 } else {
774 info.mkdir_flags =
775 ((flags & GIT_CPDIR_CHMOD) != 0) ? GIT_MKDIR_CHMOD : 0;
776 }
777
778 error = _cp_r_callback(&info, &path);
779
780 git_buf_free(&path);
781 git_buf_free(&info.to);
782
783 return error;
784 }
785
786 int git_futils_filestamp_check(
787 git_futils_filestamp *stamp, const char *path)
788 {
789 struct stat st;
790
791 /* if the stamp is NULL, then always reload */
792 if (stamp == NULL)
793 return 1;
794
795 if (p_stat(path, &st) < 0)
796 return GIT_ENOTFOUND;
797
798 if (stamp->mtime == (git_time_t)st.st_mtime &&
799 stamp->size == (git_off_t)st.st_size &&
800 stamp->ino == (unsigned int)st.st_ino)
801 return 0;
802
803 stamp->mtime = (git_time_t)st.st_mtime;
804 stamp->size = (git_off_t)st.st_size;
805 stamp->ino = (unsigned int)st.st_ino;
806
807 return 1;
808 }
809
810 void git_futils_filestamp_set(
811 git_futils_filestamp *target, const git_futils_filestamp *source)
812 {
813 assert(target);
814
815 if (source)
816 memcpy(target, source, sizeof(*target));
817 else
818 memset(target, 0, sizeof(*target));
819 }