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