inode.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #pragma once
  2. //generic abstraction layer for common storage operations against both files and directories
  3. //these functions are not recursive; use directory::create() and directory::remove() for recursion
  4. #include <nall/platform.hpp>
  5. #include <nall/string.hpp>
  6. namespace nall {
  7. struct inode {
  8. enum class time : uint { create, modify, access };
  9. inode() = delete;
  10. inode(const inode&) = delete;
  11. auto operator=(const inode&) -> inode& = delete;
  12. static auto exists(const string& name) -> bool {
  13. return access(name, F_OK) == 0;
  14. }
  15. static auto readable(const string& name) -> bool {
  16. return access(name, R_OK) == 0;
  17. }
  18. static auto writable(const string& name) -> bool {
  19. return access(name, W_OK) == 0;
  20. }
  21. static auto executable(const string& name) -> bool {
  22. return access(name, X_OK) == 0;
  23. }
  24. static auto hidden(const string& name) -> bool {
  25. #if defined(PLATFORM_WINDOWS)
  26. auto attributes = GetFileAttributes(utf16_t(name));
  27. return attributes & FILE_ATTRIBUTE_HIDDEN;
  28. #else
  29. //todo: is this really the best way to do this? stat doesn't have S_ISHIDDEN ...
  30. return name.split("/").last().beginsWith(".");
  31. #endif
  32. }
  33. static auto mode(const string& name) -> uint {
  34. struct stat data{};
  35. stat(name, &data);
  36. return data.st_mode;
  37. }
  38. static auto uid(const string& name) -> uint {
  39. struct stat data{};
  40. stat(name, &data);
  41. return data.st_uid;
  42. }
  43. static auto gid(const string& name) -> uint {
  44. struct stat data{};
  45. stat(name, &data);
  46. return data.st_gid;
  47. }
  48. static auto owner(const string& name) -> string {
  49. #if !defined(PLATFORM_WINDOWS)
  50. struct passwd* pw = getpwuid(uid(name));
  51. if(pw && pw->pw_name) return pw->pw_name;
  52. #endif
  53. return {};
  54. }
  55. static auto group(const string& name) -> string {
  56. #if !defined(PLATFORM_WINDOWS)
  57. struct group* gr = getgrgid(gid(name));
  58. if(gr && gr->gr_name) return gr->gr_name;
  59. #endif
  60. return {};
  61. }
  62. static auto timestamp(const string& name, time mode = time::modify) -> uint64_t {
  63. struct stat data{};
  64. stat(name, &data);
  65. switch(mode) {
  66. #if defined(PLATFORM_WINDOWS)
  67. //on Windows, the last status change time (ctime) holds the file creation time instead
  68. case time::create: return data.st_ctime;
  69. #elif defined(PLATFORM_BSD) || defined(PLATFORM_MACOS)
  70. //st_birthtime may return -1 or st_atime if it is not supported by the file system
  71. //the best that can be done in this case is to return st_mtime if it's older
  72. case time::create: return min((uint)data.st_birthtime, (uint)data.st_mtime);
  73. #else
  74. //Linux simply doesn't support file creation time at all
  75. //this is also our fallback case for unsupported operating systems
  76. case time::create: return data.st_mtime;
  77. #endif
  78. case time::modify: return data.st_mtime;
  79. //for performance reasons, last access time is usually not enabled on various filesystems
  80. //ensure that the last access time is not older than the last modify time (eg for NTFS)
  81. case time::access: return max((uint)data.st_atime, data.st_mtime);
  82. }
  83. return 0;
  84. }
  85. static auto setMode(const string& name, uint mode) -> bool {
  86. #if !defined(PLATFORM_WINDOWS)
  87. return chmod(name, mode) == 0;
  88. #else
  89. return _wchmod(utf16_t(name), (mode & 0400 ? _S_IREAD : 0) | (mode & 0200 ? _S_IWRITE : 0)) == 0;
  90. #endif
  91. }
  92. static auto setOwner(const string& name, const string& owner) -> bool {
  93. #if !defined(PLATFORM_WINDOWS)
  94. struct passwd* pwd = getpwnam(owner);
  95. if(!pwd) return false;
  96. return chown(name, pwd->pw_uid, inode::gid(name)) == 0;
  97. #else
  98. return true;
  99. #endif
  100. }
  101. static auto setGroup(const string& name, const string& group) -> bool {
  102. #if !defined(PLATFORM_WINDOWS)
  103. struct group* grp = getgrnam(group);
  104. if(!grp) return false;
  105. return chown(name, inode::uid(name), grp->gr_gid) == 0;
  106. #else
  107. return true;
  108. #endif
  109. }
  110. static auto setTimestamp(const string& name, uint64_t value, time mode = time::modify) -> bool {
  111. struct utimbuf timeBuffer;
  112. timeBuffer.modtime = mode == time::modify ? value : inode::timestamp(name, time::modify);
  113. timeBuffer.actime = mode == time::access ? value : inode::timestamp(name, time::access);
  114. return utime(name, &timeBuffer) == 0;
  115. }
  116. //returns true if 'name' already exists
  117. static auto create(const string& name, uint permissions = 0755) -> bool {
  118. if(exists(name)) return true;
  119. if(name.endsWith("/")) return mkdir(name, permissions) == 0;
  120. int fd = open(name, O_CREAT | O_EXCL, permissions);
  121. if(fd < 0) return false;
  122. return close(fd), true;
  123. }
  124. //returns false if 'name' and 'targetname' are on different file systems (requires copy)
  125. static auto rename(const string& name, const string& targetname) -> bool {
  126. return ::rename(name, targetname) == 0;
  127. }
  128. //returns false if 'name' is a directory that is not empty
  129. static auto remove(const string& name) -> bool {
  130. #if defined(PLATFORM_WINDOWS)
  131. if(name.endsWith("/")) return _wrmdir(utf16_t(name)) == 0;
  132. return _wunlink(utf16_t(name)) == 0;
  133. #else
  134. if(name.endsWith("/")) return rmdir(name) == 0;
  135. return unlink(name) == 0;
  136. #endif
  137. }
  138. };
  139. }