From 31e125226d429c2f6df010a9277bb9ebfdc2cf54 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 23 Mar 1998 13:25:30 +0000 Subject: [PATCH] added wrappers around all the system calls that can change what is on disk. The wrappers check for dry_run. --- Makefile.in | 2 +- hlink.c | 6 +++--- rsync.c | 27 ++++++++++++------------ syscall.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++ util.c | 36 +++++++++++++++++-------------- 5 files changed, 98 insertions(+), 34 deletions(-) create mode 100644 syscall.c diff --git a/Makefile.in b/Makefile.in index ed0a86a4..f634863c 100644 --- a/Makefile.in +++ b/Makefile.in @@ -21,7 +21,7 @@ SHELL=/bin/sh .SUFFIXES: .c .o LIBOBJ=lib/getopt.o lib/fnmatch.o lib/zlib.o lib/compat.o -OBJS1=rsync.o exclude.o util.o md4.o main.o checksum.o match.o +OBJS1=rsync.o exclude.o util.o md4.o main.o checksum.o match.o syscall.o OBJS=$(OBJS1) flist.o io.o compat.o hlink.o token.o uidlist.o $(LIBOBJ) .c.o: diff --git a/hlink.c b/hlink.c index a0db2804..b3f1d274 100644 --- a/hlink.c +++ b/hlink.c @@ -121,7 +121,7 @@ void do_hard_links(struct file_list *flist) if (link_stat(f_name(&hlink_list[i-1]),&st1) != 0) continue; if (link_stat(f_name(&hlink_list[i]),&st2) != 0) { - if (!dry_run && link(f_name(&hlink_list[i-1]),f_name(&hlink_list[i])) != 0) { + if (do_link(f_name(&hlink_list[i-1]),f_name(&hlink_list[i])) != 0) { if (verbose > 0) fprintf(FINFO,"link %s => %s : %s\n", f_name(&hlink_list[i]), @@ -131,8 +131,8 @@ void do_hard_links(struct file_list *flist) } else { if (st2.st_dev == st1.st_dev && st2.st_ino == st1.st_ino) continue; - if (!dry_run && (unlink(f_name(&hlink_list[i])) != 0 || - link(f_name(&hlink_list[i-1]),f_name(&hlink_list[i])) != 0)) { + if (do_unlink(f_name(&hlink_list[i])) != 0 || + do_link(f_name(&hlink_list[i-1]),f_name(&hlink_list[i])) != 0) { if (verbose > 0) fprintf(FINFO,"link %s => %s : %s\n", f_name(&hlink_list[i]), diff --git a/rsync.c b/rsync.c index 7dab5288..1be378f6 100644 --- a/rsync.c +++ b/rsync.c @@ -304,7 +304,7 @@ void recv_generator(char *fname,struct file_list *flist,int i,int f_out) if (S_ISDIR(file->mode)) { if (dry_run) return; if (statret == 0 && !S_ISDIR(st.st_mode)) { - if (unlink(fname) != 0) { + if (do_unlink(fname) != 0) { fprintf(FERROR,"unlink %s : %s\n",fname,strerror(errno)); return; } @@ -337,8 +337,8 @@ void recv_generator(char *fname,struct file_list *flist,int i,int f_out) } } } - if (!dry_run) unlink(fname); - if (!dry_run && symlink(file->link,fname) != 0) { + do_unlink(fname); + if (do_symlink(file->link,fname) != 0) { fprintf(FERROR,"link %s -> %s : %s\n", fname,file->link,strerror(errno)); } else { @@ -356,12 +356,11 @@ void recv_generator(char *fname,struct file_list *flist,int i,int f_out) if (statret != 0 || st.st_mode != file->mode || st.st_rdev != file->rdev) { - if (!dry_run) unlink(fname); + do_unlink(fname); if (verbose > 2) fprintf(FERROR,"mknod(%s,0%o,0x%x)\n", fname,(int)file->mode,(int)file->rdev); - if (!dry_run && - mknod(fname,file->mode,file->rdev) != 0) { + if (do_mknod(fname,file->mode,file->rdev) != 0) { fprintf(FERROR,"mknod %s : %s\n",fname,strerror(errno)); } else { set_perms(fname,file,NULL,0); @@ -405,7 +404,7 @@ void recv_generator(char *fname,struct file_list *flist,int i,int f_out) fprintf(FERROR,"ERROR: %s is a directory\n",fname); return; } - if (unlink(fname) != 0) { + if (do_unlink(fname) != 0) { fprintf(FERROR,"%s : not a regular file (generator)\n",fname); return; } @@ -546,13 +545,13 @@ static int receive_data(int f_in,struct map_struct *buf,int fd,char *fname) static void delete_one(struct file_struct *f) { if (!S_ISDIR(f->mode)) { - if (!dry_run && unlink(f_name(f)) != 0) { + if (do_unlink(f_name(f)) != 0) { fprintf(FERROR,"unlink %s : %s\n",f_name(f),strerror(errno)); } else if (verbose) { fprintf(FERROR,"deleting %s\n",f_name(f)); } } else { - if (!dry_run && rmdir(f_name(f)) != 0) { + if (do_rmdir(f_name(f)) != 0) { if (errno != ENOTEMPTY) fprintf(FERROR,"rmdir %s : %s\n",f_name(f),strerror(errno)); } else if (verbose) { @@ -656,7 +655,7 @@ static char *cleanup_fname; void exit_cleanup(int code) { if (cleanup_fname) - unlink(cleanup_fname); + do_unlink(cleanup_fname); signal(SIGUSR1, SIG_IGN); if (code) { kill_all(SIGUSR1); @@ -771,10 +770,10 @@ int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen) close(fd1); continue; } - fd2 = open(fnametmp,O_WRONLY|O_CREAT|O_EXCL,file->mode); + fd2 = do_open(fnametmp,O_WRONLY|O_CREAT|O_EXCL,file->mode); if (fd2 == -1 && relative_paths && errno == ENOENT && create_directory_path(fnametmp) == 0) { - fd2 = open(fnametmp,O_WRONLY|O_CREAT|O_EXCL,file->mode); + fd2 = do_open(fnametmp,O_WRONLY|O_CREAT|O_EXCL,file->mode); } if (fd2 == -1) { fprintf(FERROR,"open %s : %s\n",fnametmp,strerror(errno)); @@ -825,11 +824,11 @@ int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen) } else { set_perms(fname,file,NULL,0); } - unlink(fnametmp); + do_unlink(fnametmp); } else { fprintf(FERROR,"rename %s -> %s : %s\n", fnametmp,fname,strerror(errno)); - unlink(fnametmp); + do_unlink(fnametmp); } } else { set_perms(fname,file,NULL,0); diff --git a/syscall.c b/syscall.c new file mode 100644 index 00000000..d92cb314 --- /dev/null +++ b/syscall.c @@ -0,0 +1,61 @@ +/* + Copyright (C) Andrew Tridgell 1998 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +/* + syscall wrappers to ensure that nothing gets done in dry_run mode + */ + +#include "rsync.h" + +extern int dry_run; + +int do_unlink(char *fname) +{ + if (dry_run) return 0; + return unlink(fname); +} + +int do_symlink(char *fname1, char *fname2) +{ + if (dry_run) return 0; + return symlink(fname1, fname2); +} + +int do_link(char *fname1, char *fname2) +{ + if (dry_run) return 0; + return link(fname1, fname2); +} + +int do_mknod(char *pathname, mode_t mode, dev_t dev) +{ + if (dry_run) return 0; + return mknod(pathname, mode, dev); +} + +int do_rmdir(char *pathname) +{ + if (dry_run) return 0; + return rmdir(pathname); +} + +int do_open(char *pathname, int flags, mode_t mode) +{ + if (dry_run) return -1; + return open(pathname, flags, mode); +} diff --git a/util.c b/util.c index 15cf2bcd..4d293f22 100644 --- a/util.c +++ b/util.c @@ -170,24 +170,28 @@ void out_of_memory(char *str) int set_modtime(char *fname,time_t modtime) { + extern int dry_run; + if (dry_run) return 0; + { #ifdef HAVE_UTIMBUF - struct utimbuf tbuf; - tbuf.actime = time(NULL); - tbuf.modtime = modtime; - return utime(fname,&tbuf); + struct utimbuf tbuf; + tbuf.actime = time(NULL); + tbuf.modtime = modtime; + return utime(fname,&tbuf); #elif defined(HAVE_UTIME) - time_t t[2]; - t[0] = time(NULL); - t[1] = modtime; - return utime(fname,t); + time_t t[2]; + t[0] = time(NULL); + t[1] = modtime; + return utime(fname,t); #else - struct timeval t[2]; - t[0].tv_sec = time(NULL); - t[0].tv_usec = 0; - t[1].tv_sec = modtime; - t[1].tv_usec = 0; - return utimes(fname,t); + struct timeval t[2]; + t[0].tv_sec = time(NULL); + t[0].tv_usec = 0; + t[1].tv_sec = modtime; + t[1].tv_usec = 0; + return utimes(fname,t); #endif + } } @@ -310,13 +314,13 @@ int copy_file(char *source, char *dest, mode_t mode) return -1; } - if (unlink(dest) && errno != ENOENT) { + if (do_unlink(dest) && errno != ENOENT) { fprintf(FERROR,"unlink %s: %s\n", dest,strerror(errno)); return -1; } - ofd = open(dest, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, mode); + ofd = do_open(dest, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, mode); if (ofd < 0) { fprintf(FERROR,"open %s: %s\n", dest,strerror(errno)); -- 2.34.1