Browse Source

Add construction of TFileStat from struct stat

spreis 1 year ago
parent
commit
1738956637
2 changed files with 19 additions and 5 deletions
  1. 17 5
      util/system/fstat.cpp
  2. 2 0
      util/system/fstat.h

+ 17 - 5
util/system/fstat.cpp

@@ -63,24 +63,32 @@ struct TSystemFStat: public BY_HANDLE_FILE_INFORMATION {
     ULONG ReparseTag = 0;
 };
 
-#else
-
+#elif defined(_unix_)
 using TSystemFStat = struct stat;
-
+#else
+    #error unsupported platform
 #endif
 
-static void MakeStat(TFileStat& st, const TSystemFStat& fs) {
-#ifdef _unix_
+static void MakeStatFromStructStat(TFileStat& st, const struct stat& fs) {
     st.Mode = fs.st_mode;
     st.NLinks = fs.st_nlink;
     st.Uid = fs.st_uid;
     st.Gid = fs.st_gid;
     st.Size = fs.st_size;
+#ifdef _unix_
     st.AllocationSize = fs.st_blocks * 512;
+#else
+    st.AllocationSize = st.Size; // FIXME
+#endif
     st.ATime = fs.st_atime;
     st.MTime = fs.st_mtime;
     st.CTime = fs.st_ctime;
     st.INode = fs.st_ino;
+}
+
+static void MakeStat(TFileStat& st, const TSystemFStat& fs) {
+#ifdef _unix_
+    MakeStatFromStructStat(st, fs);
 #else
     timeval tv;
     FileTimeToTimeval(&fs.ftCreationTime, &tv);
@@ -142,6 +150,10 @@ TFileStat::TFileStat(FHANDLE f) {
     }
 }
 
+TFileStat::TFileStat(const struct stat& st) {
+    MakeStatFromStructStat(*this, st);
+}
+
 void TFileStat::MakeFromFileName(const char* fileName, bool nofollow) {
     TSystemFStat st;
     if (GetStatByName(st, fileName, nofollow)) {

+ 2 - 0
util/system/fstat.h

@@ -5,6 +5,7 @@
 
 class TFile;
 class TFsPath;
+struct stat;
 
 struct TFileStat {
     ui32 Mode = 0; /* protection */
@@ -29,6 +30,7 @@ public:
     bool IsDir() const noexcept;
     bool IsSymlink() const noexcept;
 
+    explicit TFileStat(const struct stat& fs);
     explicit TFileStat(const TFile& f);
     explicit TFileStat(FHANDLE f);
     TFileStat(const TFsPath& fileName, bool nofollow = false);