summaryrefslogtreecommitdiffabout
path: root/src/backup.c
blob: b268fb66d657132198dd5b06c41b8a1b94f29fea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* This file is part of Idest.
   Copyright (C) 2009-2011, 2016 Sergey Poznyakoff
 
   Idest is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.
 
   Idest is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 
   You should have received a copy of the GNU General Public License
   along with Idest.  If not, see <http://www.gnu.org/licenses/>. */

#include "idest.h"
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include "dirname.h"
#include "backupfile.h"

#define MKDIR_PERMISSIONS 0777
#define CREAT_PERMISSIONS 0666

/* Concatenate BASE directory name, a single directory separator (/) and
   NAME. Return in PBASELEN the length of BASE, not counting eventual trailing
   slash. */
char *
concat_dir(const char *base, const char *name, size_t *pbaselen)
{
	size_t len = strlen(base);
	size_t size;
	char *dir;

	while (len > 0 && base[len - 1] == '/')
		len--;

	size = len + 1 + strlen(name);
	dir = xmalloc(size + 1);
	memcpy(dir, base, len);
	dir[len++] = '/';
	strcpy(dir + len, name);

	if (pbaselen)
		*pbaselen = len;
	return dir;
}

/* Create the directory DIR, eventually creating all intermediate directories
   starting from DIR + BASELEN. */
int
create_hierarchy(char *dir, size_t baselen)
{
	int rc;
	struct stat st;
	char *p;

	if (stat(dir, &st) == 0) {
		if (!S_ISDIR(st.st_mode)) {
			error(0, 0, "component %s is not a directory", dir);
			return 1;
		}
		return 0;
	} else if (errno != ENOENT) {
		error(0, errno, "cannot stat file %s", dir);
		return 1;
	}

	p = strrchr(dir, '/');
	if (p) {
		if (p - dir + 1 < baselen) {
			error(0, 0, "base directory %s does not exist", dir);
			return 1;
		}
		*p = 0;
	}

	rc = create_hierarchy(dir, baselen);
	if (rc == 0) {
		if (p)
			*p = '/';
		if (mkdir(dir, MKDIR_PERMISSIONS)) {
			error(0, errno, "cannot create directory %s", dir);
			rc = 1;
		}
	}
	return rc;
}

/* Create a directory BASE/NAME (with eventual intermediate directories in
   NAME).  */
char *
create_directory(const char *base, const char *name)
{
	size_t baselen;
	char *dir = concat_dir(base, name, &baselen);

	if (create_hierarchy(dir, baselen)) {
		free(dir);
		dir = NULL;
	}
	return dir;
}


/* Copy FILE to DST_FILE. */
int
copy_file(const char *file, const char *dst_file)
{
	int in_fd, out_fd;
	struct stat st;
	char *buf = NULL;
	size_t bufsize;
	size_t fsize;
	int rc;
	
	in_fd = open(file, O_RDONLY);

	if (in_fd == -1) {
		error(0, errno,
		       "cannot open source file %s for reading", file);
		return 1;
	}

	if (fstat(in_fd, &st)) {
		error(0, errno, "cannot stat source file %s", file);
		close(in_fd);
		return 1;
	}

	out_fd = creat(dst_file, CREAT_PERMISSIONS);
	if (out_fd == -1) {
		error(0, errno, "cannot create destination file %s", file);
		close(in_fd);
		return 1;
	}

	fsize = st.st_size;

	for (bufsize = fsize;
	     bufsize > 0 && (buf = malloc(bufsize)) == NULL;
	     bufsize /= 2);
	if (bufsize == 0)
		xalloc_die();

	rc = 0;
	while (fsize > 0) {
		size_t rest;
		size_t rdbytes;

		rest = fsize > bufsize ? bufsize : fsize;
		rdbytes = read(in_fd, buf, rest);
		if (rdbytes == -1) {
			error(0, errno, "unexpected error reading %s", file);
			rc = 1;
			break;
		}
		rest = write(out_fd, buf, rdbytes);
		if (rest == -1) {
			error(0, errno, "unexpected error writing to %s",
			       dst_file);
			rc = 1;
			break;
		} else if (rest != rdbytes) {
			error(0, 0, "short write on %s", dst_file);
			rc = 1;
		}
		fsize -= rdbytes;
	}
	free(buf);

	close(in_fd);
	close(out_fd);
	if (rc) {
		unlink(dst_file);
		return 1;
	}
	return 0;
}

/* Backup a file */
int
backup_file(const char *file)
{
	int rc = 0;
	char *new_name;

	if (backup_dir) {
		if (file[0] == '/') 
			new_name = concat_dir(backup_dir,
					       last_component(file), NULL);
		else
			new_name = concat_dir(backup_dir, file, NULL);
		if (access(new_name, F_OK) == 0) {
			if (backup_type == no_backups) {
				if (unlink(new_name)) 
					error(0, errno,
					      "cannot unlink backup file %s",
					      new_name);
				/* Try to continue anyway */
			} else {
				char *bfn = find_backup_file_name(new_name,
								  backup_type);
				copy_file(new_name, bfn);
				/* Continue no matter what the result of
				   copy_file was */
			}
		}
	} else if (backup_type == no_backups)
		return 0;
	else 
		new_name = find_backup_file_name(file, backup_type);
	 
	rc = copy_file(file, new_name);
	free(new_name);
	return rc;
}

Return to:

Send suggestions and report system problems to the System administrator.