hdir.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "hdir.h"
  2. #include <string.h>
  3. #include "hplatform.h"
  4. #ifdef OS_WIN
  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 de;
  34. struct dirent* result = NULL;
  35. struct stat st;
  36. hdir_t tmp;
  37. while (readdir_r(dp, &de, &result) == 0 && result) {
  38. memset(&tmp, 0, sizeof(hdir_t));
  39. strncpy(tmp.name, result->d_name, sizeof(tmp.name));
  40. strncpy(path+dirlen, result->d_name, sizeof(path)-dirlen);
  41. if (lstat(path, &st) == 0) {
  42. if (S_ISREG(st.st_mode)) tmp.type = 'f';
  43. else if (S_ISDIR(st.st_mode)) tmp.type = 'd';
  44. else if (S_ISLNK(st.st_mode)) tmp.type = 'l';
  45. else if (S_ISBLK(st.st_mode)) tmp.type = 'b';
  46. else if (S_ISCHR(st.st_mode)) tmp.type = 'c';
  47. else if (S_ISSOCK(st.st_mode)) tmp.type = 's';
  48. else if (S_ISFIFO(st.st_mode)) tmp.type = 'p';
  49. else tmp.type = '-';
  50. tmp.mode = st.st_mode & 0777;
  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. 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.mode = 0777;
  76. tmp.size = (((uint64_t)data.nFileSizeHigh) << 32) | data.nFileSizeLow;
  77. tmp.atime = FileTime2Epoch(data.ftLastAccessTime);
  78. tmp.mtime = FileTime2Epoch(data.ftLastWriteTime);
  79. tmp.ctime = FileTime2Epoch(data.ftCreationTime);
  80. dirs.push_back(tmp);
  81. } while (FindNextFile(h, &data));
  82. FindClose(h);
  83. #endif
  84. dirs.sort(less);
  85. return dirs.size();
  86. }