Skip to content

Commit 12fcb4f

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 e301774 commit 12fcb4f

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
@@ -624,16 +624,60 @@ int mingw_lstat(const char *file_name, struct stat *buf)
624624
{
625625
return do_lstat(0, file_name, buf);
626626
}
627+
628+
static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
629+
{
630+
BY_HANDLE_FILE_INFORMATION fdata;
631+
if (!GetFileInformationByHandle(hnd, &fdata)) {
632+
errno = err_win_to_posix(GetLastError());
633+
return -1;
634+
}
635+
buf->st_ino = 0;
636+
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
637+
buf->st_gid = buf->st_uid = 0;
638+
buf->st_nlink = 1;
639+
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes);
640+
buf->st_size = fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
641+
buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime));
642+
buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime));
643+
buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime));
644+
return 0;
645+
}
646+
627647
int mingw_stat(const char *file_name, struct stat *buf)
628648
{
629-
return do_lstat(1, file_name, buf);
649+
wchar_t wfile_name[MAX_LONG_PATH];
650+
HANDLE hnd;
651+
int result;
652+
653+
/* if symlinks are disabled, use lstat() (without following links) */
654+
if (!has_symlinks) {
655+
result = lstat(file_name, buf);
656+
if (!result && S_ISLNK(buf->st_mode)) {
657+
errno = ELOOP;
658+
return -1;
659+
}
660+
return result;
661+
}
662+
663+
/* otherwise just open the file and let Windows resolve the links */
664+
if (xutftowcs_long_path(wfile_name, file_name) < 0)
665+
return -1;
666+
hnd = CreateFileW(wfile_name, 0,
667+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
668+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
669+
if (hnd == INVALID_HANDLE_VALUE) {
670+
errno = err_win_to_posix(GetLastError());
671+
return -1;
672+
}
673+
result = get_file_info_by_handle(hnd, buf);
674+
CloseHandle(hnd);
675+
return result;
630676
}
631677

632678
int mingw_fstat(int fd, struct stat *buf)
633679
{
634680
HANDLE fh = (HANDLE)_get_osfhandle(fd);
635-
BY_HANDLE_FILE_INFORMATION fdata;
636-
637681
if (fh == INVALID_HANDLE_VALUE) {
638682
errno = EBADF;
639683
return -1;
@@ -642,20 +686,8 @@ int mingw_fstat(int fd, struct stat *buf)
642686
if (GetFileType(fh) != FILE_TYPE_DISK)
643687
return _fstati64(fd, buf);
644688

645-
if (GetFileInformationByHandle(fh, &fdata)) {
646-
buf->st_ino = 0;
647-
buf->st_gid = 0;
648-
buf->st_uid = 0;
649-
buf->st_nlink = 1;
650-
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes);
651-
buf->st_size = fdata.nFileSizeLow |
652-
(((off_t)fdata.nFileSizeHigh)<<32);
653-
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
654-
buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime));
655-
buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime));
656-
buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime));
689+
if (!get_file_info_by_handle(fh, buf))
657690
return 0;
658-
}
659691
errno = EBADF;
660692
return -1;
661693
}

0 commit comments

Comments
 (0)