hdir.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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* result = NULL;
  33. struct stat st;
  34. hdir_t tmp;
  35. while ((result = readdir(dp))) {
  36. memset(&tmp, 0, sizeof(hdir_t));
  37. strncpy(tmp.name, result->d_name, sizeof(tmp.name));
  38. strncpy(path+dirlen, result->d_name, sizeof(path)-dirlen);
  39. if (lstat(path, &st) == 0) {
  40. if (S_ISREG(st.st_mode)) tmp.type = 'f';
  41. else if (S_ISDIR(st.st_mode)) tmp.type = 'd';
  42. else if (S_ISLNK(st.st_mode)) tmp.type = 'l';
  43. else if (S_ISBLK(st.st_mode)) tmp.type = 'b';
  44. else if (S_ISCHR(st.st_mode)) tmp.type = 'c';
  45. else if (S_ISSOCK(st.st_mode)) tmp.type = 's';
  46. else if (S_ISFIFO(st.st_mode)) tmp.type = 'p';
  47. else tmp.type = '-';
  48. tmp.mode = st.st_mode & 0777;
  49. tmp.size = st.st_size;
  50. tmp.atime = st.st_atime;
  51. tmp.mtime = st.st_mtime;
  52. tmp.ctime = st.st_ctime;
  53. }
  54. dirs.push_back(tmp);
  55. }
  56. closedir(dp);
  57. #elif defined(OS_WIN)
  58. // FindFirstFile -> FindNextFile -> FindClose
  59. strcat(path, "*");
  60. WIN32_FIND_DATAA data;
  61. HANDLE h = FindFirstFileA(path, &data);
  62. if (h == NULL) {
  63. return -1;
  64. }
  65. hdir_t tmp;
  66. do {
  67. memset(&tmp, 0, sizeof(hdir_t));
  68. strncpy(tmp.name, data.cFileName, sizeof(tmp.name));
  69. tmp.type = 'f';
  70. if (data.dwFileAttributes & _A_SUBDIR) {
  71. tmp.type = 'd';
  72. }
  73. tmp.mode = 0777;
  74. tmp.size = (((uint64_t)data.nFileSizeHigh) << 32) | data.nFileSizeLow;
  75. tmp.atime = FileTime2Epoch(data.ftLastAccessTime);
  76. tmp.mtime = FileTime2Epoch(data.ftLastWriteTime);
  77. tmp.ctime = FileTime2Epoch(data.ftCreationTime);
  78. dirs.push_back(tmp);
  79. } while (FindNextFileA(h, &data));
  80. FindClose(h);
  81. #endif
  82. dirs.sort(less);
  83. return dirs.size();
  84. }