2 * Copyright (C) 2009-2012 the libgit2 contributors
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
)
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 if (errno
== EEXIST
) {
304 if (!lastch
&& (flags
& GIT_MKDIR_VERIFY_DIR
) != 0) {
305 if (!git_path_isdir(make_path
.ptr
)) {
307 GITERR_OS
, "Existing path is not a directory '%s'",
309 error
= GIT_ENOTFOUND
;
313 if ((flags
& GIT_MKDIR_EXCL
) != 0) {
314 giterr_set(GITERR_OS
, "Directory already exists '%s'",
320 giterr_set(GITERR_OS
, "Failed to make directory '%s'",
326 /* chmod if requested */
327 if ((flags
& GIT_MKDIR_CHMOD_PATH
) != 0 ||
328 ((flags
& GIT_MKDIR_CHMOD
) != 0 && lastch
== '\0'))
330 if (p_chmod(make_path
.ptr
, mode
) < 0) {
331 giterr_set(GITERR_OS
, "Failed to set permissions on '%s'",
340 git_buf_free(&make_path
);
344 git_buf_free(&make_path
);
348 int git_futils_mkdir_r(const char *path
, const char *base
, const mode_t mode
)
350 return git_futils_mkdir(path
, base
, mode
, GIT_MKDIR_PATH
);
358 } futils__rmdir_data
;
360 static int futils__error_cannot_rmdir(const char *path
, const char *filemsg
)
363 giterr_set(GITERR_OS
, "Could not remove directory. File '%s' %s",
366 giterr_set(GITERR_OS
, "Could not remove directory '%s'", path
);
371 static int futils__rm_first_parent(git_buf
*path
, const char *ceiling
)
373 int error
= GIT_ENOTFOUND
;
376 while (error
== GIT_ENOTFOUND
) {
377 git_buf_rtruncate_at_char(path
, '/');
379 if (!path
->size
|| git__prefixcmp(path
->ptr
, ceiling
) != 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
)
391 futils__error_cannot_rmdir(path
->ptr
, "cannot remove parent");
396 static int futils__rmdir_recurs_foreach(void *opaque
, git_buf
*path
)
399 futils__rmdir_data
*data
= opaque
;
401 if ((data
->error
= p_lstat_posixly(path
->ptr
, &st
)) < 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
);
409 futils__error_cannot_rmdir(
410 path
->ptr
, "parent is not directory");
413 futils__error_cannot_rmdir(path
->ptr
, "cannot access");
416 else if (S_ISDIR(st
.st_mode
)) {
417 int error
= git_path_direach(path
, futils__rmdir_recurs_foreach
, data
);
419 return (error
== GIT_EUSER
) ? data
->error
: error
;
421 data
->error
= p_rmdir(path
->ptr
);
423 if (data
->error
< 0) {
424 if ((data
->flags
& GIT_RMDIR_SKIP_NONEMPTY
) != 0 &&
425 (errno
== ENOTEMPTY
|| errno
== EEXIST
))
428 futils__error_cannot_rmdir(path
->ptr
, NULL
);
432 else if ((data
->flags
& GIT_RMDIR_REMOVE_FILES
) != 0) {
433 data
->error
= p_unlink(path
->ptr
);
436 futils__error_cannot_rmdir(path
->ptr
, "cannot be removed");
439 else if ((data
->flags
& GIT_RMDIR_SKIP_NONEMPTY
) == 0)
440 data
->error
= futils__error_cannot_rmdir(path
->ptr
, "still present");
445 static int futils__rmdir_empty_parent(void *opaque
, git_buf
*path
)
447 futils__rmdir_data
*data
= opaque
;
450 if (git_buf_len(path
) <= data
->baselen
)
453 error
= p_rmdir(git_buf_cstr(path
));
458 if (en
== ENOENT
|| en
== ENOTDIR
) {
461 } else if (en
== ENOTEMPTY
|| en
== EEXIST
) {
463 error
= GIT_ITEROVER
;
465 futils__error_cannot_rmdir(git_buf_cstr(path
), NULL
);
472 int git_futils_rmdir_r(
473 const char *path
, const char *base
, uint32_t flags
)
476 git_buf fullpath
= GIT_BUF_INIT
;
477 futils__rmdir_data data
;
479 /* build path and find "root" where we should start calling mkdir */
480 if (git_path_join_unrooted(&fullpath
, path
, base
, NULL
) < 0)
483 data
.base
= base
? base
: "";
484 data
.baselen
= base
? strlen(base
) : 0;
488 error
= futils__rmdir_recurs_foreach(&data
, &fullpath
);
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
);
495 if (error
== GIT_ITEROVER
)
499 git_buf_free(&fullpath
);
504 int git_futils_find_system_file(git_buf
*path
, const char *filename
)
507 // try to find git.exe/git.cmd on path
508 if (!win32_find_system_file_using_path(path
, filename
))
511 // try to find msysgit installation path using registry
512 if (!win32_find_system_file_using_registry(path
, filename
))
515 if (git_buf_joinpath(path
, "/etc", filename
) < 0)
518 if (git_path_exists(path
->ptr
) == true)
523 giterr_set(GITERR_OS
, "The system file '%s' doesn't exist", filename
);
524 return GIT_ENOTFOUND
;
527 int git_futils_find_global_file(git_buf
*path
, const char *filename
)
530 struct win32_path root
;
531 static const wchar_t *tmpls
[4] = {
533 L
"%HOMEDRIVE%%HOMEPATH%\\",
537 const wchar_t **tmpl
;
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
'%')
544 /* try to look up file under path */
545 if (!win32_find_file(path
, &root
, filename
))
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.
555 giterr_set(GITERR_OS
, "The global file '%s' doesn't exist", filename
);
558 return GIT_ENOTFOUND
;
560 const char *home
= getenv("HOME");
563 giterr_set(GITERR_OS
, "Global file lookup failed. "
564 "Cannot locate the user's home directory");
565 return GIT_ENOTFOUND
;
568 if (git_buf_joinpath(path
, home
, filename
) < 0)
571 if (git_path_exists(path
->ptr
) == false) {
572 giterr_set(GITERR_OS
, "The global file '%s' doesn't exist", filename
);
574 return GIT_ENOTFOUND
;
581 int git_futils_fake_symlink(const char *old
, const char *new)
583 int retcode
= GIT_ERROR
;
584 int fd
= git_futils_creat_withpath(new, 0755, 0644);
586 retcode
= p_write(fd
, old
, strlen(old
));
592 static int cp_by_fd(int ifd
, int ofd
, bool close_fd_when_done
)
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.
602 error
= p_write(ofd
, buffer
, len
);
605 giterr_set(GITERR_OS
, "Read error while copying file");
609 if (close_fd_when_done
) {
617 int git_futils_cp(const char *from
, const char *to
, mode_t filemode
)
621 if ((ifd
= git_futils_open_ro(from
)) < 0)
624 if ((ofd
= p_open(to
, O_WRONLY
| O_CREAT
| O_EXCL
, filemode
)) < 0) {
625 if (errno
== ENOENT
|| errno
== ENOTDIR
)
627 giterr_set(GITERR_OS
, "Failed to open '%s' for writing", to
);
632 return cp_by_fd(ifd
, ofd
, true);
635 static int cp_link(const char *from
, const char *to
, size_t link_size
)
639 char *link_data
= git__malloc(link_size
+ 1);
640 GITERR_CHECK_ALLOC(link_data
);
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
);
648 link_data
[read_len
] = '\0';
650 if (p_symlink(link_data
, to
) < 0) {
651 giterr_set(GITERR_OS
, "Could not symlink '%s' as '%s'",
657 git__free(link_data
);
666 uint32_t mkdir_flags
;
670 static int _cp_r_callback(void *ref
, git_buf
*from
)
672 cp_r_info
*info
= ref
;
673 struct stat from_st
, to_st
;
676 if ((info
->flags
& GIT_CPDIR_COPY_DOTFILES
) == 0 &&
677 from
->ptr
[git_path_basename_offset(from
)] == '.')
680 if (git_buf_joinpath(
681 &info
->to
, info
->to_root
, from
->ptr
+ info
->from_prefix
) < 0)
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
);
693 if (git_path_lstat(from
->ptr
, &from_st
) < 0)
696 if (S_ISDIR(from_st
.st_mode
)) {
698 mode_t oldmode
= info
->dirmode
;
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
;
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
);
709 /* recurse onto target directory */
710 if (!exists
|| S_ISDIR(to_st
.st_mode
))
711 error
= git_path_direach(from
, _cp_r_callback
, info
);
714 info
->dirmode
= oldmode
;
720 if ((info
->flags
& GIT_CPDIR_OVERWRITE
) == 0)
723 if (p_unlink(info
->to
.ptr
) < 0) {
724 giterr_set(GITERR_OS
, "Cannot overwrite existing file '%s'",
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))
736 /* Make container directory on demand if needed */
737 if ((info
->flags
& GIT_CPDIR_CREATE_EMPTY_DIRS
) == 0 &&
739 info
->to
.ptr
, NULL
, info
->dirmode
, info
->mkdir_flags
) < 0)
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
);
746 return git_futils_cp(from
->ptr
, info
->to
.ptr
, from_st
.st_mode
);
756 git_buf path
= GIT_BUF_INIT
;
759 if (git_buf_sets(&path
, from
) < 0)
764 info
.dirmode
= dirmode
;
765 info
.from_prefix
= path
.size
;
766 git_buf_init(&info
.to
, 0);
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
;
775 ((flags
& GIT_CPDIR_CHMOD
) != 0) ? GIT_MKDIR_CHMOD
: 0;
778 error
= _cp_r_callback(&info
, &path
);
781 git_buf_free(&info
.to
);
786 int git_futils_filestamp_check(
787 git_futils_filestamp
*stamp
, const char *path
)
791 /* if the stamp is NULL, then always reload */
795 if (p_stat(path
, &st
) < 0)
796 return GIT_ENOTFOUND
;
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
)
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
;
810 void git_futils_filestamp_set(
811 git_futils_filestamp
*target
, const git_futils_filestamp
*source
)
816 memcpy(target
, source
, sizeof(*target
));
818 memset(target
, 0, sizeof(*target
));