FileCache.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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_t* FileCache::Open(const char* filepath, void* ctx) {
  9. std::lock_guard<std::mutex> locker(mutex_);
  10. file_cache_t* fc = Get(filepath);
  11. bool modified = false;
  12. if (fc) {
  13. time_t tt;
  14. time(&tt);
  15. if (tt - fc->stat_time > file_stat_interval) {
  16. time_t mtime = fc->st.st_mtime;
  17. stat(filepath, &fc->st);
  18. fc->stat_time = tt;
  19. fc->stat_cnt++;
  20. if (mtime != fc->st.st_mtime) {
  21. modified = true;
  22. fc->stat_cnt = 1;
  23. }
  24. }
  25. }
  26. if (fc == NULL || modified) {
  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. return NULL;
  34. }
  35. defer(close(fd);)
  36. if (fc == NULL) {
  37. struct stat st;
  38. fstat(fd, &st);
  39. if (S_ISREG(st.st_mode) ||
  40. (S_ISDIR(st.st_mode) &&
  41. filepath[strlen(filepath)-1] == '/')) {
  42. fc = new file_cache_t;
  43. //fc->filepath = filepath;
  44. fc->st = st;
  45. time(&fc->open_time);
  46. fc->stat_time = fc->open_time;
  47. fc->stat_cnt = 1;
  48. cached_files[filepath] = fc;
  49. }
  50. else {
  51. return NULL;
  52. }
  53. }
  54. if (S_ISREG(fc->st.st_mode)) {
  55. // FILE
  56. fc->resize_buf(fc->st.st_size);
  57. int nread = read(fd, fc->filebuf.base, fc->filebuf.len);
  58. if (nread != fc->filebuf.len) {
  59. hloge("Too large file: %s", filepath);
  60. return NULL;
  61. }
  62. const char* suffix = strrchr(filepath, '.');
  63. if (suffix) {
  64. fc->content_type = http_content_type_str_by_suffix(++suffix);
  65. }
  66. }
  67. else if (S_ISDIR(fc->st.st_mode)) {
  68. // DIR
  69. std::string page;
  70. make_index_of_page(filepath, page, (const char*)ctx);
  71. fc->resize_buf(page.size());
  72. memcpy(fc->filebuf.base, page.c_str(), page.size());
  73. fc->content_type = http_content_type_str(TEXT_HTML);
  74. }
  75. gmtime_fmt(fc->st.st_mtime, fc->last_modified);
  76. snprintf(fc->etag, sizeof(fc->etag), ETAG_FMT, (size_t)fc->st.st_mtime, (size_t)fc->st.st_size);
  77. }
  78. return fc;
  79. }
  80. int FileCache::Close(const char* filepath) {
  81. std::lock_guard<std::mutex> locker(mutex_);
  82. auto iter = cached_files.find(filepath);
  83. if (iter != cached_files.end()) {
  84. delete iter->second;
  85. iter = cached_files.erase(iter);
  86. return 0;
  87. }
  88. return -1;
  89. }
  90. file_cache_t* FileCache::Get(const char* filepath) {
  91. auto iter = cached_files.find(filepath);
  92. if (iter != cached_files.end()) {
  93. return iter->second;
  94. }
  95. return NULL;
  96. }