separated out the make_backup code in preparation for some patches
[rsync/rsync.git] / backup.c
CommitLineData
3d19b4c8
AT
1/*
2 Copyright (C) Andrew Tridgell 1999
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17*/
18
19/* backup handling code */
20
21#include "rsync.h"
22
23extern int verbose;
24extern char *backup_suffix;
25
26
27int make_backup(char *fname)
28{
29 char fnamebak[MAXPATHLEN];
30 if (strlen(fname) + strlen(backup_suffix) > (MAXPATHLEN-1)) {
31 rprintf(FERROR,"backup filename too long\n");
32 return 0;
33 }
34
35 slprintf(fnamebak,sizeof(fnamebak),"%s%s",fname,backup_suffix);
36 if (do_rename(fname,fnamebak) != 0) {
37 /* cygwin (at least version b19) reports EINVAL */
38 if (errno != ENOENT && errno != EINVAL) {
39 rprintf(FERROR,"rename %s %s : %s\n",fname,fnamebak,strerror(errno));
40 return 0;
41 }
42 } else if (verbose > 1) {
43 rprintf(FINFO,"backed up %s to %s\n",fname,fnamebak);
44 }
45 return 1;
46}