FileCache.cpp 2.7 KB

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