1
0

hdir.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. strlcpy(tmp.name, result->d_name, sizeof(tmp.name));
  41. strlcpy(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.mode = st.st_mode & 0777;
  52. tmp.size = st.st_size;
  53. tmp.atime = st.st_atime;
  54. tmp.mtime = st.st_mtime;
  55. tmp.ctime = st.st_ctime;
  56. }
  57. dirs.push_back(tmp);
  58. }
  59. closedir(dp);
  60. #elif defined(OS_WIN)
  61. // FindFirstFile -> FindNextFile -> FindClose
  62. strcat(path, "*");
  63. WIN32_FIND_DATA data;
  64. HANDLE h = FindFirstFile(path, &data);
  65. if (h == NULL) {
  66. return -1;
  67. }
  68. hdir_t tmp;
  69. do {
  70. memset(&tmp, 0, sizeof(hdir_t));
  71. strlcpy(tmp.name, data.cFileName, sizeof(tmp.name));
  72. tmp.type = 'f';
  73. if (data.dwFileAttributes & _A_SUBDIR) {
  74. tmp.type = 'd';
  75. }
  76. tmp.mod = 0777;
  77. tmp.size = (((uint64_t)data.nFileSizeHigh) << 32) | data.nFileSizeLow;
  78. tmp.atime = FileTime2Epoch(data.ftLastAccessTime);
  79. tmp.mtime = FileTime2Epoch(data.ftLastWriteTime);
  80. tmp.ctime = FileTime2Epoch(data.ftCreationTime);
  81. dirs.push_back(tmp);
  82. } while (FindNextFile(h, &data));
  83. FindClose(h);
  84. #endif
  85. dirs.sort(less);
  86. return dirs.size();
  87. }