File: dds_extract.c

package info (click to toggle)
dds2tar 2.4.21-2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 352 kB
  • ctags: 167
  • sloc: ansic: 1,822; makefile: 182; sh: 158; csh: 50
file content (574 lines) | stat: -rw-r--r-- 12,607 bytes parent folder | download | duplicates (2)
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574

/*
 * This file is part of dds2tar.
 * Copyright by J"org Weule
 */

#include <stdlib.h>
#include <stdio.h>
#include <sys/mtio.h>
#include <string.h>
#include <unistd.h>

/*
 * fnmatch() and FNM_LEADING_DIR
 */
#define _GNU_SOURCE 1
#include <fnmatch.h>

#include "dds2tar.h"
#include "dds_tape.h"

/*
 * Compare two strings
 *
 */
int dds_strcmp(const char*n1,const char*n2){
	int i = 0 ;
	int j = 0 ;
	while ( 1 ){
		while ( n1[i] == '\\' ) i++ ;
		while ( n2[j] == '\\' ) j++ ;
		if ( i > 90 || j > 90 || n1[i] == '\0' || n2[j] == '\0' )
			return 0 ;
		if  ( n1[i] != n2[j] ) return 1 ;
		i++ ;
		j++ ;
	}
	return 0 ;
}

/*
 * To count the number of blocks written, we use the variable nblocks.
 */
static int nblocks = 0;

/*
 * Fill the buffer 'cur_block' the block of the given number.
 * first we check the block number of the copy that is hold inside the
 * buffer cur_block.
 */
static int
set_cur_block(int const sb)
{
	int     n;

#ifdef DDS_TRACE
	fprintf(stderr,"%d\n",__LINE__);
	fprintf(stderr, "set_cur_block(%d)\n", sb);
#endif

	if (sb != cur_blkno) {
		/*
		 * We have to read the block.
		 */
		next_blkno = dds_getpos(device);
		/*
		 * next_blkno stores now the number of the next block of the
		 * tape.
		 */
		n = sb - next_blkno;
		/*
		 * In some cases reading is faster then seeking.
		 */
		if ((n > 0) && (n < DONT_SKIP)) {
			do {
				dds_read_block();
			} while ((--n) > 0);
		}
		/*
		 * Now we should be at the position.
		 */
		n = sb - next_blkno;
		if (n != 0) {
			dds_seek(device, sb);
			next_blkno = sb;
		}
		/*
		 * Now we read the block. cur_n == 0 indicates a filemark.
		 */
		dds_read_block();
	}
	return 0;
}

/*
 * procedure to extract the files from the specified area (sb:sr-eb:er).
 * If area is of the form (sb:sr-0:0), the file at position (sb,sr) is
 * extracted. The length of the file is read from the tar header record.
 *
 * If list_only is set to 1, only the name of the extraction is printed.
 */
