amanda /amanda/tags/amanda252p1/restore-src/amrestore.c

Language C Lines 433
MD5 Hash 6532f47c8d3aebcdb508fdb69484ba47 Estimated Cost $7,819 (why?)
Repository https://amanda.svn.sourceforge.net/svnroot/amanda View Raw File
  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
/*
 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
 * Copyright (c) 1991-1998 University of Maryland at College Park
 * All Rights Reserved.
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of U.M. not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  U.M. makes no representations about the
 * suitability of this software for any purpose.  It is provided "as is"
 * without express or implied warranty.
 *
 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Authors: the Amanda Development Team.  Its members are listed in a
 * file named AUTHORS, in the root directory of this distribution.
 */
/*
 * $Id: amrestore.c,v 1.63 2006/07/25 18:58:10 martinea Exp $
 *
 * retrieves files from an amanda tape
 */
/*
 * Pulls all files from the tape that match the hostname, diskname and
 * datestamp regular expressions.
 *
 * If the header is output, only up to DISK_BLOCK_BYTES worth of it is
 * sent, regardless of the tape blocksize.  This makes the disk image
 * look like a holding disk image, and also makes it easier to remove
 * the header (e.g. in amrecover) since it has a fixed size.
 */

#include "amanda.h"
#include "util.h"
#include "tapeio.h"
#include "fileheader.h"
#include "restore.h"

#define CREAT_MODE	0640

static off_t file_number;
static pid_t comp_enc_pid = -1;
static int tapedev;
static off_t filefsf = (off_t)-1;

/* local functions */

static void errexit(void);
static void usage(void);
int main(int argc, char **argv);

/*
 * Do exit(2) after an error, rather than exit(1).
 */

static void
errexit(void)
{
    exit(2);
}


/*
 * Print usage message and terminate.
 */

static void
usage(void)
{
    error("Usage: amrestore [-b blocksize] [-r|-c] [-p] [-h] [-f fileno] "
    	  "[-l label] tape-device|holdingfile [hostname [diskname [datestamp "
	  "[hostname [diskname [datestamp ... ]]]]]]");
    /*NOTREACHED*/
}


/*
 * Parses command line, then loops through all files on tape, restoring
 * files that match the command line criteria.
 */

