Browse code

win32 safe_open() wrappage

aCaB authored on 2009/10/22 00:51:33
Showing 9 changed files
... ...
@@ -1,3 +1,7 @@
1
+Wed Oct 21 17:50:05 CEST 2009 (acab)
2
+------------------------------------
3
+ * win32: introduce safe_open() (sic!)
4
+
1 5
 Tue Oct 20 17:48:59 CEST 2009 (acab)
2 6
 ------------------------------------
3 7
  * shared, win32: make hardcoded paths relocable in win32 builds
... ...
@@ -127,7 +127,7 @@ static int scanfile(const char *filename, struct cl_engine *engine, const struct
127 127
 
128 128
     logg("*Scanning %s\n", filename);
129 129
 
130
-    if((fd = open(filename, O_RDONLY|O_BINARY)) == -1) {
130
+    if((fd = safe_open(filename, O_RDONLY|O_BINARY)) == -1) {
131 131
 	logg("^Can't open file %s: %s\n", filename, strerror(errno));
132 132
 	return 54;
133 133
     }
... ...
@@ -2221,7 +2221,7 @@ static int cli_scanfile(const char *filename, cli_ctx *ctx)
2221 2221
 	int fd, ret;
2222 2222
 
2223 2223
     /* internal version of cl_scanfile with arec/mrec preserved */
2224
-    if((fd = open(filename, O_RDONLY|O_BINARY)) == -1)
2224
+    if((fd = safe_open(filename, O_RDONLY|O_BINARY)) == -1)
2225 2225
 	return CL_EOPEN;
2226 2226
 
2227 2227
     ret = cli_magic_scandesc(fd, ctx);
... ...
@@ -38,3 +38,6 @@ typedef	unsigned	int	in_addr_t;
38 38
 #define CONFDIR_FRESHCLAM CONFDIR PATHSEP "freshclam.conf"
39 39
 #define CONFDIR_MILTER CONFDIR PATHSEP "clamav-milter.conf"
40 40
 
41
+/* Nothing is safe in windows, not even open */
42
+#define safe_open open
43
+
... ...
@@ -19,11 +19,14 @@
19 19
  */
20 20
 
21 21
 #include <errno.h>
22
+#include <sys/types.h>
23
+#include <sys/stat.h>
24
+#include <fcntl.h>
25
+#include <io.h>
22 26
 
23 27
 #include "others.h"
24 28
 #include "shared/misc.h"
25 29
 
26
-
27 30
 wchar_t *uncpath(const char *path) {
28 31
     DWORD len = 0;
29 32
     wchar_t *dest = cli_malloc((PATH_MAX + 1) * sizeof(wchar_t));
... ...
@@ -69,6 +72,26 @@ wchar_t *uncpath(const char *path) {
69 69
     return dest;
70 70
 }
71 71
 
72
+int safe_open(const char *path, int flags, ... ) {
73
+    wchar_t *wpath = uncpath(path);
74
+    int ret;
75
+
76
+    if(!wpath)
77
+	return -1;
78
+
79
+    if(flags & O_CREAT) {
80
+	int mode;
81
+	va_list ap;
82
+	va_start(ap, flags);
83
+	mode = va_arg(ap, int);
84
+	va_end(ap);
85
+	ret = _wopen(wpath, flags, mode);
86
+    } else
87
+	ret = _wopen(wpath, flags);
88
+    free(wpath);
89
+    return ret;
90
+}
91
+
72 92
 
73 93
 w32_stat(const char *path, struct stat *buf) {
74 94
     int len;
... ...
@@ -40,6 +40,7 @@ int w32_stat(const char *path, struct stat *buf);
40 40
 #define stat(path, buf) w32_stat(path, buf)
41 41
 
42 42
 wchar_t *uncpath(const char *path);
43
+int safe_open(const char *path, int flags, ... );
43 44
 
44 45
 #endif
45 46
 
... ...
@@ -239,10 +239,6 @@
239 239
 					>
240 240
 				</File>
241 241
 				<File
242
-					RelativePath=".\compat\resolv.h"
243
-					>
244
-				</File>
245
-				<File
246 242
 					RelativePath=".\compat\strptime.c"
247 243
 					>
248 244
 				</File>
... ...
@@ -39,6 +39,7 @@ EXPORTS have_rar
39 39
 EXPORTS opendir
40 40
 EXPORTS readdir
41 41
 EXPORTS closedir
42
+EXPORTS safe_open
42 43
 EXPORTS snprintf
43 44
 EXPORTS mp_init
44 45
 EXPORTS mp_read_radix
... ...
@@ -799,10 +799,6 @@
799 799
 					RelativePath=".\compat\w32_stat.c"
800 800
 					>
801 801
 				</File>
802
-				<File
803
-					RelativePath=".\compat\w32_stat.h"
804
-					>
805
-				</File>
806 802
 			</Filter>
807 803
 		</Filter>
808 804
 		<Filter