Windows: Use a customized struct stat that also has the st_blocks member.
Windows's struct stat does not have a st_blocks member. Since we already have our own stat/lstat/fstat implementations, we can just as well use a customized struct stat. This patch introduces just that, and also fills in the st_blocks member. On the other hand, we don't provide members that are never used. Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
Johannes Sixt committed
Nov 12, 2007 at 12:52 UTC
fc2ded5b08e071beed974117c0148781b1acc94a
2 files changed
+41
-13
compat/mingw.c
+28
-8
@@ -31,6 +31,11 @@ static inline time_t filetime_to_time_t(const FILETIME *ft)
31
return (time_t)winTime;
32
}
33
34
+static inline size_t size_to_blocks(size_t s)
35
+{
36
+ return (s+511)/512;
37
+}
38
+
39
extern int _getdrive( void );
40
/* We keep the do_lstat code in a separate function to avoid recursion.
41
* When a path ends with a slash, the stat will fail with ENOENT. In
@@ -52,10 +57,10 @@ static int do_lstat(const char *file_name, struct stat *buf)
57
buf->st_ino = 0;
58
buf->st_gid = 0;
59
buf->st_uid = 0;
55
- buf->st_nlink = 1;
60
buf->st_mode = fMode;
61
buf->st_size = fdata.nFileSizeLow; /* Can't use nFileSizeHigh, since it's not a stat64 */
58
- buf->st_dev = buf->st_rdev = (_getdrive() - 1);
62
+ buf->st_blocks = size_to_blocks(buf->st_size);
63
+ buf->st_dev = _getdrive() - 1;
64
buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime));
65
buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime));
66
buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime));
@@ -89,7 +94,7 @@ static int do_lstat(const char *file_name, struct stat *buf)
94
* complete. Note that Git stat()s are redirected to mingw_lstat()
95
* too, since Windows doesn't really handle symlinks that well.
96
*/
92
-int mingw_lstat(const char *file_name, struct stat *buf)
97
+int mingw_lstat(const char *file_name, struct mingw_stat *buf)
98
{
99
int namelen;
100
static char alt_name[PATH_MAX];
@@ -117,7 +122,8 @@ int mingw_lstat(const char *file_name, struct stat *buf)
122
}
123
124
#undef fstat
120
-int mingw_fstat(int fd, struct stat *buf)
125
+#undef stat
126
+int mingw_fstat(int fd, struct mingw_stat *buf)
127
{
128
HANDLE fh = (HANDLE)_get_osfhandle(fd);
129
BY_HANDLE_FILE_INFORMATION fdata;
@@ -127,8 +133,22 @@ int mingw_fstat(int fd, struct stat *buf)
133
return -1;
134
}
135
/* direct non-file handles to MS's fstat() */
130
- if (GetFileType(fh) != FILE_TYPE_DISK)
131
- return fstat(fd, buf);
136
+ if (GetFileType(fh) != FILE_TYPE_DISK) {
137
+ struct stat st;
138
+ if (fstat(fd, &st))
139
+ return -1;
140
+ buf->st_ino = st.st_ino;
141
+ buf->st_gid = st.st_gid;
142
+ buf->st_uid = st.st_uid;
143
+ buf->st_mode = st.st_mode;
144
+ buf->st_size = st.st_size;
145
+ buf->st_blocks = size_to_blocks(buf->st_size);
146
+ buf->st_dev = st.st_dev;
147
+ buf->st_atime = st.st_atime;
148
+ buf->st_mtime = st.st_mtime;
149
+ buf->st_ctime = st.st_ctime;
150
+ return 0;
151
+ }
152
153
if (GetFileInformationByHandle(fh, &fdata)) {
154
int fMode = S_IREAD;
@@ -142,10 +162,10 @@ int mingw_fstat(int fd, struct stat *buf)
162
buf->st_ino = 0;
163
buf->st_gid = 0;
164
buf->st_uid = 0;
145
- buf->st_nlink = 1;
165
buf->st_mode = fMode;
166
buf->st_size = fdata.nFileSizeLow; /* Can't use nFileSizeHigh, since it's not a stat64 */
148
- buf->st_dev = buf->st_rdev = (_getdrive() - 1);
167
+ buf->st_blocks = size_to_blocks(buf->st_size);
168
+ buf->st_dev = _getdrive() - 1;
169
buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime));
170
buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime));
171
buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime));
compat/mingw.h
+13
-5
@@ -59,8 +59,6 @@ struct itimerval {
59
};
60
#define ITIMER_REAL 0
61
62
-#define st_blocks st_size/512 /* will be cleaned up later */
63
-
62
/*
63
* trivial stubs
64
*/
@@ -161,12 +159,22 @@ int mingw_rename(const char*, const char*);
159
160
/* Use mingw_lstat() instead of lstat()/stat() and
161
* mingw_fstat() instead of fstat() on Windows.
162
+ * struct stat is redefined because it lacks the st_blocks member.
163
*/
165
-int mingw_lstat(const char *file_name, struct stat *buf);
166
-int mingw_fstat(int fd, struct stat *buf);
164
+struct mingw_stat {
165
+ unsigned st_mode;
166
+ time_t st_mtime, st_atime, st_ctime;
167
+ unsigned st_dev, st_ino, st_uid, st_gid;
168
+ size_t st_size;
169
+ size_t st_blocks;
170
+};
171
+int mingw_lstat(const char *file_name, struct mingw_stat *buf);
172
+int mingw_fstat(int fd, struct mingw_stat *buf);
173
#define fstat mingw_fstat
174
#define lstat mingw_lstat
169
-#define stat(x,y) mingw_lstat(x,y)
175
+#define stat mingw_stat
176
+static inline int mingw_stat(const char *file_name, struct mingw_stat *buf)
177
+{ return mingw_lstat(file_name, buf); }
178
179
int mingw_utime(const char *file_name, const struct utimbuf *times);
180
#define utime mingw_utime