int
main(
    int		argc,
    char **	argv)
{
    extern int optind;
    int opt;
    char *errstr;
    int isafile;
    struct stat stat_tape;
    dumpfile_t file;
    char *filename = NULL;
    char *tapename = NULL;
    struct match_list {
	char *hostname;
	char *diskname;
	char *datestamp;
	struct match_list *next;
    } *match_list = NULL, *me = NULL;
    int found_match;
    int arg_state;
    amwait_t compress_status;
    int r = 0;
    char *e;
    char *err;
    char *label = NULL;
    rst_flags_t *rst_flags;
    int count_error;
    long tmplong;
    ssize_t read_result;

    safe_fd(-1, 0);

    set_pname("amrestore");

    dbopen(DBG_SUBDIR_SERVER);

    /* Don't die when child closes pipe */
    signal(SIGPIPE, SIG_IGN);

    erroutput_type = ERR_INTERACTIVE;

    onerror(errexit);

    rst_flags = new_rst_flags();
    rst_flags->inline_assemble = 0;

    /* handle options */
    while( (opt = getopt(argc, argv, "b:cCd:rphf:l:")) != -1) {
	switch(opt) {
	case 'b':
	    tmplong = strtol(optarg, &e, 10);
	    rst_flags->blocksize = (ssize_t)tmplong;
	    if(*e == 'k' || *e == 'K') {
		rst_flags->blocksize *= 1024;
	    } else if(*e == 'm' || *e == 'M') {
		rst_flags->blocksize *= 1024 * 1024;
	    } else if(*e != '\0') {
		error("invalid rst_flags->blocksize value \"%s\"", optarg);
		/*NOTREACHED*/
	    }
	    if(rst_flags->blocksize < DISK_BLOCK_BYTES) {
		error("minimum block size is %dk", DISK_BLOCK_BYTES / 1024);
		/*NOTREACHED*/
	    }
	    if(rst_flags->blocksize > MAX_TAPE_BLOCK_KB * 1024) {
		fprintf(stderr,"maximum block size is %dk, using it\n",
			MAX_TAPE_BLOCK_KB);
		rst_flags->blocksize = MAX_TAPE_BLOCK_KB * 1024;
		/*NOTREACHED*/
	    }
	    break;
	case 'c': rst_flags->compress = 1; break;
	case 'C':
	    rst_flags->compress = 1;
	    rst_flags->comp_type = COMPRESS_BEST_OPT;
	    break;
	case 'r': rst_flags->raw = 1; break;
	case 'p': rst_flags->pipe_to_fd = fileno(stdout); break;
	case 'h': rst_flags->headers = 1; break;
	case 'f':
	    filefsf = (off_t)OFF_T_STRTOL(optarg, &e, 10);
	    /*@ignore@*/
	    if(*e != '\0') {
		error("invalid fileno value \"%s\"", optarg);
		/*NOTREACHED*/
	    }
	    /*@end@*/
	    break;
	case 'l':
	    label = stralloc(optarg);
	    break;
	default:
	    usage();
	}
    }

    if(rst_flags->compress && rst_flags->raw) {
	fprintf(stderr,
		"Cannot specify both -r (raw) and -c (compressed) output.\n");
	usage();
    }

    if(optind >= argc) {
	fprintf(stderr, "%s: Must specify tape-device or holdingfile\n",
			get_pname());
	usage();
    }

    tapename = argv[optind++];

#define ARG_GET_HOST 0
#define ARG_GET_DISK 1
#define ARG_GET_DATE 2

    arg_state = ARG_GET_HOST;
    while(optind < argc) {
	switch(arg_state) {
	case ARG_GET_HOST:
	    /*
	     * This is a new host/disk/date triple, so allocate a match_list.
	     */
	    me = alloc(SIZEOF(*me));
	    me->hostname = argv[optind++];
	    me->diskname = "";
	    me->datestamp = "";
	    me->next = match_list;
	    match_list = me;
	    if(me->hostname[0] != '\0'
	       && (errstr=validate_regexp(me->hostname)) != NULL) {
	        fprintf(stderr, "%s: bad hostname regex \"%s\": %s\n",
		        get_pname(), me->hostname, errstr);
	        usage();
	    }
	    arg_state = ARG_GET_DISK;
	    break;
	case ARG_GET_DISK:
	    me->diskname = argv[optind++];
	    if(me->diskname[0] != '\0'
	       && (errstr=validate_regexp(me->diskname)) != NULL) {
	        fprintf(stderr, "%s: bad diskname regex \"%s\": %s\n",
		        get_pname(), me->diskname, errstr);
	        usage();
	    }
	    arg_state = ARG_GET_DATE;
	    break;
	case ARG_GET_DATE:
	    me->datestamp = argv[optind++];
	    if(me->datestamp[0] != '\0'
	       && (errstr=validate_regexp(me->datestamp)) != NULL) {
	        fprintf(stderr, "%s: bad datestamp regex \"%s\": %s\n",
		        get_pname(), me->datestamp, errstr);
	        usage();
	    }
	    arg_state = ARG_GET_HOST;
	    break;
	}
    }
    if(match_list == NULL) {
	match_list = alloc(SIZEOF(*match_list));
	match_list->hostname = "";
	match_list->diskname = "";
	match_list->datestamp = "";
	match_list->next = NULL;
    }

    if(tape_stat(tapename,&stat_tape)!=0) {
	error("could not stat %s: %s", tapename, strerror(errno));
	/*NOTREACHED*/
    }
    isafile=S_ISREG((stat_tape.st_mode));

    if(label) {
	if(isafile) {
	    fprintf(stderr,"%s: ignoring -l flag when restoring from a file.\n",
		    get_pname());
	}
	else {
	    if((err = tape_rewind(tapename)) != NULL) {
		error("Could not rewind device '%s': %s", tapename, err);
		/*NOTREACHED*/
	    }
	    if ((tapedev = tape_open(tapename, 0)) == -1) {;
		error("Could not open device '%s': %s", tapename, err);
		/*NOTREACHED*/
	    }
	    read_file_header(&file, tapedev, isafile, rst_flags);
	    if(file.type != F_TAPESTART) {
		fprintf(stderr,"Not an amanda tape\n");
		exit (1);
	    }
	    if(strcmp(label, file.name) != 0) {
		fprintf(stderr,"Wrong label: '%s'\n", file.name);
		exit (1);
	    }
	    tapefd_close(tapedev);
	    if((err = tape_rewind(tapename)) != NULL) {
		error("Could not rewind device '%s': %s", tapename, err);
		/*NOTREACHED*/
	    }
	}
    }
    file_number = (off_t)0;
    if(filefsf != (off_t)-1) {
	if(isafile) {
	    fprintf(stderr,"%s: ignoring -f flag when restoring from a file.\n",
		    get_pname());
	}
	else {
	    if((err = tape_rewind(tapename)) != NULL) {
		error("Could not rewind device '%s': %s", tapename, err);
		/*NOTREACHED*/
	    }
	    if((err = tape_fsf(tapename, filefsf)) != NULL) {
		error("Could not fsf device '%s': %s", tapename, err);
		/*NOTREACHED*/
	    }
	    file_number = filefsf;
	}
    }

    if(isafile) {
	tapedev = open(tapename, O_RDWR);
    } else {
	tapedev = tape_open(tapename, 0);
    }
    if(tapedev < 0) {
	error("could not open %s: %s", tapename, strerror(errno));
	/*NOTREACHED*/
    }

    read_result = read_file_header(&file, tapedev, isafile, rst_flags);
    if(file.type != F_TAPESTART && !isafile && filefsf == (off_t)-1) {
	fprintf(stderr, "%s: WARNING: not at start of tape, file numbers will be offset\n",
			get_pname());
    }

    count_error = 0;
    while(count_error < 10) {
	if(file.type == F_TAPEEND) break;
	found_match = 0;
	if(file.type == F_DUMPFILE || file.type == F_SPLIT_DUMPFILE) {
	    amfree(filename);
	    filename = make_filename(&file);
	    for(me = match_list; me; me = me->next) {
		if(disk_match(&file,me->datestamp,me->hostname,me->diskname,"") != 0) {
		    found_match = 1;
		    break;
		}
	    }
	    fprintf(stderr, "%s: " OFF_T_FMT ": %s ",
			    get_pname(),
			    (OFF_T_FMT_TYPE)file_number,
			    found_match ? "restoring" : "skipping");
	    if(file.type != F_DUMPFILE  && file.type != F_SPLIT_DUMPFILE) {
		print_header(stderr, &file);
	    } else {
		fprintf(stderr, "%s\n", filename);
	    }
	}
	if(found_match) {
	    count_error=0;
	    read_result = restore(&file, filename,
		tapedev, isafile, rst_flags);
	    if(comp_enc_pid > 0) {
		waitpid(comp_enc_pid, &compress_status, 0);
		comp_enc_pid = -1;
	    }
	    if(rst_flags->pipe_to_fd != -1) {
		file_number++;			/* for the last message */
		break;
	    }
	}
	if(isafile) {
	    break;
	}
	/*
	 * Note that at this point we know we are working with a tape,
	 * not a holding disk file, so we can call the tape functions
	 * without checking.
	 */
	if(read_result == 0) {
	    /*
	     * If the last read got EOF, how to get to the next
	     * file depends on how the tape device driver is acting.
	     * If it is BSD-like, we do not really need to do anything.
	     * If it is Sys-V-like, we need to either fsf or close/open.
	     * The good news is, a close/open works in either case,
	     * so that's what we do.
	     */
	    tapefd_close(tapedev);
	    if((tapedev = tape_open(tapename, 0)) < 0) {
		error("could not open %s: %s", tapename, strerror(errno));
		/*NOTREACHED*/
	    }
	    count_error++;
	} else {
	    /*
	     * If the last read got something (even an error), we can
	     * do an fsf to get to the next file.
	     */
	    if(tapefd_fsf(tapedev, (off_t)1) < 0) {
		error("could not fsf %s: %s", tapename, strerror(errno));
		/*NOTREACHED*/
	    }
	    count_error=0;
	}
	file_number++;
	read_result = read_file_header(&file, tapedev, isafile, rst_flags);
    }
    if(isafile) {
	close(tapedev);
    } else {
	/*
	 * See the notes above about advancing to the next file.
	 */
	if(read_result == 0) {
	    tapefd_close(tapedev);
	    if((tapedev = tape_open(tapename, 0)) < 0) {
		error("could not open %s: %s", tapename, strerror(errno));
		/*NOTREACHED*/
	    }
	} else {
	    if(tapefd_fsf(tapedev, (off_t)1) < 0) {
		error("could not fsf %s: %s", tapename, strerror(errno));
		/*NOTREACHED*/
	    }
	}
	tapefd_close(tapedev);
    }

    if((read_result <= 0 || file.type == F_TAPEEND) && !isafile) {
	fprintf(stderr, "%s: " OFF_T_FMT ": reached ",
		get_pname(), (OFF_T_FMT_TYPE)file_number);
	if(read_result <= 0) {
	    fprintf(stderr, "end of information\n");
	} else {
	    print_header(stderr,&file);
	}
	r = 1;
    }
    return r;
}
Back to Top