From 46b9f263ac780ae6a921fc24611c75bbec92ebca Mon Sep 17 00:00:00 2001 From: Jeff Kowalski Date: Sat, 12 Mar 2016 11:21:52 -0800 Subject: [PATCH] Handle return values from fchown MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The function fchown is annotated with warn_unused_result. As formerly used in the code, it would emit a compiler warning ```warning: ignoring return value of ‘fchown’, declared with attribute warn_unused_result [-Wunused-result]``` This commit notes the return value and emits appropriate error/logging messages if the call fails, creating more traceable results and satisfying the compiler. --- src/env_universal_common.cpp | 9 +++++---- src/history.cpp | 5 ++++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/env_universal_common.cpp b/src/env_universal_common.cpp index bee25a1ea..5cd9edb14 100644 --- a/src/env_universal_common.cpp +++ b/src/env_universal_common.cpp @@ -859,22 +859,23 @@ bool env_universal_t::sync(callback_data_list_t *callbacks) success = this->write_to_fd(private_fd, private_file_path); if (! success) UNIVERSAL_LOG("write_to_fd() failed"); } - + if (success) { /* Ensure we maintain ownership and permissions (#2176) */ struct stat sbuf; if (wstat(vars_path, &sbuf) >= 0) { - fchown(private_fd, sbuf.st_uid, sbuf.st_gid); + if (0 > fchown(private_fd, sbuf.st_uid, sbuf.st_gid)) + UNIVERSAL_LOG("fchown() failed"); fchmod(private_fd, sbuf.st_mode); } - + /* Linux by default stores the mtime with low precision, low enough that updates that occur in quick succession may result in the same mtime (even the nanoseconds field). So manually set the mtime of the new file to a high-precision clock. Note that this is only necessary because Linux aggressively reuses inodes, causing the ABA problem; on other platforms we tend to notice the file has changed due to a different inode (or file size!) - + It's probably worth finding a simpler solution to this. The tests ran into this, but it's unlikely to affect users. */ #if HAVE_CLOCK_GETTIME && HAVE_FUTIMENS diff --git a/src/history.cpp b/src/history.cpp index 1bada96d3..022524c35 100644 --- a/src/history.cpp +++ b/src/history.cpp @@ -1453,7 +1453,10 @@ bool history_t::save_internal_via_rewrite() if (wstat(new_name, &sbuf) >= 0) { /* Success */ - fchown(out_fd, sbuf.st_uid, sbuf.st_gid); + if (0 > fchown(out_fd, sbuf.st_uid, sbuf.st_gid)) + { + debug(2, L"Error when changing ownership of history file"); + } fchmod(out_fd, sbuf.st_mode); }