diff options
author | Lasse Collin <lasse.collin@tukaani.org> | 2015-11-02 15:19:10 +0200 |
---|---|---|
committer | Lasse Collin <lasse.collin@tukaani.org> | 2016-06-28 21:09:46 +0300 |
commit | d9311647fc1ab512a3394596221ab8039c00af6b (patch) | |
tree | 4650186934985af7c4ca9f0ac1897b2d9505afa4 /src/xz/file_io.c | |
parent | Update THANKS. (diff) | |
download | xz-d9311647fc1ab512a3394596221ab8039c00af6b.tar.xz |
xz: Always close the file before trying to delete it.
unlink() can return EBUSY in errno for open files on some
operating systems and file systems.
Diffstat (limited to '')
-rw-r--r-- | src/xz/file_io.c | 25 |
1 files changed, 12 insertions, 13 deletions
diff --git a/src/xz/file_io.c b/src/xz/file_io.c index 9bd515dd..493dca8a 100644 --- a/src/xz/file_io.c +++ b/src/xz/file_io.c @@ -675,23 +675,22 @@ io_close_src(file_pair *pair, bool success) #endif if (pair->src_fd != STDIN_FILENO && pair->src_fd != -1) { -#ifdef TUKLIB_DOSLIKE + // Close the file before possibly unlinking it. On DOS-like + // systems this is always required since unlinking will fail + // if the file is open. On POSIX systems it usually works + // to unlink open files, but in some cases it doesn't and + // one gets EBUSY in errno. + // + // xz 5.2.2 and older unlinked the file before closing it + // (except on DOS-like systems). The old code didn't handle + // EBUSY and could fail e.g. on some CIFS shares. The + // advantage of unlinking before closing is negligible + // (avoids a race between close() and stat()/lstat() and + // unlink()), so let's keep this simple. (void)close(pair->src_fd); -#endif - // If we are going to unlink(), do it before closing the file. - // This way there's no risk that someone replaces the file and - // happens to get same inode number, which would make us - // unlink() wrong file. - // - // NOTE: DOS-like systems are an exception to this, because - // they don't allow unlinking files that are open. *sigh* if (success && !opt_keep_original) io_unlink(pair->src_name, &pair->src_st); - -#ifndef TUKLIB_DOSLIKE - (void)close(pair->src_fd); -#endif } return; |