FileCache.cpp 5.3 KB

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