File: gfunc.c

package info (click to toggle)
rdup 0.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 784 kB
  • ctags: 108
  • sloc: sh: 3,225; ansic: 1,624; perl: 565; makefile: 65
file content (516 lines) | stat: -rw-r--r-- 10,708 bytes parent folder | download
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/*
 * Copyright (c) 2005 - 2008 Miek Gieben
 * See LICENSE for the license
 *
 * g_tree_foreach helper functions
 */

#include "rdup.h"

extern gboolean opt_null;
extern gboolean opt_removed;
extern gboolean opt_modified;
extern gboolean opt_skip;
extern gboolean opt_local;
extern gint opt_verbose;
extern char *opt_format;
extern char qstr[];
extern time_t opt_timestamp;
extern size_t opt_size;
extern sig_atomic_t sig;

/* sha1.c */
int sha1_stream(FILE *stream, void *resblock);

/*
 * calculates a files sha1 sum
 */
static gboolean
sha1(FILE *fp, char *filename) 
{
	unsigned char digest[SHA1_LEN];
	gint i;
	FILE *file;

	if ((file = fopen(filename, "r")) == NULL) {
		msg(_("Could not open '%s\': %s"), filename, strerror(errno));
		return FALSE;
	}
	if (sha1_stream(file, digest) != 0) {
		msg(_("Failed to calculate sha1 digest: `%s\'"), filename);
		fclose(file);
		return FALSE;
	}
	fclose(file);
	for(i = 0; i < SHA1_LEN; i++) {
		fprintf(fp, "%02x", digest[i]);
	}
	return TRUE;
}
/*
 * cat the files' contents
 */
static gboolean
cat(FILE *fp, char *filename, off_t f_size)
{
	char buf[BUFSIZE + 1];
	FILE *file;
	size_t i;
	size_t t;
	size_t missing;

	if ((file = fopen(filename, "r")) == NULL) {
		msg(_("Could not open '%s\': %s"), filename, strerror(errno));
		return FALSE;
	}

	t = 0;
	while (!feof(file) && (!ferror(file))) {
		if (sig != 0) {
			fclose(file);
			signal_abort(sig);
		}
		i = fread(buf, sizeof(char), BUFSIZE, file);
		t += i;
		if (t > (size_t) f_size) {
			/* the file has grown. Break off the write!! */
			msg(_("File grown larger (%zd) than original file size (%zd) , cutting off: `%s\'"), t, (size_t) f_size, filename);
			/* what's missing and what is read in the previous read */
			missing = t - i;
			missing = (size_t) f_size - missing;
			if (missing > 0) {
				/* write the missing bytes till f_size */
				if (fwrite(buf, sizeof(char), missing, fp) != missing) {
					msg(_("Write failure `%s\': %s"), filename, strerror(errno));
					fclose(file);
					return FALSE;
				}
			}
			fclose(file);
			return TRUE;
		}

		if (fwrite(buf, sizeof(char), i, fp) != i) {
			msg(_("Write failure `%s\': %s"), filename, strerror(errno));
			fclose(file);
			return FALSE;
		}
	}
	fclose(file);

	/* file has shrunken! Fill the rest with NULLs, this works
	 * but is slow! */
	if (t < (size_t) f_size) {
		msg(_("File has shrunk, filling with NULLs: `%s\'"), filename);
		for(i = t; i < (size_t) f_size; i++) {
			fputc('\0', fp);
		}
	}
	return TRUE;
}

/*
 * cat the contents, only when adding and only for files/links
 */
static void
entry_cat_data(FILE *fp, struct entry *e)
{
	if (S_ISREG(e->f_mode) && e->f_lnk == 0) {
		if (!cat(fp, e->f_name, e->f_size)) {
			exit(EXIT_FAILURE);
		}
		return;
	}
	/* special case for links isn't needed anymore
	 * link name is now embedded in the path: link -> target
	 */
#if 0
	if (S_ISLNK(e->f_mode)) {
		char buf[BUFSIZE + 1];
		ssize_t i;
		if ((i = readlink(e->f_name, buf, BUFSIZE)) == -1) {
			msg(_("Error reading link `%s\': %s"), e->f_name, strerror(errno));
			exit(EXIT_FAILURE);
		}
		buf[i] = '\0';
		fprintf(fp, "%s", buf);
		return;
	}
#endif
}

/**
 * print an escape sequence correctly
 */
