fstat.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. }
  40. mode |= GetWinFileType(fileAttributes, reparseTag);
  41. if ((fileAttributes & FILE_ATTRIBUTE_READONLY) == 0) {
  42. mode |= _S_IWRITE;
  43. }
  44. return mode;
  45. }
  46. #define S_ISDIR(st_mode) (st_mode & _S_IFDIR)
  47. #define S_ISREG(st_mode) (st_mode & _S_IFREG)
  48. #define S_ISLNK(st_mode) (st_mode & _S_IFLNK)
  49. struct TSystemFStat: public BY_HANDLE_FILE_INFORMATION {
  50. ULONG ReparseTag = 0;
  51. };
  52. #elif defined(_unix_)
  53. using TSystemFStat = struct stat;
  54. #else
  55. #error unsupported platform
  56. #endif
  57. #if defined(_unix_)
  58. static void MakeStatFromStructStat(TFileStat& st, const struct stat& fs) {
  59. st.Mode = fs.st_mode;
  60. st.NLinks = fs.st_nlink;
  61. st.Uid = fs.st_uid;
  62. st.Gid = fs.st_gid;
  63. st.Size = fs.st_size;
  64. st.AllocationSize = fs.st_blocks * 512;
  65. #if defined(_linux_)
  66. st.ATime = fs.st_atim.tv_sec;
  67. st.ATimeNSec = fs.st_atim.tv_nsec;
  68. st.MTime = fs.st_mtim.tv_sec;
  69. st.MTimeNSec = fs.st_mtim.tv_nsec;
  70. st.CTime = fs.st_ctim.tv_sec;
  71. st.CTimeNSec = fs.st_ctim.tv_nsec;
  72. #elif defined(_darwin_)
  73. st.ATime = fs.st_atimespec.tv_sec;
  74. st.ATimeNSec = fs.st_atimespec.tv_nsec;
  75. st.MTime = fs.st_mtimespec.tv_sec;
  76. st.MTimeNSec = fs.st_mtimespec.tv_nsec;
  77. st.CTime = fs.st_birthtimespec.tv_sec;
  78. st.CTimeNSec = fs.st_birthtimespec.tv_nsec;
  79. #else
  80. // Fallback.
  81. st.ATime = fs.st_atime;
  82. st.MTime = fs.st_mtime;
  83. st.CTime = fs.st_ctime;
  84. #endif
  85. st.INode = fs.st_ino;
  86. }
  87. #endif
  88. static void MakeStat(TFileStat& st, const TSystemFStat& fs) {
  89. #ifdef _unix_
  90. MakeStatFromStructStat(st, fs);
  91. #else
  92. timespec timeSpec;
  93. FileTimeToTimespec(fs.ftCreationTime, &timeSpec);
  94. st.CTime = timeSpec.tv_sec;
  95. st.CTimeNSec = timeSpec.tv_nsec;
  96. FileTimeToTimespec(fs.ftLastAccessTime, &timeSpec);
  97. st.ATime = timeSpec.tv_sec;
  98. st.ATimeNSec = timeSpec.tv_nsec;
  99. FileTimeToTimespec(fs.ftLastWriteTime, &timeSpec);
  100. st.MTime = timeSpec.tv_sec;
  101. st.MTimeNSec = timeSpec.tv_nsec;
  102. st.NLinks = fs.nNumberOfLinks;
  103. st.Mode = GetFileMode(fs.dwFileAttributes, fs.ReparseTag);
  104. st.Uid = 0;
  105. st.Gid = 0;
  106. st.Size = ((ui64)fs.nFileSizeHigh << 32) | fs.nFileSizeLow;
  107. st.AllocationSize = st.Size; // FIXME
  108. st.INode = ((ui64)fs.nFileIndexHigh << 32) | fs.nFileIndexLow;
  109. #endif
  110. }
  111. static bool GetStatByHandle(TSystemFStat& fs, FHANDLE f) {
  112. #ifdef _win_
  113. if (!GetFileInformationByHandle(f, &fs)) {
  114. return false;
  115. }
  116. if (fs.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
  117. fs.ReparseTag = NFsPrivate::WinReadReparseTag(f);
  118. }
  119. return true;
  120. #else
  121. return !fstat(f, &fs);
  122. #endif
  123. }
  124. static bool GetStatByName(TSystemFStat& fs, const char* fileName, bool nofollow) {
  125. #ifdef _win_
  126. TFileHandle h = NFsPrivate::CreateFileWithUtf8Name(
  127. fileName,
  128. FILE_READ_ATTRIBUTES | FILE_READ_EA,
  129. FILE_SHARE_READ | FILE_SHARE_WRITE,
  130. OPEN_EXISTING,
  131. (nofollow ? FILE_FLAG_OPEN_REPARSE_POINT : 0) | FILE_FLAG_BACKUP_SEMANTICS,
  132. true);
  133. if (!h.IsOpen()) {
  134. return false;
  135. }
  136. return GetStatByHandle(fs, h);
  137. #else
  138. return !(nofollow ? lstat : stat)(fileName, &fs);
  139. #endif
  140. }
  141. TFileStat::TFileStat() = default;
  142. TFileStat::TFileStat(const TFile& f) {
  143. *this = TFileStat(f.GetHandle());
  144. }
  145. TFileStat::TFileStat(FHANDLE f) {
  146. TSystemFStat st;
  147. if (GetStatByHandle(st, f)) {
  148. MakeStat(*this, st);
  149. } else {
  150. *this = TFileStat();
  151. }
  152. }
  153. #if defined(_unix_)
  154. TFileStat::TFileStat(const struct stat& st) {
  155. MakeStatFromStructStat(*this, st);
  156. }
  157. #endif
  158. bool TFileStat::operator==(const TFileStat& other) const noexcept = default;
  159. void TFileStat::MakeFromFileName(const char* fileName, bool nofollow) {
  160. TSystemFStat st;
  161. if (GetStatByName(st, fileName, nofollow)) {
  162. MakeStat(*this, st);
  163. } else {
  164. *this = TFileStat();
  165. }
  166. }
  167. TFileStat::TFileStat(const TFsPath& fileName, bool nofollow) {
  168. MakeFromFileName(fileName.GetPath().data(), nofollow);
  169. }
  170. TFileStat::TFileStat(const TString& fileName, bool nofollow) {
  171. MakeFromFileName(fileName.data(), nofollow);
  172. }
  173. TFileStat::TFileStat(const char* fileName, bool nofollow) {
  174. MakeFromFileName(fileName, nofollow);
  175. }
  176. bool TFileStat::IsNull() const noexcept {
  177. return *this == TFileStat();
  178. }
  179. bool TFileStat::IsFile() const noexcept {
  180. return S_ISREG(Mode);
  181. }
  182. bool TFileStat::IsDir() const noexcept {
  183. return S_ISDIR(Mode);
  184. }
  185. bool TFileStat::IsSymlink() const noexcept {
  186. return S_ISLNK(Mode);
  187. }
  188. i64 GetFileLength(FHANDLE fd) {
  189. #if defined(_win_)
  190. LARGE_INTEGER pos;
  191. if (!::GetFileSizeEx(fd, &pos)) {
  192. return -1L;
  193. }
  194. return pos.QuadPart;
  195. #elif defined(_unix_)
  196. struct stat statbuf;
  197. if (::fstat(fd, &statbuf) != 0) {
  198. return -1L;
  199. }
  200. if (!(statbuf.st_mode & (S_IFREG | S_IFBLK | S_IFCHR))) {
  201. // st_size only makes sense for regular files or devices
  202. errno = EINVAL;
  203. return -1L;
  204. }
  205. return statbuf.st_size;
  206. #else
  207. #error unsupported platform
  208. #endif
  209. }
  210. i64 GetFileLength(const char* name) {
  211. #if defined(_win_)
  212. WIN32_FIND_DATA fData;
  213. HANDLE h = FindFirstFileA(name, &fData);
  214. if (h == INVALID_HANDLE_VALUE) {
  215. return -1;
  216. }
  217. FindClose(h);
  218. return (((i64)fData.nFileSizeHigh) * (i64(MAXDWORD) + 1)) + (i64)fData.nFileSizeLow;
  219. #elif defined(_unix_)
  220. struct stat buf;
  221. int r = ::stat(name, &buf);
  222. if (r == -1) {
  223. return -1;
  224. }
  225. if (!(buf.st_mode & (S_IFREG | S_IFBLK | S_IFCHR))) {
  226. // st_size only makes sense for regular files or devices
  227. errno = EINVAL;
  228. return -1;
  229. }
  230. return (i64)buf.st_size;
  231. #else
  232. #error unsupported platform
  233. #endif
  234. }
  235. i64 GetFileLength(const TString& name) {
  236. return GetFileLength(name.data());
  237. }