From 035ca23226bfdb7f99edfb8ae30a363444b15eeb Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 6 Oct 2021 08:09:16 -0500 Subject: [PATCH] fix: sonarcloud false error from O_SEQUENTIAL use. (#1882) Xref: https://sonarcloud.io/project/issues?id=transmission_transmission&issues=AXvcFXmBqMngl89XhIRU&open=AXvcFXmBqMngl89XhIRU --- libtransmission/file-posix.cc | 47 +++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/libtransmission/file-posix.cc b/libtransmission/file-posix.cc index ff7ab5164..04a2b8e89 100644 --- a/libtransmission/file-posix.cc +++ b/libtransmission/file-posix.cc @@ -10,6 +10,7 @@ #define _GNU_SOURCE #include +#include #include #include /* PATH_MAX */ #include /* SIZE_MAX */ @@ -608,31 +609,35 @@ tr_sys_file_t tr_sys_file_open(char const* path, int flags, int permissions, tr_ TR_ASSERT(path != nullptr); TR_ASSERT((flags & (TR_SYS_FILE_READ | TR_SYS_FILE_WRITE)) != 0); - tr_sys_file_t ret; - int native_flags = 0; + struct native_map_item + { + int symbolic_mask; + int symbolic_value; + int native_value; + }; - if ((flags & (TR_SYS_FILE_READ | TR_SYS_FILE_WRITE)) == (TR_SYS_FILE_READ | TR_SYS_FILE_WRITE)) + auto constexpr native_map = std::array{ + { { TR_SYS_FILE_READ | TR_SYS_FILE_WRITE, TR_SYS_FILE_READ | TR_SYS_FILE_WRITE, O_RDWR }, + { TR_SYS_FILE_READ | TR_SYS_FILE_WRITE, TR_SYS_FILE_READ, O_RDONLY }, + { TR_SYS_FILE_READ | TR_SYS_FILE_WRITE, TR_SYS_FILE_WRITE, O_WRONLY }, + { TR_SYS_FILE_CREATE, TR_SYS_FILE_CREATE, O_CREAT }, + { TR_SYS_FILE_CREATE_NEW, TR_SYS_FILE_CREATE_NEW, O_CREAT | O_EXCL }, + { TR_SYS_FILE_APPEND, TR_SYS_FILE_APPEND, O_APPEND }, + { TR_SYS_FILE_TRUNCATE, TR_SYS_FILE_TRUNCATE, O_TRUNC }, + { TR_SYS_FILE_SEQUENTIAL, TR_SYS_FILE_SEQUENTIAL, O_SEQUENTIAL } } + }; + + int native_flags = O_BINARY | O_LARGEFILE | O_CLOEXEC; + + for (auto const& item : native_map) { - native_flags |= O_RDWR; - } - else if ((flags & TR_SYS_FILE_READ) != 0) - { - native_flags |= O_RDONLY; - } - else if ((flags & TR_SYS_FILE_WRITE) != 0) - { - native_flags |= O_WRONLY; + if ((flags & item.symbolic_mask) == item.symbolic_value) + { + native_flags |= item.native_value; + } } - native_flags |= // - ((flags & TR_SYS_FILE_CREATE) != 0 ? O_CREAT : 0) | // - ((flags & TR_SYS_FILE_CREATE_NEW) != 0 ? O_CREAT | O_EXCL : 0) | // - ((flags & TR_SYS_FILE_APPEND) != 0 ? O_APPEND : 0) | // - ((flags & TR_SYS_FILE_TRUNCATE) != 0 ? O_TRUNC : 0) | // - ((flags & TR_SYS_FILE_SEQUENTIAL) != 0 ? O_SEQUENTIAL : 0) | // - O_BINARY | O_LARGEFILE | O_CLOEXEC; - - ret = open(path, native_flags, permissions); + tr_sys_file_t ret = open(path, native_flags, permissions); if (ret != TR_BAD_SYS_FILE) {