static int
extract(char const *const name, int const sb, int const sr)
{

	int     cur_rec, n, size;
	char   *p;

#ifdef DDS_TRACE
	fprintf(stderr,"%d\n",__LINE__);
	fprintf(stderr, "extract(%s,%d,%d)\n", name, sb, sr);
#endif

	/*
	 * Print only the name.
	 */
	if (list_only == 1) {
		printf("%7d%3d: %s\n", sb, sr, name);
		return 0;
	}
	/*
	 * Print the filename and the location for verbose output.
	 */
	if (verbose != 0) {
		fprintf(stderr,
			"dds2tar at rec %d: %7d%3d: %s\n",
			nblocks, sb, sr, name);
	}
	/*
	 * Check the buffer for the right tape block.
	 */
	set_cur_block(sb);

	cur_rec = sr;
	/*
	 * Check the header block.
	 * The Name field should contain the given name.
	 * The Program will stop if its not.
	 *
	 * Note that for links we can only compare the first
	 * characters, when the index contains somethins like
	 * 'linkname -> filename' (soft link) or
	 * 'linkname link to filename' (hard link)
	 * doesn't match 'linkname'.
	 */
	p = (char*) (cur_block + cur_rec) ;
	if ( dds_is_tar_header_record((tar_record*)p) == 0 ) {
		fprintf(stderr,
			" dds2tar: FATAL ERROR\n"
			" dds2tar: header expected, not found\n"
		);
		if ( force_nochk == 0 ) exit(5);
	}
	if (name != NULL)
	if ( ( (dds_strcmp(name, p)!=0 )
		&& (((tar_record*)p)->hdr.linkflag!='2')
		&& (((tar_record*)p)->hdr.linkflag!='1')
		) || ( strstr(name, p)!=name ) ) {
		fprintf(stderr,
			"dds2tar: FATAL ERROR\n"
			"dds2tar: looked for %s at %d %d\n"
			"dds2tar: not found %s at %d %d\n"
			"dds2tar: Link flag: %c\n"
			"dds2tar: Is it the right tape?\n",
			name, sb , sr ,
			name, dds_getpos(device) - 1, cur_rec,
			((tar_record*)p)->hdr.linkflag);
		if ( force_nochk == 0 ) exit(5);
	}
	/*
	 * First calculate the number of records of the object.
	 */
	size = 0;
	n = 1;

	p = cur_block[cur_rec].hdr.size;
	if (*p != '\0') {
		sscanf(p, "%o", &size);
		n = size;
		n += 1023;
		n >>= 9;
	}
	/*
	 * Now write the records to stdout.
	 */
	if (verbose) {
		fprintf(stderr,
			"dd2tar: %s needs %d blocks\n", name, n);
	}
	if ( write_body == 1 ) {
		cur_rec++;
		while ((size > 0) && (cur_n > 0)) {
			if (cur_rec >= cur_bs) {
				dds_read_block();
				cur_rec = 0;
			}
			write(1, cur_block + cur_rec, (size>=512)? 512:size );
			size -= 512 ;
			cur_rec++;
		}
		exit(0);
	}
	while ((n > 0) && (cur_n > 0)) {
		if (cur_rec >= cur_bs) {
			dds_read_block();
			cur_rec = 0;
		}
		write(1, cur_block + cur_rec, 512);
		nblocks++;
		n--;
		cur_rec++;
	}
	return 0;
}

#ifdef EXP_STUFF
int
extract_loc(char const *const *loc_list)
{
	int cur_rec ;

	while (*loc_list != NULL) {
		int     eb, er, sb, sr;

		sscanf(*loc_list, "%u:%u-%u:%u", &sb, &sr, &eb, &er);
		set_cur_block(sb);
		cur_rec = sr ;
		while (cur_n > 0) {
			int     i;

			if ((cur_n & 0x1ff) != 0) {
				fprintf(stderr,
					"tape record size (%d) is not a"
					" multiple of tar records\n"
					, cur_n
					);
				close(device);
				exit(6);
			}
			i = cur_n >> 9;
			if (cur_blkno == eb)
				i = er;
			while (cur_rec < i) {
				write(1, cur_block + cur_rec++, 512);
				nblocks++;
			}
			/*
			 * if eb==-1 extract until eof
			 */
			if ((cur_rec == er && cur_blkno == eb))
				break;
			i = cur_blkno + 1;
			cur_rec = 0;
			if ((cur_rec == er && cur_blkno == eb))
				break;
			dds_read_block();
		}
		loc_list++;
	}
	return 0;
}

#endif

/*
 * Now we are scanning the table of contents (index file) and match the
 * pathnames there with the given pattern. If a pattern matches, we
 * extract the parent directories (dir_extract()) and the file.
 */
