FileCache.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "FileCache.h"
  2. #include "hscope.h"
  3. #include "htime.h"
  4. #include "httpdef.h" // for http_content_type_str_by_suffix
  5. #include "http_page.h" //make_index_of_page
  6. #define ETAG_FMT "\"%zx-%zx\""
  7. file_cache_t* FileCache::Open(const char* filepath, void* ctx) {
  8. std::lock_guard<std::mutex> locker(mutex_);
  9. file_cache_t* fc = Get(filepath);
  10. bool modified = false;
  11. if (fc) {
  12. time_t tt;
  13. time(&tt);
  14. if (tt - fc->stat_time > file_stat_interval) {
  15. time_t mtime = fc->st.st_mtime;
  16. stat(filepath, &fc->st);
  17. fc->stat_time = tt;
  18. fc->stat_cnt++;
  19. if (mtime != fc->st.st_mtime) {
  20. modified = true;
  21. fc->stat_cnt = 1;
  22. }
  23. }
  24. }
  25. if (fc == NULL || modified) {
  26. int fd = open(filepath, O_RDONLY);
  27. if (fd < 0) {
  28. return NULL;
  29. }
  30. defer(close(fd);)
  31. if (fc == NULL) {
  32. struct stat st;
  33. fstat(fd, &st);
  34. if (S_ISREG(st.st_mode) ||
  35. (S_ISDIR(st.st_mode) &&
  36. filepath[strlen(filepath)-1] == '/')) {
  37. fc = new file_cache_t;
  38. //fc->filepath = filepath;
  39. fc->st = st;
  40. time(&fc->open_time);
  41. fc->stat_time = fc->open_time;
  42. fc->stat_cnt = 1;
  43. cached_files[filepath] = fc;
  44. }
  45. else {
  46. return NULL;
  47. }
  48. }
  49. if (S_ISREG(fc->st.st_mode)) {
  50. // FILE
  51. fc->resize_buf(fc->st.st_size);
  52. read(fd, fc->filebuf.base, fc->filebuf.len);
  53. const char* suffix = strrchr(filepath, '.');
  54. if (suffix) {
  55. fc->content_type = http_content_type_str_by_suffix(++suffix);
  56. }
  57. }
  58. else if (S_ISDIR(fc->st.st_mode)) {
  59. // DIR
  60. std::string page;
  61. make_index_of_page(filepath, page, (const char*)ctx);
  62. fc->resize_buf(page.size());
  63. memcpy(fc->filebuf.base, page.c_str(), page.size());
  64. fc->content_type = http_content_type_str(TEXT_HTML);
  65. }
  66. gmtime_fmt(fc->st.st_mtime, fc->last_modified);
  67. snprintf(fc->etag, sizeof(fc->etag), ETAG_FMT, fc->st.st_mtime, fc->st.st_size);
  68. }
  69. return fc;
  70. }
  71. int FileCache::Close(const char* filepath) {
  72. std::lock_guard<std::mutex> locker(mutex_);
  73. auto iter = cached_files.find(filepath);
  74. if (iter != cached_files.end()) {
  75. delete iter->second;
  76. iter = cached_files.erase(iter);
  77. return 0;
  78. }
  79. return -1;
  80. }
  81. file_cache_t* FileCache::Get(const char* filepath) {
  82. auto iter = cached_files.find(filepath);
  83. if (iter != cached_files.end()) {
  84. return iter->second;
  85. }
  86. return NULL;
  87. }