Drop dead variable.
[rsync/rsync.git] / cleanup.c
CommitLineData
2f03f956
AT
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 */
25int cleanup_got_literal=0;
26
27static char *cleanup_fname;
28static char *cleanup_new_fname;
29static struct file_struct *cleanup_file;
c6b81a98
AT
30static int cleanup_fd1, cleanup_fd2;
31static struct map_struct *cleanup_buf;
8638dd48 32static int cleanup_pid = 0;
9bd65976 33extern int io_error;
2f03f956 34
a9766ef1 35void _exit_cleanup(int code, const char *file, int line)
2f03f956
AT
36{
37 extern int keep_partial;
38
9bd65976
AT
39 if (code == 0 && io_error) code = RERR_FILEIO;
40
2f03f956 41 signal(SIGUSR1, SIG_IGN);
8b35435f 42 signal(SIGUSR2, SIG_IGN);
2f03f956
AT
43
44 if (cleanup_got_literal && cleanup_fname && keep_partial) {
45 char *fname = cleanup_fname;
46 cleanup_fname = NULL;
c6b81a98
AT
47 if (cleanup_buf) unmap_file(cleanup_buf);
48 if (cleanup_fd1 != -1) close(cleanup_fd1);
49 if (cleanup_fd2 != -1) close(cleanup_fd2);
2f03f956
AT
50 finish_transfer(cleanup_new_fname, fname, cleanup_file);
51 }
52 io_flush();
53 if (cleanup_fname)
54 do_unlink(cleanup_fname);
55 if (code) {
56 kill_all(SIGUSR1);
57 }
27d3cdbc
AT
58 if ((cleanup_pid != 0) && (cleanup_pid == (int) getpid())) {
59 char *pidf = lp_pid_file();
60 if (pidf && *pidf) {
61 unlink(lp_pid_file());
62 }
63 }
9b73d1c0 64
a9766ef1 65 if (code) log_exit(code, file, line);
9b73d1c0 66
2f03f956
AT
67 exit(code);
68}
69
70void cleanup_disable(void)
71{
72 cleanup_fname = NULL;
73 cleanup_got_literal = 0;
74}
75
76
c6b81a98
AT
77void cleanup_set(char *fnametmp, char *fname, struct file_struct *file,
78 struct map_struct *buf, int fd1, int fd2)
2f03f956
AT
79{
80 cleanup_fname = fnametmp;
81 cleanup_new_fname = fname;
82 cleanup_file = file;
c6b81a98
AT
83 cleanup_buf = buf;
84 cleanup_fd1 = fd1;
85 cleanup_fd2 = fd2;
2f03f956 86}
8638dd48
DD
87
88void cleanup_set_pid(int pid)
89{
90 cleanup_pid = pid;
91}