]> Dogcows Code - chaz/tar/blob - src/create.c
(is_regular_file): Don't succeed on files that we can't access due to
[chaz/tar] / src / create.c
1 /* Create a tar archive.
2 Copyright 1985, 92, 93, 94, 96, 97, 1999 Free Software Foundation, Inc.
3 Written by John Gilmore, on 1985-08-25.
4
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any later
8 version.
9
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18
19 #include "system.h"
20
21 #if !MSDOS
22 # include <pwd.h>
23 # include <grp.h>
24 #endif
25
26 #if HAVE_UTIME_H
27 # include <utime.h>
28 #else
29 struct utimbuf
30 {
31 long actime;
32 long modtime;
33 };
34 #endif
35
36 #include "common.h"
37
38 #ifndef MSDOS
39 extern dev_t ar_dev;
40 extern ino_t ar_ino;
41 #endif
42
43 extern struct name *gnu_list_name;
44
45 /* This module is the only one that cares about `struct link's. */
46
47 struct link
48 {
49 struct link *next;
50 dev_t dev;
51 ino_t ino;
52 short linkcount;
53 char name[1];
54 };
55
56 struct link *linklist = NULL; /* points to first link in list */
57 \f
58
59 /*------------------------------------------------------------------------.
60 | Convert VALUE (with substitute SUBSTITUTE if VALUE is out of range) |
61 | into a size-SIZE field at WHERE, including a |
62 | trailing space. For example, 3 for SIZE means two digits and a space. |
63 | |
64 | We assume the trailing NUL is already there and don't fill it in. This |
65 | fact is used by start_header and finish_header, so don't change it! |
66 `------------------------------------------------------------------------*/
67
68 /* Output VALUE in octal, using SUBSTITUTE if value won't fit.
69 Output to buffer WHERE with size SIZE.
70 TYPE is the kind of value being output (useful for diagnostics).
71 Prefer SIZE - 1 octal digits (with leading '0's), followed by '\0';
72 but if SIZE octal digits would fit, omit the '\0'. */
73
74 static void
75 to_oct (uintmax_t value, uintmax_t substitute, char *where, size_t size, const char *type)
76 {
77 uintmax_t v = value;
78 size_t i = size;
79
80 # define MAX_OCTAL_VAL_WITH_DIGITS(digits) \
81 ((digits) * 3 < sizeof (uintmax_t) * CHAR_BIT \
82 ? ((uintmax_t) 1 << ((digits) * 3)) - 1 \
83 : (uintmax_t) -1)
84
85 /* Output a trailing NUL unless the value is too large. */
86 if (value <= MAX_OCTAL_VAL_WITH_DIGITS (size - 1))
87 where[--i] = '\0';
88
89 /* Produce the digits -- at least one. */
90
91 do
92 {
93 where[--i] = '0' + (int) (v & 7); /* one octal digit */
94 v >>= 3;
95 }
96 while (i != 0 && v != 0);
97
98 /* Leading zeros, if necessary. */
99 while (i != 0)
100 where[--i] = '0';
101
102 if (v != 0)
103 {
104 uintmax_t maxval = MAX_OCTAL_VAL_WITH_DIGITS (size);
105 char buf1[UINTMAX_STRSIZE_BOUND];
106 char buf2[UINTMAX_STRSIZE_BOUND];
107 char buf3[UINTMAX_STRSIZE_BOUND];
108 char *value_string = STRINGIFY_BIGINT (value, buf1);
109 char *maxval_string = STRINGIFY_BIGINT (maxval, buf2);
110 if (substitute)
111 {
112 substitute &= maxval;
113 WARN ((0, 0, _("%s value %s too large (max=%s); substituting %s"),
114 type, value_string, maxval_string,
115 STRINGIFY_BIGINT (substitute, buf3)));
116 to_oct (substitute, (uintmax_t) 0, where, size, type);
117 }
118 else
119 ERROR ((0, 0, _("%s value %s too large (max=%s)"),
120 type, value_string, maxval_string));
121 }
122 }
123 #ifndef GID_NOBODY
124 #define GID_NOBODY 0
125 #endif
126 void
127 gid_to_oct (gid_t v, char *p, size_t s)
128 {
129 to_oct ((uintmax_t) v, (uintmax_t) GID_NOBODY, p, s, "gid_t");
130 }
131 void
132 major_to_oct (major_t v, char *p, size_t s)
133 {
134 to_oct ((uintmax_t) v, (uintmax_t) 0, p, s, "major_t");
135 }
136 void
137 minor_to_oct (minor_t v, char *p, size_t s)
138 {
139 to_oct ((uintmax_t) v, (uintmax_t) 0, p, s, "minor_t");
140 }
141 void
142 mode_to_oct (mode_t v, char *p, size_t s)
143 {
144 /* In the common case where the internal and external mode bits are the same,
145 propagate all unknown bits to the external mode.
146 This matches historical practice.
147 Otherwise, just copy the bits we know about. */
148 uintmax_t u =
149 ((S_ISUID == TSUID && S_ISGID == TSGID && S_ISVTX == TSVTX
150 && S_IRUSR == TUREAD && S_IWUSR == TUWRITE && S_IXUSR == TUEXEC
151 && S_IRGRP == TGREAD && S_IWGRP == TGWRITE && S_IXGRP == TGEXEC
152 && S_IROTH == TOREAD && S_IWOTH == TOWRITE && S_IXOTH == TOEXEC)
153 ? v
154 : ((v & S_ISUID ? TSUID : 0)
155 | (v & S_ISGID ? TSGID : 0)
156 | (v & S_ISVTX ? TSVTX : 0)
157 | (v & S_IRUSR ? TUREAD : 0)
158 | (v & S_IWUSR ? TUWRITE : 0)
159 | (v & S_IXUSR ? TUEXEC : 0)
160 | (v & S_IRGRP ? TGREAD : 0)
161 | (v & S_IWGRP ? TGWRITE : 0)
162 | (v & S_IXGRP ? TGEXEC : 0)
163 | (v & S_IROTH ? TOREAD : 0)
164 | (v & S_IWOTH ? TOWRITE : 0)
165 | (v & S_IXOTH ? TOEXEC : 0)));
166 to_oct (u, (uintmax_t) 0, p, s, "mode_t");
167 }
168 void
169 off_to_oct (off_t v, char *p, size_t s)
170 {
171 to_oct ((uintmax_t) v, (uintmax_t) 0, p, s, "off_t");
172 }
173 void
174 size_to_oct (size_t v, char *p, size_t s)
175 {
176 to_oct ((uintmax_t) v, (uintmax_t) 0, p, s, "size_t");
177 }
178 void
179 time_to_oct (time_t v, char *p, size_t s)
180 {
181 to_oct ((uintmax_t) v, (uintmax_t) 0, p, s, "time_t");
182 }
183 #ifndef UID_NOBODY
184 #define UID_NOBODY 0
185 #endif
186 void
187 uid_to_oct (uid_t v, char *p, size_t s)
188 {
189 to_oct ((uintmax_t) v, (uintmax_t) UID_NOBODY, p, s, "uid_t");
190 }
191 void
192 uintmax_to_oct (uintmax_t v, char *p, size_t s)
193 {
194 to_oct (v, (uintmax_t) 0, p, s, "uintmax_t");
195 }
196 \f
197 /* Writing routines. */
198
199 /*-----------------------------------------------------------------------.
200 | Just zeroes out the buffer so we don't confuse ourselves with leftover |
201 | data. |
202 `-----------------------------------------------------------------------*/
203
204 static void
205 clear_buffer (char *buffer)
206 {
207 memset (buffer, 0, BLOCKSIZE);
208 }
209
210 /*-------------------------------------------------------------------------.
211 | Write the EOT block(s). We actually zero at least one block, through |
212 | the end of the record. Old tar, as previous versions of GNU tar, writes |
213 | garbage after two zeroed blocks. |
214 `-------------------------------------------------------------------------*/
215
216 void
217 write_eot (void)
218 {
219 union block *pointer = find_next_block ();
220
221 if (pointer)
222 {
223 size_t space = available_space_after (pointer);
224
225 memset (pointer->buffer, 0, space);
226 set_next_block_after (pointer);
227 }
228 }
229
230 /*-----------------------------------------------------.
231 | Write a GNUTYPE_LONGLINK or GNUTYPE_LONGNAME block. |
232 `-----------------------------------------------------*/
233
234 /* FIXME: Cross recursion between start_header and write_long! */
235
236 static union block *start_header PARAMS ((const char *, struct stat *));
237
238 static void
239 write_long (const char *p, char type)
240 {
241 size_t size = strlen (p) + 1;
242 size_t bufsize;
243 union block *header;
244 struct stat foo;
245
246 memset (&foo, 0, sizeof foo);
247 foo.st_size = size;
248
249 header = start_header ("././@LongLink", &foo);
250 header->header.typeflag = type;
251 finish_header (header);
252
253 header = find_next_block ();
254
255 bufsize = available_space_after (header);
256
257 while (bufsize < size)
258 {
259 memcpy (header->buffer, p, bufsize);
260 p += bufsize;
261 size -= bufsize;
262 set_next_block_after (header + (bufsize - 1) / BLOCKSIZE);
263 header = find_next_block ();
264 bufsize = available_space_after (header);
265 }
266 memcpy (header->buffer, p, size);
267 memset (header->buffer + size, 0, bufsize - size);
268 set_next_block_after (header + (size - 1) / BLOCKSIZE);
269 }
270 \f
271 /* Header handling. */
272
273 /*---------------------------------------------------------------------.
274 | Make a header block for the file name whose stat info is st. Return |
275 | header pointer for success, NULL if the name is too long. |
276 `---------------------------------------------------------------------*/
277
278 static union block *
279 start_header (const char *name, struct stat *st)
280 {
281 union block *header;
282
283 if (!absolute_names_option)
284 {
285 static int warned_once = 0;
286
287 #if MSDOS
288 if (name[1] == ':')
289 {
290 name += 2;
291 if (!warned_once)
292 {
293 warned_once = 1;
294 WARN ((0, 0, _("Removing drive spec from names in the archive")));
295 }
296 }
297 #endif
298
299 while (*name == '/')
300 {
301 name++; /* force relative path */
302 if (!warned_once)
303 {
304 warned_once = 1;
305 WARN ((0, 0, _("\
306 Removing leading `/' from absolute path names in the archive")));
307 }
308 }
309 }
310
311 /* Check the file name and put it in the block. */
312
313 if (strlen (name) >= (size_t) NAME_FIELD_SIZE)
314 write_long (name, GNUTYPE_LONGNAME);
315 header = find_next_block ();
316 memset (header->buffer, 0, sizeof (union block));
317
318 assign_string (&current_file_name, name);
319
320 strncpy (header->header.name, name, NAME_FIELD_SIZE);
321 header->header.name[NAME_FIELD_SIZE - 1] = '\0';
322
323 /* Override some stat fields, if requested to do so. */
324
325 if (owner_option != (uid_t) -1)
326 st->st_uid = owner_option;
327 if (group_option != (gid_t) -1)
328 st->st_gid = group_option;
329 if (mode_option)
330 st->st_mode = ((st->st_mode & S_IFMT)
331 | mode_adjust (st->st_mode, mode_option));
332
333 /* Paul Eggert tried the trivial test ($WRITER cf a b; $READER tvf a)
334 for a few tars and came up with the following interoperability
335 matrix:
336
337 WRITER
338 1 2 3 4 5 6 7 8 9 READER
339 . . . . . . . . . 1 = SunOS 4.2 tar
340 # . . # # . . # # 2 = NEC SVR4.0.2 tar
341 . . . # # . . # . 3 = Solaris 2.1 tar
342 . . . . . . . . . 4 = GNU tar 1.11.1
343 . . . . . . . . . 5 = HP-UX 8.07 tar
344 . . . . . . . . . 6 = Ultrix 4.1
345 . . . . . . . . . 7 = AIX 3.2
346 . . . . . . . . . 8 = Hitachi HI-UX 1.03
347 . . . . . . . . . 9 = Omron UNIOS-B 4.3BSD 1.60Beta
348
349 . = works
350 # = ``impossible file type''
351
352 The following mask for old archive removes the `#'s in column 4
353 above, thus making GNU tar both a universal donor and a universal
354 acceptor for Paul's test. */
355
356 if (archive_format == V7_FORMAT)
357 MODE_TO_OCT (st->st_mode & MODE_ALL, header->header.mode);
358 else
359 MODE_TO_OCT (st->st_mode, header->header.mode);
360
361 UID_TO_OCT (st->st_uid, header->header.uid);
362 GID_TO_OCT (st->st_gid, header->header.gid);
363 OFF_TO_OCT (st->st_size, header->header.size);
364 TIME_TO_OCT (st->st_mtime, header->header.mtime);
365
366 if (incremental_option)
367 if (archive_format == OLDGNU_FORMAT)
368 {
369 TIME_TO_OCT (st->st_atime, header->oldgnu_header.atime);
370 TIME_TO_OCT (st->st_ctime, header->oldgnu_header.ctime);
371 }
372
373 header->header.typeflag = archive_format == V7_FORMAT ? AREGTYPE : REGTYPE;
374
375 switch (archive_format)
376 {
377 case DEFAULT_FORMAT:
378 case V7_FORMAT:
379 break;
380
381 case OLDGNU_FORMAT:
382 /* Overwrite header->header.magic and header.version in one blow. */
383 strcpy (header->header.magic, OLDGNU_MAGIC);
384 break;
385
386 case POSIX_FORMAT:
387 case GNU_FORMAT:
388 strncpy (header->header.magic, TMAGIC, TMAGLEN);
389 strncpy (header->header.version, TVERSION, TVERSLEN);
390 break;
391 }
392
393 if (archive_format == V7_FORMAT || numeric_owner_option)
394 {
395 /* header->header.[ug]name are left as the empty string. */
396 }
397 else
398 {
399 uid_to_uname (st->st_uid, header->header.uname);
400 gid_to_gname (st->st_gid, header->header.gname);
401 }
402
403 return header;
404 }
405
406 /*-------------------------------------------------------------------------.
407 | Finish off a filled-in header block and write it out. We also print the |
408 | file name and/or full info if verbose is on. |
409 `-------------------------------------------------------------------------*/
410
411 void
412 finish_header (union block *header)
413 {
414 size_t i;
415 int sum;
416 char *p;
417
418 memcpy (header->header.chksum, CHKBLANKS, sizeof (header->header.chksum));
419
420 sum = 0;
421 p = header->buffer;
422 for (i = sizeof (*header); i-- != 0; )
423 /* We can't use unsigned char here because of old compilers, e.g. V7. */
424 sum += 0xFF & *p++;
425
426 /* Fill in the checksum field. It's formatted differently from the
427 other fields: it has [6] digits, a null, then a space -- rather than
428 digits, then a null. We use to_oct.
429 The final space is already there, from
430 checksumming, and to_oct doesn't modify it.
431
432 This is a fast way to do:
433
434 sprintf(header->header.chksum, "%6o", sum); */
435
436 uintmax_to_oct ((uintmax_t) sum, header->header.chksum, 7);
437
438 set_next_block_after (header);
439
440 if (verbose_option
441 && header->header.typeflag != GNUTYPE_LONGLINK
442 && header->header.typeflag != GNUTYPE_LONGNAME)
443 {
444 /* These globals are parameters to print_header, sigh. */
445
446 current_header = header;
447 /* current_stat is already set up. */
448 current_format = archive_format;
449 print_header ();
450 }
451 }
452 \f
453 /* Sparse file processing. */
454
455 /*-------------------------------------------------------------------------.
456 | Takes a blockful of data and basically cruises through it to see if it's |
457 | made *entirely* of zeros, returning a 0 the instant it finds something |
458 | that is a nonzero, i.e., useful data. |
459 `-------------------------------------------------------------------------*/
460
461 static int
462 zero_block_p (char *buffer)
463 {
464 int counter;
465
466 for (counter = 0; counter < BLOCKSIZE; counter++)
467 if (buffer[counter] != '\0')
468 return 0;
469 return 1;
470 }
471
472 /*---.
473 | ? |
474 `---*/
475
476 static void
477 init_sparsearray (void)
478 {
479 int counter;
480
481 sp_array_size = 10;
482
483 /* Make room for our scratch space -- initially is 10 elts long. */
484
485 sparsearray = (struct sp_array *)
486 xmalloc (sp_array_size * sizeof (struct sp_array));
487 for (counter = 0; counter < sp_array_size; counter++)
488 {
489 sparsearray[counter].offset = 0;
490 sparsearray[counter].numbytes = 0;
491 }
492 }
493
494 /*---.
495 | ? |
496 `---*/
497
498 static void
499 find_new_file_size (off_t *filesize, int highest_index)
500 {
501 int counter;
502
503 *filesize = 0;
504 for (counter = 0;
505 sparsearray[counter].numbytes && counter <= highest_index;
506 counter++)
507 *filesize += sparsearray[counter].numbytes;
508 }
509
510 /*-----------------------------------------------------------------------.
511 | Make one pass over the file NAME, studying where any non-zero data is, |
512 | that is, how far into the file each instance of data is, and how many |
513 | bytes are there. Save this information in the sparsearray, which will |
514 | later be translated into header information. |
515 `-----------------------------------------------------------------------*/
516
517 /* There is little point in trimming small amounts of null data at the head
518 and tail of blocks, only avoid dumping full null blocks. */
519
520 /* FIXME: this routine might accept bits of algorithmic cleanup, it is
521 too kludgey for my taste... */
522
523 static int
524 deal_with_sparse (char *name, union block *header)
525 {
526 size_t numbytes = 0;
527 off_t offset = 0;
528 int file;
529 int sparse_index = 0;
530 ssize_t count;
531 char buffer[BLOCKSIZE];
532
533 if (archive_format == OLDGNU_FORMAT)
534 header->oldgnu_header.isextended = 0;
535
536 if (file = open (name, O_RDONLY), file < 0)
537 /* This problem will be caught later on, so just return. */
538 return 0;
539
540 init_sparsearray ();
541 clear_buffer (buffer);
542
543 while (count = safe_read (file, buffer, sizeof buffer), count != 0)
544 {
545 /* Realloc the scratch area as necessary. FIXME: should reallocate
546 only at beginning of a new instance of non-zero data. */
547
548 if (sparse_index > sp_array_size - 1)
549 {
550
551 sparsearray = (struct sp_array *)
552 xrealloc (sparsearray,
553 2 * sp_array_size * sizeof (struct sp_array));
554 sp_array_size *= 2;
555 }
556
557 /* Process one block. */
558
559 if (count == sizeof buffer)
560
561 if (zero_block_p (buffer))
562 {
563 if (numbytes)
564 {
565 sparsearray[sparse_index++].numbytes = numbytes;
566 numbytes = 0;
567 }
568 }
569 else
570 {
571 if (!numbytes)
572 sparsearray[sparse_index].offset = offset;
573 numbytes += count;
574 }
575
576 else
577
578 /* Since count < sizeof buffer, we have the last bit of the file. */
579
580 if (!zero_block_p (buffer))
581 {
582 if (!numbytes)
583 sparsearray[sparse_index].offset = offset;
584 numbytes += count;
585 }
586 else
587 /* The next two lines are suggested by Andreas Degert, who says
588 they are required for trailing full blocks to be written to the
589 archive, when all zeroed. Yet, it seems to me that the case
590 does not apply. Further, at restore time, the file is not as
591 sparse as it should. So, some serious cleanup is *also* needed
592 in this area. Just one more... :-(. FIXME. */
593 if (numbytes)
594 numbytes += count;
595
596 /* Prepare for next block. */
597
598 offset += count;
599 /* FIXME: do not clear unless necessary. */
600 clear_buffer (buffer);
601 }
602
603 if (numbytes)
604 sparsearray[sparse_index++].numbytes = numbytes;
605 else
606 {
607 sparsearray[sparse_index].offset = offset - 1;
608 sparsearray[sparse_index++].numbytes = 1;
609 }
610
611 close (file);
612 return sparse_index - 1;
613 }
614
615 /*---.
616 | ? |
617 `---*/
618
619 static int
620 finish_sparse_file (int file, off_t *sizeleft, off_t fullsize, char *name)
621 {
622 union block *start;
623 size_t bufsize;
624 int sparse_index = 0;
625 ssize_t count;
626
627 while (*sizeleft > 0)
628 {
629 start = find_next_block ();
630 memset (start->buffer, 0, BLOCKSIZE);
631 bufsize = sparsearray[sparse_index].numbytes;
632 if (!bufsize)
633 {
634 /* We blew it, maybe. */
635 char buf1[UINTMAX_STRSIZE_BOUND];
636 char buf2[UINTMAX_STRSIZE_BOUND];
637
638 ERROR ((0, 0, _("Wrote %s of %s bytes to file %s"),
639 STRINGIFY_BIGINT (fullsize - *sizeleft, buf1),
640 STRINGIFY_BIGINT (fullsize, buf2),
641 name));
642 break;
643 }
644
645 if (lseek (file, sparsearray[sparse_index++].offset, SEEK_SET) < 0)
646 {
647 char buf[UINTMAX_STRSIZE_BOUND];
648 ERROR ((0, errno, _("lseek error at byte %s in file %s"),
649 STRINGIFY_BIGINT (sparsearray[sparse_index - 1].offset, buf),
650 name));
651 break;
652 }
653
654 /* If the number of bytes to be written here exceeds the size of
655 the temporary buffer, do it in steps. */
656
657 while (bufsize > BLOCKSIZE)
658 {
659 #if 0
660 if (amount_read)
661 {
662 count = safe_read (file, start->buffer + amount_read,
663 BLOCKSIZE - amount_read);
664 bufsize -= BLOCKSIZE - amount_read;
665 amount_read = 0;
666 set_next_block_after (start);
667 start = find_next_block ();
668 memset (start->buffer, 0, BLOCKSIZE);
669 }
670 #endif
671 /* Store the data. */
672
673 count = safe_read (file, start->buffer, BLOCKSIZE);
674 if (count < 0)
675 {
676 char buf[UINTMAX_STRSIZE_BOUND];
677 ERROR ((0, errno, _("\
678 Read error at byte %s, reading %lu bytes, in file %s"),
679 STRINGIFY_BIGINT (fullsize - *sizeleft, buf),
680 (unsigned long) bufsize, name));
681 return 1;
682 }
683 bufsize -= count;
684 *sizeleft -= count;
685 set_next_block_after (start);
686 start = find_next_block ();
687 memset (start->buffer, 0, BLOCKSIZE);
688 }
689
690 {
691 char buffer[BLOCKSIZE];
692
693 clear_buffer (buffer);
694 count = safe_read (file, buffer, bufsize);
695 memcpy (start->buffer, buffer, BLOCKSIZE);
696 }
697
698 if (count < 0)
699 {
700 char buf[UINTMAX_STRSIZE_BOUND];
701
702 ERROR ((0, errno,
703 _("Read error at byte %s, reading %lu bytes, in file %s"),
704 STRINGIFY_BIGINT (fullsize - *sizeleft, buf),
705 (unsigned long) bufsize, name));
706 return 1;
707 }
708 #if 0
709 if (amount_read >= BLOCKSIZE)
710 {
711 amount_read = 0;
712 set_next_block_after (start + (count - 1) / BLOCKSIZE);
713 if (count != bufsize)
714 {
715 ERROR ((0, 0,
716 _("File %s shrunk, padding with zeros"),
717 name));
718 return 1;
719 }
720 start = find_next_block ();
721 }
722 else
723 amount_read += bufsize;
724 #endif
725 *sizeleft -= count;
726 set_next_block_after (start);
727
728 }
729 free (sparsearray);
730 #if 0
731 set_next_block_after (start + (count - 1) / BLOCKSIZE);
732 #endif
733 return 0;
734 }
735 \f
736 /* Main functions of this module. */
737
738 /*---.
739 | ? |
740 `---*/
741
742 void
743 create_archive (void)
744 {
745 char *p;
746
747 open_archive (ACCESS_WRITE);
748
749 if (incremental_option)
750 {
751 char *buffer = xmalloc (PATH_MAX);
752 const char *q;
753 char *bufp;
754
755 collect_and_sort_names ();
756
757 while (p = name_from_list (), p)
758 dump_file (p, (dev_t) -1, 1);
759
760 blank_name_list ();
761 while (p = name_from_list (), p)
762 {
763 strcpy (buffer, p);
764 if (p[strlen (p) - 1] != '/')
765 strcat (buffer, "/");
766 bufp = buffer + strlen (buffer);
767 for (q = gnu_list_name->dir_contents;
768 q && *q;
769 q += strlen (q) + 1)
770 {
771 if (*q == 'Y')
772 {
773 strcpy (bufp, q + 1);
774 dump_file (buffer, (dev_t) -1, 1);
775 }
776 }
777 }
778 free (buffer);
779 }
780 else
781 {
782 while (p = name_next (1), p)
783 dump_file (p, (dev_t) -1, 1);
784 }
785
786 write_eot ();
787 close_archive ();
788
789 if (listed_incremental_option)
790 write_dir_file ();
791 }
792
793 /*----------------------------------------------------------------------.
794 | Dump a single file. Recurse on directories. Result is nonzero for |
795 | success. P is file name to dump. PARENT_DEVICE is device our parent |
796 | directory was on. TOP_LEVEL tells wether we are a toplevel call. |
797 | |
798 | Sets global CURRENT_STAT to stat output for this file. |
799 `----------------------------------------------------------------------*/
800
801 /* FIXME: One should make sure that for *every* path leading to setting
802 exit_status to failure, a clear diagnostic has been issued. */
803
804 void
805 dump_file (char *p, dev_t parent_device, int top_level)
806 {
807 union block *header;
808 char type;
809 union block *exhdr;
810 char save_typeflag;
811 struct utimbuf restore_times;
812 off_t restore_size;
813
814 /* FIXME: `header' and `upperbound' might be used uninitialized in this
815 function. Reported by Bruno Haible. */
816
817 if (interactive_option && !confirm ("add", p))
818 return;
819
820 /* Use stat if following (rather than dumping) 4.2BSD's symbolic links.
821 Otherwise, use lstat (which falls back to stat if no symbolic links). */
822
823 if (dereference_option != 0
824 #if STX_HIDDEN && !_LARGE_FILES /* AIX */
825 ? statx (p, &current_stat, STATSIZE, STX_HIDDEN)
826 : statx (p, &current_stat, STATSIZE, STX_HIDDEN | STX_LINK)
827 #else
828 ? stat (p, &current_stat) : lstat (p, &current_stat)
829 #endif
830 )
831 {
832 WARN ((0, errno, _("Cannot add file %s"), p));
833 if (!ignore_failed_read_option)
834 exit_status = TAREXIT_FAILURE;
835 return;
836 }
837
838 restore_times.actime = current_stat.st_atime;
839 restore_times.modtime = current_stat.st_mtime;
840 restore_size = current_stat.st_size;
841
842 #ifdef S_ISHIDDEN
843 if (S_ISHIDDEN (current_stat.st_mode))
844 {
845 char *new = (char *) alloca (strlen (p) + 2);
846 if (new)
847 {
848 strcpy (new, p);
849 strcat (new, "@");
850 p = new;
851 }
852 }
853 #endif
854
855 /* See if we only want new files, and check if this one is too old to
856 put in the archive. */
857
858 if (!incremental_option && !S_ISDIR (current_stat.st_mode)
859 && current_stat.st_mtime < newer_mtime_option
860 && (!after_date_option || current_stat.st_ctime < newer_ctime_option))
861 {
862 if (parent_device == (dev_t) -1)
863 WARN ((0, 0, _("%s: is unchanged; not dumped"), p));
864 /* FIXME: recheck this return. */
865 return;
866 }
867
868 #if !MSDOS
869 /* See if we are trying to dump the archive. */
870
871 if (ar_dev && current_stat.st_dev == ar_dev && current_stat.st_ino == ar_ino)
872 {
873 WARN ((0, 0, _("%s is the archive; not dumped"), p));
874 return;
875 }
876 #endif
877
878 /* Check for multiple links.
879
880 We maintain a list of all such files that we've written so far. Any
881 time we see another, we check the list and avoid dumping the data
882 again if we've done it once already. */
883
884 if (current_stat.st_nlink > 1
885 && (S_ISREG (current_stat.st_mode)
886 #ifdef S_ISCTG
887 || S_ISCTG (current_stat.st_mode)
888 #endif
889 #ifdef S_ISCHR
890 || S_ISCHR (current_stat.st_mode)
891 #endif
892 #ifdef S_ISBLK
893 || S_ISBLK (current_stat.st_mode)
894 #endif
895 #ifdef S_ISFIFO
896 || S_ISFIFO (current_stat.st_mode)
897 #endif
898 ))
899 {
900 struct link *lp;
901
902 /* FIXME: First quick and dirty. Hashing, etc later. */
903
904 for (lp = linklist; lp; lp = lp->next)
905 if (lp->ino == current_stat.st_ino && lp->dev == current_stat.st_dev)
906 {
907 char *link_name = lp->name;
908
909 /* We found a link. */
910
911 while (!absolute_names_option && *link_name == '/')
912 {
913 static int warned_once = 0;
914
915 if (!warned_once)
916 {
917 warned_once = 1;
918 WARN ((0, 0, _("\
919 Removing leading `/' from absolute links")));
920 }
921 link_name++;
922 }
923 if (strlen (link_name) >= NAME_FIELD_SIZE)
924 write_long (link_name, GNUTYPE_LONGLINK);
925 assign_string (&current_link_name, link_name);
926
927 current_stat.st_size = 0;
928 header = start_header (p, &current_stat);
929 if (header == NULL)
930 {
931 exit_status = TAREXIT_FAILURE;
932 return;
933 }
934 strncpy (header->header.linkname,
935 link_name, NAME_FIELD_SIZE);
936
937 /* Force null truncated. */
938
939 header->header.linkname[NAME_FIELD_SIZE - 1] = 0;
940
941 header->header.typeflag = LNKTYPE;
942 finish_header (header);
943
944 /* FIXME: Maybe remove from list after all links found? */
945
946 if (remove_files_option)
947 if (unlink (p) == -1)
948 ERROR ((0, errno, _("Cannot remove %s"), p));
949
950 /* We dumped it. */
951 return;
952 }
953
954 /* Not found. Add it to the list of possible links. */
955
956 lp = (struct link *)
957 xmalloc ((size_t) (sizeof (struct link) + strlen (p)));
958 lp->ino = current_stat.st_ino;
959 lp->dev = current_stat.st_dev;
960 strcpy (lp->name, p);
961 lp->next = linklist;
962 linklist = lp;
963 }
964
965 /* This is not a link to a previously dumped file, so dump it. */
966
967 if (S_ISREG (current_stat.st_mode)
968 #ifdef S_ISCTG
969 || S_ISCTG (current_stat.st_mode)
970 #endif
971 )
972 {
973 int f; /* file descriptor */
974 size_t bufsize;
975 ssize_t count;
976 off_t sizeleft;
977 union block *start;
978 int header_moved;
979 char isextended = 0;
980 int upperbound;
981 #if 0
982 static int cried_once = 0;
983 #endif
984
985 header_moved = 0;
986
987 if (sparse_option)
988 {
989 /* Check the size of the file against the number of blocks
990 allocated for it, counting both data and indirect blocks.
991 If there is a smaller number of blocks that would be
992 necessary to accommodate a file of this size, this is safe
993 to say that we have a sparse file: at least one of those
994 blocks in the file is just a useless hole. For sparse
995 files not having more hole blocks than indirect blocks, the
996 sparseness will go undetected. */
997
998 /* Bruno Haible sent me these statistics for Linux. It seems
999 that some filesystems count indirect blocks in st_blocks,
1000 while others do not seem to:
1001
1002 minix-fs tar: size=7205, st_blocks=18 and ST_NBLOCKS=18
1003 extfs tar: size=7205, st_blocks=18 and ST_NBLOCKS=18
1004 ext2fs tar: size=7205, st_blocks=16 and ST_NBLOCKS=16
1005 msdos-fs tar: size=7205, st_blocks=16 and ST_NBLOCKS=16
1006
1007 Dick Streefland reports the previous numbers as misleading,
1008 because ext2fs use 12 direct blocks, while minix-fs uses only
1009 6 direct blocks. Dick gets:
1010
1011 ext2 size=20480 ls listed blocks=21
1012 minix size=20480 ls listed blocks=21
1013 msdos size=20480 ls listed blocks=20
1014
1015 It seems that indirect blocks *are* included in st_blocks.
1016 The minix filesystem does not account for phantom blocks in
1017 st_blocks, so `du' and `ls -s' give wrong results. So, the
1018 --sparse option would not work on a minix filesystem. */
1019
1020 if (ST_NBLOCKS (current_stat)
1021 < (current_stat.st_size / ST_NBLOCKSIZE
1022 + (current_stat.st_size % ST_NBLOCKSIZE != 0)))
1023 {
1024 off_t filesize = current_stat.st_size;
1025 int counter;
1026
1027 header = start_header (p, &current_stat);
1028 if (header == NULL)
1029 {
1030 exit_status = TAREXIT_FAILURE;
1031 return;
1032 }
1033 header->header.typeflag = GNUTYPE_SPARSE;
1034 header_moved = 1;
1035
1036 /* Call the routine that figures out the layout of the
1037 sparse file in question. UPPERBOUND is the index of the
1038 last element of the "sparsearray," i.e., the number of
1039 elements it needed to describe the file. */
1040
1041 upperbound = deal_with_sparse (p, header);
1042
1043 /* See if we'll need an extended header later. */
1044
1045 if (upperbound > SPARSES_IN_OLDGNU_HEADER - 1)
1046 header->oldgnu_header.isextended = 1;
1047
1048 /* We store the "real" file size so we can show that in
1049 case someone wants to list the archive, i.e., tar tvf
1050 <file>. It might be kind of disconcerting if the
1051 shrunken file size was the one that showed up. */
1052
1053 OFF_TO_OCT (current_stat.st_size,
1054 header->oldgnu_header.realsize);
1055
1056 /* This will be the new "size" of the file, i.e., the size
1057 of the file minus the blocks of holes that we're
1058 skipping over. */
1059
1060 find_new_file_size (&filesize, upperbound);
1061 current_stat.st_size = filesize;
1062 OFF_TO_OCT (filesize, header->header.size);
1063
1064 for (counter = 0; counter < SPARSES_IN_OLDGNU_HEADER; counter++)
1065 {
1066 if (!sparsearray[counter].numbytes)
1067 break;
1068
1069 OFF_TO_OCT (sparsearray[counter].offset,
1070 header->oldgnu_header.sp[counter].offset);
1071 SIZE_TO_OCT (sparsearray[counter].numbytes,
1072 header->oldgnu_header.sp[counter].numbytes);
1073 }
1074
1075 }
1076 }
1077 else
1078 upperbound = SPARSES_IN_OLDGNU_HEADER - 1;
1079
1080 sizeleft = current_stat.st_size;
1081
1082 /* Don't bother opening empty, world readable files. Also do not open
1083 files when archive is meant for /dev/null. */
1084
1085 if (dev_null_output
1086 || (sizeleft == 0
1087 && MODE_R == (MODE_R & current_stat.st_mode)))
1088 f = -1;
1089 else
1090 {
1091 f = open (p, O_RDONLY | O_BINARY);
1092 if (f < 0)
1093 {
1094 WARN ((0, errno, _("Cannot add file %s"), p));
1095 if (!ignore_failed_read_option)
1096 exit_status = TAREXIT_FAILURE;
1097 return;
1098 }
1099 }
1100
1101 /* If the file is sparse, we've already taken care of this. */
1102
1103 if (!header_moved)
1104 {
1105 header = start_header (p, &current_stat);
1106 if (header == NULL)
1107 {
1108 if (f >= 0)
1109 close (f);
1110 exit_status = TAREXIT_FAILURE;
1111 return;
1112 }
1113 }
1114 #ifdef S_ISCTG
1115 /* Mark contiguous files, if we support them. */
1116
1117 if (archive_format != V7_FORMAT && S_ISCTG (current_stat.st_mode))
1118 header->header.typeflag = CONTTYPE;
1119 #endif
1120 isextended = header->oldgnu_header.isextended;
1121 save_typeflag = header->header.typeflag;
1122 finish_header (header);
1123 if (isextended)
1124 {
1125 #if 0
1126 int sum = 0;
1127 #endif
1128 int counter;
1129 #if 0
1130 union block *exhdr;
1131 int arraybound = SPARSES_IN_SPARSE_HEADER;
1132 #endif
1133 /* static */ int index_offset = SPARSES_IN_OLDGNU_HEADER;
1134
1135 extend:
1136 exhdr = find_next_block ();
1137
1138 if (exhdr == NULL)
1139 {
1140 exit_status = TAREXIT_FAILURE;
1141 return;
1142 }
1143 memset (exhdr->buffer, 0, BLOCKSIZE);
1144 for (counter = 0; counter < SPARSES_IN_SPARSE_HEADER; counter++)
1145 {
1146 if (counter + index_offset > upperbound)
1147 break;
1148
1149 SIZE_TO_OCT (sparsearray[counter + index_offset].numbytes,
1150 exhdr->sparse_header.sp[counter].numbytes);
1151 OFF_TO_OCT (sparsearray[counter + index_offset].offset,
1152 exhdr->sparse_header.sp[counter].offset);
1153 }
1154 set_next_block_after (exhdr);
1155 #if 0
1156 sum += counter;
1157 if (sum < upperbound)
1158 goto extend;
1159 #endif
1160 if (index_offset + counter <= upperbound)
1161 {
1162 index_offset += counter;
1163 exhdr->sparse_header.isextended = 1;
1164 goto extend;
1165 }
1166
1167 }
1168 if (save_typeflag == GNUTYPE_SPARSE)
1169 {
1170 if (f < 0
1171 || finish_sparse_file (f, &sizeleft, current_stat.st_size, p))
1172 goto padit;
1173 }
1174 else
1175 while (sizeleft > 0)
1176 {
1177 if (multi_volume_option)
1178 {
1179 assign_string (&save_name, p);
1180 save_sizeleft = sizeleft;
1181 save_totsize = current_stat.st_size;
1182 }
1183 start = find_next_block ();
1184
1185 bufsize = available_space_after (start);
1186
1187 if (sizeleft < bufsize)
1188 {
1189 /* Last read -- zero out area beyond. */
1190
1191 bufsize = sizeleft;
1192 count = bufsize % BLOCKSIZE;
1193 if (count)
1194 memset (start->buffer + sizeleft, 0,
1195 (size_t) (BLOCKSIZE - count));
1196 }
1197 if (f < 0)
1198 count = bufsize;
1199 else
1200 count = safe_read (f, start->buffer, bufsize);
1201 if (count < 0)
1202 {
1203 char buf[UINTMAX_STRSIZE_BOUND];
1204 ERROR ((0, errno, _("\
1205 Read error at byte %s, reading %lu bytes, in file %s"),
1206 STRINGIFY_BIGINT (current_stat.st_size - sizeleft,
1207 buf),
1208 (unsigned long) bufsize, p));
1209 goto padit;
1210 }
1211 sizeleft -= count;
1212
1213 /* This is nonportable (the type of set_next_block_after's arg). */
1214
1215 set_next_block_after (start + (count - 1) / BLOCKSIZE);
1216
1217 if (count == bufsize)
1218 continue;
1219 else
1220 {
1221 char buf[UINTMAX_STRSIZE_BOUND];
1222 ERROR ((0, 0,
1223 _("File %s shrunk by %s bytes, padding with zeros"),
1224 p, STRINGIFY_BIGINT (sizeleft, buf)));
1225 goto padit; /* short read */
1226 }
1227 }
1228
1229 if (multi_volume_option)
1230 assign_string (&save_name, NULL);
1231
1232 if (f >= 0)
1233 {
1234 struct stat final_stat;
1235 if (fstat (f, &final_stat) != 0)
1236 ERROR ((0, errno, "%s: fstat", p));
1237 else if (final_stat.st_mtime != restore_times.modtime
1238 || final_stat.st_size != restore_size)
1239 ERROR ((0, errno, _("%s: file changed as we read it"), p));
1240 if (close (f) != 0)
1241 ERROR ((0, errno, _("%s: close"), p));
1242 if (atime_preserve_option)
1243 utime (p, &restore_times);
1244 }
1245 if (remove_files_option)
1246 {
1247 if (unlink (p) == -1)
1248 ERROR ((0, errno, _("Cannot remove %s"), p));
1249 }
1250 return;
1251
1252 /* File shrunk or gave error, pad out tape to match the size we
1253 specified in the header. */
1254
1255 padit:
1256 while (sizeleft > 0)
1257 {
1258 save_sizeleft = sizeleft;
1259 start = find_next_block ();
1260 memset (start->buffer, 0, BLOCKSIZE);
1261 set_next_block_after (start);
1262 sizeleft -= BLOCKSIZE;
1263 }
1264 if (multi_volume_option)
1265 assign_string (&save_name, NULL);
1266 if (f >= 0)
1267 {
1268 close (f);
1269 if (atime_preserve_option)
1270 utime (p, &restore_times);
1271 }
1272 return;
1273 }
1274
1275 #ifdef S_ISLNK
1276 else if (S_ISLNK (current_stat.st_mode))
1277 {
1278 int size;
1279 char *buffer = (char *) alloca (PATH_MAX + 1);
1280
1281 size = readlink (p, buffer, PATH_MAX + 1);
1282 if (size < 0)
1283 {
1284 WARN ((0, errno, _("Cannot add file %s"), p));
1285 if (!ignore_failed_read_option)
1286 exit_status = TAREXIT_FAILURE;
1287 return;
1288 }
1289 buffer[size] = '\0';
1290 if (size >= NAME_FIELD_SIZE)
1291 write_long (buffer, GNUTYPE_LONGLINK);
1292 assign_string (&current_link_name, buffer);
1293
1294 current_stat.st_size = 0; /* force 0 size on symlink */
1295 header = start_header (p, &current_stat);
1296 if (header == NULL)
1297 {
1298 exit_status = TAREXIT_FAILURE;
1299 return;
1300 }
1301 strncpy (header->header.linkname, buffer, NAME_FIELD_SIZE);
1302 header->header.linkname[NAME_FIELD_SIZE - 1] = '\0';
1303 header->header.typeflag = SYMTYPE;
1304 finish_header (header); /* nothing more to do to it */
1305 if (remove_files_option)
1306 {
1307 if (unlink (p) == -1)
1308 ERROR ((0, errno, _("Cannot remove %s"), p));
1309 }
1310 return;
1311 }
1312 #endif /* S_ISLNK */
1313
1314 else if (S_ISDIR (current_stat.st_mode))
1315 {
1316 DIR *directory;
1317 struct dirent *entry;
1318 char *namebuf;
1319 size_t buflen;
1320 size_t len;
1321 dev_t our_device = current_stat.st_dev;
1322
1323 /* If this tar program is installed suid root, like for Amanda, the
1324 access might look like denied, while it is not really.
1325
1326 FIXME: I have the feeling this test is done too early. Couldn't it
1327 just be bundled in later actions? I guess that the proper support
1328 of --ignore-failed-read is the key of the current writing. */
1329
1330 if (access (p, R_OK) == -1 && geteuid () != 0)
1331 {
1332 WARN ((0, errno, _("Cannot add directory %s"), p));
1333 if (!ignore_failed_read_option)
1334 exit_status = TAREXIT_FAILURE;
1335 return;
1336 }
1337
1338 /* Build new prototype name. Ensure exactly one trailing slash. */
1339
1340 len = strlen (p);
1341 buflen = len + NAME_FIELD_SIZE;
1342 namebuf = xmalloc (buflen + 1);
1343 strncpy (namebuf, p, buflen);
1344 while (len >= 1 && namebuf[len - 1] == '/')
1345 len--;
1346 namebuf[len++] = '/';
1347 namebuf[len] = '\0';
1348
1349 if (1)
1350 {
1351 /* The "1" above used to be "archive_format != V7_FORMAT", GNU tar
1352 was just not writing directory blocks at all. Daniel Trinkle
1353 writes: ``All old versions of tar I have ever seen have
1354 correctly archived an empty directory. The really old ones I
1355 checked included HP-UX 7 and Mt. Xinu More/BSD. There may be
1356 some subtle reason for the exclusion that I don't know, but the
1357 current behavior is broken.'' I do not know those subtle
1358 reasons either, so until these are reported (anew?), just allow
1359 directory blocks to be written even with old archives. */
1360
1361 current_stat.st_size = 0; /* force 0 size on dir */
1362
1363 /* FIXME: If people could really read standard archives, this
1364 should be:
1365
1366 header
1367 = start_header (standard_option ? p : namebuf, &current_stat);
1368
1369 but since they'd interpret DIRTYPE blocks as regular
1370 files, we'd better put the / on the name. */
1371
1372 header = start_header (namebuf, &current_stat);
1373 if (header == NULL)
1374 {
1375 exit_status = TAREXIT_FAILURE;
1376 return; /* eg name too long */
1377 }
1378
1379 if (incremental_option)
1380 header->header.typeflag = GNUTYPE_DUMPDIR;
1381 else /* if (standard_option) */
1382 header->header.typeflag = DIRTYPE;
1383
1384 /* If we're gnudumping, we aren't done yet so don't close it. */
1385
1386 if (!incremental_option)
1387 finish_header (header); /* done with directory header */
1388 }
1389
1390 if (incremental_option && gnu_list_name->dir_contents)
1391 {
1392 off_t sizeleft;
1393 off_t totsize;
1394 size_t bufsize;
1395 union block *start;
1396 ssize_t count;
1397 const char *buffer, *p_buffer;
1398
1399 buffer = gnu_list_name->dir_contents; /* FOO */
1400 totsize = 0;
1401 for (p_buffer = buffer; p_buffer && *p_buffer;)
1402 {
1403 size_t tmp;
1404
1405 tmp = strlen (p_buffer) + 1;
1406 totsize += tmp;
1407 p_buffer += tmp;
1408 }
1409 totsize++;
1410 OFF_TO_OCT (totsize, header->header.size);
1411 finish_header (header);
1412 p_buffer = buffer;
1413 sizeleft = totsize;
1414 while (sizeleft > 0)
1415 {
1416 if (multi_volume_option)
1417 {
1418 assign_string (&save_name, p);
1419 save_sizeleft = sizeleft;
1420 save_totsize = totsize;
1421 }
1422 start = find_next_block ();
1423 bufsize = available_space_after (start);
1424 if (sizeleft < bufsize)
1425 {
1426 bufsize = sizeleft;
1427 count = bufsize % BLOCKSIZE;
1428 if (count)
1429 memset (start->buffer + sizeleft, 0,
1430 (size_t) (BLOCKSIZE - count));
1431 }
1432 memcpy (start->buffer, p_buffer, bufsize);
1433 sizeleft -= bufsize;
1434 p_buffer += bufsize;
1435 set_next_block_after (start + (bufsize - 1) / BLOCKSIZE);
1436 }
1437 if (multi_volume_option)
1438 assign_string (&save_name, NULL);
1439 if (atime_preserve_option)
1440 utime (p, &restore_times);
1441 return;
1442 }
1443
1444 /* See if we are about to recurse into a directory, and avoid doing
1445 so if the user wants that we do not descend into directories. */
1446
1447 if (no_recurse_option)
1448 return;
1449
1450 /* See if we are crossing from one file system to another, and
1451 avoid doing so if the user only wants to dump one file system. */
1452
1453 if (one_file_system_option && !top_level
1454 && parent_device != current_stat.st_dev)
1455 {
1456 if (verbose_option)
1457 WARN ((0, 0, _("%s: On a different filesystem; not dumped"), p));
1458 return;
1459 }
1460
1461 /* Now output all the files in the directory. */
1462
1463 errno = 0; /* FIXME: errno should be read-only */
1464
1465 directory = opendir (p);
1466 if (!directory)
1467 {
1468 ERROR ((0, errno, _("Cannot open directory %s"), p));
1469 return;
1470 }
1471
1472 /* Hack to remove "./" from the front of all the file names. */
1473
1474 if (len == 2 && namebuf[0] == '.' && namebuf[1] == '/')
1475 len = 0;
1476
1477 /* FIXME: Should speed this up by cd-ing into the dir. */
1478
1479 while (entry = readdir (directory), entry)
1480 {
1481 /* Skip `.', `..', and excluded file names. */
1482
1483 if (is_dot_or_dotdot (entry->d_name)
1484 || excluded_filename (excluded, entry->d_name))
1485 continue;
1486
1487 if ((int) NAMLEN (entry) + len >= buflen)
1488 {
1489 buflen = len + NAMLEN (entry);
1490 namebuf = (char *) xrealloc (namebuf, buflen + 1);
1491 #if 0
1492 namebuf[len] = '\0';
1493 ERROR ((0, 0, _("File name %s%s too long"),
1494 namebuf, entry->d_name));
1495 continue;
1496 #endif
1497 }
1498 strcpy (namebuf + len, entry->d_name);
1499 dump_file (namebuf, our_device, 0);
1500 }
1501
1502 closedir (directory);
1503 free (namebuf);
1504 if (atime_preserve_option)
1505 utime (p, &restore_times);
1506 return;
1507 }
1508
1509 #ifdef S_ISCHR
1510 else if (S_ISCHR (current_stat.st_mode))
1511 type = CHRTYPE;
1512 #endif
1513
1514 #ifdef S_ISBLK
1515 else if (S_ISBLK (current_stat.st_mode))
1516 type = BLKTYPE;
1517 #endif
1518
1519 /* Avoid screwy apollo lossage where S_IFIFO == S_IFSOCK. */
1520
1521 #if (_ISP__M68K == 0) && (_ISP__A88K == 0) && defined(S_ISFIFO)
1522 else if (S_ISFIFO (current_stat.st_mode))
1523 type = FIFOTYPE;
1524 #endif
1525
1526 #ifdef S_ISSOCK
1527 else if (S_ISSOCK (current_stat.st_mode))
1528 type = FIFOTYPE;
1529 #endif
1530
1531 else
1532 goto unknown;
1533
1534 if (archive_format == V7_FORMAT)
1535 goto unknown;
1536
1537 current_stat.st_size = 0; /* force 0 size */
1538 header = start_header (p, &current_stat);
1539 if (header == NULL)
1540 {
1541 exit_status = TAREXIT_FAILURE;
1542 return; /* eg name too long */
1543 }
1544
1545 header->header.typeflag = type;
1546
1547 #if defined(S_IFBLK) || defined(S_IFCHR)
1548 if (type != FIFOTYPE)
1549 {
1550 MAJOR_TO_OCT (major (current_stat.st_rdev), header->header.devmajor);
1551 MINOR_TO_OCT (minor (current_stat.st_rdev), header->header.devminor);
1552 }
1553 #endif
1554
1555 finish_header (header);
1556 if (remove_files_option)
1557 {
1558 if (unlink (p) == -1)
1559 ERROR ((0, errno, _("Cannot remove %s"), p));
1560 }
1561 return;
1562
1563 unknown:
1564 ERROR ((0, 0, _("%s: Unknown file type; file ignored"), p));
1565 }
This page took 0.104628 seconds and 4 git commands to generate.