diff options
author | David Robillard <d@drobilla.net> | 2023-08-23 17:58:15 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-08-23 18:58:46 -0400 |
commit | 1420be7916429425f826a214d2bc7e1e19ae11e8 (patch) | |
tree | 4e7b6acb766d54bc559b810a3ef617c2c78d597c /src | |
parent | 4f1822cd1e428252b0dfc5ba88c1147e91aa7aa8 (diff) | |
download | zix-1420be7916429425f826a214d2bc7e1e19ae11e8.tar.gz zix-1420be7916429425f826a214d2bc7e1e19ae11e8.tar.bz2 zix-1420be7916429425f826a214d2bc7e1e19ae11e8.zip |
Improve test coverage
Diffstat (limited to 'src')
-rw-r--r-- | src/filesystem.c | 40 | ||||
-rw-r--r-- | src/path.c | 4 | ||||
-rw-r--r-- | src/posix/filesystem_posix.c | 17 |
3 files changed, 32 insertions, 29 deletions
diff --git a/src/filesystem.c b/src/filesystem.c index c3e8f10..56e1fb2 100644 --- a/src/filesystem.c +++ b/src/filesystem.c @@ -37,31 +37,35 @@ zix_create_directories(ZixAllocator* const allocator, // Allocate a working copy of the path to chop along the way const size_t path_len = strlen(dir_path); char* const path = (char*)zix_malloc(allocator, path_len + 1U); - memcpy(path, dir_path, path_len + 1U); - - // Start at the root directory (past any name) - ZixPathIter p = zix_path_begin(path); - while (p.state < ZIX_PATH_FILE_NAME) { - p = zix_path_next(path, p); - } + ZixStatus st = path ? ZIX_STATUS_SUCCESS : ZIX_STATUS_NO_MEM; + if (path) { + // Copy directory path as prefix + memcpy(path, dir_path, path_len + 1U); + + // Start at the root directory (past any name) + ZixPathIter p = zix_path_begin(path); + while (p.state < ZIX_PATH_FILE_NAME) { + p = zix_path_next(path, p); + } - // Create each directory down the path - ZixStatus st = ZIX_STATUS_SUCCESS; - while (p.state != ZIX_PATH_END) { - const char old_end = path[p.range.end]; + // Create each directory down the path + while (p.state != ZIX_PATH_END) { + const char old_end = path[p.range.end]; - path[p.range.end] = '\0'; - if (zix_file_type(path) != ZIX_FILE_TYPE_DIRECTORY) { - if ((st = zix_create_directory(path))) { - break; + path[p.range.end] = '\0'; + if (zix_file_type(path) != ZIX_FILE_TYPE_DIRECTORY) { + if ((st = zix_create_directory(path))) { + break; + } } + + path[p.range.end] = old_end; + p = zix_path_next(path, p); } - path[p.range.end] = old_end; - p = zix_path_next(path, p); + zix_free(allocator, path); } - zix_free(allocator, path); return st; } @@ -435,9 +435,7 @@ zix_path_begin(const char* const path) { const ZixPathIter iter = {zix_path_root_name_range(path), ZIX_PATH_ROOT_NAME}; - return (iter.range.end > iter.range.begin) ? iter - : path ? zix_path_next(path, iter) - : zix_path_next("", iter); + return (iter.range.end > iter.range.begin) ? iter : zix_path_next(path, iter); } ZixPathIter diff --git a/src/posix/filesystem_posix.c b/src/posix/filesystem_posix.c index 8d65611..a5ac857 100644 --- a/src/posix/filesystem_posix.c +++ b/src/posix/filesystem_posix.c @@ -429,22 +429,23 @@ zix_current_path(ZixAllocator* const allocator) { #if defined(PATH_MAX) // Some POSIX systems have a static PATH_MAX so we can store it on the stack - char buffer[PATH_MAX] = {0}; - if (getcwd(buffer, PATH_MAX)) { - return copy_path(allocator, buffer, strlen(buffer)); - } + char buffer[PATH_MAX] = {0}; + char* const cwd = getcwd(buffer, PATH_MAX); + return cwd ? copy_path(allocator, cwd, strlen(cwd)) : NULL; #elif USE_PATHCONF // Others don't so we have to query PATH_MAX at runtime to allocate the result const size_t size = max_path_size(); char* const buffer = (char*)zix_calloc(allocator, size, 1); char* const current = getcwd(buffer, size); - if (current) { - return current; + if (!current) { + zix_free(allocator, buffer); } - zix_free(allocator, buffer); -#endif + return current; +#else return NULL; + +#endif } |