]> 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 while(!f_absolute_paths && *link_name == '/') {
303 static int link_warn = 0;
304
305 if (!link_warn) {
306 msg("Removing leading / from absolute links");
307 link_warn++;
308 }
309 link_name++;
310 }
311 if (link_name - lp->name >= NAMSIZ)
312 write_longlink (link_name);
313
314 hstat.st_size = 0;
315 header = start_header(p, &hstat);
316 if (header == NULL)
317 {
318 critical_error = 1;
319 goto badfile;
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 if (f_atime_preserve)
581 utime (p, restore_times);
582 return;
583
584 /*
585 * File shrunk or gave error, pad out tape to match
586 * the size we specified in the header.
587 */
588 padit:
589 while(sizeleft>0) {
590 save_sizeleft=sizeleft;
591 start=findrec();
592 bzero(start->charptr,RECORDSIZE);
593 userec(start);
594 sizeleft-=RECORDSIZE;
595 }
596 if(f_multivol)
597 save_name=0;
598 if(f>=0)
599 (void)close(f);
600 if (f_atime_preserve)
601 utime (p, restore_times);
602 return;
603 }
604
605 #ifdef S_ISLNK
606 else if(S_ISLNK(hstat.st_mode))
607 {
608 int size;
609
610 hstat.st_size = 0; /* Force 0 size on symlink */
611 header = start_header(p, &hstat);
612 if (header == NULL)
613 {
614 critical_error = 1;
615 goto badfile;
616 }
617 size = readlink(p, header->header.linkname, NAMSIZ);
618 if (size < 0) goto badperror;
619 if (size == NAMSIZ) {
620 char *buf = ck_malloc(PATH_MAX);
621
622 readlink(p,buf,PATH_MAX);
623 /* next_mangle(header->header.linkname); */
624 add_symlink_mangle(buf,p,header->header.linkname);
625 msg("symbolic link %s too long: mangling to %s",p, header->header.linkname);
626 /* size=strlen(header->header.linkname); */
627 free(buf);
628 } else
629 header->header.linkname[size] = '\0';
630 header->header.linkflag = LF_SYMLINK;
631 finish_header(header); /* Nothing more to do to it */
632 if (f_remove_files)
633 {
634 if (unlink (p) == -1)
635 msg_perror ("cannot remove %s", p);
636 }
637 return;
638 }
639 #endif
640
641 else if (S_ISDIR(hstat.st_mode))
642 {
643 register DIR *dirp;
644 register struct direct *d;
645 char *namebuf;
646 int buflen;
647 register int len;
648 int our_device = hstat.st_dev;
649
650 /* Build new prototype name */
651 len = strlen(p);
652 buflen=len+NAMSIZ;
653 namebuf=ck_malloc(buflen+1);
654 strncpy(namebuf, p, buflen);
655 while (len >= 1 && '/' == namebuf[len-1])
656 len--; /* Delete trailing slashes */
657 namebuf[len++] = '/'; /* Now add exactly one back */
658 namebuf[len] = '\0'; /* Make sure null-terminated */
659
660 /*
661 * Output directory header record with permissions
662 * FIXME, do this AFTER files, to avoid R/O dir problems?
663 * If old archive format, don't write record at all.
664 */
665 if (!f_oldarch) {
666 hstat.st_size = 0; /* Force 0 size on dir */
667 /*
668 * If people could really read standard archives,
669 * this should be: (FIXME)
670 header = start_header(f_standard? p: namebuf, &hstat);
671 * but since they'd interpret LF_DIR records as
672 * regular files, we'd better put the / on the name.
673 */
674 header = start_header(namebuf, &hstat);
675 if (header == NULL)
676 {
677 critical_error = 1;
678 goto badfile; /* eg name too long */
679 }
680
681 if (f_gnudump)
682 header->header.linkflag = LF_DUMPDIR;
683 else if (f_standard)
684 header->header.linkflag = LF_DIR;
685
686 /* If we're gnudumping, we aren't done yet so don't close it. */
687 if(!f_gnudump)
688 finish_header(header); /* Done with directory header */
689 }
690
691 if(f_gnudump) {
692 int sizeleft;
693 int totsize;
694 int bufsize;
695 union record *start;
696 int count;
697 char *buf,*p_buf;
698
699 buf=gnu_list_name->dir_contents; /* FOO */
700 totsize=0;
701 for(p_buf=buf;p_buf && *p_buf;) {
702 int tmp;
703
704 tmp=strlen(p_buf)+1;
705 totsize+=tmp;
706 p_buf+=tmp;
707 }
708 totsize++;
709 to_oct((long)totsize,1+12,header->header.size);
710 finish_header(header);
711 p_buf=buf;
712 sizeleft=totsize;
713 while(sizeleft>0) {
714 if(f_multivol) {
715 save_name=p;
716 save_sizeleft=sizeleft;
717 save_totsize=totsize;
718 }
719 start=findrec();
720 bufsize=endofrecs()->charptr - start->charptr;
721 if(sizeleft<bufsize) {
722 bufsize=sizeleft;
723 count=bufsize%RECORDSIZE;
724 if(count)
725 bzero(start->charptr+sizeleft,RECORDSIZE-count);
726 }
727 bcopy(p_buf,start->charptr,bufsize);
728 sizeleft-=bufsize;
729 p_buf+=bufsize;
730 userec(start+(bufsize-1)/RECORDSIZE);
731 }
732 if(f_multivol)
733 save_name = 0;
734 if (f_atime_preserve)
735 utime (p, restore_times);
736 return;
737 }
738
739 /* Now output all the files in the directory */
740 #if 0
741 if (f_dironly)
742 return; /* Unless the cmdline said not to */
743 #endif
744 /*
745 * See if we are crossing from one file system to another,
746 * and avoid doing so if the user only wants to dump one file system.
747 */
748 if (f_local_filesys && !toplevel && curdev != hstat.st_dev) {
749 if(f_verbose)
750 msg("%s: is on a different filesystem; not dumped",p);
751 return;
752 }
753
754
755 errno = 0;
756 dirp = opendir(p);
757 if (!dirp) {
758 if (errno) {
759 msg_perror ("can't open directory %s",p);
760 } else {
761 msg("error opening directory %s",
762 p);
763 }
764 return;
765 }
766
767 /* Hack to remove "./" from the front of all the file names */
768 if (len == 2 && namebuf[0] == '.' && namebuf[1]=='/')
769 len = 0;
770
771 /* Should speed this up by cd-ing into the dir, FIXME */
772 while (NULL != (d=readdir(dirp))) {
773 /* Skip . and .. */
774 if(is_dot_or_dotdot(d->d_name))
775 continue;
776
777 if (DP_NAMELEN(d) + len >= buflen) {
778 buflen=len+DP_NAMELEN(d);
779 namebuf=ck_realloc(namebuf,buflen+1);
780 /* namebuf[len]='\0';
781 msg("file name %s%s too long",
782 namebuf, d->d_name);
783 continue; */
784 }
785 strcpy(namebuf+len, d->d_name);
786 if(f_exclude && check_exclude(namebuf))
787 continue;
788 dump_file(namebuf, our_device, 0);
789 }
790
791 closedir(dirp);
792 free(namebuf);
793 if (f_atime_preserve)
794 utime (p, restore_times);
795 return;
796 }
797
798 #ifdef S_ISCHR
799 else if (S_ISCHR(hstat.st_mode)) {
800 type = LF_CHR;
801 }
802 #endif
803
804 #ifdef S_ISBLK
805 else if (S_ISBLK(hstat.st_mode)) {
806 type = LF_BLK;
807 }
808 #endif
809
810 /* Avoid screwy apollo lossage where S_IFIFO == S_IFSOCK */
811 #if (_ISP__M68K == 0) && (_ISP__A88K == 0) && defined(S_ISFIFO)
812 else if (S_ISFIFO(hstat.st_mode)) {
813 type = LF_FIFO;
814 }
815 #endif
816
817 #ifdef S_ISSOCK
818 else if (S_ISSOCK(hstat.st_mode)) {
819 type = LF_FIFO;
820 }
821 #endif
822 else
823 goto unknown;
824
825 if (!f_standard) goto unknown;
826
827 hstat.st_size = 0; /* Force 0 size */
828 header = start_header(p, &hstat);
829 if (header == NULL)
830 {
831 critical_error = 1;
832 goto badfile; /* eg name too long */
833 }
834
835 header->header.linkflag = type;
836 #if defined(S_IFBLK) || defined(S_IFCHR)
837 if (type != LF_FIFO) {
838 to_oct((long) major(hstat.st_rdev), 8,
839 header->header.devmajor);
840 to_oct((long) minor(hstat.st_rdev), 8,
841 header->header.devminor);
842 }
843 #endif
844
845 finish_header(header);
846 if (f_remove_files)
847 {
848 if (unlink (p) == -1)
849 msg_perror ("cannot remove %s", p);
850 }
851 return;
852
853 unknown:
854 msg("%s: Unknown file type; file ignored.", p);
855 }
856
857 int
858 finish_sparse_file(fd, sizeleft, fullsize, name)
859 int fd;
860 long *sizeleft,
861 fullsize;
862 char *name;
863 {
864 union record *start;
865 char tempbuf[RECORDSIZE];
866 int bufsize,
867 sparse_ind = 0,
868 count;
869 long pos;
870 long nwritten = 0;
871
872
873 while (*sizeleft > 0) {
874 start = findrec();
875 bzero(start->charptr, RECORDSIZE);
876 bufsize = sparsearray[sparse_ind].numbytes;
877 if (!bufsize) { /* we blew it, maybe */
878 msg("Wrote %ld of %ld bytes to file %s",
879 fullsize - *sizeleft, fullsize, name);
880 break;
881 }
882 pos = lseek(fd, sparsearray[sparse_ind++].offset, 0);
883 /*
884 * If the number of bytes to be written here exceeds
885 * the size of the temporary buffer, do it in steps.
886 */
887 while (bufsize > RECORDSIZE) {
888 /* if (amt_read) {
889 count = read(fd, start->charptr+amt_read, RECORDSIZE-amt_read);
890 bufsize -= RECORDSIZE - amt_read;
891 amt_read = 0;
892 userec(start);
893 start = findrec();
894 bzero(start->charptr, RECORDSIZE);
895 }*/
896 /* store the data */
897 count = read(fd, start->charptr, RECORDSIZE);
898 if (count < 0) {
899 msg_perror("read error at byte %ld, reading %d bytes, in file %s",
900 fullsize - *sizeleft, bufsize, name);
901 return 1;
902 }
903 bufsize -= count;
904 *sizeleft -= count;
905 userec(start);
906 nwritten += RECORDSIZE; /* XXX */
907 start = findrec();
908 bzero(start->charptr, RECORDSIZE);
909 }
910
911
912 clear_buffer(tempbuf);
913 count = read(fd, tempbuf, bufsize);
914 bcopy(tempbuf, start->charptr, RECORDSIZE);
915 if (count < 0) {
916 msg_perror("read error at byte %ld, reading %d bytes, in file %s",
917 fullsize - *sizeleft, bufsize, name);
918 return 1;
919 }
920 /* if (amt_read >= RECORDSIZE) {
921 amt_read = 0;
922 userec(start+(count-1)/RECORDSIZE);
923 if (count != bufsize) {
924 msg("file %s shrunk by %d bytes, padding with zeros.", name, sizeleft);
925 return 1;
926 }
927 start = findrec();
928 } else
929 amt_read += bufsize;*/
930 nwritten += count; /* XXX */
931 *sizeleft -= count;
932 userec(start);
933
934 }
935 free(sparsearray);
936 /* printf ("Amount actually written is (I hope) %d.\n", nwritten); */
937 /* userec(start+(count-1)/RECORDSIZE);*/
938 return 0;
939
940 }
941
942 void
943 init_sparsearray()
944 {
945 register int i;
946
947 sp_array_size = 10;
948 /*
949 * Make room for our scratch space -- initially is 10 elts long
950 */
951 sparsearray = (struct sp_array *) malloc(sp_array_size * sizeof(struct sp_array));
952 for (i = 0; i < sp_array_size; i++) {
953 sparsearray[i].offset = 0;
954 sparsearray[i].numbytes = 0;
955 }
956 }
957
958
959
960 /*
961 * Okay, we've got a sparse file on our hands -- now, what we need to do is
962 * make a pass through the file and carefully note where any data is, i.e.,
963 * we want to find how far into the file each instance of data is, and how
964 * many bytes are there. We store this information in the sparsearray,
965 * which will later be translated into header information. For now, we use
966 * the sparsearray as convenient storage.
967 *
968 * As a side note, this routine is a mess. If I could have found a cleaner
969 * way to do it, I would have. If anyone wants to find a nicer way to do
970 * this, feel free.
971 */
972
973 /* There is little point in trimming small amounts of null data at the */
974 /* head and tail of blocks -- it's ok if we only avoid dumping blocks */
975 /* of complete null data */
976 int
977 deal_with_sparse(name, header, nulls_at_end)
978 char *name;
979 union record *header;
980 int nulls_at_end;
981 {
982 long numbytes = 0;
983 long offset = 0;
984 /* long save_offset;*/
985 int fd;
986 /* int current_size = hstat.st_size;*/
987 int sparse_ind = 0,
988 cc;
989 char buf[RECORDSIZE];
990 #if 0
991 int read_last_data = 0; /* did we just read the last record? */
992 #endif
993 int amidst_data = 0;
994
995 header->header.isextended = 0;
996 /*
997 * Can't open the file -- this problem will be caught later on,
998 * so just return.
999 */
1000 if ((fd = open(name, O_RDONLY)) < 0)
1001 return 0;
1002
1003 init_sparsearray();
1004 clear_buffer(buf);
1005
1006 while ((cc = read(fd, buf, sizeof buf)) != 0) {
1007
1008 if (sparse_ind > sp_array_size-1) {
1009
1010 /*
1011 * realloc the scratch area, since we've run out of room --
1012 */
1013 sparsearray = (struct sp_array *)
1014 realloc(sparsearray,
1015 2 * sp_array_size * (sizeof(struct sp_array)));
1016 sp_array_size *= 2;
1017 }
1018 if (cc == sizeof buf) {
1019 if (zero_record(buf)) {
1020 if (amidst_data) {
1021 sparsearray[sparse_ind++].numbytes
1022 = numbytes;
1023 amidst_data = 0;
1024 }
1025 } else { /* !zero_record(buf) */
1026 if (amidst_data)
1027 numbytes += cc;
1028 else {
1029 amidst_data = 1;
1030 numbytes = cc;
1031 sparsearray[sparse_ind].offset
1032 = offset;
1033 }
1034 }
1035 } else if (cc < sizeof buf) {
1036 /* This has to be the last bit of the file, so this */
1037 /* is somewhat shorter than the above. */
1038 if (!zero_record(buf)) {
1039 if (!amidst_data) {
1040 amidst_data = 1;
1041 numbytes = cc;
1042 sparsearray[sparse_ind].offset
1043 = offset;
1044 } else
1045 numbytes += cc;
1046 }
1047 }
1048 offset += cc;
1049 clear_buffer(buf);
1050 }
1051 if (amidst_data)
1052 sparsearray[sparse_ind++].numbytes = numbytes;
1053 close(fd);
1054
1055 return sparse_ind - 1;
1056 }
1057
1058 /*
1059 * Just zeroes out the buffer so we don't confuse ourselves with leftover
1060 * data.
1061 */
1062 void
1063 clear_buffer(buf)
1064 char *buf;
1065 {
1066 register int i;
1067
1068 for (i = 0; i < RECORDSIZE; i++)
1069 buf[i] = '\0';
1070 }
1071
1072 #if 0 /* I'm leaving this as a monument to Joy Kendall, who wrote it -mib */
1073 /*
1074 * JK -
1075 * This routine takes a character array, and tells where within that array
1076 * the data can be found. It skips over any zeros, and sets the first
1077 * non-zero point in the array to be the "start", and continues until it
1078 * finds non-data again, which is marked as the "end." This routine is
1079 * mainly for 1) seeing how far into a file we must lseek to data, given
1080 * that we have a sparse file, and 2) determining the "real size" of the
1081 * file, i.e., the number of bytes in the sparse file that are data, as
1082 * opposed to the zeros we are trying to skip.
1083 */
1084 where_is_data(from, to, buffer)
1085 int *from,
1086 *to;
1087 char *buffer;
1088 {
1089 register int i = 0;
1090 register int save_to = *to;
1091 int amidst_data = 0;
1092
1093
1094 while (!buffer[i])
1095 i++;
1096 *from = i;
1097
1098 if (*from < 16) /* don't bother */
1099 *from = 0;
1100 /* keep going to make sure there isn't more real
1101 data in this record */
1102 while (i < RECORDSIZE) {
1103 if (!buffer[i]) {
1104 if (amidst_data) {
1105 save_to = i;
1106 amidst_data = 0;
1107 }
1108 i++;
1109 }
1110 else if (buffer[i]) {
1111 if (!amidst_data)
1112 amidst_data = 1;
1113 i++;
1114 }
1115 }
1116 if (i == RECORDSIZE)
1117 *to = i;
1118 else
1119 *to = save_to;
1120
1121 }
1122 #endif
1123
1124 /* Note that this routine is only called if zero_record returned true */
1125 #if 0 /* But we actually don't need it at all. */
1126 where_is_data (from, to, buffer)
1127 int *from, *to;
1128 char *buffer;
1129 {
1130 char *fp, *tp;
1131
1132 for (fp = buffer; ! *fp; fp++)
1133 ;
1134 for (tp = buffer + RECORDSIZE - 1; ! *tp; tp--)
1135 ;
1136 *from = fp - buffer;
1137 *to = tp - buffer + 1;
1138 }
1139 #endif
1140
1141
1142
1143 /*
1144 * Takes a recordful of data and basically cruises through it to see if
1145 * it's made *entirely* of zeros, returning a 0 the instant it finds
1146 * something that is a non-zero, i.e., useful data.
1147 */
1148 int
1149 zero_record(buffer)
1150 char *buffer;
1151 {
1152 register int i;
1153
1154 for (i = 0; i < RECORDSIZE; i++)
1155 if (buffer[i] != '\000')
1156 return 0;
1157 return 1;
1158 }
1159
1160 void
1161 find_new_file_size(filesize, highest_index)
1162 int *filesize;
1163 int highest_index;
1164 {
1165 register int i;
1166
1167 *filesize = 0;
1168 for (i = 0; sparsearray[i].numbytes && i <= highest_index; i++)
1169 *filesize += sparsearray[i].numbytes;
1170 }
1171
1172 /*
1173 * Make a header block for the file name whose stat info is st .
1174 * Return header pointer for success, NULL if the name is too long.
1175 */
1176 union record *
1177 start_header(name, st)
1178 char *name;
1179 register struct stat *st;
1180 {
1181 register union record *header;
1182
1183 header = (union record *) findrec();
1184 bzero(header->charptr, sizeof(*header)); /* XXX speed up */
1185
1186 /*
1187 * Check the file name and put it in the record.
1188 */
1189 if(!f_absolute_paths) {
1190 static int warned_once = 0;
1191 #ifdef __MSDOS__
1192 if(name[1]==':') {
1193 name+=2;
1194 if(!warned_once++)
1195 msg("Removing drive spec from names in the archive");
1196 }
1197 #endif
1198 while ('/' == *name) {
1199 name++; /* Force relative path */
1200 if (!warned_once++)
1201 msg("Removing leading / from absolute path names in the archive.");
1202 }
1203 }
1204 strncpy(header->header.name, name, NAMSIZ);
1205 if (header->header.name[NAMSIZ-1]) {
1206 /* char *mangled;*/
1207
1208 /* next_mangle(header->header.name); */
1209 add_mangle(name,header->header.name);
1210 msg("%s: is too long: mangling to %s", name, header->header.name);
1211 }
1212
1213 to_oct((long) (st->st_mode & 07777),
1214 8, header->header.mode);
1215 to_oct((long) st->st_uid, 8, header->header.uid);
1216 to_oct((long) st->st_gid, 8, header->header.gid);
1217 to_oct((long) st->st_size, 1+12, header->header.size);
1218 to_oct((long) st->st_mtime, 1+12, header->header.mtime);
1219 /* header->header.linkflag is left as null */
1220 if(f_gnudump) {
1221 to_oct((long) st->st_atime, 1+12, header->header.atime);
1222 to_oct((long) st->st_ctime, 1+12, header->header.ctime);
1223 }
1224
1225 #ifndef NONAMES
1226 /* Fill in new Unix Standard fields if desired. */
1227 if (f_standard) {
1228 header->header.linkflag = LF_NORMAL; /* New default */
1229 strcpy(header->header.magic, TMAGIC); /* Mark as Unix Std */
1230 finduname(header->header.uname, st->st_uid);
1231 findgname(header->header.gname, st->st_gid);
1232 }
1233 #endif
1234 return header;
1235 }
1236
1237 /*
1238 * Finish off a filled-in header block and write it out.
1239 * We also print the file name and/or full info if verbose is on.
1240 */
1241 void
1242 finish_header(header)
1243 register union record *header;
1244 {
1245 register int i, sum;
1246 register char *p;
1247
1248 bcopy(CHKBLANKS, header->header.chksum, sizeof(header->header.chksum));
1249
1250 sum = 0;
1251 p = header->charptr;
1252 for (i = sizeof(*header); --i >= 0; ) {
1253 /*
1254 * We can't use unsigned char here because of old compilers,
1255 * e.g. V7.
1256 */
1257 sum += 0xFF & *p++;
1258 }
1259
1260 /*
1261 * Fill in the checksum field. It's formatted differently
1262 * from the other fields: it has [6] digits, a null, then a
1263 * space -- rather than digits, a space, then a null.
1264 * We use to_oct then write the null in over to_oct's space.
1265 * The final space is already there, from checksumming, and
1266 * to_oct doesn't modify it.
1267 *
1268 * This is a fast way to do:
1269 * (void) sprintf(header->header.chksum, "%6o", sum);
1270 */
1271 to_oct((long) sum, 8, header->header.chksum);
1272 header->header.chksum[6] = '\0'; /* Zap the space */
1273
1274 userec(header);
1275
1276 if (f_verbose) {
1277 extern union record *head; /* Points to current tape header */
1278 extern int head_standard; /* Tape header is in ANSI format */
1279
1280 /* These globals are parameters to print_header, sigh */
1281 head = header;
1282 /* hstat is already set up */
1283 head_standard = f_standard;
1284 print_header();
1285 }
1286
1287 return;
1288 }
1289
1290
1291 /*
1292 * Quick and dirty octal conversion.
1293 * Converts long "value" into a "digs"-digit field at "where",
1294 * including a trailing space and room for a null. "digs"==3 means
1295 * 1 digit, a space, and room for a null.
1296 *
1297 * We assume the trailing null is already there and don't fill it in.
1298 * This fact is used by start_header and finish_header, so don't change it!
1299 *
1300 * This should be equivalent to:
1301 * (void) sprintf(where, "%*lo ", digs-2, value);
1302 * except that sprintf fills in the trailing null and we don't.
1303 */
1304 void
1305 to_oct(value, digs, where)
1306 register long value;
1307 register int digs;
1308 register char *where;
1309 {
1310
1311 --digs; /* Trailing null slot is left alone */
1312 where[--digs] = ' '; /* Put in the space, though */
1313
1314 /* Produce the digits -- at least one */
1315 do {
1316 where[--digs] = '0' + (char)(value & 7); /* one octal digit */
1317 value >>= 3;
1318 } while (digs > 0 && value != 0);
1319
1320 /* Leading spaces, if necessary */
1321 while (digs > 0)
1322 where[--digs] = ' ';
1323
1324 }
1325
1326
1327 /*
1328 * Write the EOT record(s).
1329 * We actually zero at least one record, through the end of the block.
1330 * Old tar writes garbage after two zeroed records -- and PDtar used to.
1331 */
1332 void
1333 write_eot()
1334 {
1335 union record *p;
1336 int bufsize;
1337
1338 p = findrec();
1339 if (p)
1340 {
1341 bufsize = endofrecs()->charptr - p->charptr;
1342 bzero(p->charptr, bufsize);
1343 userec(p);
1344 }
1345 }
1346
1347 /* Write a LF_LONGLINK or LF_LONGNAME record. */
1348 void
1349 write_long (p)
1350 {
1351 /* Link name won't fit, so we write
1352 an LF_LONGLINK record. */
1353 hstat.st_size = strlen (link_name) + 1;
1354 header = start_header ("././@LongLink", &hstat);
1355 header->header.linkflag = LF_NAMES;
1356 finish_header (header);
This page took 0.10357 seconds and 5 git commands to generate.