Add compat/fopen.c which returns NULL on attempt to open directory
Some systems do not fail as expected when fread et al. are called on a directory stream. Replace fopen on such systems which will fail when the supplied path is a directory. Signed-off-by: Brandon Casey <casey@nrlssc.navy.mil> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Brandon Casey committed
Feb 8, 2008 at 20:32 UTC
cba22528fa897728ebbffb95c05037ec9a20ea7c
3 files changed
+38
Makefile
+7
@@ -3,6 +3,9 @@ all::
3
4
# Define V=1 to have a more verbose compile.
5
#
6
+# Define FREAD_READS_DIRECTORIES if your are on a system which succeeds
7
+# when attempting to read from an fopen'ed directory.
8
+#
9
# Define NO_OPENSSL environment variable if you do not have OpenSSL.
10
# This also implies MOZILLA_SHA1.
11
#
@@ -618,6 +621,10 @@ endif
621
ifdef NO_C99_FORMAT
622
BASIC_CFLAGS += -DNO_C99_FORMAT
623
endif
624
+ifdef FREAD_READS_DIRECTORIES
625
+ COMPAT_CFLAGS += -DFREAD_READS_DIRECTORIES
626
+ COMPAT_OBJS += compat/fopen.o
627
+endif
628
ifdef NO_SYMLINK_HEAD
629
BASIC_CFLAGS += -DNO_SYMLINK_HEAD
630
endif
compat/fopen.c
new
+26
@@ -0,0 +1,26 @@
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
+}
git-compat-util.h
+5
@@ -204,6 +204,11 @@ void *gitmemmem(const void *haystack, size_t haystacklen,
204
const void *needle, size_t needlelen);
205
#endif
206
207
+#ifdef FREAD_READS_DIRECTORIES
208
+#define fopen(a,b) git_fopen(a,b)
209
+extern FILE *git_fopen(const char*, const char*);
210
+#endif
211
+
212
#ifdef __GLIBC_PREREQ
213
#if __GLIBC_PREREQ(2, 1)
214
#define HAVE_STRCHRNUL