Index: src/xz/file_io.c |
=================================================================== |
--- src/xz/file_io.c (revision 233270) |
+++ src/xz/file_io.c (working copy) |
@@ -41,9 +41,10 @@ |
static bool try_sparse = true; |
#ifndef TUKLIB_DOSLIKE |
-/// File status flags of standard output. This is used by io_open_dest() |
-/// and io_close_dest(). |
-static int stdout_flags = 0; |
+/// Original file status flags of standard output. This is used by |
+/// io_open_dest() and io_close_dest() to save and restore the flags. |
+static int stdout_flags; |
+static bool restore_stdout_flags = false; |
#endif |
@@ -397,10 +398,6 @@ |
was_symlink = true; |
# elif defined(__NetBSD__) |
- // As of 2010-09-05, NetBSD doesn't document what errno is |
- // used with O_NOFOLLOW. It is EFTYPE though, and I |
- // understood that is very unlikely to change even though |
- // it is undocumented. |
if (errno == EFTYPE) |
was_symlink = true; |
@@ -441,7 +438,7 @@ |
flags &= ~O_NONBLOCK; |
- if (fcntl(pair->src_fd, F_SETFL, flags)) |
+ if (fcntl(pair->src_fd, F_SETFL, flags) == -1) |
goto error_msg; |
} |
#endif |
@@ -634,11 +631,11 @@ |
if (!S_ISREG(pair->dest_st.st_mode)) |
return false; |
- const int flags = fcntl(STDOUT_FILENO, F_GETFL); |
- if (flags == -1) |
+ stdout_flags = fcntl(STDOUT_FILENO, F_GETFL); |
+ if (stdout_flags == -1) |
return false; |
- if (flags & O_APPEND) { |
+ if (stdout_flags & O_APPEND) { |
// Creating a sparse file is not possible |
// when O_APPEND is active (it's used by |
// shell's >> redirection). As I understand |
@@ -657,12 +654,14 @@ |
return false; |
if (fcntl(STDOUT_FILENO, F_SETFL, |
- stdout_flags & ~O_APPEND)) |
+ stdout_flags & ~O_APPEND) |
+ == -1) |
return false; |
- // Remember the flags so that io_close_dest() |
- // can restore them. |
- stdout_flags = flags; |
+ // Disabling O_APPEND succeeded. Mark |
+ // that the flags should be restored |
+ // in io_close_dest(). |
+ restore_stdout_flags = true; |
} else if (lseek(STDOUT_FILENO, 0, SEEK_CUR) |
!= pair->dest_st.st_size) { |
@@ -703,13 +702,12 @@ |
{ |
#ifndef TUKLIB_DOSLIKE |
// If io_open_dest() has disabled O_APPEND, restore it here. |
- if (stdout_flags != 0) { |
+ if (restore_stdout_flags) { |
assert(pair->dest_fd == STDOUT_FILENO); |
- const int fail = fcntl(STDOUT_FILENO, F_SETFL, stdout_flags); |
- stdout_flags = 0; |
+ restore_stdout_flags = false; |
- if (fail) { |
+ if (fcntl(STDOUT_FILENO, F_SETFL, stdout_flags) == -1) { |
message_error(_("Error restoring the O_APPEND flag " |
"to standard output: %s"), |
strerror(errno)); |
@@ -882,7 +880,7 @@ |
if (amount == -1) { |
if (errno == EINTR) { |
if (user_abort) |
- return -1; |
+ return true; |
continue; |
} |