From c1047b80097bc54203f8a6e917190fa5dbb2d7e4 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 29 Oct 2024 00:14:53 -0500 Subject: [PATCH] refactor: remove unused TR_SYS_FILE_LOCK_UN (#7207) --- libtransmission/file-posix.cc | 14 ++++---------- libtransmission/file-win32.cc | 31 +++++++++++-------------------- libtransmission/file.h | 3 +-- 3 files changed, 16 insertions(+), 32 deletions(-) diff --git a/libtransmission/file-posix.cc b/libtransmission/file-posix.cc index 7e2b93124..280c2537f 100644 --- a/libtransmission/file-posix.cc +++ b/libtransmission/file-posix.cc @@ -914,15 +914,14 @@ bool tr_sys_file_preallocate(tr_sys_file_t handle, uint64_t size, int flags, tr_ bool tr_sys_file_lock([[maybe_unused]] tr_sys_file_t handle, [[maybe_unused]] int operation, tr_error* error) { TR_ASSERT(handle != TR_BAD_SYS_FILE); - TR_ASSERT((operation & ~(TR_SYS_FILE_LOCK_SH | TR_SYS_FILE_LOCK_EX | TR_SYS_FILE_LOCK_NB | TR_SYS_FILE_LOCK_UN)) == 0); - TR_ASSERT( - !!(operation & TR_SYS_FILE_LOCK_SH) + !!(operation & TR_SYS_FILE_LOCK_EX) + !!(operation & TR_SYS_FILE_LOCK_UN) == 1); + TR_ASSERT((operation & ~(TR_SYS_FILE_LOCK_SH | TR_SYS_FILE_LOCK_EX | TR_SYS_FILE_LOCK_NB)) == 0); + TR_ASSERT(!!(operation & TR_SYS_FILE_LOCK_SH) + !!(operation & TR_SYS_FILE_LOCK_EX) == 1); #if defined(F_OFD_SETLK) struct flock fl = {}; - switch (operation & (TR_SYS_FILE_LOCK_SH | TR_SYS_FILE_LOCK_EX | TR_SYS_FILE_LOCK_UN)) + switch (operation & (TR_SYS_FILE_LOCK_SH | TR_SYS_FILE_LOCK_EX)) { case TR_SYS_FILE_LOCK_SH: fl.l_type = F_RDLCK; @@ -932,10 +931,6 @@ bool tr_sys_file_lock([[maybe_unused]] tr_sys_file_t handle, [[maybe_unused]] in fl.l_type = F_WRLCK; break; - case TR_SYS_FILE_LOCK_UN: - fl.l_type = F_UNLCK; - break; - default: errno = EINVAL; break; @@ -963,8 +958,7 @@ bool tr_sys_file_lock([[maybe_unused]] tr_sys_file_t handle, [[maybe_unused]] in int const native_operation = // (((operation & TR_SYS_FILE_LOCK_SH) != 0) ? LOCK_SH : 0) | // (((operation & TR_SYS_FILE_LOCK_EX) != 0) ? LOCK_EX : 0) | // - (((operation & TR_SYS_FILE_LOCK_NB) != 0) ? LOCK_NB : 0) | // - (((operation & TR_SYS_FILE_LOCK_UN) != 0) ? LOCK_UN : 0); + (((operation & TR_SYS_FILE_LOCK_NB) != 0) ? LOCK_NB : 0); auto result = std::optional{}; while (!result) diff --git a/libtransmission/file-win32.cc b/libtransmission/file-win32.cc index 45a89ed82..f96d11359 100644 --- a/libtransmission/file-win32.cc +++ b/libtransmission/file-win32.cc @@ -1054,34 +1054,25 @@ bool tr_sys_file_preallocate(tr_sys_file_t handle, uint64_t size, int flags, tr_ bool tr_sys_file_lock(tr_sys_file_t handle, int operation, tr_error* error) { TR_ASSERT(handle != TR_BAD_SYS_FILE); - TR_ASSERT((operation & ~(TR_SYS_FILE_LOCK_SH | TR_SYS_FILE_LOCK_EX | TR_SYS_FILE_LOCK_NB | TR_SYS_FILE_LOCK_UN)) == 0); - TR_ASSERT( - !!(operation & TR_SYS_FILE_LOCK_SH) + !!(operation & TR_SYS_FILE_LOCK_EX) + !!(operation & TR_SYS_FILE_LOCK_UN) == 1); + TR_ASSERT((operation & ~(TR_SYS_FILE_LOCK_SH | TR_SYS_FILE_LOCK_EX | TR_SYS_FILE_LOCK_NB)) == 0); + TR_ASSERT(!!(operation & TR_SYS_FILE_LOCK_SH) + !!(operation & TR_SYS_FILE_LOCK_EX) == 1); - bool ret = false; auto overlapped = OVERLAPPED{}; - if ((operation & TR_SYS_FILE_LOCK_UN) == 0) + DWORD native_flags = 0; + + if ((operation & TR_SYS_FILE_LOCK_EX) != 0) { - DWORD native_flags = 0; - - if ((operation & TR_SYS_FILE_LOCK_EX) != 0) - { - native_flags |= LOCKFILE_EXCLUSIVE_LOCK; - } - - if ((operation & TR_SYS_FILE_LOCK_NB) != 0) - { - native_flags |= LOCKFILE_FAIL_IMMEDIATELY; - } - - ret = LockFileEx(handle, native_flags, 0, MAXDWORD, MAXDWORD, &overlapped) != FALSE; + native_flags |= LOCKFILE_EXCLUSIVE_LOCK; } - else + + if ((operation & TR_SYS_FILE_LOCK_NB) != 0) { - ret = UnlockFileEx(handle, 0, MAXDWORD, MAXDWORD, &overlapped) != FALSE; + native_flags |= LOCKFILE_FAIL_IMMEDIATELY; } + bool const ret = LockFileEx(handle, native_flags, 0, MAXDWORD, MAXDWORD, &overlapped) != FALSE; + if (!ret) { set_system_error(error, GetLastError()); diff --git a/libtransmission/file.h b/libtransmission/file.h index 23562130e..12ccb3301 100644 --- a/libtransmission/file.h +++ b/libtransmission/file.h @@ -59,8 +59,7 @@ enum tr_sys_file_lock_flags_t { TR_SYS_FILE_LOCK_SH = (1 << 0), TR_SYS_FILE_LOCK_EX = (1 << 1), - TR_SYS_FILE_LOCK_NB = (1 << 2), - TR_SYS_FILE_LOCK_UN = (1 << 3) + TR_SYS_FILE_LOCK_NB = (1 << 2) }; enum tr_sys_path_get_info_flags_t