- only keep a partial file if some literal data has been transferred,
[rsync/rsync.git] / rsync.c
diff --git a/rsync.c b/rsync.c
index 3832145..48ed69f 100644 (file)
--- a/rsync.c
+++ b/rsync.c
@@ -53,6 +53,37 @@ extern int io_timeout;
 extern int io_error;
 extern struct stats stats;
 
+
+/* handling the cleanup when a transfer is interrupted is tricky when
+   --partial is selected. We need to ensure that the partial file is
+   kept if any real data has been transferred */
+static int cleanup_got_literal;
+static char *cleanup_fname;
+static char *cleanup_new_fname;
+static struct file_struct *cleanup_file;
+static void finish_transfer(char *fname, char *fnametmp, struct file_struct *file);
+
+void exit_cleanup(int code)
+{
+       extern int keep_partial;
+
+       signal(SIGUSR1, SIG_IGN);
+
+       if (cleanup_got_literal && cleanup_fname && keep_partial) {
+               char *fname = cleanup_fname;
+               cleanup_fname = NULL;
+               finish_transfer(cleanup_new_fname, fname, cleanup_file);
+       }
+       io_flush();
+       if (cleanup_fname)
+               do_unlink(cleanup_fname);
+       if (code) {
+               kill_all(SIGUSR1);
+       }
+       exit(code);
+}
+
+
 /*
   free a sums struct
   */
@@ -568,6 +599,8 @@ static int receive_data(int f_in,struct map_struct *buf,int fd,char *fname)
        rprintf(FINFO,"data recv %d at %d\n",i,(int)offset);
 
       stats.literal_data += i;
+      cleanup_got_literal = 1;
+      
       sum_update(data,i);
 
       if (fd != -1 && write_file(fd,data,i) != i) {
@@ -730,20 +763,6 @@ static void delete_files(struct file_list *flist)
        }
 }
 
-static char *cleanup_fname;
-
-void exit_cleanup(int code)
-{
-       io_flush();
-       if (cleanup_fname)
-               do_unlink(cleanup_fname);
-       signal(SIGUSR1, SIG_IGN);
-       if (code) {
-               kill_all(SIGUSR1);
-       }
-       exit(code);
-}
-
 void sig_int(void)
 {
   exit_cleanup(1);
@@ -790,6 +809,46 @@ static int get_tmpname(char *fnametmp, char *fname)
        return 1;
 }
 
+/* finish off a file transfer, renaming the file and setting the permissions
+   and ownership */
+static void finish_transfer(char *fname, char *fnametmp, struct file_struct *file)
+{
+       if (make_backups) {
+               char fnamebak[MAXPATHLEN];
+               if (strlen(fname) + strlen(backup_suffix) > (MAXPATHLEN-1)) {
+                       rprintf(FERROR,"backup filename too long\n");
+                       return;
+               }
+               slprintf(fnamebak,sizeof(fnamebak)-1,"%s%s",fname,backup_suffix);
+               if (do_rename(fname,fnamebak) != 0 && errno != ENOENT) {
+                       rprintf(FERROR,"rename %s %s : %s\n",fname,fnamebak,strerror(errno));
+                       return;
+               }
+       }
+
+       /* move tmp file over real file */
+       if (do_rename(fnametmp,fname) != 0) {
+               if (errno == EXDEV) {
+                       /* rename failed on cross-filesystem link.  
+                          Copy the file instead. */
+                       if (copy_file(fnametmp,fname, file->mode)) {
+                               rprintf(FERROR,"copy %s -> %s : %s\n",
+                                       fnametmp,fname,strerror(errno));
+                       } else {
+                               set_perms(fname,file,NULL,0);
+                       }
+                       do_unlink(fnametmp);
+               } else {
+                       rprintf(FERROR,"rename %s -> %s : %s\n",
+                               fnametmp,fname,strerror(errno));
+                       do_unlink(fnametmp);
+               }
+       } else {
+               set_perms(fname,file,NULL,0);
+       }
+}
+
+
 int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen)
 {  
        int fd1,fd2;
@@ -811,6 +870,9 @@ int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen)
        }
 
        while (1) {      
+               cleanup_fname = NULL;
+               cleanup_got_literal = 0;
+
                i = read_int(f_in);
                if (i == -1) {
                        if (phase==0 && remote_version >= 13) {
@@ -909,6 +971,8 @@ int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen)
                }
       
                cleanup_fname = fnametmp;
+               cleanup_new_fname = fname;
+               cleanup_file = file;
 
                if (!am_server && verbose)
                        printf("%s\n",fname);
@@ -924,43 +988,10 @@ int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen)
                
                if (verbose > 2)
                        rprintf(FINFO,"renaming %s to %s\n",fnametmp,fname);
-               
-               if (make_backups) {
-                       char fnamebak[MAXPATHLEN];
-                       if (strlen(fname) + strlen(backup_suffix) > (MAXPATHLEN-1)) {
-                               rprintf(FERROR,"backup filename too long\n");
-                               continue;
-                       }
-                       slprintf(fnamebak,sizeof(fnamebak)-1,"%s%s",fname,backup_suffix);
-                       if (do_rename(fname,fnamebak) != 0 && errno != ENOENT) {
-                               rprintf(FERROR,"rename %s %s : %s\n",fname,fnamebak,strerror(errno));
-                               continue;
-                       }
-               }
-
-               /* move tmp file over real file */
-               if (do_rename(fnametmp,fname) != 0) {
-                       if (errno == EXDEV) {
-                               /* rename failed on cross-filesystem link.  
-                                  Copy the file instead. */
-                               if (copy_file(fnametmp,fname, file->mode)) {
-                                       rprintf(FERROR,"copy %s -> %s : %s\n",
-                                               fnametmp,fname,strerror(errno));
-                               } else {
-                                       set_perms(fname,file,NULL,0);
-                               }
-                               do_unlink(fnametmp);
-                       } else {
-                               rprintf(FERROR,"rename %s -> %s : %s\n",
-                                       fnametmp,fname,strerror(errno));
-                               do_unlink(fnametmp);
-                       }
-               } else {
-                       set_perms(fname,file,NULL,0);
-               }
 
+               finish_transfer(fname, fnametmp, file);
+               
                cleanup_fname = NULL;
-
                
                if (!recv_ok) {
                        if (csum_length == SUM_LENGTH) {