diff th_file.c @ 475:77d1af382e08

Improve th_stat_path() by changing flags into a struct and adding more information.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 09 Jul 2018 07:28:13 +0300
parents 85fa3d333556
children b1e80180818a
line wrap: on
line diff
--- a/th_file.c	Mon Jul 02 00:01:50 2018 +0300
+++ b/th_file.c	Mon Jul 09 07:28:13 2018 +0300
@@ -69,26 +69,20 @@
 }
 
 
-BOOL th_stat_path(const char *path, int *flags)
+BOOL th_stat_path(const char *path, th_stat_data *data)
 {
-    *flags = 0;
-
-#ifdef TH_PLAT_WINDOWS
-    DWORD attr = GetFileAttributes(path);
-
-    *flags |= (attr & FILE_ATTRIBUTE_DIRECTORY) ? TH_IS_DIR : 0;
-    *flags |= (attr & FILE_ATTRIBUTE_READONLY) ? 0 : TH_IS_WRITABLE;
-    *flags |= TH_IS_READABLE;
-#else
     uid_t id = geteuid();
     struct stat sb;
     if (stat(path, &sb) < 0)
         return FALSE;
 
-    *flags |= S_ISDIR(sb.st_mode) ? TH_IS_DIR : 0;
-    *flags |= (id == sb.st_uid && (sb.st_mode & S_IWUSR)) ? TH_IS_WRITABLE : 0;
-    *flags |= (id == sb.st_uid && (sb.st_mode & S_IRUSR)) ? TH_IS_READABLE : 0;
-#endif
+    data->size   = sb.st_size;
+    data->atime  = sb.st_atime;
+    data->mtime  = sb.st_mtime;
+
+    data->flags  = S_ISDIR(sb.st_mode) ? TH_IS_DIR : 0;
+    data->flags |= (id == sb.st_uid && (sb.st_mode & S_IWUSR)) ? TH_IS_WRITABLE : 0;
+    data->flags |= (id == sb.st_uid && (sb.st_mode & S_IRUSR)) ? TH_IS_READABLE : 0;
 
     return TRUE;
 }
@@ -119,9 +113,9 @@
         // If the element is there, create it
         if (path[start] != 0)
         {
-            int flags;
-            BOOL exists = th_stat_path(path, &flags);
-            if (exists && (flags & TH_IS_DIR) == 0)
+            th_stat_data sdata;
+            BOOL exists = th_stat_path(path, &sdata);
+            if (exists && (sdata.flags & TH_IS_DIR) == 0)
                 goto error;
 
             if (!exists)