2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
13 #include "win32/findfile.h"
18 int git_futils_mkpath2file(const char *file_path
, const mode_t mode
)
20 return git_futils_mkdir(
21 file_path
, NULL
, mode
,
22 GIT_MKDIR_PATH
| GIT_MKDIR_SKIP_LAST
| GIT_MKDIR_VERIFY_DIR
);
25 int git_futils_mktmp(git_buf
*path_out
, const char *filename
, mode_t mode
)
30 p_umask(mask
= p_umask(0));
32 git_buf_sets(path_out
, filename
);
33 git_buf_puts(path_out
, "_git2_XXXXXX");
35 if (git_buf_oom(path_out
))
38 if ((fd
= p_mkstemp(path_out
->ptr
)) < 0) {
40 "Failed to create temporary file '%s'", path_out
->ptr
);
44 if (p_chmod(path_out
->ptr
, (mode
& ~mask
))) {
46 "Failed to set permissions on file '%s'", path_out
->ptr
);
53 int git_futils_creat_withpath(const char *path
, const mode_t dirmode
, const mode_t mode
)
57 if (git_futils_mkpath2file(path
, dirmode
) < 0)
60 fd
= p_creat(path
, mode
);
62 giterr_set(GITERR_OS
, "Failed to create file '%s'", path
);
69 int git_futils_creat_locked(const char *path
, const mode_t mode
)
71 int fd
= p_open(path
, O_WRONLY
| O_CREAT
| O_TRUNC
|
72 O_EXCL
| O_BINARY
| O_CLOEXEC
, mode
);
75 giterr_set(GITERR_OS
, "Failed to create locked file '%s'", path
);
76 return errno
== EEXIST
? GIT_ELOCKED
: -1;
82 int git_futils_creat_locked_withpath(const char *path
, const mode_t dirmode
, const mode_t mode
)
84 if (git_futils_mkpath2file(path
, dirmode
) < 0)
87 return git_futils_creat_locked(path
, mode
);
90 int git_futils_open_ro(const char *path
)
92 int fd
= p_open(path
, O_RDONLY
);
94 return git_path_set_error(errno
, path
, "open");
98 git_off_t
git_futils_filesize(git_file fd
)
102 if (p_fstat(fd
, &sb
)) {
103 giterr_set(GITERR_OS
, "Failed to stat file descriptor");
110 mode_t
git_futils_canonical_mode(mode_t raw_mode
)
112 if (S_ISREG(raw_mode
))
113 return S_IFREG
| GIT_PERMS_CANONICAL(raw_mode
);
114 else if (S_ISLNK(raw_mode
))
116 else if (S_ISGITLINK(raw_mode
))
118 else if (S_ISDIR(raw_mode
))
124 int git_futils_readbuffer_fd(git_buf
*buf
, git_file fd
, size_t len
)
126 ssize_t read_size
= 0;
131 if (!git__is_ssizet(len
)) {
132 giterr_set(GITERR_INVALID
, "Read too large.");
136 GITERR_CHECK_ALLOC_ADD(&alloc_len
, len
, 1);
137 if (git_buf_grow(buf
, alloc_len
) < 0)
140 /* p_read loops internally to read len bytes */
141 read_size
= p_read(fd
, buf
->ptr
, len
);
143 if (read_size
!= (ssize_t
)len
) {
144 giterr_set(GITERR_OS
, "Failed to read descriptor");
149 buf
->ptr
[read_size
] = '\0';
150 buf
->size
= read_size
;
155 int git_futils_readbuffer_updated(
156 git_buf
*buf
, const char *path
, time_t *mtime
, size_t *size
, int *updated
)
160 bool changed
= false;
162 assert(buf
&& path
&& *path
);
167 if (p_stat(path
, &st
) < 0)
168 return git_path_set_error(errno
, path
, "stat");
171 if (S_ISDIR(st
.st_mode
)) {
172 giterr_set(GITERR_INVALID
, "requested file is a directory");
173 return GIT_ENOTFOUND
;
176 if (!git__is_sizet(st
.st_size
+1)) {
177 giterr_set(GITERR_OS
, "Invalid regular file stat for '%s'", path
);
182 * If we were given a time and/or a size, we only want to read the file
183 * if it has been modified.
185 if (size
&& *size
!= (size_t)st
.st_size
)
187 if (mtime
&& *mtime
!= (time_t)st
.st_mtime
)
197 *mtime
= st
.st_mtime
;
199 *size
= (size_t)st
.st_size
;
201 if ((fd
= git_futils_open_ro(path
)) < 0)
204 if (git_futils_readbuffer_fd(buf
, fd
, (size_t)st
.st_size
) < 0) {
217 int git_futils_readbuffer(git_buf
*buf
, const char *path
)
219 return git_futils_readbuffer_updated(buf
, path
, NULL
, NULL
, NULL
);
222 int git_futils_writebuffer(
223 const git_buf
*buf
, const char *path
, int flags
, mode_t mode
)
228 flags
= O_CREAT
| O_TRUNC
| O_WRONLY
;
230 mode
= GIT_FILEMODE_BLOB
;
232 if ((fd
= p_open(path
, flags
, mode
)) < 0) {
233 giterr_set(GITERR_OS
, "Could not open '%s' for writing", path
);
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
);
243 if ((error
= p_close(fd
)) < 0)
244 giterr_set(GITERR_OS
, "Error while closing '%s'", path
);
249 int git_futils_mv_withpath(const char *from
, const char *to
, const mode_t dirmode
)
251 if (git_futils_mkpath2file(to
, dirmode
) < 0)
254 if (p_rename(from
, to
) < 0) {
255 giterr_set(GITERR_OS
, "Failed to rename '%s' to '%s'", from
, to
);
262 int git_futils_mmap_ro(git_map
*out
, git_file fd
, git_off_t begin
, size_t len
)
264 return p_mmap(out
, len
, GIT_PROT_READ
, GIT_MAP_SHARED
, fd
, begin
);
267 int git_futils_mmap_ro_file(git_map
*out
, const char *path
)
269 git_file fd
= git_futils_open_ro(path
);
276 len
= git_futils_filesize(fd
);
277 if (!git__is_sizet(len
)) {
278 giterr_set(GITERR_OS
, "File `%s` too large to mmap", path
);
282 result
= git_futils_mmap_ro(out
, fd
, 0, (size_t)len
);
287 void git_futils_mmap_free(git_map
*out
)
292 GIT_INLINE(int) validate_existing(
293 const char *make_path
,
297 struct git_futils_mkdir_perfdata
*perfdata
)
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
);
307 perfdata
->mkdir_calls
++;
309 if (p_mkdir(make_path
, mode
) < 0) {
310 giterr_set(GITERR_OS
, "Failed to make directory '%s'", make_path
);
315 else if (S_ISLNK(st
->st_mode
)) {
316 /* Re-stat the target, make sure it's a directory */
317 perfdata
->stat_calls
++;
319 if (p_stat(make_path
, st
) < 0) {
320 giterr_set(GITERR_OS
, "Failed to make directory '%s'", make_path
);
325 else if (!S_ISDIR(st
->st_mode
)) {
326 giterr_set(GITERR_FILESYSTEM
,
327 "Failed to make directory '%s': directory exists", make_path
);
334 int git_futils_mkdir_ext(
339 struct git_futils_mkdir_options
*opts
)
342 git_buf make_path
= GIT_BUF_INIT
;
343 ssize_t root
= 0, min_root_len
, root_len
;
344 char lastch
= '/', *tail
;
347 /* build path and find "root" where we should start calling mkdir */
348 if (git_path_join_unrooted(&make_path
, path
, base
, &root
) < 0)
351 if (make_path
.size
== 0) {
352 giterr_set(GITERR_OS
, "Attempt to create empty path");
356 /* Trim trailing slashes (except the root) */
357 if ((root_len
= git_path_root(make_path
.ptr
)) < 0)
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';
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
;
371 if ((flags
& GIT_MKDIR_SKIP_LAST
) != 0) {
372 git_path_dirname_r(&make_path
, make_path
.ptr
);
375 /* We were either given the root path (or trimmed it to
376 * the root), we don't have anything to do.
378 if (make_path
.size
<= (size_t)root_len
) {
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
, '/');
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
)
391 while (root
>= 0 && make_path
.ptr
[root
] == '/')
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 */
400 /* walk down tail of path making each directory */
401 for (tail
= &make_path
.ptr
[root
]; *tail
; *tail
= lastch
) {
402 bool mkdir_attempted
= false;
404 /* advance tail to include next path component */
407 while (*tail
&& *tail
!= '/')
410 /* truncate path at next component */
415 if (opts
->dir_map
&& git_strmap_exists(opts
->dir_map
, make_path
.ptr
))
418 /* See what's going on with this path component */
419 opts
->perfdata
.stat_calls
++;
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
);
430 opts
->perfdata
.mkdir_calls
++;
431 mkdir_attempted
= true;
432 if (p_mkdir(make_path
.ptr
, mode
) < 0) {
435 giterr_set(GITERR_OS
, "Failed to make directory '%s'", make_path
.ptr
);
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
);
447 if ((error
= validate_existing(
448 make_path
.ptr
, &st
, mode
, flags
, &opts
->perfdata
)) < 0)
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
) {
457 opts
->perfdata
.chmod_calls
++;
459 if ((error
= p_chmod(make_path
.ptr
, mode
)) < 0 &&
461 giterr_set(GITERR_OS
, "Failed to set permissions on '%s'",
467 if (opts
->dir_map
&& opts
->pool
) {
471 GITERR_CHECK_ALLOC_ADD(&alloc_size
, make_path
.size
, 1);
472 if (!git__is_uint32(alloc_size
))
474 cache_path
= git_pool_malloc(opts
->pool
, (uint32_t)alloc_size
);
475 GITERR_CHECK_ALLOC(cache_path
);
477 memcpy(cache_path
, make_path
.ptr
, make_path
.size
+ 1);
479 git_strmap_insert(opts
->dir_map
, cache_path
, cache_path
, error
);
487 /* check that full path really is a directory if requested & needed */
488 if ((flags
& GIT_MKDIR_VERIFY_DIR
) != 0 &&
490 opts
->perfdata
.stat_calls
++;
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'",
495 error
= GIT_ENOTFOUND
;
500 git_buf_free(&make_path
);
504 int git_futils_mkdir(
510 struct git_futils_mkdir_options options
= {0};
511 return git_futils_mkdir_ext(path
, base
, mode
, flags
, &options
);
514 int git_futils_mkdir_r(const char *path
, const char *base
, const mode_t mode
)
516 return git_futils_mkdir(path
, base
, mode
, GIT_MKDIR_PATH
);
524 } futils__rmdir_data
;
526 #define FUTILS_MAX_DEPTH 100
528 static int futils__error_cannot_rmdir(const char *path
, const char *filemsg
)
531 giterr_set(GITERR_OS
, "Could not remove directory. File '%s' %s",
534 giterr_set(GITERR_OS
, "Could not remove directory '%s'", path
);
539 static int futils__rm_first_parent(git_buf
*path
, const char *ceiling
)
541 int error
= GIT_ENOTFOUND
;
544 while (error
== GIT_ENOTFOUND
) {
545 git_buf_rtruncate_at_char(path
, '/');
547 if (!path
->size
|| git__prefixcmp(path
->ptr
, ceiling
) != 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
)
559 futils__error_cannot_rmdir(path
->ptr
, "cannot remove parent");
564 static int futils__rmdir_recurs_foreach(void *opaque
, git_buf
*path
)
567 futils__rmdir_data
*data
= opaque
;
570 if (data
->depth
> FUTILS_MAX_DEPTH
)
571 error
= futils__error_cannot_rmdir(
572 path
->ptr
, "directory nesting too deep");
574 else if ((error
= p_lstat_posixly(path
->ptr
, &st
)) < 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
);
582 futils__error_cannot_rmdir(
583 path
->ptr
, "parent is not directory");
586 error
= git_path_set_error(errno
, path
->ptr
, "rmdir");
589 else if (S_ISDIR(st
.st_mode
)) {
592 error
= git_path_direach(path
, 0, futils__rmdir_recurs_foreach
, data
);
599 if (data
->depth
== 0 && (data
->flags
& GIT_RMDIR_SKIP_ROOT
) != 0)
602 if ((error
= p_rmdir(path
->ptr
)) < 0) {
603 if ((data
->flags
& GIT_RMDIR_SKIP_NONEMPTY
) != 0 &&
604 (errno
== ENOTEMPTY
|| errno
== EEXIST
|| errno
== EBUSY
))
607 error
= git_path_set_error(errno
, path
->ptr
, "rmdir");
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");
616 else if ((data
->flags
& GIT_RMDIR_SKIP_NONEMPTY
) == 0)
617 error
= futils__error_cannot_rmdir(path
->ptr
, "still present");
622 static int futils__rmdir_empty_parent(void *opaque
, const char *path
)
624 futils__rmdir_data
*data
= opaque
;
627 if (strlen(path
) <= data
->baselen
)
628 error
= GIT_ITEROVER
;
630 else if (p_rmdir(path
) < 0) {
633 if (en
== ENOENT
|| en
== ENOTDIR
) {
635 } else if (en
== ENOTEMPTY
|| en
== EEXIST
|| en
== EBUSY
) {
636 error
= GIT_ITEROVER
;
638 error
= git_path_set_error(errno
, path
, "rmdir");
645 int git_futils_rmdir_r(
646 const char *path
, const char *base
, uint32_t flags
)
649 git_buf fullpath
= GIT_BUF_INIT
;
650 futils__rmdir_data data
;
652 /* build path and find "root" where we should start calling mkdir */
653 if (git_path_join_unrooted(&fullpath
, path
, base
, NULL
) < 0)
656 memset(&data
, 0, sizeof(data
));
657 data
.base
= base
? base
: "";
658 data
.baselen
= base
? strlen(base
) : 0;
661 error
= futils__rmdir_recurs_foreach(&data
, &fullpath
);
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
);
668 if (error
== GIT_ITEROVER
) {
673 git_buf_free(&fullpath
);
678 int git_futils_fake_symlink(const char *old
, const char *new)
680 int retcode
= GIT_ERROR
;
681 int fd
= git_futils_creat_withpath(new, 0755, 0644);
683 retcode
= p_write(fd
, old
, strlen(old
));
689 static int cp_by_fd(int ifd
, int ofd
, bool close_fd_when_done
)
692 char buffer
[FILEIO_BUFSIZE
];
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.
699 error
= p_write(ofd
, buffer
, len
);
702 giterr_set(GITERR_OS
, "Read error while copying file");
707 giterr_set(GITERR_OS
, "write error while copying file");
709 if (close_fd_when_done
) {
717 int git_futils_cp(const char *from
, const char *to
, mode_t filemode
)
721 if ((ifd
= git_futils_open_ro(from
)) < 0)
724 if ((ofd
= p_open(to
, O_WRONLY
| O_CREAT
| O_EXCL
, filemode
)) < 0) {
726 return git_path_set_error(errno
, to
, "open for writing");
729 return cp_by_fd(ifd
, ofd
, true);
732 static int cp_link(const char *from
, const char *to
, size_t link_size
)
739 GITERR_CHECK_ALLOC_ADD(&alloc_size
, link_size
, 1);
740 link_data
= git__malloc(alloc_size
);
741 GITERR_CHECK_ALLOC(link_data
);
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
);
749 link_data
[read_len
] = '\0';
751 if (p_symlink(link_data
, to
) < 0) {
752 giterr_set(GITERR_OS
, "Could not symlink '%s' as '%s'",
758 git__free(link_data
);
767 uint32_t mkdir_flags
;
771 #define GIT_CPDIR__MKDIR_DONE_FOR_TO_ROOT (1u << 10)
773 static int _cp_r_mkdir(cp_r_info
*info
, git_buf
*from
)
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);
783 info
->flags
|= GIT_CPDIR__MKDIR_DONE_FOR_TO_ROOT
;
786 /* create directory with root as base to prevent excess chmods */
788 error
= git_futils_mkdir(
789 from
->ptr
+ info
->from_prefix
, info
->to_root
,
790 info
->dirmode
, info
->mkdir_flags
);
795 static int _cp_r_callback(void *ref
, git_buf
*from
)
798 cp_r_info
*info
= ref
;
799 struct stat from_st
, to_st
;
802 if ((info
->flags
& GIT_CPDIR_COPY_DOTFILES
) == 0 &&
803 from
->ptr
[git_path_basename_offset(from
)] == '.')
806 if ((error
= git_buf_joinpath(
807 &info
->to
, info
->to_root
, from
->ptr
+ info
->from_prefix
)) < 0)
810 if (!(error
= git_path_lstat(info
->to
.ptr
, &to_st
)))
812 else if (error
!= GIT_ENOTFOUND
)
819 if ((error
= git_path_lstat(from
->ptr
, &from_st
)) < 0)
822 if (S_ISDIR(from_st
.st_mode
)) {
823 mode_t oldmode
= info
->dirmode
;
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
;
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
);
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
);
838 info
->dirmode
= oldmode
;
844 if ((info
->flags
& GIT_CPDIR_OVERWRITE
) == 0)
847 if (p_unlink(info
->to
.ptr
) < 0) {
848 giterr_set(GITERR_OS
, "Cannot overwrite existing file '%s'",
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))
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)
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
);
872 mode_t usemode
= from_st
.st_mode
;
874 if ((info
->flags
& GIT_CPDIR_SIMPLE_TO_MODE
) != 0)
875 usemode
= GIT_PERMS_FOR_WRITE(usemode
);
877 error
= git_futils_cp(from
->ptr
, info
->to
.ptr
, usemode
);
890 git_buf path
= GIT_BUF_INIT
;
893 if (git_buf_joinpath(&path
, from
, "") < 0) /* ensure trailing slash */
896 memset(&info
, 0, sizeof(info
));
899 info
.dirmode
= dirmode
;
900 info
.from_prefix
= path
.size
;
901 git_buf_init(&info
.to
, 0);
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.
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
;
912 /* otherwise, we will do simple mkdir as directories are encountered */
914 ((flags
& GIT_CPDIR_CHMOD_DIRS
) != 0) ? GIT_MKDIR_CHMOD
: 0;
917 error
= _cp_r_callback(&info
, &path
);
920 git_buf_free(&info
.to
);
925 int git_futils_filestamp_check(
926 git_futils_filestamp
*stamp
, const char *path
)
930 /* if the stamp is NULL, then always reload */
934 if (p_stat(path
, &st
) < 0)
935 return GIT_ENOTFOUND
;
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
)
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
;
949 void git_futils_filestamp_set(
950 git_futils_filestamp
*target
, const git_futils_filestamp
*source
)
955 memcpy(target
, source
, sizeof(*target
));
957 memset(target
, 0, sizeof(*target
));
961 void git_futils_filestamp_set_from_stat(
962 git_futils_filestamp
*stamp
, struct stat
*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
;
969 memset(stamp
, 0, sizeof(*stamp
));