Fix copyright.
[rsync/rsync.git] / cleanup.c
CommitLineData
ef1aa910
MP
1/* -*- c-file-style: "linux" -*-
2
3 Copyright (C) 1996-2000 by Andrew Tridgell
2f03f956 4 Copyright (C) Paul Mackerras 1996
e0fde757 5 Copyright (C) 2002 by Martin Pool
2f03f956
AT
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20*/
21
22#include "rsync.h"
23
e0fde757
MP
24/**
25 * @file cleanup.c
26 *
27 * Code for handling interrupted transfers. Depending on the @c
28 * --partial option, we may either delete the temporary file, or go
29 * ahead and overwrite the destination. This second behaviour only
30 * occurs if we've sent literal data and therefore hopefully made
31 * progress on the transfer.
32 **/
33
34/**
35 * Set to True once literal data has been sent across the link for the
36 * current file. (????)
37 *
38 * Handling the cleanup when a transfer is interrupted is tricky when
39 * --partial is selected. We need to ensure that the partial file is
40 * kept if any real data has been transferred.
41 **/
2f03f956
AT
42int cleanup_got_literal=0;
43
44static char *cleanup_fname;
45static char *cleanup_new_fname;
46static struct file_struct *cleanup_file;
c6b81a98
AT
47static int cleanup_fd1, cleanup_fd2;
48static struct map_struct *cleanup_buf;
8638dd48 49static int cleanup_pid = 0;
9bd65976 50extern int io_error;
2f03f956 51
19b27a48 52pid_t cleanup_child_pid = -1;
ef1aa910 53
e0fde757
MP
54/**
55 * Eventually calls exit(), therefore does not return.
56 *
57 * @param code one of the RERR_* codes from errcode.h.
58 **/
a9766ef1 59void _exit_cleanup(int code, const char *file, int line)
2f03f956 60{
9098bbf3 61 int ocode = code;
2f03f956 62 extern int keep_partial;
ff81e809 63 extern int log_got_error;
2f03f956
AT
64
65 signal(SIGUSR1, SIG_IGN);
8b35435f 66 signal(SIGUSR2, SIG_IGN);
2f03f956 67
9098bbf3
MP
68 if (verbose > 3)
69 rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): entered\n",
70 code, file, line);
71
19b27a48
AT
72 if (cleanup_child_pid != -1) {
73 int status;
74 if (waitpid(cleanup_child_pid, &status, WNOHANG) == cleanup_child_pid) {
75 status = WEXITSTATUS(status);
76 if (status > code) code = status;
77 }
78 }
79
2f03f956
AT
80 if (cleanup_got_literal && cleanup_fname && keep_partial) {
81 char *fname = cleanup_fname;
82 cleanup_fname = NULL;
c6b81a98
AT
83 if (cleanup_buf) unmap_file(cleanup_buf);
84 if (cleanup_fd1 != -1) close(cleanup_fd1);
85 if (cleanup_fd2 != -1) close(cleanup_fd2);
2f03f956
AT
86 finish_transfer(cleanup_new_fname, fname, cleanup_file);
87 }
88 io_flush();
89 if (cleanup_fname)
90 do_unlink(cleanup_fname);
91 if (code) {
92 kill_all(SIGUSR1);
93 }
27d3cdbc
AT
94 if ((cleanup_pid != 0) && (cleanup_pid == (int) getpid())) {
95 char *pidf = lp_pid_file();
96 if (pidf && *pidf) {
97 unlink(lp_pid_file());
98 }
99 }
9b73d1c0 100
19b27a48
AT
101 if (code == 0 && (io_error || log_got_error)) {
102 code = RERR_PARTIAL;
ff81e809
AT
103 }
104
19b27a48
AT
105 if (code) log_exit(code, file, line);
106
9098bbf3
MP
107 if (verbose > 2)
108 rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): about to call exit(%d)\n",
109 ocode, file, line, code);
110
2f03f956
AT
111 exit(code);
112}
113
114void cleanup_disable(void)
115{
116 cleanup_fname = NULL;
117 cleanup_got_literal = 0;
118}
119
120
c6b81a98
AT
121void cleanup_set(char *fnametmp, char *fname, struct file_struct *file,
122 struct map_struct *buf, int fd1, int fd2)
2f03f956
AT
123{
124 cleanup_fname = fnametmp;
125 cleanup_new_fname = fname;
126 cleanup_file = file;
c6b81a98
AT
127 cleanup_buf = buf;
128 cleanup_fd1 = fd1;
129 cleanup_fd2 = fd2;
2f03f956 130}
8638dd48
DD
131
132void cleanup_set_pid(int pid)
133{
134 cleanup_pid = pid;
135}