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