Skip to content

Commit 485eb0b

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 3fc6727 commit 485eb0b

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
@@ -642,16 +642,60 @@ int mingw_lstat(const char *file_name, struct stat *buf)
642642
{
643643
return do_lstat(0, file_name, buf);
644644
}
645+
646+
static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
647+
{
648+
BY_HANDLE_FILE_INFORMATION fdata;
649+
if (!GetFileInformationByHandle(hnd, &fdata)) {
650+
errno = err_win_to_posix(GetLastError());
651+
return -1;
652+
}
653+
buf->st_ino = 0;
654+
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
655+
buf->st_gid = buf->st_uid = 0;
656+
buf->st_nlink = 1;
657+
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes);
658+
buf->st_size = fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
659+
buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime));
660+
buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime));
661+
buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime));
662+
return 0;
663+
}
664+
645665
int mingw_stat(const char *file_name, struct stat *buf)
646666
{
647-
return do_lstat(1, file_name, buf);
667+
wchar_t wfile_name[MAX_LONG_PATH];
668+
HANDLE hnd;
669+
int result;
670+
671+
/* if symlinks are disabled, use lstat() (without following links) */
672+
if (!has_symlinks) {
673+
result = lstat(file_name, buf);
674+
if (!result && S_ISLNK(buf->st_mode)) {
675+
errno = ELOOP;
676+
return -1;
677+
}
678+
return result;
679+
}
680+
681+
/* otherwise just open the file and let Windows resolve the links */
682+
if (xutftowcs_long_path(wfile_name, file_name) < 0)
683+
return -1;
684+
hnd = CreateFileW(wfile_name, 0,
685+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
686+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
687+
if (hnd == INVALID_HANDLE_VALUE) {
688+
errno = err_win_to_posix(GetLastError());
689+
return -1;
690+
}
691+
result = get_file_info_by_handle(hnd, buf);
692+
CloseHandle(hnd);
693+
return result;
648694
}
649695

650696
int mingw_fstat(int fd, struct stat *buf)
651697
{
652698
HANDLE fh = (HANDLE)_get_osfhandle(fd);
653-
BY_HANDLE_FILE_INFORMATION fdata;
654-
655699
if (fh == INVALID_HANDLE_VALUE) {
656700
errno = EBADF;
657701
return -1;
@@ -660,20 +704,8 @@ int mingw_fstat(int fd, struct stat *buf)
660704
if (GetFileType(fh) != FILE_TYPE_DISK)
661705
return _fstati64(fd, buf);
662706

663-
if (GetFileInformationByHandle(fh, &fdata)) {
664-
buf->st_ino = 0;
665-
buf->st_gid = 0;
666-
buf->st_uid = 0;
667-
buf->st_nlink = 1;
668-
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes);
669-
buf->st_size = fdata.nFileSizeLow |
670-
(((off_t)fdata.nFileSizeHigh)<<32);
671-
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
672-
buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime));
673-
buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime));
674-
buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime));
707+
if (!get_file_info_by_handle(fh, buf))
675708
return 0;
676-
}
677709
errno = EBADF;
678710
return -1;
679711
}

0 commit comments

Comments
 (0)