Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Related Pages  

cleanup.c

Go to the documentation of this file.
00001 /* -*- c-file-style: "linux" -*-
00002    
00003    Copyright (C) 1996-2000 by Andrew Tridgell
00004    Copyright (C) Paul Mackerras 1996
00005    Copyright (C) 2002 by Martin Pool
00006    
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 2 of the License, or
00010    (at your option) any later version.
00011    
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016    
00017    You should have received a copy of the GNU General Public License
00018    along with this program; if not, write to the Free Software
00019    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020 */
00021 
00022 #include "rsync.h"
00023 
00024 /**
00025  * @file cleanup.c
00026  *
00027  * Code for handling interrupted transfers.  Depending on the @c
00028  * --partial option, we may either delete the temporary file, or go
00029  * ahead and overwrite the destination.  This second behaviour only
00030  * occurs if we've sent literal data and therefore hopefully made
00031  * progress on the transfer.
00032  **/
00033 
00034 /**
00035  * Set to True once literal data has been sent across the link for the
00036  * current file. (????)
00037  *
00038  * Handling the cleanup when a transfer is interrupted is tricky when
00039  * --partial is selected.  We need to ensure that the partial file is
00040  * kept if any real data has been transferred.
00041  **/
00042 int cleanup_got_literal=0;
00043 
00044 static char *cleanup_fname;
00045 static char *cleanup_new_fname;
00046 static struct file_struct *cleanup_file;
00047 static int cleanup_fd1, cleanup_fd2;
00048 static struct map_struct *cleanup_buf;
00049 static int cleanup_pid = 0;
00050 extern int io_error;
00051 
00052 pid_t cleanup_child_pid = -1;
00053 
00054 /**
00055  * Eventually calls exit(), passing @p code, therefore does not return.
00056  *
00057  * @param code one of the RERR_* codes from errcode.h.
00058  **/
00059 void _exit_cleanup(int code, const char *file, int line)
00060 {
00061         int ocode = code;
00062         extern int keep_partial;
00063         extern int log_got_error;
00064 
00065         signal(SIGUSR1, SIG_IGN);
00066         signal(SIGUSR2, SIG_IGN);
00067 
00068         if (verbose > 3)
00069                 rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): entered\n", 
00070                         code, file, line);
00071 
00072         if (cleanup_child_pid != -1) {
00073                 int status;
00074                 if (waitpid(cleanup_child_pid, &status, WNOHANG) == cleanup_child_pid) {
00075                         status = WEXITSTATUS(status);
00076                         if (status > code) code = status;
00077                 }
00078         }
00079 
00080         if (cleanup_got_literal && cleanup_fname && keep_partial) {
00081                 char *fname = cleanup_fname;
00082                 cleanup_fname = NULL;
00083                 if (cleanup_buf) unmap_file(cleanup_buf);
00084                 if (cleanup_fd1 != -1) close(cleanup_fd1);
00085                 if (cleanup_fd2 != -1) close(cleanup_fd2);
00086                 finish_transfer(cleanup_new_fname, fname, cleanup_file);
00087         }
00088         io_flush();
00089         if (cleanup_fname)
00090                 do_unlink(cleanup_fname);
00091         if (code) {
00092                 kill_all(SIGUSR1);
00093         }
00094         if ((cleanup_pid != 0) && (cleanup_pid == (int) getpid())) {
00095                 char *pidf = lp_pid_file();
00096                 if (pidf && *pidf) {
00097                         unlink(lp_pid_file());
00098                 }
00099         }
00100 
00101         if (code == 0 && (io_error || log_got_error)) {
00102                 code = RERR_PARTIAL;
00103         }
00104 
00105         if (code) log_exit(code, file, line);
00106 
00107         if (verbose > 2)
00108                 rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): about to call exit(%d)\n", 
00109                         ocode, file, line, code);
00110 
00111         exit(code);
00112 }
00113 
00114 void cleanup_disable(void)
00115 {
00116         cleanup_fname = NULL;
00117         cleanup_got_literal = 0;
00118 }
00119 
00120 
00121 void cleanup_set(char *fnametmp, char *fname, struct file_struct *file,
00122                  struct map_struct *buf, int fd1, int fd2)
00123 {
00124         cleanup_fname = fnametmp;
00125         cleanup_new_fname = fname;
00126         cleanup_file = file;
00127         cleanup_buf = buf;
00128         cleanup_fd1 = fd1;
00129         cleanup_fd2 = fd2;
00130 }
00131 
00132 void cleanup_set_pid(int pid)
00133 {
00134         cleanup_pid = pid;
00135 }

Generated on Tue Apr 16 12:37:36 2002 for rsync by doxygen1.2.15