1
0

FileCache.cpp 3.0 KB

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