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 <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Schindelin committed
Oct 23, 2018 at 03:23 UTC
7bf198388645995d72073d331fd2bc00243ffea1
1 file changed
+25
-14
compat/mingw.c
+25
-14
@@ -736,6 +736,29 @@ static int do_stat_internal(int follow, const char *file_name, struct stat *buf)
736
return do_lstat(follow, alt_name, buf);
737
}
738
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
+
762
int mingw_lstat(const char *file_name, struct stat *buf)
763
{
764
return do_stat_internal(0, file_name, buf);
@@ -748,7 +771,6 @@ int mingw_stat(const char *file_name, struct stat *buf)
771
int mingw_fstat(int fd, struct stat *buf)
772
{
773
HANDLE fh = (HANDLE)_get_osfhandle(fd);
751
- BY_HANDLE_FILE_INFORMATION fdata;
774
775
if (fh == INVALID_HANDLE_VALUE) {
776
errno = EBADF;
@@ -758,20 +780,9 @@ int mingw_fstat(int fd, struct stat *buf)
780
if (GetFileType(fh) != FILE_TYPE_DISK)
781
return _fstati64(fd, buf);
782
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))
784
return 0;
774
- }
785
+
786
errno = EBADF;
787
return -1;
788
}