Mercurial > hg > nnchat
changeset 548:28688eb812de
Add helper functions for checking if target path is writable, readable or directory and creating directory structures recursively.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 12 Nov 2012 14:18:09 +0200 |
parents | 1176c4b9ed93 |
children | dd7b58eca06d |
files | main.c |
diffstat | 1 files changed, 62 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/main.c Mon Nov 12 11:45:35 2012 +0200 +++ b/main.c Mon Nov 12 14:18:09 2012 +0200 @@ -19,6 +19,7 @@ #include <sys/wait.h> #include <sys/stat.h> #include <sys/types.h> +#include <unistd.h> #endif #ifdef __WIN32 @@ -1578,6 +1579,67 @@ } +BOOL nn_mkdir_rec(const char *cpath) +{ + char save, *path = th_strdup(cpath); + size_t start = 0, end; + BOOL res = FALSE; + + do + { + for (save = 0, end = start; path[end] != 0; end++) + if (path[end] == SET_DIR_SEPARATOR) + { + save = path[end]; + path[end] = 0; + break; + } + + if (path[start] != 0) + { +#ifdef __WIN32 + if (!CreateDirectory(path, NULL)) + goto error; +#else + if (mkdir(path, 711) < 0) + goto error; +#endif + } + + path[end] = save; + start = end + 1; + } while (save != 0); + + res = TRUE; + +error: + th_free(path); + return res; +} + + +BOOL nn_stat_path(const char *path, BOOL *isDirectory, BOOL *isWritable, BOOL *isReadable) +{ +#ifdef __WIN32 + DWORD attr = GetFileAttributes(path); + + *isDirectory = (attr & FILE_ATTRIBUTE_DIRECTORY); + *isWritable = (attr & FILE_ATTRIBUTE_READONLY) == 0 && *isDirectory == FALSE; + *isReadable = TRUE; +#else + uid_t id = geteuid(); + struct stat sb; + if (stat(path, &sb) < 0) + return FALSE; + + *isDirectory = (S_ISDIR(sb.st_mode)); + *isWritable = (id == sb.st_uid && (sb.st_mode & S_IWUSR)); + *isReadable = (id == sb.st_uid && (sb.st_mode & S_IRUSR)); +#endif + return TRUE; +} + + int main(int argc, char *argv[]) { char *tmpStr;