# HG changeset patch # User Matti Hamalainen # Date 1352722689 -7200 # Node ID 28688eb812de5aa9c821268ec270fee397b34f0a # Parent 1176c4b9ed93eac9f019f5f02e76bfaeca66c195 Add helper functions for checking if target path is writable, readable or directory and creating directory structures recursively. diff -r 1176c4b9ed93 -r 28688eb812de main.c --- 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 #include #include +#include #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;