| 1 | /* |
| 2 | Copyright (C) Andrew Tridgell 1996 |
| 3 | Copyright (C) Paul Mackerras 1996 |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation; either version 2 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program; if not, write to the Free Software |
| 17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 18 | */ |
| 19 | |
| 20 | #include "rsync.h" |
| 21 | |
| 22 | /* handling the cleanup when a transfer is interrupted is tricky when |
| 23 | --partial is selected. We need to ensure that the partial file is |
| 24 | kept if any real data has been transferred */ |
| 25 | int cleanup_got_literal=0; |
| 26 | |
| 27 | static char *cleanup_fname; |
| 28 | static char *cleanup_new_fname; |
| 29 | static struct file_struct *cleanup_file; |
| 30 | static int cleanup_fd1, cleanup_fd2; |
| 31 | static struct map_struct *cleanup_buf; |
| 32 | static int cleanup_pid = 0; |
| 33 | |
| 34 | void _exit_cleanup(int code, const char *file, int line) |
| 35 | { |
| 36 | extern int keep_partial; |
| 37 | |
| 38 | signal(SIGUSR1, SIG_IGN); |
| 39 | |
| 40 | if (cleanup_got_literal && cleanup_fname && keep_partial) { |
| 41 | char *fname = cleanup_fname; |
| 42 | cleanup_fname = NULL; |
| 43 | if (cleanup_buf) unmap_file(cleanup_buf); |
| 44 | if (cleanup_fd1 != -1) close(cleanup_fd1); |
| 45 | if (cleanup_fd2 != -1) close(cleanup_fd2); |
| 46 | finish_transfer(cleanup_new_fname, fname, cleanup_file); |
| 47 | } |
| 48 | io_flush(); |
| 49 | if (cleanup_fname) |
| 50 | do_unlink(cleanup_fname); |
| 51 | if (code) { |
| 52 | kill_all(SIGUSR1); |
| 53 | } |
| 54 | if ((cleanup_pid != 0) && (cleanup_pid == (int) getpid())) { |
| 55 | char *pidf = lp_pid_file(); |
| 56 | if (pidf && *pidf) { |
| 57 | unlink(lp_pid_file()); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | if (code) log_exit(code, file, line); |
| 62 | |
| 63 | exit(code); |
| 64 | } |
| 65 | |
| 66 | void cleanup_disable(void) |
| 67 | { |
| 68 | cleanup_fname = NULL; |
| 69 | cleanup_got_literal = 0; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | void cleanup_set(char *fnametmp, char *fname, struct file_struct *file, |
| 74 | struct map_struct *buf, int fd1, int fd2) |
| 75 | { |
| 76 | cleanup_fname = fnametmp; |
| 77 | cleanup_new_fname = fname; |
| 78 | cleanup_file = file; |
| 79 | cleanup_buf = buf; |
| 80 | cleanup_fd1 = fd1; |
| 81 | cleanup_fd2 = fd2; |
| 82 | } |
| 83 | |
| 84 | void cleanup_set_pid(int pid) |
| 85 | { |
| 86 | cleanup_pid = pid; |
| 87 | } |