hdir.cpp 2.8 KB

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