summaryrefslogtreecommitdiffstats
path: root/lib/backup-files.c
blob: 36996c7cc123a82721d08726a69c84ce26ccaaa4 (plain) (blame)
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
  File: backup-files.c

  Copyright (C) 2003 Andreas Gruenbacher <agruen@suse.de>
  SuSE Labs, SuSE Linux AG

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU Library General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  This program 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
  Library General Public License for more details.

  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

/*
 * Create backup files of a list of files similar to GNU patch. A path
 * name prefix and suffix for the backup file can be specified with the
 * -B and -Z options.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

const char *progname;

enum { what_backup, what_restore, what_remove };

const char *opt_prefix="", *opt_suffix="", *opt_file=NULL;
int opt_silent=0, opt_what=what_backup;

#define LINE_LENGTH 1024


void
usage(void)
{
	printf("Usage: %s [-B prefix] [-z suffix] [-f {filelist|-}] [-s] [-r|-x] filename ...\n"
	       "\n"
	       "\tCreate hard linked backup copies of a list of files\n"
	       "\tread from standard input.\n"
	       "\n"
	       "\t-r\tRestore the backup\n"
	       "\t-x\tRemove backup files and empty parent directories\n"
	       "\t-B\tPath name prefix for backup files\n"
	       "\t-z\tPath name suffix for backup files\n"
	       "\t-s\tSilent operation; only print error messages\n\n",
	       progname);
}

void
create_parents(char *filename)
{
	struct stat st;
	char *f = strchr(filename, '/');

	if (f == NULL)
		return;
	*f = '\0';
	if (stat(f, &st) != 0) {
		while (f != NULL) {
			*f = '\0';
			mkdir(filename, 0777);
			*f = '/';
			f = strchr(f+1, '/');
		}
	} else {
		*f = '/';
	}
}

void
remove_parents(char *filename)
{
	char *f, *g = NULL;

	f = strrchr(filename, '/');
	while ((f = strrchr(filename, '/')) != NULL) {
		if (g != NULL)
			*g = '/';
		g = f;
		*f= '\0';
		
		rmdir(filename);
	}
	if (g != NULL)
		*g = '/';
}

static int
link_or_copy(const char *from, const char *to)
{
	char buffer[4096];
	int from_fd, to_fd, error = 1;
	size_t len;

	if (link(from, to) == 0)
		return 0;
	if (errno != EXDEV && errno != EPERM && errno != EMLINK) {
		fprintf(stderr, "Could not link file `%s' to `%s': %s\n",
		       from, to, strerror(errno));
		return 1;
	}

	if ((from_fd = open(from, O_RDONLY)) == -1) {
		perror(from);
		return 1;
	}
	if ((to_fd = open(to, O_WRONLY|O_TRUNC))) {
		perror(to);
		close(from_fd);
		return 1;
	}
	while ((len = read(from_fd, buffer, sizeof(buffer))) > 0) {
		if ((write(to_fd, buffer, len)) == -1) {
			perror(to);
			unlink(to);
			goto out;
		}
	}
	if (len != 0) {
		perror(from);
		unlink(to);
		goto out;
	}

	error = 0;
out:
	close(from_fd);
	close(to_fd);

	return error;
}

int
process_file(char *file)
{
	char backup[LINE_LENGTH];

	if (strlen(opt_prefix) + strlen(file) +
	    strlen(opt_suffix) >= sizeof(backup)) {
		perror("Line buffer too small\n");
		return 1;
	}

	snprintf(backup, sizeof(backup), "%s%s%s",
		 opt_prefix, file, opt_suffix);

	if (opt_what == what_backup) {
		struct stat st;
		int new_file = (stat(file, &st) == -1 && errno == ENOENT);

		unlink(backup);
		create_parents(backup);
		if (new_file) {
			int fd;

			if (!opt_silent)
				printf("New file %s\n", file);
			if ((fd = creat(backup, 0666)) == -1) {
				perror(backup);
				return 1;
			}
			close(fd);
		} else {
			if (!opt_silent)
				printf("Copying %s\n", file);
			if (link_or_copy(file, backup) != 0)
				return 1;
		}
		return 0;
	} else if (opt_what == what_restore) {
		struct stat st;

		create_parents(file);
		if (stat(backup, &st) != 0) {
			perror(backup);
			return 1;
		}
		if (st.st_size == 0) {
			if (unlink(file) == 0 || errno == ENOENT) {
				if (!opt_silent)
					printf("Removing %s\n", file);
				unlink(backup);
				remove_parents(backup);
			} else {
				perror(file);
				return 1;
			}
		} else {
			if (!opt_silent)
				printf("Restoring %s\n", file);
			unlink(file);
			if (link(backup, file) == -1) {
				if (link_or_copy(backup, file) != 0)
					return 1;
				unlink(backup);
				remove_parents(backup);
			}
		}
		return 0;
	} else if (opt_what == what_remove) {
		unlink(backup);
		remove_parents(backup);
		return 0;
	} else
		return 1;
}

int
main(int argc, char *argv[])
{
	int opt, status=0;
	
	progname = argv[0];

	while ((opt = getopt(argc, argv, "rxB:z:f:sh")) != -1) {
		switch(opt) {
			case 'r':
				opt_what = what_restore;
				break;

			case 'x':
				opt_what = what_remove;
				break;

			case 'B':
				opt_prefix = optarg;
				break;
				
			case 'f':
				opt_file = optarg;
				break;

			case 'z':
				opt_suffix = optarg;
				break;

			case 's':
				opt_silent = 1;
				break;

			case 'h':
			default:
				usage();
				return 0;
		}
	}

	if ((*opt_prefix == '\0' && *opt_suffix == '\0') ||
	    (opt_file == NULL && optind == argc)) {
		usage();
		return 1;
	}

	if (opt_file != NULL) {
		FILE *file;
		char line[LINE_LENGTH];

		if (!strcmp(opt_file, "-")) {
			file = stdin;
		} else {
			if ((file = fopen(opt_file, "r")) == NULL) {
				perror(opt_file);
				return 1;
			}
		}

		while (fgets(line, sizeof(line), file)) {
			char *l = strchr(line, '\0');

			if (l > line && *(l-1) == '\n')
				*(l-1) = '\0';
			if (*line == '\0')
				continue;
				
			if ((status = process_file(line)) != 0)
				return status;
		}

		if (file != stdin) {
			fclose(file);
		}
	}
	for (; optind < argc; optind++) {
		if ((status = process_file(argv[optind])) != 0)
			return status;
	}

	return status;
}