hdir.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "hdir.h"
  2. #include <string.h>
  3. #include "hplatform.h"
  4. #include "hbase.h"
  5. #ifdef OS_WIN
  6. //FILETIME starts from 1601-01-01 UTC, epoch from 1970-01-01 UTC
  7. //FILETIME unit (100ns)
  8. #define FILETIME_EPOCH_DIFF 11644473600 // s
  9. time_t FileTime2Epoch(FILETIME filetime) {
  10. uint64_t ll = (((uint64_t)filetime.dwHighDateTime) << 32) | filetime.dwLowDateTime;
  11. ll /= 1e7; // s
  12. return ll - FILETIME_EPOCH_DIFF;
  13. }
  14. #endif
  15. static bool less(const hdir_t& lhs, const hdir_t& rhs) {
  16. return stricmp(lhs.name, rhs.name) < 0;
  17. }
  18. int listdir(const char* dir, std::list<hdir_t>& dirs) {
  19. int dirlen = strlen(dir);
  20. if (dirlen > 256) {
  21. return -1;
  22. }
  23. char path[512];
  24. strcpy(path, dir);
  25. if (dir[dirlen-1] != '/') {
  26. strcat(path, "/");
  27. ++dirlen;
  28. }
  29. dirs.clear();
  30. #ifdef OS_UNIX
  31. // opendir -> readdir -> closedir
  32. DIR* dp = opendir(dir);
  33. if (dp == NULL) return -1;
  34. struct dirent de;
  35. struct dirent* result = NULL;
  36. struct stat st;
  37. hdir_t tmp;
  38. while (readdir_r(dp, &de, &result) == 0 && result) {
  39. memset(&tmp, 0, sizeof(hdir_t));
  40. safe_strncpy(tmp.name, result->d_name, sizeof(tmp.name));
  41. safe_strncpy(path+dirlen, result->d_name, sizeof(path)-dirlen);
  42. if (lstat(path, &st) == 0) {
  43. if (S_ISREG(st.st_mode)) tmp.type = 'f';
  44. else if (S_ISDIR(st.st_mode)) tmp.type = 'd';
  45. else if (S_ISLNK(st.st_mode)) tmp.type = 'l';
  46. else if (S_ISBLK(st.st_mode)) tmp.type = 'b';
  47. else if (S_ISCHR(st.st_mode)) tmp.type = 'c';
  48. else if (S_ISSOCK(st.st_mode)) tmp.type = 's';
  49. else if (S_ISFIFO(st.st_mode)) tmp.type = 'p';
  50. else tmp.type = '-';
  51. tmp.size = st.st_size;
  52. tmp.atime = st.st_atime;
  53. tmp.mtime = st.st_mtime;
  54. tmp.ctime = st.st_ctime;
  55. }
  56. dirs.push_back(tmp);
  57. }
  58. closedir(dp);
  59. #elif defined(OS_WIN)
  60. // FindFirstFile -> FindNextFile -> FindClose
  61. strcat(path, "*");
  62. WIN32_FIND_DATA data;
  63. HANDLE h = FindFirstFile(path, &data);
  64. if (h == NULL) {
  65. return -1;
  66. }
  67. hdir_t tmp;
  68. do {
  69. memset(&tmp, 0, sizeof(hdir_t));
  70. safe_strncpy(tmp.name, data.cFileName, sizeof(tmp.name));
  71. tmp.type = 'f';
  72. if (data.dwFileAttributes & _A_SUBDIR) {
  73. tmp.type = 'd';
  74. }
  75. tmp.size = (((uint64_t)data.nFileSizeHigh) << 32) | data.nFileSizeLow;
  76. tmp.atime = FileTime2Epoch(data.ftLastAccessTime);
  77. tmp.mtime = FileTime2Epoch(data.ftLastWriteTime);
  78. tmp.ctime = FileTime2Epoch(data.ftCreationTime);
  79. dirs.push_back(tmp);
  80. } while (FindNextFile(h, &data));
  81. FindClose(h);
  82. #endif
  83. dirs.sort(less);
  84. return dirs.size();
  85. }