FileCache.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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(size_t capacity) : hv::LRUCache<std::string, file_cache_ptr>(capacity) {
  13. stat_interval = 10; // s
  14. expired_time = 60; // s
  15. }
  16. file_cache_ptr FileCache::Open(const char* filepath, OpenParam* param) {
  17. file_cache_ptr fc = Get(filepath);
  18. #ifdef OS_WIN
  19. std::wstring wfilepath;
  20. #endif
  21. bool modified = false;
  22. if (fc) {
  23. time_t now = time(NULL);
  24. if (now - fc->stat_time > stat_interval) {
  25. fc->stat_time = now;
  26. fc->stat_cnt++;
  27. #ifdef OS_WIN
  28. wfilepath = hv::utf8_to_wchar(filepath);
  29. now = fc->st.st_mtime;
  30. _wstat(wfilepath.c_str(), (struct _stat*)&fc->st);
  31. modified = now != fc->st.st_mtime;
  32. #else
  33. modified = fc->is_modified();
  34. #endif
  35. }
  36. if (param->need_read) {
  37. if (!modified && fc->is_complete()) {
  38. param->need_read = false;
  39. }
  40. }
  41. }
  42. if (fc == NULL || modified || param->need_read) {
  43. struct stat st;
  44. int flags = O_RDONLY;
  45. #ifdef O_BINARY
  46. flags |= O_BINARY;
  47. #endif
  48. int fd = -1;
  49. #ifdef OS_WIN
  50. if(wfilepath.empty()) wfilepath = hv::utf8_to_wchar(filepath);
  51. if(_wstat(wfilepath.c_str(), (struct _stat*)&st) != 0) {
  52. param->error = ERR_OPEN_FILE;
  53. return NULL;
  54. }
  55. if(S_ISREG(st.st_mode)) {
  56. fd = _wopen(wfilepath.c_str(), flags);
  57. }else if (S_ISDIR(st.st_mode)) {
  58. // NOTE: open(dir) return -1 on windows
  59. fd = 0;
  60. }
  61. #else
  62. if(stat(filepath, &st) != 0) {
  63. param->error = ERR_OPEN_FILE;
  64. return NULL;
  65. }
  66. fd = open(filepath, flags);
  67. #endif
  68. if (fd < 0) {
  69. param->error = ERR_OPEN_FILE;
  70. return NULL;
  71. }
  72. defer(if (fd > 0) { close(fd); })
  73. if (fc == NULL) {
  74. if (S_ISREG(st.st_mode) ||
  75. (S_ISDIR(st.st_mode) &&
  76. filepath[strlen(filepath)-1] == '/')) {
  77. fc = std::make_shared<file_cache_t>();
  78. fc->filepath = filepath;
  79. fc->st = st;
  80. time(&fc->open_time);
  81. fc->stat_time = fc->open_time;
  82. fc->stat_cnt = 1;
  83. put(filepath, fc);
  84. }
  85. else {
  86. param->error = ERR_MISMATCH;
  87. return NULL;
  88. }
  89. }
  90. if (S_ISREG(fc->st.st_mode)) {
  91. param->filesize = fc->st.st_size;
  92. // FILE
  93. if (param->need_read) {
  94. if (fc->st.st_size > param->max_read) {
  95. param->error = ERR_OVER_LIMIT;
  96. return NULL;
  97. }
  98. fc->resize_buf(fc->st.st_size);
  99. int nread = read(fd, fc->filebuf.base, fc->filebuf.len);
  100. if (nread != fc->filebuf.len) {
  101. hloge("Failed to read file: %s", filepath);
  102. param->error = ERR_READ_FILE;
  103. return NULL;
  104. }
  105. }
  106. const char* suffix = strrchr(filepath, '.');
  107. if (suffix) {
  108. http_content_type content_type = http_content_type_enum_by_suffix(suffix+1);
  109. if (content_type == TEXT_HTML) {
  110. fc->content_type = "text/html; charset=utf-8";
  111. } else if (content_type == TEXT_PLAIN) {
  112. fc->content_type = "text/plain; charset=utf-8";
  113. } else {
  114. fc->content_type = http_content_type_str_by_suffix(suffix+1);
  115. }
  116. }
  117. }
  118. else if (S_ISDIR(fc->st.st_mode)) {
  119. // DIR
  120. std::string page;
  121. make_index_of_page(filepath, page, param->path);
  122. fc->resize_buf(page.size());
  123. memcpy(fc->filebuf.base, page.c_str(), page.size());
  124. fc->content_type = "text/html; charset=utf-8";
  125. }
  126. gmtime_fmt(fc->st.st_mtime, fc->last_modified);
  127. snprintf(fc->etag, sizeof(fc->etag), ETAG_FMT, (size_t)fc->st.st_mtime, (size_t)fc->st.st_size);
  128. }
  129. return fc;
  130. }
  131. bool FileCache::Exists(const char* filepath) const {
  132. return contains(filepath);
  133. }
  134. bool FileCache::Close(const char* filepath) {
  135. return remove(filepath);
  136. }
  137. file_cache_ptr FileCache::Get(const char* filepath) {
  138. file_cache_ptr fc;
  139. if (get(filepath, fc)) {
  140. return fc;
  141. }
  142. return NULL;
  143. }
  144. void FileCache::RemoveExpiredFileCache() {
  145. time_t now = time(NULL);
  146. remove_if([this, now](const std::string& filepath, const file_cache_ptr& fc) {
  147. return (now - fc->stat_time > expired_time);
  148. });
  149. }