From 42439bc6e35eef7c3cc905c60b5924c200b49ec8 Mon Sep 17 00:00:00 2001
From: David Robillard <d@drobilla.net>
Date: Sat, 12 Nov 2022 17:54:24 -0500
Subject: Use zix_file_lock()

---
 src/filesystem.c       | 30 ------------------------------
 src/filesystem.h       | 16 ----------------
 src/state.c            |  4 ++--
 test/test_filesystem.c | 22 ----------------------
 4 files changed, 2 insertions(+), 70 deletions(-)

diff --git a/src/filesystem.c b/src/filesystem.c
index 7e42dbd..b17f67a 100644
--- a/src/filesystem.c
+++ b/src/filesystem.c
@@ -19,10 +19,6 @@
 #  include <unistd.h>
 #endif
 
-#if USE_FLOCK && USE_FILENO
-#  include <sys/file.h>
-#endif
-
 #include <sys/stat.h>
 
 #include <stdbool.h>
@@ -210,32 +206,6 @@ lilv_symlink(const char* oldpath, const char* newpath)
   return ret;
 }
 
-int
-lilv_flock(FILE* file, bool lock, bool block)
-{
-#ifdef _WIN32
-  HANDLE     handle     = (HANDLE)_get_osfhandle(fileno(file));
-  OVERLAPPED overlapped = {0};
-
-  if (lock) {
-    const DWORD flags =
-      (LOCKFILE_EXCLUSIVE_LOCK | (block ? 0 : LOCKFILE_FAIL_IMMEDIATELY));
-
-    return !LockFileEx(handle, flags, 0, UINT32_MAX, UINT32_MAX, &overlapped);
-  } else {
-    return !UnlockFileEx(handle, 0, UINT32_MAX, UINT32_MAX, &overlapped);
-  }
-#elif USE_FLOCK && USE_FILENO
-  return flock(fileno(file),
-               (lock ? LOCK_EX : LOCK_UN) | (block ? 0 : LOCK_NB));
-#else
-  (void)file;
-  (void)lock;
-  (void)block;
-  return 0;
-#endif
-}
-
 void
 lilv_dir_for_each(const char* path,
                   void*       data,
diff --git a/src/filesystem.h b/src/filesystem.h
index 33294da..e50b1af 100644
--- a/src/filesystem.h
+++ b/src/filesystem.h
@@ -2,7 +2,6 @@
 // SPDX-License-Identifier: ISC
 
 #include <stdbool.h>
-#include <stdio.h>
 
 /// Return true iff `path` is an absolute path
 bool
@@ -54,21 +53,6 @@ lilv_is_directory(const char* path);
 int
 lilv_symlink(const char* oldpath, const char* newpath);
 
-/**
-   Set or remove an advisory exclusive lock on `file`.
-
-   If the `lock` is true and the file is already locked by another process, or
-   by this process via a different file handle, then this will not succeed and
-   non-zero will be returned.
-
-   @param file Handle for open file to lock.
-   @param lock True to set lock, false to release lock.
-   @param block If true, then this call will block until the lock is acquired.
-   @return Zero on success.
-*/
-int
-lilv_flock(FILE* file, bool lock, bool block);
-
 /**
    Visit every file in the directory at `path`.
 
diff --git a/src/state.c b/src/state.c
index c0c9400..7ee8f87 100644
--- a/src/state.c
+++ b/src/state.c
@@ -990,9 +990,9 @@ add_state_to_manifest(LilvWorld*      lworld,
   }
 
   SerdWriter* writer = ttl_file_writer(wfd, &manifest, &env);
-  lilv_flock(wfd, true, true);
+  zix_file_lock(wfd, ZIX_FILE_LOCK_BLOCK);
   sord_write(model, writer, NULL);
-  lilv_flock(wfd, false, true);
+  zix_file_unlock(wfd, ZIX_FILE_LOCK_BLOCK);
   serd_writer_free(writer);
   fclose(wfd);
 
diff --git a/test/test_filesystem.c b/test/test_filesystem.c
index 021d66f..75db963 100644
--- a/test/test_filesystem.c
+++ b/test/test_filesystem.c
@@ -155,27 +155,6 @@ test_is_directory(void)
   free(temp_dir);
 }
 
-static void
-test_flock(void)
-{
-  char* const temp_dir  = lilv_create_temporary_directory("lilvXXXXXX");
-  char* const file_path = zix_path_join(NULL, temp_dir, "lilv_test_file");
-
-  FILE* const f1 = fopen(file_path, "w");
-  FILE* const f2 = fopen(file_path, "w");
-
-  assert(!lilv_flock(f1, true, false));
-  assert(lilv_flock(f2, true, false));
-  assert(!lilv_flock(f1, false, false));
-
-  fclose(f2);
-  fclose(f1);
-  assert(!zix_remove(file_path));
-  assert(!zix_remove(temp_dir));
-  free(file_path);
-  free(temp_dir);
-}
-
 typedef struct {
   size_t n_names;
   char** names;
@@ -238,7 +217,6 @@ main(void)
   test_path_parent();
   test_path_filename();
   test_is_directory();
-  test_flock();
   test_dir_for_each();
 
   return 0;
-- 
cgit v1.2.1