static void
entry_print_escape(char n, FILE *out)
{
	switch (n) {
		case 'a':
			fputc('\a', out);
			break;
		case 'b':
			fputc('\b', out);
			break;
		case 'e':
			fputc('\e', out);
			break;
		case 'f':
			fputc('\f', out);
			break;
		case 'r':
			fputc('\r', out);
			break;
		case 't':
			fputc('\t', out);
			break;
		case 'v':
			fputc('\v', out);
			break;
		case '0':
			fputc('\0', out);
			break;
		case 'n':
			fputc('\n', out);
			break;
		default:
			fputc(n, out);
			break;
	}
}

/**
 * print arbitrary data field
 */
static void
entry_print_data(FILE *out, char n, struct entry *e)
{
	switch (n) {
		case 'n':
			fputs(e->f_name, out);
			break;
		case 'l':
			fprintf(out, "%ld", (unsigned long)e->f_name_size);
			break;
		case 'u':
			fprintf(out, "%ld", (unsigned long)e->f_uid);
			break;
		case 'g':
			fprintf(out, "%ld", (unsigned long)e->f_gid);
			break;
		case 'm':
			fprintf(out, "%d", (int)e->f_mode);
			break;
	        case 'b':
                       fprintf(out, "%.4o", (int)e->f_mode & F_PERM);
                       break;
		case 't':
			fprintf(out, "%ld", (unsigned long)e->f_ctime);
			break;
		case 's':
			/* don't report size for directories. */
			if (S_ISDIR(e->f_mode)) {
				fputc('0', out);
				break;
			}
			fprintf(out, "%zd", (size_t)e->f_size);
			break;
		case 'H': /* sha1 hash */
			if (S_ISREG(e->f_mode)) {
				/* normal file and no hardlink */
				if (e->f_lnk == 0) {
					sha1(out, e->f_name);
				}
			} else {
				fprintf(out, NO_SHA);
			}
			break;
		case 'T': /* file type */
			if (S_ISDIR(e->f_mode)) {
				fputc('d', out);
			} else if (S_ISLNK(e->f_mode)) {
				fputc('l', out);
			} else {
				if (e->f_lnk == 1) {
					fputc('h', out);
				} else
					fputc('-', out);
			}
			break;
		default:
			fputc(' ', out);
			break;
	}
}

/**
 * print function
 */
void
entry_print(FILE *out, char plusmin, struct entry *e)
{
	char *pos;
	struct stat s;

	if ((plusmin == '+') && (opt_modified == FALSE)) {
		return;
	}

	if ((plusmin == '-') && (opt_removed == FALSE)) {
		return;
	}

	if (opt_verbose > 1) {
		fputs("** ", stderr);
		fputc(plusmin, stderr);
		fprintf(stderr, " %s\n", e->f_name);
	}

	if (S_ISREG(e->f_mode) && plusmin == '+' && !opt_local
		&& e->f_lnk == 0) {
		/* check if the file has changed since we first
		 * visited it. If so, skip it as it will tear
		 * up the entire print. Esp. when also printing
		 * the contents. The recheck here, minimizes the
		 * race, it's NOT GONE!!
		 *
		 * This is not a problem for directories, nor sym- and
		 * hard links
		 */
		if (lstat(e->f_name, &s) != 0) {
			msg(_("Could not stat path `%s\': %s"), e->f_name,
				strerror(errno));
			return;
		}
#ifndef _DEBUG_RACE
		if (e->f_size != s.st_size) {
			msg(_("File size has changed, skipping `%s\'"), e->f_name);
			return;
		}
#endif /* _DEBUG_RACE */
	}

	for (pos = opt_format; *pos != '\0';  ++pos) {
		switch (*pos) {
			/* c-style escapes are valid */
			case '\\':
				++pos;
				entry_print_escape(*pos, out);
				break;
				/* emit data */
			case '%':
				++pos;
				switch (*pos) {
					case '%':
						  fputc('%', out);
						  break;
					case 'p':
						  fputc(plusmin, out);
						  break;
					case 'C':
						  if (plusmin == '+') {
						  	entry_cat_data(out, e);
						  }
						  break;
					default:
						  entry_print_data(out, *pos, e);
						  break;
				}
				break;
				/* don't know? echo it. */
			default:
				fputc(*pos, out);
				break;
		}
	}
}

/**
 * Write our internal filelist
 */
gboolean
gfunc_write(gpointer data, gpointer value, gpointer fp)
{
	struct entry *e = (struct entry*)data;
	if (sig != 0)
		signal_abort(sig);

	if (value == NO_PRINT)
		return FALSE;

	fprintf((FILE*) fp, "%ld %ld %ld %ld %s", (long int)e->f_mode, (long int)e->f_dev, 
			(long int)e->f_ino, (long int)e->f_name_size, e->f_name);
	if (opt_null) {
		fputc('\0', (FILE*)fp);
	} else {
		fputc('\n', (FILE*)fp);
	}
	return FALSE;
}

