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.
11 #include "win32/findfile.h"
14 int git_futils_mkpath2file(const char *file_path
, const mode_t mode
)
16 return git_futils_mkdir(
17 file_path
, NULL
, mode
,
18 GIT_MKDIR_PATH
| GIT_MKDIR_SKIP_LAST
| GIT_MKDIR_VERIFY_DIR
);
21 int git_futils_mktmp(git_buf
*path_out
, const char *filename
)
25 git_buf_sets(path_out
, filename
);
26 git_buf_puts(path_out
, "_git2_XXXXXX");
28 if (git_buf_oom(path_out
))
31 if ((fd
= p_mkstemp(path_out
->ptr
)) < 0) {
33 "Failed to create temporary file '%s'", path_out
->ptr
);
40 int git_futils_creat_withpath(const char *path
, const mode_t dirmode
, const mode_t mode
)
44 if (git_futils_mkpath2file(path
, dirmode
) < 0)
47 fd
= p_creat(path
, mode
);
49 giterr_set(GITERR_OS
, "Failed to create file '%s'", path
);
56 int git_futils_creat_locked(const char *path
, const mode_t mode
)
61 wchar_t buf
[GIT_WIN_PATH
];
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
);
66 fd
= open(path
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
| O_EXCL
, mode
);
70 giterr_set(GITERR_OS
, "Failed to create locked file '%s'", path
);
77 int git_futils_creat_locked_withpath(const char *path
, const mode_t dirmode
, const mode_t mode
)
79 if (git_futils_mkpath2file(path
, dirmode
) < 0)
82 return git_futils_creat_locked(path
, mode
);
85 int git_futils_open_ro(const char *path
)
87 int fd
= p_open(path
, O_RDONLY
);
89 if (errno
== ENOENT
|| errno
== ENOTDIR
)
91 giterr_set(GITERR_OS
, "Failed to open '%s'", path
);
96 git_off_t
git_futils_filesize(git_file fd
)
100 if (p_fstat(fd
, &sb
)) {
101 giterr_set(GITERR_OS
, "Failed to stat file descriptor");
108 mode_t
git_futils_canonical_mode(mode_t raw_mode
)
110 if (S_ISREG(raw_mode
))
111 return S_IFREG
| GIT_CANONICAL_PERMS(raw_mode
);
112 else if (S_ISLNK(raw_mode
))
114 else if (S_ISGITLINK(raw_mode
))
116 else if (S_ISDIR(raw_mode
))
122 int git_futils_readbuffer_fd(git_buf
*buf
, git_file fd
, size_t len
)
124 ssize_t read_size
= 0;
128 if (git_buf_grow(buf
, len
+ 1) < 0)
131 /* p_read loops internally to read len bytes */
132 read_size
= p_read(fd
, buf
->ptr
, len
);
134 if (read_size
!= (ssize_t
)len
) {
135 giterr_set(GITERR_OS
, "Failed to read descriptor");
139 buf
->ptr
[read_size
] = '\0';
140 buf
->size
= read_size
;
145 int git_futils_readbuffer_updated(
146 git_buf
*buf
, const char *path
, time_t *mtime
, size_t *size
, int *updated
)
150 bool changed
= false;
152 assert(buf
&& path
&& *path
);
157 if ((fd
= git_futils_open_ro(path
)) < 0)
160 if (p_fstat(fd
, &st
) < 0 || S_ISDIR(st
.st_mode
) || !git__is_sizet(st
.st_size
+1)) {
162 giterr_set(GITERR_OS
, "Invalid regular file stat for '%s'", path
);
167 * If we were given a time and/or a size, we only want to read the file
168 * if it has been modified.
170 if (size
&& *size
!= (size_t)st
.st_size
)
172 if (mtime
&& *mtime
!= st
.st_mtime
)
183 *mtime
= st
.st_mtime
;
185 *size
= (size_t)st
.st_size
;
187 if (git_futils_readbuffer_fd(buf
, fd
, (size_t)st
.st_size
) < 0) {
200 int git_futils_readbuffer(git_buf
*buf
, const char *path
)
202 return git_futils_readbuffer_updated(buf
, path
, NULL
, NULL
, NULL
);
205 int git_futils_mv_withpath(const char *from
, const char *to
, const mode_t dirmode
)
207 if (git_futils_mkpath2file(to
, dirmode
) < 0)
210 if (p_rename(from
, to
) < 0) {
211 giterr_set(GITERR_OS
, "Failed to rename '%s' to '%s'", from
, to
);
218 int git_futils_mmap_ro(git_map
*out
, git_file fd
, git_off_t begin
, size_t len
)
220 return p_mmap(out
, len
, GIT_PROT_READ
, GIT_MAP_SHARED
, fd
, begin
);
223 int git_futils_mmap_ro_file(git_map
*out
, const char *path
)
225 git_file fd
= git_futils_open_ro(path
);
232 len
= git_futils_filesize(fd
);
233 if (!git__is_sizet(len
)) {
234 giterr_set(GITERR_OS
, "File `%s` too large to mmap", path
);
238 result
= git_futils_mmap_ro(out
, fd
, 0, (size_t)len
);
243 void git_futils_mmap_free(git_map
*out
)
248 int git_futils_mkdir(
255 git_buf make_path
= GIT_BUF_INIT
;
259 /* build path and find "root" where we should start calling mkdir */
260 if (git_path_join_unrooted(&make_path
, path
, base
, &root
) < 0)
263 if (make_path
.size
== 0) {
264 giterr_set(GITERR_OS
, "Attempt to create empty path");
268 /* remove trailing slashes on path */
269 while (make_path
.ptr
[make_path
.size
- 1] == '/') {
271 make_path
.ptr
[make_path
.size
] = '\0';
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
, '/');
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
, '/');
282 /* clip root to make_path length */
283 if (root
>= (ssize_t
)make_path
.size
)
284 root
= (ssize_t
)make_path
.size
- 1;
288 tail
= & make_path
.ptr
[root
];
291 /* advance tail to include next path component */
294 while (*tail
&& *tail
!= '/')
297 /* truncate path at next component */
302 if (p_mkdir(make_path
.ptr
, mode
) < 0) {
303 int already_exists
= 0;
307 if (!lastch
&& (flags
& GIT_MKDIR_VERIFY_DIR
) != 0 &&
308 !git_path_isdir(make_path
.ptr
)) {
310 GITERR_OS
, "Existing path is not a directory '%s'",
312 error
= GIT_ENOTFOUND
;
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
)) {
331 giterr_set(GITERR_OS
, "Failed to make directory '%s'",
336 if (already_exists
&& (flags
& GIT_MKDIR_EXCL
) != 0) {
337 giterr_set(GITERR_OS
, "Directory already exists '%s'",
344 /* chmod if requested */
345 if ((flags
& GIT_MKDIR_CHMOD_PATH
) != 0 ||
346 ((flags
& GIT_MKDIR_CHMOD
) != 0 && lastch
== '\0'))
348 if (p_chmod(make_path
.ptr
, mode
) < 0) {
349 giterr_set(GITERR_OS
, "Failed to set permissions on '%s'",
358 git_buf_free(&make_path
);
362 git_buf_free(&make_path
);
366 int git_futils_mkdir_r(const char *path
, const char *base
, const mode_t mode
)
368 return git_futils_mkdir(path
, base
, mode
, GIT_MKDIR_PATH
);
376 } futils__rmdir_data
;
378 static int futils__error_cannot_rmdir(const char *path
, const char *filemsg
)
381 giterr_set(GITERR_OS
, "Could not remove directory. File '%s' %s",
384 giterr_set(GITERR_OS
, "Could not remove directory '%s'", path
);
389 static int futils__rm_first_parent(git_buf
*path
, const char *ceiling
)
391 int error
= GIT_ENOTFOUND
;
394 while (error
== GIT_ENOTFOUND
) {
395 git_buf_rtruncate_at_char(path
, '/');
397 if (!path
->size
|| git__prefixcmp(path
->ptr
, ceiling
) != 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
)
409 futils__error_cannot_rmdir(path
->ptr
, "cannot remove parent");
414 static int futils__rmdir_recurs_foreach(void *opaque
, git_buf
*path
)
417 futils__rmdir_data
*data
= opaque
;
419 if ((data
->error
= p_lstat_posixly(path
->ptr
, &st
)) < 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
);
427 futils__error_cannot_rmdir(
428 path
->ptr
, "parent is not directory");
431 futils__error_cannot_rmdir(path
->ptr
, "cannot access");
434 else if (S_ISDIR(st
.st_mode
)) {
435 int error
= git_path_direach(path
, futils__rmdir_recurs_foreach
, data
);
437 return (error
== GIT_EUSER
) ? data
->error
: error
;
439 data
->error
= p_rmdir(path
->ptr
);
441 if (data
->error
< 0) {
442 if ((data
->flags
& GIT_RMDIR_SKIP_NONEMPTY
) != 0 &&
443 (errno
== ENOTEMPTY
|| errno
== EEXIST
))
446 futils__error_cannot_rmdir(path
->ptr
, NULL
);
450 else if ((data
->flags
& GIT_RMDIR_REMOVE_FILES
) != 0) {
451 data
->error
= p_unlink(path
->ptr
);
454 futils__error_cannot_rmdir(path
->ptr
, "cannot be removed");
457 else if ((data
->flags
& GIT_RMDIR_SKIP_NONEMPTY
) == 0)
458 data
->error
= futils__error_cannot_rmdir(path
->ptr
, "still present");
463 static int futils__rmdir_empty_parent(void *opaque
, git_buf
*path
)
465 futils__rmdir_data
*data
= opaque
;
468 if (git_buf_len(path
) <= data
->baselen
)
471 error
= p_rmdir(git_buf_cstr(path
));
476 if (en
== ENOENT
|| en
== ENOTDIR
) {
479 } else if (en
== ENOTEMPTY
|| en
== EEXIST
) {
481 error
= GIT_ITEROVER
;
483 futils__error_cannot_rmdir(git_buf_cstr(path
), NULL
);
490 int git_futils_rmdir_r(
491 const char *path
, const char *base
, uint32_t flags
)
494 git_buf fullpath
= GIT_BUF_INIT
;
495 futils__rmdir_data data
;
497 /* build path and find "root" where we should start calling mkdir */
498 if (git_path_join_unrooted(&fullpath
, path
, base
, NULL
) < 0)
501 data
.base
= base
? base
: "";
502 data
.baselen
= base
? strlen(base
) : 0;
506 error
= futils__rmdir_recurs_foreach(&data
, &fullpath
);
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
);
513 if (error
== GIT_ITEROVER
)
517 git_buf_free(&fullpath
);
522 int git_futils_find_system_file(git_buf
*path
, const char *filename
)
525 // try to find git.exe/git.cmd on path
526 if (!win32_find_system_file_using_path(path
, filename
))
529 // try to find msysgit installation path using registry
530 if (!win32_find_system_file_using_registry(path
, filename
))
533 if (git_buf_joinpath(path
, "/etc", filename
) < 0)
536 if (git_path_exists(path
->ptr
) == true)
541 giterr_set(GITERR_OS
, "The system file '%s' doesn't exist", filename
);
542 return GIT_ENOTFOUND
;
545 int git_futils_find_global_file(git_buf
*path
, const char *filename
)
548 struct win32_path root
;
549 static const wchar_t *tmpls
[4] = {
551 L
"%HOMEDRIVE%%HOMEPATH%\\",
555 const wchar_t **tmpl
;
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
'%')
562 /* try to look up file under path */
563 if (!win32_find_file(path
, &root
, filename
))
567 giterr_set(GITERR_OS
, "The global file '%s' doesn't exist", filename
);
570 return GIT_ENOTFOUND
;
572 const char *home
= getenv("HOME");
575 giterr_set(GITERR_OS
, "Global file lookup failed. "
576 "Cannot locate the user's home directory");
577 return GIT_ENOTFOUND
;
580 if (git_buf_joinpath(path
, home
, filename
) < 0)
583 if (git_path_exists(path
->ptr
) == false) {
584 giterr_set(GITERR_OS
, "The global file '%s' doesn't exist", filename
);
586 return GIT_ENOTFOUND
;
593 int git_futils_fake_symlink(const char *old
, const char *new)
595 int retcode
= GIT_ERROR
;
596 int fd
= git_futils_creat_withpath(new, 0755, 0644);
598 retcode
= p_write(fd
, old
, strlen(old
));
604 static int cp_by_fd(int ifd
, int ofd
, bool close_fd_when_done
)
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.
614 error
= p_write(ofd
, buffer
, len
);
617 giterr_set(GITERR_OS
, "Read error while copying file");
621 if (close_fd_when_done
) {
629 int git_futils_cp(const char *from
, const char *to
, mode_t filemode
)
633 if ((ifd
= git_futils_open_ro(from
)) < 0)
636 if ((ofd
= p_open(to
, O_WRONLY
| O_CREAT
| O_EXCL
, filemode
)) < 0) {
637 if (errno
== ENOENT
|| errno
== ENOTDIR
)
639 giterr_set(GITERR_OS
, "Failed to open '%s' for writing", to
);
644 return cp_by_fd(ifd
, ofd
, true);
647 static int cp_link(const char *from
, const char *to
, size_t link_size
)
651 char *link_data
= git__malloc(link_size
+ 1);
652 GITERR_CHECK_ALLOC(link_data
);
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
);
660 link_data
[read_len
] = '\0';
662 if (p_symlink(link_data
, to
) < 0) {
663 giterr_set(GITERR_OS
, "Could not symlink '%s' as '%s'",
669 git__free(link_data
);
678 uint32_t mkdir_flags
;
682 static int _cp_r_callback(void *ref
, git_buf
*from
)
684 cp_r_info
*info
= ref
;
685 struct stat from_st
, to_st
;
688 if ((info
->flags
& GIT_CPDIR_COPY_DOTFILES
) == 0 &&
689 from
->ptr
[git_path_basename_offset(from
)] == '.')
692 if (git_buf_joinpath(
693 &info
->to
, info
->to_root
, from
->ptr
+ info
->from_prefix
) < 0)
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
);
705 if (git_path_lstat(from
->ptr
, &from_st
) < 0)
708 if (S_ISDIR(from_st
.st_mode
)) {
710 mode_t oldmode
= info
->dirmode
;
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
;
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
);
721 /* recurse onto target directory */
722 if (!exists
|| S_ISDIR(to_st
.st_mode
))
723 error
= git_path_direach(from
, _cp_r_callback
, info
);
726 info
->dirmode
= oldmode
;
732 if ((info
->flags
& GIT_CPDIR_OVERWRITE
) == 0)
735 if (p_unlink(info
->to
.ptr
) < 0) {
736 giterr_set(GITERR_OS
, "Cannot overwrite existing file '%s'",
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))
748 /* Make container directory on demand if needed */
749 if ((info
->flags
& GIT_CPDIR_CREATE_EMPTY_DIRS
) == 0 &&
751 info
->to
.ptr
, NULL
, info
->dirmode
, info
->mkdir_flags
) < 0)
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
);
758 return git_futils_cp(from
->ptr
, info
->to
.ptr
, from_st
.st_mode
);
768 git_buf path
= GIT_BUF_INIT
;
771 if (git_buf_sets(&path
, from
) < 0)
776 info
.dirmode
= dirmode
;
777 info
.from_prefix
= path
.size
;
778 git_buf_init(&info
.to
, 0);
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
;
787 ((flags
& GIT_CPDIR_CHMOD
) != 0) ? GIT_MKDIR_CHMOD
: 0;
790 error
= _cp_r_callback(&info
, &path
);
793 git_buf_free(&info
.to
);
798 int git_futils_filestamp_check(
799 git_futils_filestamp
*stamp
, const char *path
)
803 /* if the stamp is NULL, then always reload */
807 if (p_stat(path
, &st
) < 0)
808 return GIT_ENOTFOUND
;
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
)
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
;
822 void git_futils_filestamp_set(
823 git_futils_filestamp
*target
, const git_futils_filestamp
*source
)
828 memcpy(target
, source
, sizeof(*target
));
830 memset(target
, 0, sizeof(*target
));