From 67a33e70aa2b3f2f78742e773bac5ccb7be95c20 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 12 Nov 2022 17:54:11 -0500 Subject: Use zix_file_type() and zix_symlink_type() --- src/filesystem.c | 12 ------------ src/filesystem.h | 4 ---- src/state.c | 15 ++++++++------- test/test_filesystem.c | 26 +++----------------------- test/test_state.c | 29 ++++++++++++++++++----------- 5 files changed, 29 insertions(+), 57 deletions(-) diff --git a/src/filesystem.c b/src/filesystem.c index 3c4cbbf..7897920 100644 --- a/src/filesystem.c +++ b/src/filesystem.c @@ -9,7 +9,6 @@ # include # include # include -# define F_OK 0 # define mkdir(path, flags) _mkdir(path) # define S_ISDIR(mode) (((mode)&S_IFMT) == S_IFDIR) #else @@ -275,17 +274,6 @@ lilv_path_canonical(const char* path) #endif } -bool -lilv_path_exists(const char* path) -{ -#if USE_LSTAT - struct stat st; - return !lstat(path, &st); -#else - return !access(path, F_OK); -#endif -} - bool lilv_is_directory(const char* path) { diff --git a/src/filesystem.h b/src/filesystem.h index dfff4c9..424afc6 100644 --- a/src/filesystem.h +++ b/src/filesystem.h @@ -77,10 +77,6 @@ lilv_path_join(const char* a, const char* b); char* lilv_path_canonical(const char* path); -/// Return true iff `path` points to an existing file system entry -bool -lilv_path_exists(const char* path); - /// Return true iff `path` points to an existing directory bool lilv_is_directory(const char* path); diff --git a/src/state.c b/src/state.c index a4fa11c..d215c15 100644 --- a/src/state.c +++ b/src/state.c @@ -1,4 +1,4 @@ -// Copyright 2007-2019 David Robillard +// Copyright 2007-2022 David Robillard // SPDX-License-Identifier: ISC #include "filesystem.h" @@ -8,6 +8,7 @@ #include "serd/serd.h" #include "sord/sord.h" #include "sratom/sratom.h" +#include "zix/filesystem.h" #include "zix/tree.h" #include "lv2/atom/atom.h" @@ -235,7 +236,7 @@ path_exists(const char* path, const void* ignored) { (void)ignored; - return lilv_path_exists(path); + return zix_file_type(path) != ZIX_FILE_TYPE_NONE; } static bool @@ -913,7 +914,7 @@ add_state_to_manifest(LilvWorld* lworld, SerdEnv* env = serd_env_new(&manifest); SordModel* model = sord_new(world, SORD_SPO, false); - if (lilv_path_exists(manifest_path)) { + if (zix_file_type(manifest_path) == ZIX_FILE_TYPE_REGULAR) { // Read manifest into model SerdReader* reader = sord_new_reader(model, env, SERD_TURTLE, NULL); SerdStatus st = serd_reader_read_file(reader, manifest.buf); @@ -997,7 +998,7 @@ static bool link_exists(const char* path, const void* data) { const char* target = (const char*)data; - if (!lilv_path_exists(path)) { + if (zix_file_type(path) == ZIX_FILE_TYPE_NONE) { return false; } char* real_path = lilv_path_canonical(path); @@ -1183,7 +1184,7 @@ lilv_state_make_links(const LilvState* state, const char* dir) } else { // Make a link in the link directory to external file char* lpath = lilv_find_free_path(pat, link_exists, pm->abs); - if (!lilv_path_exists(lpath)) { + if (zix_file_type(lpath) == ZIX_FILE_TYPE_NONE) { if (lilv_symlink(pm->abs, lpath)) { LILV_ERRORF("Failed to link %s => %s (%s)\n", pm->abs, @@ -1295,7 +1296,7 @@ static void try_unlink(const char* state_dir, const char* path) { if (!strncmp(state_dir, path, strlen(state_dir))) { - if (lilv_path_exists(path) && lilv_remove(path)) { + if (zix_file_type(path) != ZIX_FILE_TYPE_NONE && lilv_remove(path)) { LILV_ERRORF("Failed to remove %s (%s)\n", path, strerror(errno)); } } @@ -1322,7 +1323,7 @@ lilv_state_delete(LilvWorld* world, const LilvState* state) LilvNode* bundle = lilv_new_file_uri(world, NULL, state->dir); LilvNode* manifest = lilv_world_get_manifest_uri(world, bundle); char* manifest_path = get_canonical_path(manifest); - const bool has_manifest = lilv_path_exists(manifest_path); + const bool has_manifest = zix_file_type(manifest_path) != ZIX_FILE_TYPE_NONE; SordModel* model = sord_new(world->world, SORD_SPO, false); if (has_manifest) { diff --git a/test/test_filesystem.c b/test/test_filesystem.c index 610021d..a0e3bc8 100644 --- a/test/test_filesystem.c +++ b/test/test_filesystem.c @@ -7,6 +7,8 @@ #include "../src/filesystem.h" +#include "zix/filesystem.h" + #include #include #include @@ -280,27 +282,6 @@ test_path_canonical(void) free(temp_dir); } -static void -test_path_exists(void) -{ - char* const temp_dir = lilv_create_temporary_directory("lilvXXXXXX"); - char* const file_path = lilv_path_join(temp_dir, "lilv_test_file"); - - assert(!lilv_path_exists(file_path)); - - FILE* f = fopen(file_path, "w"); - fprintf(f, "test\n"); - fclose(f); - - assert(lilv_path_exists(file_path)); - - assert(!lilv_remove(file_path)); - assert(!lilv_remove(temp_dir)); - - free(file_path); - free(temp_dir); -} - static void test_is_directory(void) { @@ -337,7 +318,7 @@ test_copy_file(void) assert(!lilv_copy_file(file_path, copy_path)); assert(lilv_file_equals(file_path, copy_path)); - if (lilv_path_exists("/dev/full")) { + if (zix_file_type("/dev/full") != ZIX_FILE_TYPE_NONE) { // Copy short file (error after flushing) assert(lilv_copy_file(file_path, "/dev/full") == ENOSPC); @@ -527,7 +508,6 @@ main(void) test_path_filename(); test_path_join(); test_path_canonical(); - test_path_exists(); test_is_directory(); test_copy_file(); test_flock(); diff --git a/test/test_state.c b/test/test_state.c index d0ccdfe..524a495 100644 --- a/test/test_state.c +++ b/test/test_state.c @@ -13,6 +13,7 @@ #include "lv2/state/state.h" #include "lv2/urid/urid.h" #include "serd/serd.h" +#include "zix/filesystem.h" #ifdef _WIN32 # include @@ -585,7 +586,7 @@ test_to_files(void) // Check that the test plugin has made its recording scratch file char* const recfile_path = lilv_path_join(dirs.scratch, "recfile"); - assert(lilv_path_exists(recfile_path)); + assert(zix_file_type(recfile_path) == ZIX_FILE_TYPE_REGULAR); // Get state char* const bundle_1_path = lilv_path_join(dirs.top, "state1.lv2"); @@ -597,7 +598,7 @@ test_to_files(void) // Check that a snapshop of the recfile was created char* const recfile_copy_1 = lilv_path_join(dirs.copy, "recfile"); - assert(lilv_path_exists(recfile_copy_1)); + assert(zix_file_type(recfile_copy_1) == ZIX_FILE_TYPE_REGULAR); // Save state to a bundle assert(!lilv_state_save(ctx->env->world, @@ -610,14 +611,17 @@ test_to_files(void) // Check that a manifest exists char* const manifest_path = lilv_path_join(bundle_1_path, "manifest.ttl"); - assert(lilv_path_exists(manifest_path)); + assert(zix_file_type(manifest_path) == ZIX_FILE_TYPE_REGULAR); // Check that the expected statements are in the manifest file assert(count_statements(manifest_path) == 3); // Check that a link to the recfile exists in the saved bundle char* const recfile_link_1 = lilv_path_join(bundle_1_path, "recfile"); - assert(lilv_path_exists(recfile_link_1)); + assert(zix_file_type(recfile_link_1) == ZIX_FILE_TYPE_REGULAR); +#ifndef _WIN32 + assert(zix_symlink_type(recfile_link_1) == ZIX_FILE_TYPE_SYMLINK); +#endif // Check that link points to the corresponding copy assert(lilv_file_equals(recfile_link_1, recfile_copy_1)); @@ -641,11 +645,14 @@ test_to_files(void) // Check that a new snapshop of the recfile was created char* const recfile_copy_2 = lilv_path_join(dirs.copy, "recfile.2"); - assert(lilv_path_exists(recfile_copy_2)); + assert(zix_file_type(recfile_copy_2) == ZIX_FILE_TYPE_REGULAR); // Check that a link to the recfile exists in the updated bundle char* const recfile_link_2 = lilv_path_join(bundle_2_path, "recfile"); - assert(lilv_path_exists(recfile_link_2)); + assert(zix_file_type(recfile_link_2) == ZIX_FILE_TYPE_REGULAR); +#ifndef _WIN32 + assert(zix_symlink_type(recfile_link_2) == ZIX_FILE_TYPE_SYMLINK); +#endif // Check that link points to the corresponding copy assert(lilv_file_equals(recfile_link_2, recfile_copy_2)); @@ -704,11 +711,11 @@ test_multi_save(void) // Check that a manifest exists char* const manifest_path = lilv_path_join(bundle_1_path, "manifest.ttl"); - assert(lilv_path_exists(manifest_path)); + assert(zix_file_type(manifest_path) == ZIX_FILE_TYPE_REGULAR); // Check that the state file exists char* const state_path = lilv_path_join(bundle_1_path, "state.ttl"); - assert(lilv_path_exists(state_path)); + assert(zix_file_type(state_path) == ZIX_FILE_TYPE_REGULAR); // Check that the expected statements are in the files assert(count_statements(manifest_path) == 3); @@ -724,8 +731,8 @@ test_multi_save(void) "state.ttl")); // Check that everything is the same - assert(lilv_path_exists(manifest_path)); - assert(lilv_path_exists(state_path)); + assert(zix_file_type(manifest_path) == ZIX_FILE_TYPE_REGULAR); + assert(zix_file_type(state_path) == ZIX_FILE_TYPE_REGULAR); assert(count_statements(manifest_path) == 3); assert(count_statements(state_path) == 21); @@ -1066,7 +1073,7 @@ test_delete(void) assert(n_shared_files_before == n_shared_files_after); // Ensure the state directory has been deleted - assert(!lilv_path_exists(bundle_path)); + assert(zix_file_type(bundle_path) == ZIX_FILE_TYPE_NONE); cleanup_test_directories(dirs); -- cgit v1.2.1