FileCache.cpp 4.3 KB

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