/**
 * write out the list of to be backupped items
 */
gboolean
gfunc_backup(gpointer data, gpointer value,
		__attribute__((unused)) gpointer usr)
{
	if (sig != 0)
		signal_abort(sig);

	/* .nobackup seen, don't print it */
	if (value == NO_PRINT) 
		return FALSE;

	if (S_ISDIR(((struct entry*)data)->f_mode)) {
		entry_print(stdout, '+', (struct entry*)data);
		return FALSE;
	}
	if (S_ISREG(((struct entry*)data)->f_mode) ||
			S_ISLNK(((struct entry*)data)->f_mode)) {

		if (opt_size != 0 && ((struct entry*)data)->f_size > (ssize_t)opt_size) {
			return FALSE;
		}
		switch (opt_timestamp) {
			case NULL_DUMP:
				entry_print(stdout, '+', (struct entry*)data);
				return FALSE;
			default: /* INC_DUMP */
				if (((struct entry*)data)->f_ctime > opt_timestamp)
					entry_print(stdout, '+', (struct entry*)data);
				return FALSE;
		}
	}
	return FALSE;
}

/**
 * write out the list of removed items
 */
gboolean
gfunc_remove(gpointer data, gpointer value,
		__attribute__((unused)) gpointer usr)
{
	if (sig != 0)
		signal_abort(sig);

	/* should not have these here!! */
	if (value == NO_PRINT) {
		msg(_("Internal error: NO_PRINT in remove tree!"));
		return FALSE;
	}

	entry_print(stdout, '-', (struct entry*)data);
	return FALSE;
}

/**
 * Print out the list of new item
 */
gboolean
gfunc_new(gpointer data, __attribute__((unused)) gpointer value,
		__attribute__((unused)) gpointer usr)
{
	if (sig != 0)
		signal_abort(sig);

	/* it is perfectly possibly to have these here */
	if (value == NO_PRINT) 
		return FALSE;

	entry_print(stdout, '+', (struct entry*)data);
	return FALSE;
}

/**
 * decide whether 2 struct entries are equal or not
 */
gint
gfunc_equal(gconstpointer a, gconstpointer b)
{
	gint e;

	if (sig != 0)
		signal_abort(sig);

	e = strcmp(((struct entry*)a)->f_name, ((struct entry*)b)->f_name);
	if (e == 0) {
		if (((struct entry*)a)->f_dev != ((struct entry*)b)->f_dev)
			return -1;
		if (((struct entry*)a)->f_ino != ((struct entry*)b)->f_ino)
			return -1;
		if (((struct entry*)a)->f_mode == ((struct entry*)b)->f_mode)
			return 0;
	}
	return e;
}

/**
 * used in the crawler, remove specific paths
 */
gboolean
gfunc_remove_path(gpointer data, gpointer __attribute__((unused)) value, gpointer r)
{
	if (sig != 0)
		signal_abort(sig);

	if (!strncmp(((struct entry*)data)->f_name,
				((struct remove_path *)r)->path,
				((struct remove_path *)r)->len)) {

		/* don't remove the directory itself */
		if (S_ISDIR( ((struct entry*)data)->f_mode))
			return FALSE;

		g_tree_insert(
			((struct remove_path *)r)->tree, data, NO_PRINT);
	}
	return FALSE;
}

/**
 * traverse function
 * implement the tree substraction
 * everything in A, but NOT in b
 * (data := element out of A)
 * (b    := packed in diff, diff->b)
 * This function is essentially the most expensive function
 * in rdup...
 */
gboolean
gfunc_substract(gpointer data, gpointer value, gpointer diff)
{
	gpointer v;
	if (sig != 0)
		signal_abort(sig);

	v = g_tree_lookup((GTree*)((struct substract*)diff)->b, data);

	if (!v) 
		g_tree_insert(((struct substract*)diff)->d, data, value);

	return FALSE;
}

/**
 * Walk the linked list and return TRUE when a regexp
 * matches, otherwise return FALSE
 */
gboolean
gfunc_regexp(GSList *l, char *n)
{
        GSList *k;
        regex_t *R;
	int e;

        for (k = g_slist_nth(l, 0); k; k = g_slist_next(k)) { 
		if (sig != 0)
			signal_abort(sig);

                R = (regex_t*) k->data;
                if ((e = regexec(R, n, 0, NULL, 0)) == 0)
                        return TRUE;
        }
        return FALSE;
}