From 07a05e6edbf20016836a7c47d8cb8102fbfed86b Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 12 Nov 2022 17:54:30 -0500 Subject: Use zix_dir_for_each() --- src/filesystem.c | 42 ----------------------------------- src/filesystem.h | 16 -------------- src/util.c | 3 ++- src/world.c | 4 ++-- test/test_filesystem.c | 59 -------------------------------------------------- test/test_state.c | 22 +++++++++---------- 6 files changed, 15 insertions(+), 131 deletions(-) diff --git a/src/filesystem.c b/src/filesystem.c index 584eaad..734e258 100644 --- a/src/filesystem.c +++ b/src/filesystem.c @@ -9,13 +9,6 @@ #include "zix/filesystem.h" #include "zix/path.h" -#ifdef _WIN32 -# include -# include -#else -# include -#endif - #include #include #include @@ -147,41 +140,6 @@ lilv_path_filename(const char* path) return ret; } -void -lilv_dir_for_each(const char* path, - void* data, - void (*f)(const char* path, const char* name, void* data)) -{ -#ifdef _WIN32 - - char* pat = zix_path_join(NULL, path, "*"); - WIN32_FIND_DATA fd; - HANDLE fh = FindFirstFile(pat, &fd); - if (fh != INVALID_HANDLE_VALUE) { - do { - if (strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..")) { - f(path, fd.cFileName, data); - } - } while (FindNextFile(fh, &fd)); - } - FindClose(fh); - free(pat); - -#else - - DIR* dir = opendir(path); - if (dir) { - for (struct dirent* entry = NULL; (entry = readdir(dir));) { - if (strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")) { - f(path, entry->d_name, data); - } - } - closedir(dir); - } - -#endif -} - char* lilv_create_temporary_directory(const char* pattern) { diff --git a/src/filesystem.h b/src/filesystem.h index d2f14b6..5dfd3d7 100644 --- a/src/filesystem.h +++ b/src/filesystem.h @@ -33,22 +33,6 @@ lilv_path_parent(const char* path); char* lilv_path_filename(const char* path); -/** - Visit every file in the directory at `path`. - - @param path A path to a directory. - - @param data Opaque user data that is passed to `f`. - - @param f A function called on every entry in the directory. The `path` - parameter is always the directory path passed to this function, the `name` - parameter is the name of the directory entry (not its full path). -*/ -void -lilv_dir_for_each(const char* path, - void* data, - void (*f)(const char* path, const char* name, void* data)); - /** Create a unique temporary directory. diff --git a/src/util.c b/src/util.c index bed1a3d..e573a58 100644 --- a/src/util.c +++ b/src/util.c @@ -7,6 +7,7 @@ #include "lilv/lilv.h" #include "serd/serd.h" #include "zix/allocator.h" +#include "zix/filesystem.h" #include "zix/path.h" #include @@ -271,7 +272,7 @@ lilv_get_latest_copy(const char* path, const char* copy_path) LILV_ERRORF("stat(%s) (%s)\n", path, strerror(errno)); } - lilv_dir_for_each(copy_dir, &latest, update_latest); + zix_dir_for_each(copy_dir, &latest, update_latest); free(latest.pattern); free(copy_dir); diff --git a/src/world.c b/src/world.c index af0ab06..23116e9 100644 --- a/src/world.c +++ b/src/world.c @@ -1,13 +1,13 @@ // Copyright 2007-2019 David Robillard // SPDX-License-Identifier: ISC -#include "filesystem.h" #include "lilv_config.h" // IWYU pragma: keep #include "lilv_internal.h" #include "lilv/lilv.h" #include "serd/serd.h" #include "sord/sord.h" +#include "zix/filesystem.h" #include "zix/tree.h" #include "lv2/core/lv2.h" @@ -957,7 +957,7 @@ lilv_world_load_directory(LilvWorld* world, const char* dir_path) { char* path = lilv_expand(dir_path); if (path) { - lilv_dir_for_each(path, world, load_dir_entry); + zix_dir_for_each(path, world, load_dir_entry); free(path); } } diff --git a/test/test_filesystem.c b/test/test_filesystem.c index f788e90..4ed1d5e 100644 --- a/test/test_filesystem.c +++ b/test/test_filesystem.c @@ -3,16 +3,10 @@ #undef NDEBUG -#include "lilv_internal.h" - #include "../src/filesystem.h" -#include "zix/filesystem.h" -#include "zix/path.h" - #include #include -#include #include #include @@ -104,58 +98,6 @@ test_path_filename(void) #endif } -typedef struct { - size_t n_names; - char** names; -} FileList; - -static void -visit(const char* const path, const char* const name, void* const data) -{ - (void)path; - - FileList* file_list = (FileList*)data; - - file_list->names = - (char**)realloc(file_list->names, sizeof(char*) * ++file_list->n_names); - - file_list->names[file_list->n_names - 1] = lilv_strdup(name); -} - -static void -test_dir_for_each(void) -{ - char* const temp_dir = lilv_create_temporary_directory("lilvXXXXXX"); - char* const path1 = zix_path_join(NULL, temp_dir, "lilv_test_1"); - char* const path2 = zix_path_join(NULL, temp_dir, "lilv_test_2"); - - FILE* const f1 = fopen(path1, "w"); - FILE* const f2 = fopen(path2, "w"); - fprintf(f1, "test\n"); - fprintf(f2, "test\n"); - fclose(f2); - fclose(f1); - - FileList file_list = {0, NULL}; - lilv_dir_for_each(temp_dir, &file_list, visit); - - assert((!strcmp(file_list.names[0], "lilv_test_1") && - !strcmp(file_list.names[1], "lilv_test_2")) || - (!strcmp(file_list.names[0], "lilv_test_2") && - !strcmp(file_list.names[1], "lilv_test_1"))); - - assert(!zix_remove(path2)); - assert(!zix_remove(path1)); - assert(!zix_remove(temp_dir)); - - free(file_list.names[0]); - free(file_list.names[1]); - free(file_list.names); - free(path2); - free(path1); - free(temp_dir); -} - int main(void) { @@ -163,7 +105,6 @@ main(void) test_path_relative_to(); test_path_parent(); test_path_filename(); - test_dir_for_each(); return 0; } diff --git a/test/test_state.c b/test/test_state.c index 26d8d97..f29949e 100644 --- a/test/test_state.c +++ b/test/test_state.c @@ -155,9 +155,9 @@ remove_file(const char* path, const char* name, void* data) static void cleanup_test_directories(const TestDirectories dirs) { - lilv_dir_for_each(dirs.scratch, NULL, remove_file); - lilv_dir_for_each(dirs.copy, NULL, remove_file); - lilv_dir_for_each(dirs.link, NULL, remove_file); + zix_dir_for_each(dirs.scratch, NULL, remove_file); + zix_dir_for_each(dirs.copy, NULL, remove_file); + zix_dir_for_each(dirs.link, NULL, remove_file); assert(!zix_remove(dirs.link)); assert(!zix_remove(dirs.copy)); @@ -661,8 +661,8 @@ test_to_files(void) assert(zix_file_equals(NULL, recfile_link_2, recfile_copy_2)); lilv_instance_free(instance); - lilv_dir_for_each(bundle_2_path, NULL, remove_file); - lilv_dir_for_each(bundle_1_path, NULL, remove_file); + zix_dir_for_each(bundle_2_path, NULL, remove_file); + zix_dir_for_each(bundle_1_path, NULL, remove_file); assert(!zix_remove(bundle_2_path)); assert(!zix_remove(bundle_1_path)); cleanup_test_directories(dirs); @@ -741,7 +741,7 @@ test_multi_save(void) assert(count_statements(state_path) == 21); lilv_instance_free(instance); - lilv_dir_for_each(bundle_1_path, NULL, remove_file); + zix_dir_for_each(bundle_1_path, NULL, remove_file); zix_remove(bundle_1_path); cleanup_test_directories(dirs); @@ -847,9 +847,9 @@ test_files_round_trip(void) assert(!lilv_state_equals(state_1_1_loaded, state_2_loaded)); lilv_instance_free(instance); - lilv_dir_for_each(bundle_1_1_path, NULL, remove_file); - lilv_dir_for_each(bundle_1_2_path, NULL, remove_file); - lilv_dir_for_each(bundle_2_path, NULL, remove_file); + zix_dir_for_each(bundle_1_1_path, NULL, remove_file); + zix_dir_for_each(bundle_1_2_path, NULL, remove_file); + zix_dir_for_each(bundle_2_path, NULL, remove_file); zix_remove(bundle_1_1_path); zix_remove(bundle_1_2_path); zix_remove(bundle_2_path); @@ -1066,7 +1066,7 @@ test_delete(void) // Count the number of shared files before doing anything unsigned n_shared_files_before = 0; - lilv_dir_for_each(dirs.shared, &n_shared_files_before, count_file); + zix_dir_for_each(dirs.shared, &n_shared_files_before, count_file); lilv_instance_free(instance); @@ -1075,7 +1075,7 @@ test_delete(void) // Ensure the number of shared files is the same after deletion unsigned n_shared_files_after = 0; - lilv_dir_for_each(dirs.shared, &n_shared_files_after, count_file); + zix_dir_for_each(dirs.shared, &n_shared_files_after, count_file); assert(n_shared_files_before == n_shared_files_after); // Ensure the state directory has been deleted -- cgit v1.2.1