File: faubackup-gather.c

package info (click to toggle)
faubackup 0.5pre1-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 252 kB
  • ctags: 69
  • sloc: ansic: 808; perl: 402; makefile: 93; sh: 3
file content (311 lines) | stat: -rw-r--r-- 6,241 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
/*
 *  FauBackup - Backup System, using a Filesystem for Storage
 *  Copyright (C) 2000-2002 Martin Waitz, Dr. Volkmar Sieh
 *  $Id: vbackup-gather.c,v 1.6.2.1 2001/05/21 13:41:09 mnwaitz Exp $
 *
 *  This program 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 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */



#include <sys/types.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <utime.h>

#include "faubackup.h"


int error;
char* progname;


static void
send_buf(void *buf, int len)
{
	int ret;

	while( len > 0 ) {
		ret = write( 1, buf, len );
		if( ret < 0 ) {
			fprintf( stderr, "%s: write(1, ...): %s\n",
					progname, strerror(errno));
			exit(1);
		}
		buf += ret;
		len -= ret;
	}
}

static void
send_string(char *str)
{
	unsigned int tmp;
	tmp = htonl( strlen(str) );

	send_buf( &tmp, sizeof(tmp) );
	send_buf( str, strlen(str) );
}

static void
send_inode(struct stat *st)
{
	unsigned int tmp;

	/*
	 * send contents of inode
	 */
	assert( sizeof(tmp) == 4 );
	tmp = htonl(st->st_dev); send_buf( &tmp, sizeof(tmp) );
	tmp = htonl(st->st_ino); send_buf( &tmp, sizeof(tmp) );
	tmp = st->st_mode & 07777;
	switch( st->st_mode & ~07777 ) {
	case S_IFBLK: tmp |= B_IFBLK; break;
	case S_IFCHR: tmp |= B_IFCHR; break;
	case S_IFDIR: tmp |= B_IFDIR; break;
	case S_IFIFO: tmp |= B_IFIFO; break;
	case S_IFLNK: tmp |= B_IFLNK; break;
	case S_IFREG: tmp |= B_IFREG; break;
	case S_IFSOCK: tmp |= B_IFSOCK; break;
	default: assert(0); break;
	}
	tmp = htonl(tmp);
	send_buf( &tmp, sizeof(tmp) );
	tmp = htonl(st->st_uid); send_buf( &tmp, sizeof(tmp) );
	tmp = htonl(st->st_gid); send_buf( &tmp, sizeof(tmp) );
	tmp = htonl(st->st_rdev); send_buf( &tmp, sizeof(tmp) );
	tmp = htonl(st->st_size); send_buf( &tmp, sizeof(tmp) );
/*	tmp = htonl(st->st_atime); send_buf( &tmp, sizeof(tmp) );*/
	tmp = htonl(st->st_mtime); send_buf( &tmp, sizeof(tmp) );
}


static void
gather_blk(char *path, struct stat *st)
{
	assert( S_ISBLK(st->st_mode) );

	send_inode( st );
	send_string( path );
}


static void
gather_chr(char *path, struct stat *st)
{
	assert( S_ISCHR(st->st_mode) );

	send_inode( st );
	send_string( path );
}


static void
gather_reg(char *path, struct stat *st)
{
	int fd;
	int ret;
	int pos;
	struct utimbuf times;

	assert( S_ISREG(st->st_mode) );

	fd = open( path, O_RDONLY );
	if( fd < 0) {
		fprintf( stderr, "%s: open(%s, O_RDONLY): %s\n",
				progname, path, strerror(errno));
		error++;
		return;
	}

	send_inode( st );
	send_string( path );

	for( pos = 0; pos < st->st_size; ) {
		char buf[1024*1024];
		int count;
		int len;

		count = st->st_size - pos;
		if( sizeof(buf) < count ) {
			count = sizeof(buf);
		}
		len = read(fd, buf, count);
		if( len < 0) {
			fprintf(stderr, "%s: read %s: %s\n",
					progname, path, strerror(errno));
			error++;

			/* ignore any error and send `count' bytes */
			/* otherwise we will get a protocol error */
			len = count;
		}

		send_buf( buf, len );

		pos += len;
	}

	ret = close(fd);
	if( ret < 0) {
		fprintf(stderr, "%s: close %s: %s\n",
				progname, path, strerror(errno));
		error++;
		return;
	}

	/* reset atime of file */
	times.actime = st->st_atime;
	times.modtime = st->st_mtime;
	ret = utime( path, &times );
	if( ret<0 && errno != EROFS ) {
		fprintf(stderr, "%s: utime %s: %s\n",
				progname, path, strerror(errno));
		return;
	}
}


static void
gather_lnk(char *path, struct stat *st)
{
	char link[MAXLEN];
	int ret;

	assert( S_ISLNK(st->st_mode) );

	ret = readlink(path, link, sizeof(link) - 1);
	if( ret < 0) {
		fprintf(stderr, "%s: readlink %s: %s\n",
				progname, path, strerror(errno));
		error++;
		return;
	}
	link[ret] = '\0';

	send_inode( st );
	send_string( path );
	send_string( link );
}


static void
gather_dir(char *path, struct stat *st)
{
	assert( S_ISDIR(st->st_mode) );

	send_inode( st );
	send_string( path );
}


static void
gather(char *path)
{
	int ret;
	struct stat st;

	ret = stat(".", &st);
	if( ret < 0) {
		fprintf(stderr, "%s: stat(\".\", &st): %s\n",
				progname, strerror(errno));
		error++;
		exit( 1 );
	}

	ret = lstat( path, &st );
	if( ret < 0) {
		fprintf(stderr, "%s: lstat %s: %s\n",
				progname, path, strerror(errno));
		error++;
		return;
	}

	if( S_ISBLK(st.st_mode) ) {
		gather_blk(path, &st);
	} else if( S_ISCHR(st.st_mode) ) {
		gather_chr(path, &st);
	} else if( S_ISREG(st.st_mode) ) {
		gather_reg(path, &st);
	} else if( S_ISLNK(st.st_mode) ) {
		gather_lnk(path, &st);
	} else if( S_ISDIR(st.st_mode) ) {
		gather_dir(path, &st);
	} else if( S_ISFIFO(st.st_mode)
		|| S_ISSOCK(st.st_mode) ) {
		/* skip fifo/socket */
		/* nothing to do */
	} else {
		fprintf( stderr, "%s: %s: unknown mode 0%o\n",
				progname, path, (int) st.st_mode );
		error++;
		return;
	}

}

int
main(int argc, char* argv[])
{
	char path[MAXLEN];
	int verbose;
	int c, len;

	error=0;
	progname=argv[0];
	verbose=0;

	if( argc==2 && strcmp(argv[1], "-v")==0 ) {
		verbose=1;
	} else if( argc>1 ) {
		fprintf(stderr, "Usage: %s [-v]\n", progname);
		exit( 1 );
	}

	len = 0;
	while( (c=fgetc(stdin)) != EOF ) {
		path[len++] = c;

		if( len==sizeof(path) ) {
			/* didn't fit in here -> skip */
			path[sizeof(path)-1] = '\0';
			fprintf(stderr, "path too long: %s...!\n", path);
			while( fgetc(stdin)>0 );
			len = 0;
			continue;
		}

		if( c!='\0' ) continue;
		/* one filename read, process it */

		if( verbose) {
			fprintf(stderr, "%s\n", path);
		}

		gather( path );
		len = 0;
	}

	return( error!=0 );
}