1
0

FileCache.cpp 4.9 KB

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