# HG changeset patch # User Matti Hamalainen # Date 1531110493 -10800 # Node ID 77d1af382e08f526bceee8918ef17d97a0f77aea # Parent b3b8f90bbbe660b4ac827130cd8b971195923b1f Improve th_stat_path() by changing flags into a struct and adding more information. diff -r b3b8f90bbbe6 -r 77d1af382e08 th_file.c --- 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) diff -r b3b8f90bbbe6 -r 77d1af382e08 th_file.h --- a/th_file.h Mon Jul 02 00:01:50 2018 +0300 +++ b/th_file.h Mon Jul 09 07:28:13 2018 +0300 @@ -35,10 +35,18 @@ }; +typedef struct +{ + int flags; + off_t size; + uint64_t atime, mtime; +} th_stat_data; + + char * th_get_home_dir(); char * th_get_config_dir(const char *name); -BOOL th_stat_path(const char *path, int *flags); +BOOL th_stat_path(const char *path, th_stat_data *data); BOOL th_mkdir_path(const char *cpath, int mode);