1
0

FileCache.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include "FileCache.h"
  2. #include "hscope.h"
  3. #include "htime.h"
  4. #include "hlog.h"
  5. #include "httpdef.h" // for http_content_type_str_by_suffix
  6. #include "http_page.h" //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->if_modified(filepath);
  16. fc->stat_time = now;
  17. fc->stat_cnt++;
  18. }
  19. if (need_read) {
  20. if ((!modified) && fc->filebuf.len == fc->st.st_size) {
  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. fc->content_type = http_content_type_str_by_suffix(++suffix);
  70. }
  71. }
  72. else if (S_ISDIR(fc->st.st_mode)) {
  73. // DIR
  74. std::string page;
  75. make_index_of_page(filepath, page, (const char*)ctx);
  76. fc->resize_buf(page.size());
  77. memcpy(fc->filebuf.base, page.c_str(), page.size());
  78. fc->content_type = http_content_type_str(TEXT_HTML);
  79. }
  80. gmtime_fmt(fc->st.st_mtime, fc->last_modified);
  81. snprintf(fc->etag, sizeof(fc->etag), ETAG_FMT, (size_t)fc->st.st_mtime, (size_t)fc->st.st_size);
  82. }
  83. return fc;
  84. }
  85. bool FileCache::Close(const char* filepath) {
  86. std::lock_guard<std::mutex> locker(mutex_);
  87. auto iter = cached_files.find(filepath);
  88. if (iter != cached_files.end()) {
  89. iter = cached_files.erase(iter);
  90. return true;
  91. }
  92. return false;
  93. }
  94. bool FileCache::Close(const file_cache_ptr& fc) {
  95. std::lock_guard<std::mutex> locker(mutex_);
  96. auto iter = cached_files.begin();
  97. while (iter != cached_files.end()) {
  98. if (iter->second == fc) {
  99. iter = cached_files.erase(iter);
  100. return true;
  101. } else {
  102. ++iter;
  103. }
  104. }
  105. return false;
  106. }
  107. file_cache_ptr FileCache::Get(const char* filepath) {
  108. auto iter = cached_files.find(filepath);
  109. if (iter != cached_files.end()) {
  110. return iter->second;
  111. }
  112. return NULL;
  113. }
  114. void FileCache::RemoveExpiredFileCache() {
  115. std::lock_guard<std::mutex> locker(mutex_);
  116. time_t now = time(NULL);
  117. auto iter = cached_files.begin();
  118. while (iter != cached_files.end()) {
  119. if (now - iter->second->stat_time > file_expired_time) {
  120. iter = cached_files.erase(iter);
  121. } else {
  122. ++iter;
  123. }
  124. }
  125. }