fstat.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #include "fstat.h"
  2. #include "file.h"
  3. #include <sys/stat.h>
  4. #include <util/folder/path.h>
  5. #include <cerrno>
  6. #if defined(_win_)
  7. #include "fs_win.h"
  8. #ifdef _S_IFLNK
  9. #undef _S_IFLNK
  10. #endif
  11. #define _S_IFLNK 0x80000000
  12. // See https://docs.microsoft.com/en-us/windows/win32/fileio/file-attribute-constants
  13. // for possible flag values
  14. static ui32 GetWinFileType(DWORD fileAttributes, ULONG reparseTag) {
  15. // I'm not really sure, why it is done like this. MSDN tells that
  16. // FILE_ATTRIBUTE_DEVICE is reserved for system use. Some more info is
  17. // available at https://stackoverflow.com/questions/3419527/setting-file-attribute-device-in-visual-studio
  18. // We should probably replace this with GetFileType call and check for
  19. // FILE_TYPE_CHAR and FILE_TYPE_PIPE return values.
  20. if (fileAttributes & FILE_ATTRIBUTE_DEVICE) {
  21. return _S_IFCHR;
  22. }
  23. if (fileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
  24. // We consider IO_REPARSE_TAG_SYMLINK and IO_REPARSE_TAG_MOUNT_POINT
  25. // both to be symlinks to align with current WinReadLink behaviour.
  26. if (reparseTag == IO_REPARSE_TAG_SYMLINK || reparseTag == IO_REPARSE_TAG_MOUNT_POINT) {
  27. return _S_IFLNK;
  28. }
  29. }
  30. if (fileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  31. return _S_IFDIR;
  32. }
  33. return _S_IFREG;
  34. }
  35. static ui32 GetFileMode(DWORD fileAttributes, ULONG reparseTag) {
  36. ui32 mode = 0;
  37. if (fileAttributes == 0xFFFFFFFF)
  38. return mode;
  39. mode |= GetWinFileType(fileAttributes, reparseTag);
  40. if ((fileAttributes & FILE_ATTRIBUTE_READONLY) == 0) {
  41. mode |= _S_IWRITE;
  42. }
  43. return mode;
  44. }
  45. #define S_ISDIR(st_mode) (st_mode & _S_IFDIR)
  46. #define S_ISREG(st_mode) (st_mode & _S_IFREG)
  47. #define S_ISLNK(st_mode) (st_mode & _S_IFLNK)
  48. struct TSystemFStat: public BY_HANDLE_FILE_INFORMATION {
  49. ULONG ReparseTag = 0;
  50. };
  51. #elif defined(_unix_)
  52. using TSystemFStat = struct stat;
  53. #else
  54. #error unsupported platform
  55. #endif
  56. #if defined(_unix_)
  57. static void MakeStatFromStructStat(TFileStat& st, const struct stat& fs) {
  58. st.Mode = fs.st_mode;
  59. st.NLinks = fs.st_nlink;
  60. st.Uid = fs.st_uid;
  61. st.Gid = fs.st_gid;
  62. st.Size = fs.st_size;
  63. st.AllocationSize = fs.st_blocks * 512;
  64. #if defined(_linux_)
  65. st.ATime = fs.st_atim.tv_sec;
  66. st.ATimeNSec = fs.st_atim.tv_nsec;
  67. st.MTime = fs.st_mtim.tv_sec;
  68. st.MTimeNSec = fs.st_mtim.tv_nsec;
  69. st.CTime = fs.st_ctim.tv_sec;
  70. st.CTimeNSec = fs.st_ctim.tv_nsec;
  71. #elif defined(_darwin_)
  72. st.ATime = fs.st_atimespec.tv_sec;
  73. st.ATimeNSec = fs.st_atimespec.tv_nsec;
  74. st.MTime = fs.st_mtimespec.tv_sec;
  75. st.MTimeNSec = fs.st_mtimespec.tv_nsec;
  76. st.CTime = fs.st_birthtimespec.tv_sec;
  77. st.CTimeNSec = fs.st_birthtimespec.tv_nsec;
  78. #else
  79. // Fallback.
  80. st.ATime = fs.st_atime;
  81. st.MTime = fs.st_mtime;
  82. st.CTime = fs.st_ctime;
  83. #endif
  84. st.INode = fs.st_ino;
  85. }
  86. #endif
  87. static void MakeStat(TFileStat& st, const TSystemFStat& fs) {
  88. #ifdef _unix_
  89. MakeStatFromStructStat(st, fs);
  90. #else
  91. timespec timeSpec;
  92. FileTimeToTimespec(fs.ftCreationTime, &timeSpec);
  93. st.CTime = timeSpec.tv_sec;
  94. st.CTimeNSec = timeSpec.tv_nsec;
  95. FileTimeToTimespec(fs.ftLastAccessTime, &timeSpec);
  96. st.ATime = timeSpec.tv_sec;
  97. st.ATimeNSec = timeSpec.tv_nsec;
  98. FileTimeToTimespec(fs.ftLastWriteTime, &timeSpec);
  99. st.MTime = timeSpec.tv_sec;
  100. st.MTimeNSec = timeSpec.tv_nsec;
  101. st.NLinks = fs.nNumberOfLinks;
  102. st.Mode = GetFileMode(fs.dwFileAttributes, fs.ReparseTag);
  103. st.Uid = 0;
  104. st.Gid = 0;
  105. st.Size = ((ui64)fs.nFileSizeHigh << 32) | fs.nFileSizeLow;
  106. st.AllocationSize = st.Size; // FIXME
  107. st.INode = ((ui64)fs.nFileIndexHigh << 32) | fs.nFileIndexLow;
  108. #endif
  109. }
  110. static bool GetStatByHandle(TSystemFStat& fs, FHANDLE f) {
  111. #ifdef _win_
  112. if (!GetFileInformationByHandle(f, &fs)) {
  113. return false;
  114. }
  115. if (fs.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
  116. fs.ReparseTag = NFsPrivate::WinReadReparseTag(f);
  117. }
  118. return true;
  119. #else
  120. return !fstat(f, &fs);
  121. #endif
  122. }
  123. static bool GetStatByName(TSystemFStat& fs, const char* fileName, bool nofollow) {
  124. #ifdef _win_
  125. TFileHandle h = NFsPrivate::CreateFileWithUtf8Name(
  126. fileName,
  127. FILE_READ_ATTRIBUTES | FILE_READ_EA,
  128. FILE_SHARE_READ | FILE_SHARE_WRITE,
  129. OPEN_EXISTING,
  130. (nofollow ? FILE_FLAG_OPEN_REPARSE_POINT : 0) | FILE_FLAG_BACKUP_SEMANTICS,
  131. true);
  132. if (!h.IsOpen()) {
  133. return false;
  134. }
  135. return GetStatByHandle(fs, h);
  136. #else
  137. return !(nofollow ? lstat : stat)(fileName, &fs);
  138. #endif
  139. }
  140. TFileStat::TFileStat() = default;
  141. TFileStat::TFileStat(const TFile& f) {
  142. *this = TFileStat(f.GetHandle());
  143. }
  144. TFileStat::TFileStat(FHANDLE f) {
  145. TSystemFStat st;
  146. if (GetStatByHandle(st, f)) {
  147. MakeStat(*this, st);
  148. } else {
  149. *this = TFileStat();
  150. }
  151. }
  152. #if defined(_unix_)
  153. TFileStat::TFileStat(const struct stat& st) {
  154. MakeStatFromStructStat(*this, st);
  155. }
  156. #endif
  157. bool TFileStat::operator==(const TFileStat& other) const noexcept = default;
  158. void TFileStat::MakeFromFileName(const char* fileName, bool nofollow) {
  159. TSystemFStat st;
  160. if (GetStatByName(st, fileName, nofollow)) {
  161. MakeStat(*this, st);
  162. } else {
  163. *this = TFileStat();
  164. }
  165. }
  166. TFileStat::TFileStat(const TFsPath& fileName, bool nofollow) {
  167. MakeFromFileName(fileName.GetPath().data(), nofollow);
  168. }
  169. TFileStat::TFileStat(const TString& fileName, bool nofollow) {
  170. MakeFromFileName(fileName.data(), nofollow);
  171. }
  172. TFileStat::TFileStat(const char* fileName, bool nofollow) {
  173. MakeFromFileName(fileName, nofollow);
  174. }
  175. bool TFileStat::IsNull() const noexcept {
  176. return *this == TFileStat();
  177. }
  178. bool TFileStat::IsFile() const noexcept {
  179. return S_ISREG(Mode);
  180. }
  181. bool TFileStat::IsDir() const noexcept {
  182. return S_ISDIR(Mode);
  183. }
  184. bool TFileStat::IsSymlink() const noexcept {
  185. return S_ISLNK(Mode);
  186. }
  187. i64 GetFileLength(FHANDLE fd) {
  188. #if defined(_win_)
  189. LARGE_INTEGER pos;
  190. if (!::GetFileSizeEx(fd, &pos))
  191. return -1L;
  192. return pos.QuadPart;
  193. #elif defined(_unix_)
  194. struct stat statbuf;
  195. if (::fstat(fd, &statbuf) != 0) {
  196. return -1L;
  197. }
  198. if (!(statbuf.st_mode & (S_IFREG | S_IFBLK | S_IFCHR))) {
  199. // st_size only makes sense for regular files or devices
  200. errno = EINVAL;
  201. return -1L;
  202. }
  203. return statbuf.st_size;
  204. #else
  205. #error unsupported platform
  206. #endif
  207. }
  208. i64 GetFileLength(const char* name) {
  209. #if defined(_win_)
  210. WIN32_FIND_DATA fData;
  211. HANDLE h = FindFirstFileA(name, &fData);
  212. if (h == INVALID_HANDLE_VALUE)
  213. return -1;
  214. FindClose(h);
  215. return (((i64)fData.nFileSizeHigh) * (i64(MAXDWORD) + 1)) + (i64)fData.nFileSizeLow;
  216. #elif defined(_unix_)
  217. struct stat buf;
  218. int r = ::stat(name, &buf);
  219. if (r == -1) {
  220. return -1;
  221. }
  222. if (!(buf.st_mode & (S_IFREG | S_IFBLK | S_IFCHR))) {
  223. // st_size only makes sense for regular files or devices
  224. errno = EINVAL;
  225. return -1;
  226. }
  227. return (i64)buf.st_size;
  228. #else
  229. #error unsupported platform
  230. #endif
  231. }
  232. i64 GetFileLength(const TString& name) {
  233. return GetFileLength(name.data());
  234. }