]> asedeno.scripts.mit.edu Git - git.git/blob - compat/mingw.c
Windows: Work around misbehaved rename().
[git.git] / compat / mingw.c
1 #include "../git-compat-util.h"
2
3 unsigned int _CRT_fmode = _O_BINARY;
4
5 #undef open
6 int mingw_open (const char *filename, int oflags, ...)
7 {
8         va_list args;
9         unsigned mode;
10         va_start(args, oflags);
11         mode = va_arg(args, int);
12         va_end(args);
13
14         if (!strcmp(filename, "/dev/null"))
15                 filename = "nul";
16         int fd = open(filename, oflags, mode);
17         if (fd < 0 && (oflags & O_CREAT) && errno == EACCES) {
18                 DWORD attrs = GetFileAttributes(filename);
19                 if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY))
20                         errno = EISDIR;
21         }
22         return fd;
23 }
24
25 unsigned int sleep (unsigned int seconds)
26 {
27         Sleep(seconds*1000);
28         return 0;
29 }
30
31 int mkstemp(char *template)
32 {
33         char *filename = mktemp(template);
34         if (filename == NULL)
35                 return -1;
36         return open(filename, O_RDWR | O_CREAT, 0600);
37 }
38
39 int gettimeofday(struct timeval *tv, void *tz)
40 {
41         return -1;
42 }
43
44 int poll(struct pollfd *ufds, unsigned int nfds, int timeout)
45 {
46         return -1;
47 }
48
49 struct tm *gmtime_r(const time_t *timep, struct tm *result)
50 {
51         /* gmtime() in MSVCRT.DLL is thread-safe, but not reentrant */
52         memcpy(result, gmtime(timep), sizeof(struct tm));
53         return result;
54 }
55
56 struct tm *localtime_r(const time_t *timep, struct tm *result)
57 {
58         /* localtime() in MSVCRT.DLL is thread-safe, but not reentrant */
59         memcpy(result, localtime(timep), sizeof(struct tm));
60         return result;
61 }
62
63 #undef getcwd
64 char *mingw_getcwd(char *pointer, int len)
65 {
66         int i;
67         char *ret = getcwd(pointer, len);
68         if (!ret)
69                 return ret;
70         for (i = 0; pointer[i]; i++)
71                 if (pointer[i] == '\\')
72                         pointer[i] = '/';
73         return ret;
74 }
75
76 #undef rename
77 int mingw_rename(const char *pold, const char *pnew)
78 {
79         /*
80          * Try native rename() first to get errno right.
81          * It is based on MoveFile(), which cannot overwrite existing files.
82          */
83         if (!rename(pold, pnew))
84                 return 0;
85         if (errno != EEXIST)
86                 return -1;
87         if (MoveFileEx(pold, pnew, MOVEFILE_REPLACE_EXISTING))
88                 return 0;
89         /* TODO: translate more errors */
90         if (GetLastError() == ERROR_ACCESS_DENIED) {
91                 DWORD attrs = GetFileAttributes(pnew);
92                 if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY)) {
93                         errno = EISDIR;
94                         return -1;
95                 }
96         }
97         errno = EACCES;
98         return -1;
99 }
100
101 struct passwd *getpwuid(int uid)
102 {
103         static char user_name[100];
104         static struct passwd p;
105
106         DWORD len = sizeof(user_name);
107         if (!GetUserName(user_name, &len))
108                 return NULL;
109         p.pw_name = user_name;
110         p.pw_gecos = "unknown";
111         p.pw_dir = NULL;
112         return &p;
113 }
114
115 int setitimer(int type, struct itimerval *in, struct itimerval *out)
116 {
117         return -1;
118 }
119
120 int sigaction(int sig, struct sigaction *in, struct sigaction *out)
121 {
122         return -1;
123 }