Skip to content

Commit 85240f7

Browse files
kbleesdscho
authored andcommitted
Win32: implement stat() with symlink support
With respect to symlinks, the current stat() implementation is almost the same as lstat(): except for the file type (st_mode & S_IFMT), it returns information about the link rather than the target. Implement stat by opening the file with as little permissions as possible and calling GetFileInformationByHandle on it. This way, all link resoltion is handled by the Windows file system layer. If symlinks are disabled, use lstat() as before, but fail with ELOOP if a symlink would have to be resolved. Signed-off-by: Karsten Blees <[email protected]>
1 parent 1aeb97b commit 85240f7

File tree

1 file changed

+48
-16
lines changed

1 file changed

+48
-16
lines changed

compat/mingw.c

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -607,16 +607,60 @@ int mingw_lstat(const char *file_name, struct stat *buf)
607607
{
608608
return do_lstat(0, file_name, buf);
609609
}
610+
611+
static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
612+
{
613+
BY_HANDLE_FILE_INFORMATION fdata;
614+
if (!GetFileInformationByHandle(hnd, &fdata)) {
615+
errno = err_win_to_posix(GetLastError());
616+
return -1;
617+
}
618+
buf->st_ino = 0;
619+
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
620+
buf->st_gid = buf->st_uid = 0;
621+
buf->st_nlink = 1;
622+
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes);
623+
buf->st_size = fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
624+
buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime));
625+
buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime));
626+
buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime));
627+
return 0;
628+
}
629+
610630
int mingw_stat(const char *file_name, struct stat *buf)
611631
{
612-
return do_lstat(1, file_name, buf);
632+
wchar_t wfile_name[MAX_LONG_PATH];
633+
HANDLE hnd;
634+
int result;
635+
636+
/* if symlinks are disabled, use lstat() (without following links) */
637+
if (!has_symlinks) {
638+
result = lstat(file_name, buf);
639+
if (!result && S_ISLNK(buf->st_mode)) {
640+
errno = ELOOP;
641+
return -1;
642+
}
643+
return result;
644+
}
645+
646+
/* otherwise just open the file and let Windows resolve the links */
647+
if (xutftowcs_long_path(wfile_name, file_name) < 0)
648+
return -1;
649+
hnd = CreateFileW(wfile_name, 0,
650+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
651+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
652+
if (hnd == INVALID_HANDLE_VALUE) {
653+
errno = err_win_to_posix(GetLastError());
654+
return -1;
655+
}
656+
result = get_file_info_by_handle(hnd, buf);
657+
CloseHandle(hnd);
658+
return result;
613659
}
614660

615661
int mingw_fstat(int fd, struct stat *buf)
616662
{
617663
HANDLE fh = (HANDLE)_get_osfhandle(fd);
618-
BY_HANDLE_FILE_INFORMATION fdata;
619-
620664
if (fh == INVALID_HANDLE_VALUE) {
621665
errno = EBADF;
622666
return -1;
@@ -625,20 +669,8 @@ int mingw_fstat(int fd, struct stat *buf)
625669
if (GetFileType(fh) != FILE_TYPE_DISK)
626670
return _fstati64(fd, buf);
627671

628-
if (GetFileInformationByHandle(fh, &fdata)) {
629-
buf->st_ino = 0;
630-
buf->st_gid = 0;
631-
buf->st_uid = 0;
632-
buf->st_nlink = 1;
633-
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes);
634-
buf->st_size = fdata.nFileSizeLow |
635-
(((off_t)fdata.nFileSizeHigh)<<32);
636-
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
637-
buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime));
638-
buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime));
639-
buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime));
672+
if (!get_file_info_by_handle(fh, buf))
640673
return 0;
641-
}
642674
errno = EBADF;
643675
return -1;
644676
}

0 commit comments

Comments
 (0)