Skip to content

Commit 444dc90

Browse files
dmpotspearce
authored andcommitted
mingw: move common functionality to win32.h
Some small Win32 specific functions will be shared by MinGW and Cygwin compatibility layer. Place them into a separate header. Signed-off-by: Dmitry Potapov <[email protected]> Signed-off-by: Shawn O. Pearce <[email protected]>
1 parent d2b0708 commit 444dc90

File tree

2 files changed

+38
-38
lines changed

2 files changed

+38
-38
lines changed

compat/mingw.c

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "../git-compat-util.h"
2+
#include "win32.h"
23
#include "../strbuf.h"
34

45
unsigned int _CRT_fmode = _O_BINARY;
@@ -39,46 +40,19 @@ static int do_lstat(const char *file_name, struct stat *buf)
3940
{
4041
WIN32_FILE_ATTRIBUTE_DATA fdata;
4142

42-
if (GetFileAttributesExA(file_name, GetFileExInfoStandard, &fdata)) {
43-
int fMode = S_IREAD;
44-
if (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
45-
fMode |= S_IFDIR;
46-
else
47-
fMode |= S_IFREG;
48-
if (!(fdata.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
49-
fMode |= S_IWRITE;
50-
43+
if (!(errno = get_file_attr(file_name, &fdata))) {
5144
buf->st_ino = 0;
5245
buf->st_gid = 0;
5346
buf->st_uid = 0;
5447
buf->st_nlink = 1;
55-
buf->st_mode = fMode;
48+
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes);
5649
buf->st_size = fdata.nFileSizeLow; /* Can't use nFileSizeHigh, since it's not a stat64 */
5750
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
5851
buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime));
5952
buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime));
6053
buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime));
61-
errno = 0;
6254
return 0;
6355
}
64-
65-
switch (GetLastError()) {
66-
case ERROR_ACCESS_DENIED:
67-
case ERROR_SHARING_VIOLATION:
68-
case ERROR_LOCK_VIOLATION:
69-
case ERROR_SHARING_BUFFER_EXCEEDED:
70-
errno = EACCES;
71-
break;
72-
case ERROR_BUFFER_OVERFLOW:
73-
errno = ENAMETOOLONG;
74-
break;
75-
case ERROR_NOT_ENOUGH_MEMORY:
76-
errno = ENOMEM;
77-
break;
78-
default:
79-
errno = ENOENT;
80-
break;
81-
}
8256
return -1;
8357
}
8458

@@ -130,19 +104,11 @@ int mingw_fstat(int fd, struct stat *buf)
130104
return fstat(fd, buf);
131105

132106
if (GetFileInformationByHandle(fh, &fdata)) {
133-
int fMode = S_IREAD;
134-
if (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
135-
fMode |= S_IFDIR;
136-
else
137-
fMode |= S_IFREG;
138-
if (!(fdata.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
139-
fMode |= S_IWRITE;
140-
141107
buf->st_ino = 0;
142108
buf->st_gid = 0;
143109
buf->st_uid = 0;
144110
buf->st_nlink = 1;
145-
buf->st_mode = fMode;
111+
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes);
146112
buf->st_size = fdata.nFileSizeLow; /* Can't use nFileSizeHigh, since it's not a stat64 */
147113
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
148114
buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime));

compat/win32.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* common Win32 functions for MinGW and Cygwin */
2+
#include <windows.h>
3+
4+
static inline int file_attr_to_st_mode (DWORD attr)
5+
{
6+
int fMode = S_IREAD;
7+
if (attr & FILE_ATTRIBUTE_DIRECTORY)
8+
fMode |= S_IFDIR;
9+
else
10+
fMode |= S_IFREG;
11+
if (!(attr & FILE_ATTRIBUTE_READONLY))
12+
fMode |= S_IWRITE;
13+
return fMode;
14+
}
15+
16+
static inline int get_file_attr(const char *fname, WIN32_FILE_ATTRIBUTE_DATA *fdata)
17+
{
18+
if (GetFileAttributesExA(fname, GetFileExInfoStandard, fdata))
19+
return 0;
20+
21+
switch (GetLastError()) {
22+
case ERROR_ACCESS_DENIED:
23+
case ERROR_SHARING_VIOLATION:
24+
case ERROR_LOCK_VIOLATION:
25+
case ERROR_SHARING_BUFFER_EXCEEDED:
26+
return EACCES;
27+
case ERROR_BUFFER_OVERFLOW:
28+
return ENAMETOOLONG;
29+
case ERROR_NOT_ENOUGH_MEMORY:
30+
return ENOMEM;
31+
default:
32+
return ENOENT;
33+
}
34+
}

0 commit comments

Comments
 (0)