1
0

FileCache.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 fd = open(filepath, O_RDONLY);
  28. if (fd < 0) {
  29. return NULL;
  30. }
  31. defer(close(fd);)
  32. if (fc == NULL) {
  33. struct stat st;
  34. fstat(fd, &st);
  35. if (S_ISREG(st.st_mode) ||
  36. (S_ISDIR(st.st_mode) &&
  37. filepath[strlen(filepath)-1] == '/')) {
  38. fc = new file_cache_t;
  39. //fc->filepath = filepath;
  40. fc->st = st;
  41. time(&fc->open_time);
  42. fc->stat_time = fc->open_time;
  43. fc->stat_cnt = 1;
  44. cached_files[filepath] = fc;
  45. }
  46. else {
  47. return NULL;
  48. }
  49. }
  50. if (S_ISREG(fc->st.st_mode)) {
  51. // FILE
  52. fc->resize_buf(fc->st.st_size);
  53. int nread = read(fd, fc->filebuf.base, fc->filebuf.len);
  54. if (nread != fc->filebuf.len) {
  55. hloge("Too large file: %s", filepath);
  56. return NULL;
  57. }
  58. const char* suffix = strrchr(filepath, '.');
  59. if (suffix) {
  60. fc->content_type = http_content_type_str_by_suffix(++suffix);
  61. }
  62. }
  63. else if (S_ISDIR(fc->st.st_mode)) {
  64. // DIR
  65. std::string page;
  66. make_index_of_page(filepath, page, (const char*)ctx);
  67. fc->resize_buf(page.size());
  68. memcpy(fc->filebuf.base, page.c_str(), page.size());
  69. fc->content_type = http_content_type_str(TEXT_HTML);
  70. }
  71. gmtime_fmt(fc->st.st_mtime, fc->last_modified);
  72. snprintf(fc->etag, sizeof(fc->etag), ETAG_FMT, (size_t)fc->st.st_mtime, (size_t)fc->st.st_size);
  73. }
  74. return fc;
  75. }
  76. int FileCache::Close(const char* filepath) {
  77. std::lock_guard<std::mutex> locker(mutex_);
  78. auto iter = cached_files.find(filepath);
  79. if (iter != cached_files.end()) {
  80. delete iter->second;
  81. iter = cached_files.erase(iter);
  82. return 0;
  83. }
  84. return -1;
  85. }
  86. file_cache_t* FileCache::Get(const char* filepath) {
  87. auto iter = cached_files.find(filepath);
  88. if (iter != cached_files.end()) {
  89. return iter->second;
  90. }
  91. return NULL;
  92. }