8
8
#include " httpdef.h" // import http_content_type_str_by_suffix
9
9
#include " http_page.h" // import make_index_of_page
10
10
11
- #ifdef _MSC_VER
11
+ #ifdef OS_WIN
12
12
#include < codecvt>
13
13
#endif
14
14
@@ -19,27 +19,27 @@ FileCache::FileCache() {
19
19
expired_time = 60 ; // s
20
20
}
21
21
22
- static int hv_open (char const * filepath, int flags) {
23
- #ifdef _MSC_VER
24
- std::wstring_convert<std::codecvt_utf8_utf16<wchar_t >> conv;
25
- auto wfilepath = conv.from_bytes (filepath);
26
- int fd = _wopen (wfilepath.c_str (), flags);
27
- #else
28
- int fd = open (filepath, flags);
29
- #endif
30
- return fd;
31
- }
32
-
33
22
file_cache_ptr FileCache::Open (const char * filepath, OpenParam* param) {
34
23
std::lock_guard<std::mutex> locker (mutex_);
35
24
file_cache_ptr fc = Get (filepath);
25
+ #ifdef OS_WIN
26
+ std::wstring_convert<std::codecvt_utf8_utf16<wchar_t >> conv;
27
+ std::wstring wfilepath;
28
+ #endif
36
29
bool modified = false ;
37
30
if (fc) {
38
31
time_t now = time (NULL );
39
32
if (now - fc->stat_time > stat_interval) {
40
- modified = fc->is_modified ();
41
33
fc->stat_time = now;
42
34
fc->stat_cnt ++;
35
+ #ifdef OS_WIN
36
+ wfilepath = conv.from_bytes (filepath);
37
+ now = fc->st .st_mtime ;
38
+ _wstat (wfilepath.c_str (), (struct _stat *)&fc->st );
39
+ modified = now != fc->st .st_mtime ;
40
+ #else
41
+ modified = fc->is_modified ();
42
+ #endif
43
43
}
44
44
if (param->need_read ) {
45
45
if (!modified && fc->is_complete ()) {
@@ -48,31 +48,35 @@ file_cache_ptr FileCache::Open(const char* filepath, OpenParam* param) {
48
48
}
49
49
}
50
50
if (fc == NULL || modified || param->need_read ) {
51
+ struct stat st;
51
52
int flags = O_RDONLY;
52
53
#ifdef O_BINARY
53
54
flags |= O_BINARY;
54
55
#endif
55
- int fd = hv_open (filepath, flags);
56
- if (fd < 0 ) {
56
+ int fd = -1 ;
57
57
#ifdef OS_WIN
58
- // NOTE: open(dir) return -1 on windows
59
- if (!hv_isdir (filepath)) {
60
- param->error = ERR_OPEN_FILE;
61
- return NULL ;
62
- }
63
- #else
58
+ if (wfilepath.empty ()) wfilepath = conv.from_bytes (filepath);
59
+ // NOTE: Optimize Windows Not Found request performance
60
+ if (_wstat (wfilepath.c_str (), (struct _stat *)&st) != 0 ) {
64
61
param->error = ERR_OPEN_FILE;
65
62
return NULL ;
63
+ }
64
+ // NOTE: open(dir) return -1 on windows
65
+ if (S_ISREG (st.st_mode )) {
66
+ fd = _wopen (wfilepath.c_str (), flags);
67
+ }else if (S_ISDIR (st.st_mode )) {
68
+ fd = 0 ;
69
+ }
70
+ #else
71
+ fd = open (filepath, flags);
66
72
#endif
73
+ if (fd < 0 ) {
74
+ param->error = ERR_OPEN_FILE;
75
+ return NULL ;
67
76
}
68
77
defer (if (fd > 0 ) { close (fd); })
69
78
if (fc == NULL ) {
70
- struct stat st;
71
- if (fd > 0 ) {
72
- fstat (fd, &st);
73
- } else {
74
- stat (filepath, &st);
75
- }
79
+ if (fd > 0 ) fstat (fd, &st);
76
80
if (S_ISREG (st.st_mode ) ||
77
81
(S_ISDIR (st.st_mode ) &&
78
82
filepath[strlen (filepath)-1 ] == ' /' )) {
0 commit comments