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