Skip to content

Commit 67c66c4

Browse files
committed
mingw: factor out code to set stat() data
In our fstat() emulation, we convert the file metadata from Win32 data structures to an emulated POSIX structure. To structure the code better, let's factor that part out into its own function. Note: it would be tempting to try to unify this code with the part of do_lstat() that does the same thing, but they operate on different data structures: BY_HANDLE_FILE_INFORMATION vs WIN32_FILE_ATTRIBUTE_DATA. So unfortunately, they cannot be unified. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent c35a3ea commit 67c66c4

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

compat/mingw.c

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,29 @@ static int do_stat_internal(int follow, const char *file_name, struct stat *buf)
736736
return do_lstat(follow, alt_name, buf);
737737
}
738738

739+
static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
740+
{
741+
BY_HANDLE_FILE_INFORMATION fdata;
742+
743+
if (!GetFileInformationByHandle(hnd, &fdata)) {
744+
errno = err_win_to_posix(GetLastError());
745+
return -1;
746+
}
747+
748+
buf->st_ino = 0;
749+
buf->st_gid = 0;
750+
buf->st_uid = 0;
751+
buf->st_nlink = 1;
752+
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes);
753+
buf->st_size = fdata.nFileSizeLow |
754+
(((off_t)fdata.nFileSizeHigh)<<32);
755+
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
756+
buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime));
757+
buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime));
758+
buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime));
759+
return 0;
760+
}
761+
739762
int mingw_lstat(const char *file_name, struct stat *buf)
740763
{
741764
return do_stat_internal(0, file_name, buf);
@@ -748,7 +771,6 @@ int mingw_stat(const char *file_name, struct stat *buf)
748771
int mingw_fstat(int fd, struct stat *buf)
749772
{
750773
HANDLE fh = (HANDLE)_get_osfhandle(fd);
751-
BY_HANDLE_FILE_INFORMATION fdata;
752774

753775
if (fh == INVALID_HANDLE_VALUE) {
754776
errno = EBADF;
@@ -758,20 +780,9 @@ int mingw_fstat(int fd, struct stat *buf)
758780
if (GetFileType(fh) != FILE_TYPE_DISK)
759781
return _fstati64(fd, buf);
760782

761-
if (GetFileInformationByHandle(fh, &fdata)) {
762-
buf->st_ino = 0;
763-
buf->st_gid = 0;
764-
buf->st_uid = 0;
765-
buf->st_nlink = 1;
766-
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes);
767-
buf->st_size = fdata.nFileSizeLow |
768-
(((off_t)fdata.nFileSizeHigh)<<32);
769-
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
770-
buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime));
771-
buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime));
772-
buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime));
783+
if (!get_file_info_by_handle(fh, buf))
773784
return 0;
774-
}
785+
775786
errno = EBADF;
776787
return -1;
777788
}

0 commit comments

Comments
 (0)