Skip to content

Commit 7ec90aa

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 <blees@dcon.de> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 0e4aced commit 7ec90aa

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

compat/mingw.c

+18-1
Original file line numberDiff line numberDiff line change
@@ -771,9 +771,26 @@ int mingw_lstat(const char *file_name, struct stat *buf)
771771
{
772772
return do_lstat(0, file_name, buf);
773773
}
774+
774775
int mingw_stat(const char *file_name, struct stat *buf)
775776
{
776-
return do_lstat(1, file_name, buf);
777+
wchar_t wfile_name[MAX_LONG_PATH];
778+
HANDLE hnd;
779+
int result;
780+
781+
/* open the file and let Windows resolve the links */
782+
if (xutftowcs_long_path(wfile_name, file_name) < 0)
783+
return -1;
784+
hnd = CreateFileW(wfile_name, 0,
785+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
786+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
787+
if (hnd == INVALID_HANDLE_VALUE) {
788+
errno = err_win_to_posix(GetLastError());
789+
return -1;
790+
}
791+
result = get_file_info_by_handle(hnd, buf);
792+
CloseHandle(hnd);
793+
return result;
777794
}
778795

779796
int mingw_fstat(int fd, struct stat *buf)

0 commit comments

Comments
 (0)