1
0

FileCache.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 > file_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. param->error = ERR_OPEN_FILE;
  34. return NULL;
  35. }
  36. defer(close(fd);)
  37. if (fc == NULL) {
  38. struct stat st;
  39. fstat(fd, &st);
  40. if (S_ISREG(st.st_mode) ||
  41. (S_ISDIR(st.st_mode) &&
  42. filepath[strlen(filepath)-1] == '/')) {
  43. fc.reset(new file_cache_t);
  44. fc->filepath = filepath;
  45. fc->st = st;
  46. time(&fc->open_time);
  47. fc->stat_time = fc->open_time;
  48. fc->stat_cnt = 1;
  49. cached_files[filepath] = fc;
  50. }
  51. else {
  52. param->error = ERR_MISMATCH;
  53. return NULL;
  54. }
  55. }
  56. if (S_ISREG(fc->st.st_mode)) {
  57. param->filesize = fc->st.st_size;
  58. // FILE
  59. if (param->need_read) {
  60. if (fc->st.st_size > param->max_read) {
  61. param->error = ERR_OVER_LIMIT;
  62. return NULL;
  63. }
  64. fc->resize_buf(fc->st.st_size);
  65. int nread = read(fd, fc->filebuf.base, fc->filebuf.len);
  66. if (nread != fc->filebuf.len) {
  67. hloge("Failed to read file: %s", filepath);
  68. param->error = ERR_READ_FILE;
  69. return NULL;
  70. }
  71. }
  72. const char* suffix = strrchr(filepath, '.');
  73. if (suffix) {
  74. http_content_type content_type = http_content_type_enum_by_suffix(suffix+1);
  75. if (content_type == TEXT_HTML) {
  76. fc->content_type = "text/html; charset=utf-8";
  77. } else if (content_type == TEXT_PLAIN) {
  78. fc->content_type = "text/plain; charset=utf-8";
  79. } else {
  80. fc->content_type = http_content_type_str_by_suffix(suffix+1);
  81. }
  82. }
  83. }
  84. else if (S_ISDIR(fc->st.st_mode)) {
  85. // DIR
  86. std::string page;
  87. make_index_of_page(filepath, page, param->path);
  88. fc->resize_buf(page.size());
  89. memcpy(fc->filebuf.base, page.c_str(), page.size());
  90. fc->content_type = "text/html; charset=utf-8";
  91. }
  92. gmtime_fmt(fc->st.st_mtime, fc->last_modified);
  93. snprintf(fc->etag, sizeof(fc->etag), ETAG_FMT, (size_t)fc->st.st_mtime, (size_t)fc->st.st_size);
  94. }
  95. return fc;
  96. }
  97. bool FileCache::Close(const char* filepath) {
  98. std::lock_guard<std::mutex> locker(mutex_);
  99. auto iter = cached_files.find(filepath);
  100. if (iter != cached_files.end()) {
  101. iter = cached_files.erase(iter);
  102. return true;
  103. }
  104. return false;
  105. }
  106. bool FileCache::Close(const file_cache_ptr& fc) {
  107. std::lock_guard<std::mutex> locker(mutex_);
  108. auto iter = cached_files.begin();
  109. while (iter != cached_files.end()) {
  110. if (iter->second == fc) {
  111. iter = cached_files.erase(iter);
  112. return true;
  113. } else {
  114. ++iter;
  115. }
  116. }
  117. return false;
  118. }
  119. file_cache_ptr FileCache::Get(const char* filepath) {
  120. auto iter = cached_files.find(filepath);
  121. if (iter != cached_files.end()) {
  122. return iter->second;
  123. }
  124. return NULL;
  125. }
  126. void FileCache::RemoveExpiredFileCache() {
  127. std::lock_guard<std::mutex> locker(mutex_);
  128. time_t now = time(NULL);
  129. auto iter = cached_files.begin();
  130. while (iter != cached_files.end()) {
  131. if (now - iter->second->stat_time > file_expired_time) {
  132. iter = cached_files.erase(iter);
  133. } else {
  134. ++iter;
  135. }
  136. }
  137. }