FileCache.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include "FileCache.h"
  2. #include "herr.h"
  3. #include "hscope.h"
  4. #include "htime.h"
  5. #include "hlog.h"
  6. #include "httpdef.h" // import http_content_type_str_by_suffix
  7. #include "http_page.h" // import make_index_of_page
  8. #ifdef OS_WIN
  9. #include "hstring.h" // import hv::utf8_to_wchar
  10. #endif
  11. #define ETAG_FMT "\"%zx-%zx\""
  12. FileCache::FileCache() {
  13. stat_interval = 10; // s
  14. expired_time = 60; // s
  15. }
  16. file_cache_ptr FileCache::Open(const char* filepath, OpenParam* param) {
  17. std::lock_guard<std::mutex> locker(mutex_);
  18. file_cache_ptr fc = Get(filepath);
  19. #ifdef OS_WIN
  20. std::wstring wfilepath;
  21. #endif
  22. bool modified = false;
  23. if (fc) {
  24. time_t now = time(NULL);
  25. if (now - fc->stat_time > stat_interval) {
  26. fc->stat_time = now;
  27. fc->stat_cnt++;
  28. #ifdef OS_WIN
  29. wfilepath = hv::utf8_to_wchar(filepath);
  30. now = fc->st.st_mtime;
  31. _wstat(wfilepath.c_str(), (struct _stat*)&fc->st);
  32. modified = now != fc->st.st_mtime;
  33. #else
  34. modified = fc->is_modified();
  35. #endif
  36. }
  37. if (param->need_read) {
  38. if (!modified && fc->is_complete()) {
  39. param->need_read = false;
  40. }
  41. }
  42. }
  43. if (fc == NULL || modified || param->need_read) {
  44. struct stat st;
  45. int flags = O_RDONLY;
  46. #ifdef O_BINARY
  47. flags |= O_BINARY;
  48. #endif
  49. int fd = -1;
  50. #ifdef OS_WIN
  51. if(wfilepath.empty()) wfilepath = hv::utf8_to_wchar(filepath);
  52. if(_wstat(wfilepath.c_str(), (struct _stat*)&st) != 0) {
  53. param->error = ERR_OPEN_FILE;
  54. return NULL;
  55. }
  56. if(S_ISREG(st.st_mode)) {
  57. fd = _wopen(wfilepath.c_str(), flags);
  58. }else if (S_ISDIR(st.st_mode)) {
  59. // NOTE: open(dir) return -1 on windows
  60. fd = 0;
  61. }
  62. #else
  63. if(stat(filepath, &st) != 0) {
  64. param->error = ERR_OPEN_FILE;
  65. return NULL;
  66. }
  67. fd = open(filepath, flags);
  68. #endif
  69. if (fd < 0) {
  70. param->error = ERR_OPEN_FILE;
  71. return NULL;
  72. }
  73. defer(if (fd > 0) { close(fd); })
  74. if (fc == NULL) {
  75. if (S_ISREG(st.st_mode) ||
  76. (S_ISDIR(st.st_mode) &&
  77. filepath[strlen(filepath)-1] == '/')) {
  78. fc = std::make_shared<file_cache_t>();
  79. fc->filepath = filepath;
  80. fc->st = st;
  81. time(&fc->open_time);
  82. fc->stat_time = fc->open_time;
  83. fc->stat_cnt = 1;
  84. cached_files[filepath] = fc;
  85. }
  86. else {
  87. param->error = ERR_MISMATCH;
  88. return NULL;
  89. }
  90. }
  91. if (S_ISREG(fc->st.st_mode)) {
  92. param->filesize = fc->st.st_size;
  93. // FILE
  94. if (param->need_read) {
  95. if (fc->st.st_size > param->max_read) {
  96. param->error = ERR_OVER_LIMIT;
  97. return NULL;
  98. }
  99. fc->resize_buf(fc->st.st_size);
  100. int nread = read(fd, fc->filebuf.base, fc->filebuf.len);
  101. if (nread != fc->filebuf.len) {
  102. hloge("Failed to read file: %s", filepath);
  103. param->error = ERR_READ_FILE;
  104. return NULL;
  105. }
  106. }
  107. const char* suffix = strrchr(filepath, '.');
  108. if (suffix) {
  109. http_content_type content_type = http_content_type_enum_by_suffix(suffix+1);
  110. if (content_type == TEXT_HTML) {
  111. fc->content_type = "text/html; charset=utf-8";
  112. } else if (content_type == TEXT_PLAIN) {
  113. fc->content_type = "text/plain; charset=utf-8";
  114. } else {
  115. fc->content_type = http_content_type_str_by_suffix(suffix+1);
  116. }
  117. }
  118. }
  119. else if (S_ISDIR(fc->st.st_mode)) {
  120. // DIR
  121. std::string page;
  122. make_index_of_page(filepath, page, param->path);
  123. fc->resize_buf(page.size());
  124. memcpy(fc->filebuf.base, page.c_str(), page.size());
  125. fc->content_type = "text/html; charset=utf-8";
  126. }
  127. gmtime_fmt(fc->st.st_mtime, fc->last_modified);
  128. snprintf(fc->etag, sizeof(fc->etag), ETAG_FMT, (size_t)fc->st.st_mtime, (size_t)fc->st.st_size);
  129. }
  130. return fc;
  131. }
  132. bool FileCache::Close(const char* filepath) {
  133. std::lock_guard<std::mutex> locker(mutex_);
  134. auto iter = cached_files.find(filepath);
  135. if (iter != cached_files.end()) {
  136. iter = cached_files.erase(iter);
  137. return true;
  138. }
  139. return false;
  140. }
  141. bool FileCache::Close(const file_cache_ptr& fc) {
  142. std::lock_guard<std::mutex> locker(mutex_);
  143. auto iter = cached_files.begin();
  144. while (iter != cached_files.end()) {
  145. if (iter->second == fc) {
  146. iter = cached_files.erase(iter);
  147. return true;
  148. } else {
  149. ++iter;
  150. }
  151. }
  152. return false;
  153. }
  154. file_cache_ptr FileCache::Get(const char* filepath) {
  155. auto iter = cached_files.find(filepath);
  156. if (iter != cached_files.end()) {
  157. return iter->second;
  158. }
  159. return NULL;
  160. }
  161. void FileCache::RemoveExpiredFileCache() {
  162. std::lock_guard<std::mutex> locker(mutex_);
  163. time_t now = time(NULL);
  164. auto iter = cached_files.begin();
  165. while (iter != cached_files.end()) {
  166. if (now - iter->second->stat_time > expired_time) {
  167. iter = cached_files.erase(iter);
  168. } else {
  169. ++iter;
  170. }
  171. }
  172. }