hdir.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include "hdir.h"
  2. #include "hplatform.h"
  3. #ifdef OS_WIN
  4. #include "hstring.h" // import hv::utf8_to_wchar hv::wchar_to_utf8
  5. //FILETIME starts from 1601-01-01 UTC, epoch from 1970-01-01 UTC
  6. //FILETIME unit (100ns)
  7. #define FILETIME_EPOCH_DIFF 11644473600 // s
  8. time_t FileTime2Epoch(FILETIME filetime) {
  9. uint64_t ll = (((uint64_t)filetime.dwHighDateTime) << 32) | filetime.dwLowDateTime;
  10. ll /= 1e7; // s
  11. return ll - FILETIME_EPOCH_DIFF;
  12. }
  13. #endif
  14. static bool less(const hdir_t& lhs, const hdir_t& rhs) {
  15. return stricmp(lhs.name, rhs.name) < 0;
  16. }
  17. int listdir(const char* dir, std::list<hdir_t>& dirs) {
  18. int dirlen = strlen(dir);
  19. if (dirlen > 256) {
  20. return -1;
  21. }
  22. char path[512];
  23. strcpy(path, dir);
  24. if (dir[dirlen-1] != '/') {
  25. strcat(path, "/");
  26. ++dirlen;
  27. }
  28. dirs.clear();
  29. #ifdef OS_UNIX
  30. // opendir -> readdir -> closedir
  31. DIR* dp = opendir(dir);
  32. if (dp == NULL) return -1;
  33. struct dirent* result = NULL;
  34. struct stat st;
  35. hdir_t tmp;
  36. while ((result = readdir(dp))) {
  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_DATAW data;
  62. HANDLE h = FindFirstFileW(hv::utf8_to_wchar(path).c_str(), &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, hv::wchar_to_utf8(data.cFileName).c_str(), 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 (FindNextFileW(h, &data));
  81. FindClose(h);
  82. #endif
  83. dirs.sort(less);
  84. return dirs.size();
  85. }