Browse Source

fix F_OK for windows

hewei.it 4 years ago
parent
commit
1661606ae1
4 changed files with 24 additions and 9 deletions
  1. 4 1
      CMakeLists.txt
  2. 3 3
      base/hbase.c
  3. 3 3
      cpputil/hpath.cpp
  4. 14 2
      http/server/FileCache.cpp

+ 4 - 1
CMakeLists.txt

@@ -212,7 +212,10 @@ endif()
 
 if(BUILD_EXAMPLES)
     add_subdirectory(examples)
-    file(INSTALL etc DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
+    # for httpd -c etc/httpd.conf
+    file(INSTALL etc DESTINATION ${CMAKE_BINARY_DIR})
+    file(INSTALL etc DESTINATION ${CMAKE_BINARY_DIR}/bin)
+    file(INSTALL etc DESTINATION ${CMAKE_BINARY_DIR}/examples)
 endif()
 
 if(BUILD_UNITTEST)

+ 3 - 3
base/hbase.c

@@ -241,11 +241,11 @@ int hv_rmdir_p(const char* dir) {
 }
 
 bool hv_exists(const char* path) {
-    return access(path, F_OK) == 0;
+    return access(path, 0) == 0;
 }
 
 bool hv_isdir(const char* path) {
-    if (access(path, F_OK) != 0) return false;
+    if (access(path, 0) != 0) return false;
     struct stat st;
     memset(&st, 0, sizeof(st));
     stat(path, &st);
@@ -253,7 +253,7 @@ bool hv_isdir(const char* path) {
 }
 
 bool hv_isfile(const char* path) {
-    if (access(path, F_OK) != 0) return false;
+    if (access(path, 0) != 0) return false;
     struct stat st;
     memset(&st, 0, sizeof(st));
     stat(path, &st);

+ 3 - 3
cpputil/hpath.cpp

@@ -1,11 +1,11 @@
 #include "hpath.h"
 
 bool HPath::exists(const char* path) {
-    return access(path, F_OK) == 0;
+    return access(path, 0) == 0;
 }
 
 bool HPath::isdir(const char* path) {
-    if (access(path, F_OK) != 0) return false;
+    if (access(path, 0) != 0) return false;
     struct stat st;
     memset(&st, 0, sizeof(st));
     stat(path, &st);
@@ -13,7 +13,7 @@ bool HPath::isdir(const char* path) {
 }
 
 bool HPath::isfile(const char* path) {
-    if (access(path, F_OK) != 0) return false;
+    if (access(path, 0) != 0) return false;
     struct stat st;
     memset(&st, 0, sizeof(st));
     stat(path, &st);

+ 14 - 2
http/server/FileCache.cpp

@@ -34,13 +34,25 @@ file_cache_ptr FileCache::Open(const char* filepath, OpenParam* param) {
 #endif
         int fd = open(filepath, flags);
         if (fd < 0) {
+#ifdef OS_WIN
+            // NOTE: open(dir) return -1 on windows
+            if (!hv_isdir(filepath)) {
+                param->error = ERR_OPEN_FILE;
+                return NULL;
+            }
+#else
             param->error = ERR_OPEN_FILE;
             return NULL;
+#endif
         }
-        defer(close(fd);)
+        defer(if (fd > 0) { close(fd); })
         if (fc == NULL) {
             struct stat st;
-            fstat(fd, &st);
+            if (fd > 0) {
+                fstat(fd, &st);
+            } else {
+                stat(filepath, &st);
+            }
             if (S_ISREG(st.st_mode) ||
                 (S_ISDIR(st.st_mode) &&
                  filepath[strlen(filepath)-1] == '/')) {