int
dds_cmp(char const *const *const pattern_list)
{
	int i ;
	char *fgets_return_value ;
	char const *const *p;
	int so = 0 ; /*scanoffset*/

	/*
	 * To scan the line of the table of contents (index file)
	 * we need some variables.
	 */
	char   *name = NULL;
	int     blkno, recno, size;

	/*
	 * List of directories entries.
	 */
	struct dir_list {
		char    n[128-2*sizeof(int)];	/* name of the dir */
		int     b, r;	/* block address */
	}
	       *dl;
	int     de = 0;		/* first empty list entry */

	/*
	 * Format of the table of contents.
	 *      dds2index --> tar_index_file == 0
	 *      tar -Rvt  --> tar_index_file == 1
	 */
	int     tar_index_file = 1;

	/*
	 * Bug fix for tar. First record number found inside the
	 * table of contents (index file).
	 */
	int     tar_first_recno = -1;

#ifdef DDS_TRACE
	fprintf(stderr,"%d\n",__LINE__);
	fprintf(stderr,"dds_cmp(%s ...)\n",*pattern_list);
#endif


	/*
	 * First we need some memory.
	 */
	dl = malloc(sizeof (*dl) * 64);
	if (dl == NULL) {
		close(device);
		fprintf(stderr, "dds2tar: no address space available\n");
		exit(7);
	}
	memset(cur_line, 0, 1024);

	/*
	 * Scan the table of conten|s (index file) until eof.
	 */
#ifdef DDS_TRACE
	fprintf(stderr,"%d\n",__LINE__);
#endif
	while (!feof(index_fp)) {
		fgets_return_value = fgets(cur_line, MAXPATHLEN<<2, index_fp);
		if ( fgets_return_value == NULL ) {
			if ( feof(index_fp) ) {
				break ;
			} else {
				perror("dds2tar");
				exit(1);
			}
		}
#ifdef DDS_TRACE
		fprintf(stderr,"%d\n",__LINE__);
		fputs(cur_line, stderr);
#endif

		/*
		 * Check for comment and empty lines.
		 */
		if ((*cur_line == '#') ||
		    (*cur_line == ' ') ||
		    (*cur_line == '\0'))
			continue;

		/*
		 * Check the line for location information.
		 */
#ifdef DDS_TRACE
	fprintf(stderr,"%d\n",__LINE__);
#endif
		if (0 == rt_loc_line())
			continue;

		/*
		 * Check for the first line of the dds2index.
		 */
		if ((0 == strcmp(cur_line, dds_headline)) 
		|| (0 == strcmp(cur_line, dds_old_headline))) {
			tar_index_file = 0;
			tar_n = buf_n ;
			tar_bs = buf_n >> 9 ;
			if ( vid != HP ) dds_set_bs(tar_n);
			continue;
		}
#ifdef DDS_TRACE
		fprintf(stderr,"%d\n",__LINE__);
#endif

		/*
		 * dds2index indicates eof with the string '-end-'.
		 * This line has to be processed in the normal way.
		 * We can stop now processing.
		 */
		if ((*cur_line == '-') &&
		    (strncmp(cur_line, "-end-", 5) == 0)) {
#ifdef DDS_TRACE
			fprintf(stderr,"%d\n",__LINE__);
#endif
			break;
		}

		/*
		 * Scan the line of the index.
		 * Note: The index file of dds2index contains the magic string
		 *   of the tar header, witch depends on the used tar version.
		 */
		if (tar_index_file == 0) {
			rt_line(&blkno, &recno, &size, &name);
		} else {
			/*
			 * check for record line
			 */
			if ((0 != strncmp(cur_line, "rec", 3)) &&
			   (0 != strncmp(cur_line, "block", 5)))
				continue;

			/*
			 * Set scanoffset
			 */
			so = (0 == strncmp(cur_line, "block", 5))? 2 : 0 ;
			
			recno = atoi(cur_line + 4 + so );
			/*
		 	 * Now we are fixing a bug of gnu tar ...
			 * The first number should be zero, othewise we
			 * correct all the numbers.
			 * If tar_first_recno is .lt. zero, no recno is read
			 * up to now.
			 */
			if (tar_first_recno < 0)
				tar_first_recno = recno;
			recno -= tar_first_recno;
			/*
			 * Calculate the block number of the record.
			 */
#ifdef DDS_TRACE
			fprintf(stderr,"file-loc(%d,%d)\n",recno,tar_fb);
#endif
			blkno = recno / tar_bs;
			recno -= blkno * tar_bs;
			if ( vid != HP ) blkno *= tar_bs ;
#ifdef DDS_TRACE
			fprintf(stderr,"file-loc(%d:%d,%d)\n",tar_bs,blkno,recno);
#endif
			blkno += tar_fb;
#ifdef DDS_TRACE
			fprintf(stderr,"file-loc(%d:%d,%d)\n",tar_bs,blkno,recno);
#endif
			if (name == NULL) {	/* calculate only once */
				if ( strlen(cur_line) >= (66) ) {
					name = cur_line + (65);
					while ( ( name[ 0] != '\0' )
					 && ((	   ( name[-1] != ' '  )
						|| ( name[-6] != ' '  )
						|| ( name[-9] != ':'  )
					 )&&(	   ( name[-1] != ' '  )
						|| ( name[-4] != ':'  )
						|| ( name[-7] != ' '  )
						|| ( name[-10] != '-'  )
						|| ( name[-13] != '-'  )
					 ))
					) name++ ;
					if ( name[0] == '\0' ) {
						name = cur_line + (16+so);
					}
				} else {
					if ( strlen(cur_line) <= (16+so) ) {
						strcat(cur_line,
						"                    ");
					}
					name = cur_line + (16+so);
				}
#ifdef DDS_TRACE
				fprintf(stderr,"%d\n",__LINE__);
				fprintf(stderr,"filename=%s\n",name);
#endif
			}
			while ( ( name[ 0] != '\0' )
			 &&     ( name != ( cur_line + (16+so) ) )
			 && ((	   ( name[-1] != ' '  )
				|| ( name[-6] != ' '  )
				|| ( name[-9] != ':'  )
			 )&&(	   ( name[-1] != ' '  )
				|| ( name[-4] != ':'  )
				|| ( name[-7] != ' '  )
				|| ( name[-10] != '-'  )
				|| ( name[-13] != '-'  )
			 ))
			) name++ ;
		}
#ifdef DDS_TRACE
		fprintf(stderr,"%d\n",__LINE__);
#endif
		i = strlen(name) -1 ;
		if (name[i] == '\n') name[i] = '\0', i-- ;
		/*
		 * We leave the list of directories empty on quick mode.
		 */
		if (( name[i] == '/' )&&( quick_mode == 0 )) {
			struct dir_list *dp;
			for (i = 0 , dp = dl ; i < de; i++ , dp ++ ) {
				if (strstr(name, dp->n) != name)
					break;
			}
			strcpy(dp->n, name);
			dp->b = blkno;
			dp->r = recno;
			de = i + 1 ;
		}
		/*
		 * Now we try to match one pattern with the name.
		 */
#ifdef DDS_TRACE
		fprintf(stderr,"%d\n",__LINE__);
		fprintf(stderr,"scanning pattern list for '%s'\n",name);
#endif
		p = pattern_list;
		while (*p != NULL) {
			static int ll_blkno = -1 ;
			static int ll_recno = -1 ;
			static int ln_blkno = -1 ;
			static int ln_recno = -1 ;
			static const char *ll = "././@LongLink" ;
#ifdef DDS_TRACE
			fprintf(stderr," p = '%p' , *p = %p \n",p,*p);
#endif
			/*
			 * Thanks Andreas Bagge for this trick.
			 * I use the fnmatch function from the
			 * source of gnu tar.
			 */
#ifdef DDS_TRACE
			fprintf(stderr,"fnmatch for '%s'\n",*p);
#endif
			if (0 == fnmatch(*p, name, FNM_LEADING_DIR)) {
				struct dir_list *dp;
				for (i = 0, dp = dl; i < de; i++, dp++) {
					char   *p = strstr(name, dp->n);
					if (p == name) {
						extract(dp->n, dp->b, dp->r);
					} else break ;
				}
				de = 0;
				if ( ln_blkno >= 0 ) extract(
						"././@LongLink",
						ln_blkno,
						ln_recno);
				if ( ll_blkno >= 0 ) extract(
						"././@LongLink",
						ll_blkno,
						ll_recno);
				extract(name, blkno, recno);
				break;
			}
			if ( 0==strncmp(ll,name,strlen(name)) ){
				if ( ln_blkno < 0 )
				ln_blkno = blkno , ln_recno = recno ;
				else
				ll_blkno = blkno , ll_recno = recno ;
			} else {
				ln_blkno = -1 ;
				ln_recno = -1 ;
				ll_blkno = -1 ;
				ll_recno = -1 ;
			}
			p++;
		}
#ifdef DDS_TRACE
		fprintf(stderr,"end of scanning pattern list\n");
#endif
	}
	/*
	 * Write an empty record to the end of the archive.
	 */
	memset(cur_block, 0, buf_n );
	write(1, cur_block, 512);
	nblocks++;
	if (verbose)
		fprintf(stderr, "dds2tar: %d blocks written\n", nblocks);
#ifdef DDS_TRACE
	fprintf(stderr,"%d\n",__LINE__);
	fprintf(stderr,"return of dds_cmp(...)");
#endif
	return 0;
}