Open config files in text mode when O_TEXT is defined. This helps on
[rsync/rsync.git] / cleanup.c
... / ...
CommitLineData
1/* -*- c-file-style: "linux" -*-
2
3 Copyright (C) 1996-2000 by Andrew Tridgell
4 Copyright (C) Paul Mackerras 1996
5 Copyright (C) 2002 by Martin Pool
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
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 **/
42int cleanup_got_literal=0;
43
44static char *cleanup_fname;
45static char *cleanup_new_fname;
46static struct file_struct *cleanup_file;
47static int cleanup_fd1, cleanup_fd2;
48static struct map_struct *cleanup_buf;
49static int cleanup_pid = 0;
50extern int io_error;
51
52pid_t cleanup_child_pid = -1;
53
54/**
55 * Eventually calls exit(), passing @p code, therefore does not return.
56 *
57 * @param code one of the RERR_* codes from errcode.h.
58 **/
59void _exit_cleanup(int code, const char *file, int line)
60{
61 int ocode = code;
62 extern int keep_partial;
63 extern int log_got_error;
64 static int inside_cleanup = 0;
65
66 if (inside_cleanup != 0) {
67 /* prevent the occasional infinite recursion */
68 return;
69 }
70 inside_cleanup = 1;
71
72 signal(SIGUSR1, SIG_IGN);
73 signal(SIGUSR2, SIG_IGN);
74
75 if (verbose > 3)
76 rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): entered\n",
77 code, file, line);
78
79 if (cleanup_child_pid != -1) {
80 int status;
81 if (waitpid(cleanup_child_pid, &status, WNOHANG) == cleanup_child_pid) {
82 status = WEXITSTATUS(status);
83 if (status > code) code = status;
84 }
85 }
86
87 if (cleanup_got_literal && cleanup_fname && keep_partial) {
88 char *fname = cleanup_fname;
89 cleanup_fname = NULL;
90 if (cleanup_buf) unmap_file(cleanup_buf);
91 if (cleanup_fd1 != -1) close(cleanup_fd1);
92 if (cleanup_fd2 != -1) close(cleanup_fd2);
93 finish_transfer(cleanup_new_fname, fname, cleanup_file);
94 }
95 io_flush();
96 if (cleanup_fname)
97 do_unlink(cleanup_fname);
98 if (code) {
99 kill_all(SIGUSR1);
100 }
101 if ((cleanup_pid != 0) && (cleanup_pid == (int) getpid())) {
102 char *pidf = lp_pid_file();
103 if (pidf && *pidf) {
104 unlink(lp_pid_file());
105 }
106 }
107
108 if (code == 0 && (io_error || log_got_error)) {
109 code = RERR_PARTIAL;
110 }
111
112 if (code) log_exit(code, file, line);
113
114 if (verbose > 2)
115 rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): about to call exit(%d)\n",
116 ocode, file, line, code);
117
118 exit(code);
119}
120
121void cleanup_disable(void)
122{
123 cleanup_fname = NULL;
124 cleanup_got_literal = 0;
125}
126
127
128void cleanup_set(char *fnametmp, char *fname, struct file_struct *file,
129 struct map_struct *buf, int fd1, int fd2)
130{
131 cleanup_fname = fnametmp;
132 cleanup_new_fname = fname;
133 cleanup_file = file;
134 cleanup_buf = buf;
135 cleanup_fd1 = fd1;
136 cleanup_fd2 = fd2;
137}
138
139void cleanup_set_pid(int pid)
140{
141 cleanup_pid = pid;
142}