Merge branch 'maint-1.5.4' into maint-1.5.5
[git.git] / compat / fopen.c
1 #include "../git-compat-util.h"
2 #undef fopen
3 FILE *git_fopen(const char *path, const char *mode)
4 {
5         FILE *fp;
6         struct stat st;
7
8         if (mode[0] == 'w' || mode[0] == 'a')
9                 return fopen(path, mode);
10
11         if (!(fp = fopen(path, mode)))
12                 return NULL;
13
14         if (fstat(fileno(fp), &st)) {
15                 fclose(fp);
16                 return NULL;
17         }
18
19         if (S_ISDIR(st.st_mode)) {
20                 fclose(fp);
21                 errno = EISDIR;
22                 return NULL;
23         }
24
25         return fp;
26 }