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