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