summaryrefslogtreecommitdiff
path: root/src/compare.c
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2010-09-18 23:37:45 -0700
committerPaul Eggert <eggert@cs.ucla.edu>2010-09-18 23:42:54 -0700
commit4bde4f39d08f000f7e63a832b08a2525c1262f84 (patch)
tree8e899aebc873176377f3d1d85280c5ed34a31fcc /src/compare.c
parentfc5e44c99c98d764463f21a12c2c3ef1f0ba8ac7 (diff)
downloadtar-4bde4f39d08f000f7e63a832b08a2525c1262f84.tar.gz
tar: prefer openat-style functions
This change replaces traditional functions like 'open' with the POSIX.1-2008 functions like 'openat'. Mostly this is an internal refactoring change, in preparation for further changes to close some races. * gnulib.modules: Add faccessat, linkat, mkfifoat, renameat, symlinkat. Remove save-cwd. * src/Makefile.am (tar_LDADD): Add $(LIB_EACCESS). * tests/Makefile.am (LDADD): Likewise. * src/common.h (chdir_fd): New extern var. * src/compare.c (diff_file, diff_multivol): Use openat instead of open. * src/create.c (create_archive, restore_parent_fd): Likewise. * src/extract.c (create_placeholder_file): Likewise. * src/names.c (collect_and_sort_names): Likewise. * src/update.c (append_file): Likewise. * src/compare.c (diff_symlink): Use readlinkat instead of readlink. * src/compare.c (diff_file): Use chdir_fd instead of AT_FDCWD. * src/create.c (subfile_open, dump_file0): Likewise. * src/extract.c (fd_chmod, fd_chown, fd_stat, set_stat): (repair_delayed_set_stat, apply_nonancestor_delayed_set_stat): Likewise. * src/extract.c (mark_after_links, file_newer_p, extract_dir): (extract_link, apply_delayed_links): Use fstatat rather than stat or lstat. * src/misc.c (maybe_backup_file, deref_stat): Likewise. * src/extract.c (make_directories): Use mkdirat rather than mkdir. Use faccessat rather than access. This fixes a minor permissions bug when tar is running setuid (who would want to do that?!). (open_output_file): Use openat rather than open. In the process, this removes support for Masscomp's O_CTG files, which aren't compatible with openat's signature. Masscomp! Wow! That's a blast from the past. As far as I know, that operating system hasn't been supported for more than 20 years. (extract_link, apply_delayed_links): Use linkat rather than link. (extract_symlink, apply_delayed_links): Use symlinkat rather than symlink. (extract_node): Use mknodat rather than mknod. (extract_fifo): Use mkfifoat rather than mkfifo. (apply_delayed_links): Use unlinkat rather than unlink or rmdir. * src/misc.c (safer_rmdir, remove_any_file): Likewise. * src/unlink.c (flush_deferred_unlinks): Likewise. * src/extract.c (rename_directory): Use renameat rather than rename. * src/misc.c (maybe_backup_file, undo_last_backup): Likewise. * src/misc.c: Don't include <save-cwd.h>; no longer needed now that we're using openat etc. (struct wd): Add member fd. Remove members err and fd. All uses changed. (CHDIR_CACHE_SIZE): New constant. (wdcache, wdcache_count, chdir_fd): New vars. (chdir_do): Use openat rather than save_cwd. Keep the cache up to date. This code won't scale well, but is good enough for now. * src/update.c (update_archive): Use openat + fdopendir + streamsavedir rather than savedir. This file is a placeholder. It will be replaced with the actual ChangeLog by make dist. Run make ChangeLog if you wish to create it earlier.
Diffstat (limited to 'src/compare.c')
-rw-r--r--src/compare.c17
1 files changed, 9 insertions, 8 deletions
diff --git a/src/compare.c b/src/compare.c
index 204c5dc1..1ee9bcb1 100644
--- a/src/compare.c
+++ b/src/compare.c
@@ -222,9 +222,9 @@ diff_file (void)
? O_NOATIME
: 0);
- diff_handle = open (file_name,
- (O_RDONLY | O_BINARY | O_CLOEXEC | O_NOCTTY
- | O_NONBLOCK | atime_flag));
+ diff_handle = openat (chdir_fd, file_name,
+ (O_RDONLY | O_BINARY | O_CLOEXEC | O_NOCTTY
+ | O_NONBLOCK | atime_flag));
if (diff_handle < 0)
{
@@ -244,7 +244,7 @@ diff_file (void)
if (atime_preserve_option == replace_atime_preserve)
{
struct timespec atime = get_stat_atime (&stat_data);
- if (set_file_atime (diff_handle, AT_FDCWD, file_name,
+ if (set_file_atime (diff_handle, chdir_fd, file_name,
atime, 0)
!= 0)
utime_error (file_name);
@@ -279,7 +279,8 @@ diff_symlink (void)
size_t len = strlen (current_stat_info.link_name);
char *linkbuf = alloca (len + 1);
- int status = readlink (current_stat_info.file_name, linkbuf, len + 1);
+ int status = readlinkat (chdir_fd, current_stat_info.file_name,
+ linkbuf, len + 1);
if (status < 0)
{
@@ -428,9 +429,9 @@ diff_multivol (void)
}
- fd = open (current_stat_info.file_name,
- (O_RDONLY | O_BINARY | O_CLOEXEC | O_NOCTTY | O_NONBLOCK
- | atime_flag));
+ fd = openat (chdir_fd, current_stat_info.file_name,
+ (O_RDONLY | O_BINARY | O_CLOEXEC | O_NOCTTY | O_NONBLOCK
+ | atime_flag));
if (fd < 0)
{