Mercurial > hg > th-libs
changeset 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 | b3b8f90bbbe6 |
children | 61fed1286780 |
files | th_file.c th_file.h |
diffstat | 2 files changed, 20 insertions(+), 18 deletions(-) [+] |
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)
--- 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);