]> Dogcows Code - chaz/tar/blob - src/create.c
*** empty log message ***
[chaz/tar] / src / create.c
1 /* Create a tar archive.
2 Copyright (C) 1985, 1992 Free Software Foundation
3
4 This file is part of GNU Tar.
5
6 GNU Tar is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Tar is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Tar; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 /*
21 * Create a tar archive.
22 *
23 * Written 25 Aug 1985 by John Gilmore, ihnp4!hoptoad!gnu.
24 */
25
26 #ifdef _AIX
27 #pragma alloca
28 #endif
29 #include <sys/types.h>
30 #include <stdio.h>
31 #include <errno.h>
32 #ifndef STDC_HEADERS
33 extern int errno;
34 #endif
35
36 #ifdef BSD42
37 #include <sys/file.h>
38 #else
39 #ifndef V7
40 #include <fcntl.h>
41 #endif
42 #endif
43
44 #include "tar.h"
45 #include "port.h"
46
47 #ifndef __MSDOS__
48 #include <pwd.h>
49 #include <grp.h>
50 #endif
51
52 #if defined(_POSIX_VERSION) || defined(DIRENT)
53 #include <dirent.h>
54 #ifdef direct
55 #undef direct
56 #endif /* direct */
57 #define direct dirent
58 #define DP_NAMELEN(x) strlen((x)->d_name)
59 #endif /* _POSIX_VERSION or DIRENT */
60 #if !defined(_POSIX_VERSION) && !defined(DIRENT) && defined(BSD42)
61 #include <sys/dir.h>
62 #define DP_NAMELEN(x) (x)->d_namlen
63 #endif /* not _POSIX_VERSION and BSD42 */
64 #ifdef __MSDOS__
65 #include "msd_dir.h"
66 #define DP_NAMELEN(x) (x)->d_namlen
67 #define direct dirent
68 #endif
69 #if defined(USG) && !defined(_POSIX_VERSION) && !defined(DIRENT)
70 #include <ndir.h>
71 #define DP_NAMELEN(x) strlen((x)->d_name)
72 #endif /* USG and not _POSIX_VERSION and not DIRENT */
73
74 extern struct stat hstat; /* Stat struct corresponding */
75
76 #ifndef __MSDOS__
77 extern dev_t ar_dev;
78 extern ino_t ar_ino;
79 #endif
80
81 /* JF */
82 extern struct name *gnu_list_name;
83
84 /*
85 * If there are no symbolic links, there is no lstat(). Use stat().
86 */
87 #ifndef S_ISLNK
88 #define lstat stat
89 #endif
90
91 extern void print_header();
92
93 union record *start_header();
94 void add_mangle();
95 void add_symlink_mangle();
96 void blank_name_list();
97 int check_exclude();
98 PTR ck_malloc();
99 PTR ck_realloc();
100 void clear_buffer();
101 void close_archive();
102 void collect_and_sort_names();
103 int confirm();
104 int deal_with_sparse();
105 void find_new_file_size();
106 void finish_header();
107 int finish_sparse_file();
108 void finduname();
109 void findgname();
110 int is_dot_or_dotdot();
111 void open_archive();
112 char *name_next();
113 void name_close();
114 void to_oct();
115 void dump_file();
116 void write_dir_file();
117 void write_eot();
118 void write_mangled();
119 int zero_record();
120
121 /* This code moved from tar.h since create.c is the only file that cares
122 about 'struct link's. This means that other files might not have to
123 include sys/types.h any more. */
124
125 struct link {
126 struct link *next;
127 dev_t dev;
128 ino_t ino;
129 short linkcount;
130 char name[1];
131 };
132
133 struct link *linklist; /* Points to first link in list */
134
135 static nolinks; /* Gets set if we run out of RAM */
136
137 /*
138 * "Scratch" space to store the information about a sparse file before
139 * writing the info into the header or extended header
140 */
141 /* struct sp_array *sparsearray;*/
142
143 /* number of elts storable in the sparsearray */
144 /*int sparse_array_size = 10;*/
145
146 void
147 create_archive()
148 {
149 register char *p;
150 char *name_from_list();
151
152 open_archive(0); /* Open for writing */
153
154 if(f_gnudump) {
155 char *buf = ck_malloc(PATH_MAX);
156 char *q,*bufp;
157
158 collect_and_sort_names();
159
160 while(p=name_from_list())
161 dump_file(p,-1, 1);
162 /* if(!f_dironly) { */
163 blank_name_list();
164 while(p=name_from_list()) {
165 strcpy(buf,p);
166 if(p[strlen(p)-1]!='/')
167 strcat(buf,"/");
168 bufp=buf+strlen(buf);
169 for(q=gnu_list_name->dir_contents;q && *q;q+=strlen(q)+1) {
170 if(*q=='Y') {
171 strcpy(bufp,q+1);
172 dump_file(buf,-1, 1);
173 }
174 }
175 }
176 /* } */
177 free(buf);
178 } else {
179 p = name_next(1);
180 do
181 dump_file(p, -1, 1);
182 while (p = name_next(1));
183 }
184
185 write_mangled();
186 write_eot();
187 close_archive();
188 if(f_gnudump)
189 write_dir_file();
190 name_close();
191 }
192
193 /*
194 * Dump a single file. If it's a directory, recurse.
195 * Result is 1 for success, 0 for failure.
196 * Sets global "hstat" to stat() output for this file.
197 */
198 void
199 dump_file (p, curdev, toplevel)
200 char *p; /* File name to dump */
201 int curdev; /* Device our parent dir was on */
202 int toplevel; /* Whether we are a toplevel call */
203 {
204 union record *header;
205 char type;
206 extern char *save_name; /* JF for multi-volume support */
207 extern long save_totsize;
208 extern long save_sizeleft;
209 union record *exhdr;
210 char save_linkflag;
211 extern time_t new_time;
212 int critical_error = 0;
213 time_t restore_times[2];
214 /* int sparse_ind = 0;*/
215
216
217 if(f_confirm && !confirm("add",p))
218 return;
219
220 /*
221 * Use stat if following (rather than dumping) 4.2BSD's
222 * symbolic links. Otherwise, use lstat (which, on non-4.2
223 * systems, is #define'd to stat anyway.
224 */
225 #ifdef STX_HIDDEN /* AIX */
226 if (0 != f_follow_links ?
227 statx (p, &hstat, STATSIZE, STX_HIDDEN):
228 statx (p, &hstat, STATSIZE, STX_HIDDEN|STX_LINK))
229 #else
230 if (0 != f_follow_links? stat(p, &hstat): lstat(p, &hstat))
231 #endif
232 {
233 badperror:
234 msg_perror("can't add file %s",p);
235 badfile:
236 if (!f_ignore_failed_read || critical_error)
237 errors++;
238 return;
239 }
240
241 restore_times[0] = hstat.st_atime;
242 restore_times[1] = hstat.st_utime;
243
244 #ifdef S_ISHIDDEN
245 if (S_ISHIDDEN (hstat.st_mode)) {
246 char *new = (char *)alloca (strlen (p) + 2);
247 if (new) {
248 strcpy (new, p);
249 strcat (new, "@");
250 p = new;
251 }
252 }
253 #endif
254
255 /* See if we only want new files, and check if this one is too old to
256 put in the archive. */
257 if( f_new_files
258 && !f_gnudump
259 && new_time>hstat.st_mtime
260 && !S_ISDIR(hstat.st_mode)
261 && (f_new_files>1 || new_time>hstat.st_ctime)) {
262 if(curdev<0) {
263 msg("%s: is unchanged; not dumped",p);
264 }
265 return;
266 }
267
268 #ifndef __MSDOS__
269 /* See if we are trying to dump the archive */
270 if(ar_dev && hstat.st_dev==ar_dev && hstat.st_ino==ar_ino) {
271 msg("%s is the archive; not dumped",p);
272 return;
273 }
274 #endif
275 /*
276 * Check for multiple links.
277 *
278 * We maintain a list of all such files that we've written so
279 * far. Any time we see another, we check the list and
280 * avoid dumping the data again if we've done it once already.
281 */
282 if (hstat.st_nlink > 1
283 && (S_ISREG(hstat.st_mode)
284 #ifdef S_ISCTG
285 || S_ISCTG(hstat.st_mode)
286 #endif
287 #ifdef S_ISCHR
288 || S_ISCHR(hstat.st_mode)
289 #endif
290 #ifdef S_ISBLK
291 || S_ISBLK(hstat.st_mode)
292 #endif
293 #ifdef S_ISFIFO
294 || S_ISFIFO(hstat.st_mode)
295 #endif
296 )) {
297 register struct link *lp;
298
299 /* First quick and dirty. Hashing, etc later FIXME */
300 for (lp = linklist; lp; lp = lp->next) {
301 if (lp->ino == hstat.st_ino &&
302 lp->dev == hstat.st_dev) {
303 char *link_name = lp->name;
304
305 /* We found a link. */
306 hstat.st_size = 0;
307 header = start_header(p, &hstat);
308 if (header == NULL)
309 {
310 critical_error = 1;
311 goto badfile;
312 }
313 while(!f_absolute_paths && *link_name == '/') {
314 static int link_warn = 0;
315
316 if (!link_warn) {
317 msg("Removing leading / from absolute links");
318 link_warn++;
319 }
320 link_name++;
321 }
322 strncpy(header->header.linkname,
323 link_name,NAMSIZ);
324 if(header->header.linkname[NAMSIZ-1]) {
325 char *mangled;
326 extern char *find_mangled();
327
328 mangled=find_mangled(link_name);
329 msg("%s: link name too long: mangled to %s",link_name,mangled);
330 strncpy(header->header.linkname,mangled,NAMSIZ);
331 }
332 header->header.linkflag = LF_LINK;
333 finish_header(header);
334 /* FIXME: Maybe remove from list after all links found? */
335 if (f_remove_files)
336 {
337 if (unlink (p) == -1)
338 msg_perror ("cannot remove %s", p);
339 }
340 return; /* We dumped it */
341 }
342 }
343
344 /* Not found. Add it to the list of possible links. */
345 lp = (struct link *)malloc((unsigned)(sizeof(struct link)+strlen(p)));
346 if (!lp) {
347 if (!nolinks) {
348 msg(
349 "no memory for links, they will be dumped as separate files");
350 nolinks++;
351 }
352 }
353 lp->ino = hstat.st_ino;
354 lp->dev = hstat.st_dev;
355 strcpy(lp->name, p);
356 lp->next = linklist;
357 linklist = lp;
358 }
359
360 /*
361 * This is not a link to a previously dumped file, so dump it.
362 */
363 if (S_ISREG(hstat.st_mode)
364 #ifdef S_ISCTG
365 || S_ISCTG(hstat.st_mode)
366 #endif
367 )
368 {
369 int f; /* File descriptor */
370 long bufsize, count;
371 long sizeleft;
372 register union record *start;
373 int header_moved;
374 char isextended = 0;
375 int upperbound;
376 /* int end_nulls = 0; */
377
378 header_moved = 0;
379
380 #ifdef BSD42
381 if (f_sparse_files) {
382 /*
383 * JK - This is the test for sparseness: whether the
384 * "size" of the file matches the number of blocks
385 * allocated for it. If there is a smaller number
386 * of blocks that would be necessary to accommodate
387 * a file of this size, we have a sparse file, i.e.,
388 * at least one of those records in the file is just
389 * a useless hole.
390 */
391 #ifdef hpux /* Nice of HPUX to gratuitiously change it, huh? - mib */
392 if (hstat.st_size - (hstat.st_blocks * 1024) > 1024 )
393 #else
394 if (hstat.st_size - (hstat.st_blocks * RECORDSIZE) > RECORDSIZE)
395 #endif
396 {
397 int filesize = hstat.st_size;
398 register int i;
399
400 header = start_header(p, &hstat);
401 if (header == NULL)
402 {
403 critical_error = 1;
404 goto badfile;
405 }
406 header->header.linkflag = LF_SPARSE;
407 header_moved++;
408
409 /*
410 * Call the routine that figures out the
411 * layout of the sparse file in question.
412 * UPPERBOUND is the index of the last
413 * element of the "sparsearray," i.e.,
414 * the number of elements it needed to
415 * describe the file.
416 */
417
418 upperbound = deal_with_sparse(p, header);
419
420 /*
421 * See if we'll need an extended header
422 * later
423 */
424 if (upperbound > SPARSE_IN_HDR-1)
425 header->header.isextended++;
426 /*
427 * We store the "real" file size so
428 * we can show that in case someone wants
429 * to list the archive, i.e., tar tvf <file>.
430 * It might be kind of disconcerting if the
431 * shrunken file size was the one that showed
432 * up.
433 */
434 to_oct((long) hstat.st_size, 1+12,
435 header->header.realsize);
436
437 /*
438 * This will be the new "size" of the
439 * file, i.e., the size of the file
440 * minus the records of holes that we're
441 * skipping over.
442 */
443
444 find_new_file_size(&filesize, upperbound);
445 hstat.st_size = filesize;
446 to_oct((long) filesize, 1+12,
447 header->header.size);
448 /* to_oct((long) end_nulls, 1+12,
449 header->header.ending_blanks);*/
450
451 for (i = 0; i < SPARSE_IN_HDR; i++) {
452 if (!sparsearray[i].numbytes)
453 break;
454 to_oct(sparsearray[i].offset, 1+12,
455 header->header.sp[i].offset);
456 to_oct(sparsearray[i].numbytes, 1+12,
457 header->header.sp[i].numbytes);
458 }
459
460 }
461 }
462 #else
463 upperbound=SPARSE_IN_HDR-1;
464 #endif
465
466 sizeleft = hstat.st_size;
467 /* Don't bother opening empty, world readable files. */
468 if (sizeleft > 0 || 0444 != (0444 & hstat.st_mode)) {
469 f = open(p, O_RDONLY|O_BINARY);
470 if (f < 0) goto badperror;
471 } else {
472 f = -1;
473 }
474
475 /* If the file is sparse, we've already taken care of this */
476 if (!header_moved) {
477 header = start_header(p, &hstat);
478 if (header == NULL) {
479 if(f>=0)
480 (void)close(f);
481 critical_error = 1;
482 goto badfile;
483 }
484 }
485 #ifdef S_ISCTG
486 /* Mark contiguous files, if we support them */
487 if (f_standard && S_ISCTG(hstat.st_mode)) {
488 header->header.linkflag = LF_CONTIG;
489 }
490 #endif
491 isextended = header->header.isextended;
492 save_linkflag = header->header.linkflag;
493 finish_header(header);
494 if (isextended) {
495 /* int sum = 0;*/
496 register int i;
497 /* register union record *exhdr;*/
498 /* int arraybound = SPARSE_EXT_HDR;*/
499 /* static */ int index_offset = SPARSE_IN_HDR;
500
501 extend: exhdr = findrec();
502
503 if (exhdr == NULL)
504 {
505 critical_error = 1;
506 goto badfile;
507 }
508 bzero(exhdr->charptr, RECORDSIZE);
509 for (i = 0; i < SPARSE_EXT_HDR; i++) {
510 if (i+index_offset > upperbound)
511 break;
512 to_oct((long) sparsearray[i+index_offset].numbytes,
513 1+12,
514 exhdr->ext_hdr.sp[i].numbytes);
515 to_oct((long) sparsearray[i+index_offset].offset,
516 1+12,
517 exhdr->ext_hdr.sp[i].offset);
518 }
519 userec(exhdr);
520 /* sum += i;
521 if (sum < upperbound)
522 goto extend;*/
523 if (index_offset+i < upperbound) {
524 index_offset += i;
525 exhdr->ext_hdr.isextended++;
526 goto extend;
527 }
528
529 }
530 if (save_linkflag == LF_SPARSE) {
531 if (finish_sparse_file(f, &sizeleft, hstat.st_size, p))
532 goto padit;
533 }
534 else
535 while (sizeleft > 0) {
536
537 if(f_multivol) {
538 save_name = p;
539 save_sizeleft = sizeleft;
540 save_totsize = hstat.st_size;
541 }
542 start = findrec();
543
544 bufsize = endofrecs()->charptr - start->charptr;
545
546 if (sizeleft < bufsize) {
547 /* Last read -- zero out area beyond */
548 bufsize = (int)sizeleft;
549 count = bufsize % RECORDSIZE;
550 if (count)
551 bzero(start->charptr + sizeleft,
552 (int)(RECORDSIZE - count));
553 }
554 count = read(f, start->charptr, bufsize);
555 if (count < 0) {
556 msg_perror("read error at byte %ld, reading\
557 %d bytes, in file %s", hstat.st_size - sizeleft, bufsize,p);
558 goto padit;
559 }
560 sizeleft -= count;
561
562 /* This is nonportable (the type of userec's arg). */
563 userec(start+(count-1)/RECORDSIZE);
564
565 if (count == bufsize) continue;
566 msg( "file %s shrunk by %d bytes, padding with zeros.", p, sizeleft);
567 goto padit; /* Short read */
568 }
569
570 if(f_multivol)
571 save_name = 0;
572
573 if (f >= 0)
574 (void)close(f);
575
576 if (f_remove_files)
577 {
578 if (unlink (p) == -1)
579 msg_perror ("cannot remove %s", p);
580 }
581 if (f_atime_preserve)
582 utime (p, restore_times);
583 return;
584
585 /*
586 * File shrunk or gave error, pad out tape to match
587 * the size we specified in the header.
588 */
589 padit:
590 while(sizeleft>0) {
591 save_sizeleft=sizeleft;
592 start=findrec();
593 bzero(start->charptr,RECORDSIZE);
594 userec(start);
595 sizeleft-=RECORDSIZE;
596 }
597 if(f_multivol)
598 save_name=0;
599 if(f>=0)
600 (void)close(f);
601 if (f_atime_preserve)
602 utime (p, restore_times);
603 return;
604 }
605
606 #ifdef S_ISLNK
607 else if(S_ISLNK(hstat.st_mode))
608 {
609 int size;
610
611 hstat.st_size = 0; /* Force 0 size on symlink */
612 header = start_header(p, &hstat);
613 if (header == NULL)
614 {
615 critical_error = 1;
616 goto badfile;
617 }
618 size = readlink(p, header->header.linkname, NAMSIZ);
619 if (size < 0) goto badperror;
620 if (size == NAMSIZ) {
621 char *buf = ck_malloc(PATH_MAX);
622
623 readlink(p,buf,PATH_MAX);
624 /* next_mangle(header->header.linkname); */
625 add_symlink_mangle(buf,p,header->header.linkname);
626 msg("symbolic link %s too long: mangling to %s",p, header->header.linkname);
627 /* size=strlen(header->header.linkname); */
628 free(buf);
629 } else
630 header->header.linkname[size] = '\0';
631 header->header.linkflag = LF_SYMLINK;
632 finish_header(header); /* Nothing more to do to it */
633 if (f_remove_files)
634 {
635 if (unlink (p) == -1)
636 msg_perror ("cannot remove %s", p);
637 }
638 return;
639 }
640 #endif
641
642 else if (S_ISDIR(hstat.st_mode))
643 {
644 register DIR *dirp;
645 register struct direct *d;
646 char *namebuf;
647 int buflen;
648 register int len;
649 int our_device = hstat.st_dev;
650
651 /* Build new prototype name */
652 len = strlen(p);
653 buflen=len+NAMSIZ;
654 namebuf=ck_malloc(buflen+1);
655 strncpy(namebuf, p, buflen);
656 while (len >= 1 && '/' == namebuf[len-1])
657 len--; /* Delete trailing slashes */
658 namebuf[len++] = '/'; /* Now add exactly one back */
659 namebuf[len] = '\0'; /* Make sure null-terminated */
660
661 /*
662 * Output directory header record with permissions
663 * FIXME, do this AFTER files, to avoid R/O dir problems?
664 * If old archive format, don't write record at all.
665 */
666 if (!f_oldarch) {
667 hstat.st_size = 0; /* Force 0 size on dir */
668 /*
669 * If people could really read standard archives,
670 * this should be: (FIXME)
671 header = start_header(f_standard? p: namebuf, &hstat);
672 * but since they'd interpret LF_DIR records as
673 * regular files, we'd better put the / on the name.
674 */
675 header = start_header(namebuf, &hstat);
676 if (header == NULL)
677 {
678 critical_error = 1;
679 goto badfile; /* eg name too long */
680 }
681
682 if (f_gnudump)
683 header->header.linkflag = LF_DUMPDIR;
684 else if (f_standard)
685 header->header.linkflag = LF_DIR;
686
687 /* If we're gnudumping, we aren't done yet so don't close it. */
688 if(!f_gnudump)
689 finish_header(header); /* Done with directory header */
690 }
691
692 if(f_gnudump) {
693 int sizeleft;
694 int totsize;
695 int bufsize;
696 union record *start;
697 int count;
698 char *buf,*p_buf;
699
700 buf=gnu_list_name->dir_contents; /* FOO */
701 totsize=0;
702 for(p_buf=buf;p_buf && *p_buf;) {
703 int tmp;
704
705 tmp=strlen(p_buf)+1;
706 totsize+=tmp;
707 p_buf+=tmp;
708 }
709 totsize++;
710 to_oct((long)totsize,1+12,header->header.size);
711 finish_header(header);
712 p_buf=buf;
713 sizeleft=totsize;
714 while(sizeleft>0) {
715 if(f_multivol) {
716 save_name=p;
717 save_sizeleft=sizeleft;
718 save_totsize=totsize;
719 }
720 start=findrec();
721 bufsize=endofrecs()->charptr - start->charptr;
722 if(sizeleft<bufsize) {
723 bufsize=sizeleft;
724 count=bufsize%RECORDSIZE;
725 if(count)
726 bzero(start->charptr+sizeleft,RECORDSIZE-count);
727 }
728 bcopy(p_buf,start->charptr,bufsize);
729 sizeleft-=bufsize;
730 p_buf+=bufsize;
731 userec(start+(bufsize-1)/RECORDSIZE);
732 }
733 if(f_multivol)
734 save_name = 0;
735 if (f_atime_preserve)
736 utime (p, restore_times);
737 return;
738 }
739
740 /* Now output all the files in the directory */
741 #if 0
742 if (f_dironly)
743 return; /* Unless the cmdline said not to */
744 #endif
745 /*
746 * See if we are crossing from one file system to another,
747 * and avoid doing so if the user only wants to dump one file system.
748 */
749 if (f_local_filesys && !toplevel && curdev != hstat.st_dev) {
750 if(f_verbose)
751 msg("%s: is on a different filesystem; not dumped",p);
752 return;
753 }
754
755
756 errno = 0;
757 dirp = opendir(p);
758 if (!dirp) {
759 if (errno) {
760 msg_perror ("can't open directory %s",p);
761 } else {
762 msg("error opening directory %s",
763 p);
764 }
765 return;
766 }
767
768 /* Hack to remove "./" from the front of all the file names */
769 if (len == 2 && namebuf[0] == '.' && namebuf[1]=='/')
770 len = 0;
771
772 /* Should speed this up by cd-ing into the dir, FIXME */
773 while (NULL != (d=readdir(dirp))) {
774 /* Skip . and .. */
775 if(is_dot_or_dotdot(d->d_name))
776 continue;
777
778 if (DP_NAMELEN(d) + len >= buflen) {
779 buflen=len+DP_NAMELEN(d);
780 namebuf=ck_realloc(namebuf,buflen+1);
781 /* namebuf[len]='\0';
782 msg("file name %s%s too long",
783 namebuf, d->d_name);
784 continue; */
785 }
786 strcpy(namebuf+len, d->d_name);
787 if(f_exclude && check_exclude(namebuf))
788 continue;
789 dump_file(namebuf, our_device, 0);
790 }
791
792 closedir(dirp);
793 free(namebuf);
794 if (f_atime_preserve)
795 utime (p, restore_times);
796 return;
797 }
798
799 #ifdef S_ISCHR
800 else if (S_ISCHR(hstat.st_mode)) {
801 type = LF_CHR;
802 }
803 #endif
804
805 #ifdef S_ISBLK
806 else if (S_ISBLK(hstat.st_mode)) {
807 type = LF_BLK;
808 }
809 #endif
810
811 /* Avoid screwy apollo lossage where S_IFIFO == S_IFSOCK */
812 #if (_ISP__M68K == 0) && (_ISP__A88K == 0) && defined(S_ISFIFO)
813 else if (S_ISFIFO(hstat.st_mode)) {
814 type = LF_FIFO;
815 }
816 #endif
817
818 #ifdef S_ISSOCK
819 else if (S_ISSOCK(hstat.st_mode)) {
820 type = LF_FIFO;
821 }
822 #endif
823 else
824 goto unknown;
825
826 if (!f_standard) goto unknown;
827
828 hstat.st_size = 0; /* Force 0 size */
829 header = start_header(p, &hstat);
830 if (header == NULL)
831 {
832 critical_error = 1;
833 goto badfile; /* eg name too long */
834 }
835
836 header->header.linkflag = type;
837 #if defined(S_IFBLK) || defined(S_IFCHR)
838 if (type != LF_FIFO) {
839 to_oct((long) major(hstat.st_rdev), 8,
840 header->header.devmajor);
841 to_oct((long) minor(hstat.st_rdev), 8,
842 header->header.devminor);
843 }
844 #endif
845
846 finish_header(header);
847 if (f_remove_files)
848 {
849 if (unlink (p) == -1)
850 msg_perror ("cannot remove %s", p);
851 }
852 return;
853
854 unknown:
855 msg("%s: Unknown file type; file ignored.", p);
856 }
857
858 int
859 finish_sparse_file(fd, sizeleft, fullsize, name)
860 int fd;
861 long *sizeleft,
862 fullsize;
863 char *name;
864 {
865 union record *start;
866 char tempbuf[RECORDSIZE];
867 int bufsize,
868 sparse_ind = 0,
869 count;
870 long pos;
871 long nwritten = 0;
872
873
874 while (*sizeleft > 0) {
875 start = findrec();
876 bzero(start->charptr, RECORDSIZE);
877 bufsize = sparsearray[sparse_ind].numbytes;
878 if (!bufsize) { /* we blew it, maybe */
879 msg("Wrote %ld of %ld bytes to file %s",
880 fullsize - *sizeleft, fullsize, name);
881 break;
882 }
883 pos = lseek(fd, sparsearray[sparse_ind++].offset, 0);
884 /*
885 * If the number of bytes to be written here exceeds
886 * the size of the temporary buffer, do it in steps.
887 */
888 while (bufsize > RECORDSIZE) {
889 /* if (amt_read) {
890 count = read(fd, start->charptr+amt_read, RECORDSIZE-amt_read);
891 bufsize -= RECORDSIZE - amt_read;
892 amt_read = 0;
893 userec(start);
894 start = findrec();
895 bzero(start->charptr, RECORDSIZE);
896 }*/
897 /* store the data */
898 count = read(fd, start->charptr, RECORDSIZE);
899 if (count < 0) {
900 msg_perror("read error at byte %ld, reading %d bytes, in file %s",
901 fullsize - *sizeleft, bufsize, name);
902 return 1;
903 }
904 bufsize -= count;
905 *sizeleft -= count;
906 userec(start);
907 nwritten += RECORDSIZE; /* XXX */
908 start = findrec();
909 bzero(start->charptr, RECORDSIZE);
910 }
911
912
913 clear_buffer(tempbuf);
914 count = read(fd, tempbuf, bufsize);
915 bcopy(tempbuf, start->charptr, RECORDSIZE);
916 if (count < 0) {
917 msg_perror("read error at byte %ld, reading %d bytes, in file %s",
918 fullsize - *sizeleft, bufsize, name);
919 return 1;
920 }
921 /* if (amt_read >= RECORDSIZE) {
922 amt_read = 0;
923 userec(start+(count-1)/RECORDSIZE);
924 if (count != bufsize) {
925 msg("file %s shrunk by %d bytes, padding with zeros.", name, sizeleft);
926 return 1;
927 }
928 start = findrec();
929 } else
930 amt_read += bufsize;*/
931 nwritten += count; /* XXX */
932 *sizeleft -= count;
933 userec(start);
934
935 }
936 free(sparsearray);
937 /* printf ("Amount actually written is (I hope) %d.\n", nwritten); */
938 /* userec(start+(count-1)/RECORDSIZE);*/
939 return 0;
940
941 }
942
943 void
944 init_sparsearray()
945 {
946 register int i;
947
948 sp_array_size = 10;
949 /*
950 * Make room for our scratch space -- initially is 10 elts long
951 */
952 sparsearray = (struct sp_array *) malloc(sp_array_size * sizeof(struct sp_array));
953 for (i = 0; i < sp_array_size; i++) {
954 sparsearray[i].offset = 0;
955 sparsearray[i].numbytes = 0;
956 }
957 }
958
959
960
961 /*
962 * Okay, we've got a sparse file on our hands -- now, what we need to do is
963 * make a pass through the file and carefully note where any data is, i.e.,
964 * we want to find how far into the file each instance of data is, and how
965 * many bytes are there. We store this information in the sparsearray,
966 * which will later be translated into header information. For now, we use
967 * the sparsearray as convenient storage.
968 *
969 * As a side note, this routine is a mess. If I could have found a cleaner
970 * way to do it, I would have. If anyone wants to find a nicer way to do
971 * this, feel free.
972 */
973
974 /* There is little point in trimming small amounts of null data at the */
975 /* head and tail of blocks -- it's ok if we only avoid dumping blocks */
976 /* of complete null data */
977 int
978 deal_with_sparse(name, header, nulls_at_end)
979 char *name;
980 union record *header;
981 int nulls_at_end;
982 {
983 long numbytes = 0;
984 long offset = 0;
985 /* long save_offset;*/
986 int fd;
987 /* int current_size = hstat.st_size;*/
988 int sparse_ind = 0,
989 cc;
990 char buf[RECORDSIZE];
991 #if 0
992 int read_last_data = 0; /* did we just read the last record? */
993 #endif
994 int amidst_data = 0;
995
996 header->header.isextended = 0;
997 /*
998 * Can't open the file -- this problem will be caught later on,
999 * so just return.
1000 */
1001 if ((fd = open(name, O_RDONLY)) < 0)
1002 return 0;
1003
1004 init_sparsearray();
1005 clear_buffer(buf);
1006
1007 while ((cc = read(fd, buf, sizeof buf)) != 0) {
1008
1009 if (sparse_ind > sp_array_size-1) {
1010
1011 /*
1012 * realloc the scratch area, since we've run out of room --
1013 */
1014 sparsearray = (struct sp_array *)
1015 realloc(sparsearray,
1016 2 * sp_array_size * (sizeof(struct sp_array)));
1017 sp_array_size *= 2;
1018 }
1019 if (cc == sizeof buf) {
1020 if (zero_record(buf)) {
1021 if (amidst_data) {
1022 sparsearray[sparse_ind++].numbytes
1023 = numbytes;
1024 amidst_data = 0;
1025 }
1026 } else { /* !zero_record(buf) */
1027 if (amidst_data)
1028 numbytes += cc;
1029 else {
1030 amidst_data = 1;
1031 numbytes = cc;
1032 sparsearray[sparse_ind].offset
1033 = offset;
1034 }
1035 }
1036 } else if (cc < sizeof buf) {
1037 /* This has to be the last bit of the file, so this */
1038 /* is somewhat shorter than the above. */
1039 if (!zero_record(buf)) {
1040 if (!amidst_data) {
1041 amidst_data = 1;
1042 numbytes = cc;
1043 sparsearray[sparse_ind].offset
1044 = offset;
1045 } else
1046 numbytes += cc;
1047 }
1048 }
1049 offset += cc;
1050 clear_buffer(buf);
1051 }
1052 if (amidst_data)
1053 sparsearray[sparse_ind++].numbytes = numbytes;
1054 close(fd);
1055
1056 return sparse_ind - 1;
1057 }
1058
1059 /*
1060 * Just zeroes out the buffer so we don't confuse ourselves with leftover
1061 * data.
1062 */
1063 void
1064 clear_buffer(buf)
1065 char *buf;
1066 {
1067 register int i;
1068
1069 for (i = 0; i < RECORDSIZE; i++)
1070 buf[i] = '\0';
1071 }
1072
1073 #if 0 /* I'm leaving this as a monument to Joy Kendall, who wrote it -mib */
1074 /*
1075 * JK -
1076 * This routine takes a character array, and tells where within that array
1077 * the data can be found. It skips over any zeros, and sets the first
1078 * non-zero point in the array to be the "start", and continues until it
1079 * finds non-data again, which is marked as the "end." This routine is
1080 * mainly for 1) seeing how far into a file we must lseek to data, given
1081 * that we have a sparse file, and 2) determining the "real size" of the
1082 * file, i.e., the number of bytes in the sparse file that are data, as
1083 * opposed to the zeros we are trying to skip.
1084 */
1085 where_is_data(from, to, buffer)
1086 int *from,
1087 *to;
1088 char *buffer;
1089 {
1090 register int i = 0;
1091 register int save_to = *to;
1092 int amidst_data = 0;
1093
1094
1095 while (!buffer[i])
1096 i++;
1097 *from = i;
1098
1099 if (*from < 16) /* don't bother */
1100 *from = 0;
1101 /* keep going to make sure there isn't more real
1102 data in this record */
1103 while (i < RECORDSIZE) {
1104 if (!buffer[i]) {
1105 if (amidst_data) {
1106 save_to = i;
1107 amidst_data = 0;
1108 }
1109 i++;
1110 }
1111 else if (buffer[i]) {
1112 if (!amidst_data)
1113 amidst_data = 1;
1114 i++;
1115 }
1116 }
1117 if (i == RECORDSIZE)
1118 *to = i;
1119 else
1120 *to = save_to;
1121
1122 }
1123 #endif
1124
1125 /* Note that this routine is only called if zero_record returned true */
1126 #if 0 /* But we actually don't need it at all. */
1127 where_is_data (from, to, buffer)
1128 int *from, *to;
1129 char *buffer;
1130 {
1131 char *fp, *tp;
1132
1133 for (fp = buffer; ! *fp; fp++)
1134 ;
1135 for (tp = buffer + RECORDSIZE - 1; ! *tp; tp--)
1136 ;
1137 *from = fp - buffer;
1138 *to = tp - buffer + 1;
1139 }
1140 #endif
1141
1142
1143
1144 /*
1145 * Takes a recordful of data and basically cruises through it to see if
1146 * it's made *entirely* of zeros, returning a 0 the instant it finds
1147 * something that is a non-zero, i.e., useful data.
1148 */
1149 int
1150 zero_record(buffer)
1151 char *buffer;
1152 {
1153 register int i;
1154
1155 for (i = 0; i < RECORDSIZE; i++)
1156 if (buffer[i] != '\000')
1157 return 0;
1158 return 1;
1159 }
1160
1161 void
1162 find_new_file_size(filesize, highest_index)
1163 int *filesize;
1164 int highest_index;
1165 {
1166 register int i;
1167
1168 *filesize = 0;
1169 for (i = 0; sparsearray[i].numbytes && i <= highest_index; i++)
1170 *filesize += sparsearray[i].numbytes;
1171 }
1172
1173 /*
1174 * Make a header block for the file name whose stat info is st .
1175 * Return header pointer for success, NULL if the name is too long.
1176 */
1177 union record *
1178 start_header(name, st)
1179 char *name;
1180 register struct stat *st;
1181 {
1182 register union record *header;
1183
1184 header = (union record *) findrec();
1185 bzero(header->charptr, sizeof(*header)); /* XXX speed up */
1186
1187 /*
1188 * Check the file name and put it in the record.
1189 */
1190 if(!f_absolute_paths) {
1191 static int warned_once = 0;
1192 #ifdef __MSDOS__
1193 if(name[1]==':') {
1194 name+=2;
1195 if(!warned_once++)
1196 msg("Removing drive spec from names in the archive");
1197 }
1198 #endif
1199 while ('/' == *name) {
1200 name++; /* Force relative path */
1201 if (!warned_once++)
1202 msg("Removing leading / from absolute path names in the archive.");
1203 }
1204 }
1205 strncpy(header->header.name, name, NAMSIZ);
1206 if (header->header.name[NAMSIZ-1]) {
1207 /* char *mangled;*/
1208
1209 /* next_mangle(header->header.name); */
1210 add_mangle(name,header->header.name);
1211 msg("%s: is too long: mangling to %s", name, header->header.name);
1212 }
1213
1214 to_oct((long) (st->st_mode & 07777),
1215 8, header->header.mode);
1216 to_oct((long) st->st_uid, 8, header->header.uid);
1217 to_oct((long) st->st_gid, 8, header->header.gid);
1218 to_oct((long) st->st_size, 1+12, header->header.size);
1219 to_oct((long) st->st_mtime, 1+12, header->header.mtime);
1220 /* header->header.linkflag is left as null */
1221 if(f_gnudump) {
1222 to_oct((long) st->st_atime, 1+12, header->header.atime);
1223 to_oct((long) st->st_ctime, 1+12, header->header.ctime);
1224 }
1225
1226 #ifndef NONAMES
1227 /* Fill in new Unix Standard fields if desired. */
1228 if (f_standard) {
1229 header->header.linkflag = LF_NORMAL; /* New default */
1230 strcpy(header->header.magic, TMAGIC); /* Mark as Unix Std */
1231 finduname(header->header.uname, st->st_uid);
1232 findgname(header->header.gname, st->st_gid);
1233 }
1234 #endif
1235 return header;
1236 }
1237
1238 /*
1239 * Finish off a filled-in header block and write it out.
1240 * We also print the file name and/or full info if verbose is on.
1241 */
1242 void
1243 finish_header(header)
1244 register union record *header;
1245 {
1246 register int i, sum;
1247 register char *p;
1248
1249 bcopy(CHKBLANKS, header->header.chksum, sizeof(header->header.chksum));
1250
1251 sum = 0;
1252 p = header->charptr;
1253 for (i = sizeof(*header); --i >= 0; ) {
1254 /*
1255 * We can't use unsigned char here because of old compilers,
1256 * e.g. V7.
1257 */
1258 sum += 0xFF & *p++;
1259 }
1260
1261 /*
1262 * Fill in the checksum field. It's formatted differently
1263 * from the other fields: it has [6] digits, a null, then a
1264 * space -- rather than digits, a space, then a null.
1265 * We use to_oct then write the null in over to_oct's space.
1266 * The final space is already there, from checksumming, and
1267 * to_oct doesn't modify it.
1268 *
1269 * This is a fast way to do:
1270 * (void) sprintf(header->header.chksum, "%6o", sum);
1271 */
1272 to_oct((long) sum, 8, header->header.chksum);
1273 header->header.chksum[6] = '\0'; /* Zap the space */
1274
1275 userec(header);
1276
1277 if (f_verbose) {
1278 extern union record *head; /* Points to current tape header */
1279 extern int head_standard; /* Tape header is in ANSI format */
1280
1281 /* These globals are parameters to print_header, sigh */
1282 head = header;
1283 /* hstat is already set up */
1284 head_standard = f_standard;
1285 print_header();
1286 }
1287
1288 return;
1289 }
1290
1291
1292 /*
1293 * Quick and dirty octal conversion.
1294 * Converts long "value" into a "digs"-digit field at "where",
1295 * including a trailing space and room for a null. "digs"==3 means
1296 * 1 digit, a space, and room for a null.
1297 *
1298 * We assume the trailing null is already there and don't fill it in.
1299 * This fact is used by start_header and finish_header, so don't change it!
1300 *
1301 * This should be equivalent to:
1302 * (void) sprintf(where, "%*lo ", digs-2, value);
1303 * except that sprintf fills in the trailing null and we don't.
1304 */
1305 void
1306 to_oct(value, digs, where)
1307 register long value;
1308 register int digs;
1309 register char *where;
1310 {
1311
1312 --digs; /* Trailing null slot is left alone */
1313 where[--digs] = ' '; /* Put in the space, though */
1314
1315 /* Produce the digits -- at least one */
1316 do {
1317 where[--digs] = '0' + (char)(value & 7); /* one octal digit */
1318 value >>= 3;
1319 } while (digs > 0 && value != 0);
1320
1321 /* Leading spaces, if necessary */
1322 while (digs > 0)
1323 where[--digs] = ' ';
1324
1325 }
1326
1327
1328 /*
1329 * Write the EOT record(s).
1330 * We actually zero at least one record, through the end of the block.
1331 * Old tar writes garbage after two zeroed records -- and PDtar used to.
1332 */
1333 void
1334 write_eot()
1335 {
1336 union record *p;
1337 int bufsize;
1338
1339 p = findrec();
1340 if (p)
1341 {
1342 bufsize = endofrecs()->charptr - p->charptr;
1343 bzero(p->charptr, bufsize);
1344 userec(p);
1345 }
1346 }
This page took 0.090861 seconds and 5 git commands to generate.