]>
Dogcows Code - chaz/tar/blob - src/create.c
1 /* Create a tar archive.
2 Copyright (C) 1985, 1992 Free Software Foundation
4 This file is part of GNU Tar.
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)
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.
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. */
21 * Create a tar archive.
23 * Written 25 Aug 1985 by John Gilmore, ihnp4!hoptoad!gnu.
29 #include <sys/types.h>
52 #if defined (_POSIX_VERSION)
63 #if defined(_POSIX_VERSION) || defined(DIRENT)
69 #define DP_NAMELEN(x) strlen((x)->d_name)
70 #endif /* _POSIX_VERSION or DIRENT */
71 #if !defined(_POSIX_VERSION) && !defined(DIRENT) && defined(BSD42)
73 #define DP_NAMELEN(x) (x)->d_namlen
74 #endif /* not _POSIX_VERSION and BSD42 */
77 #define DP_NAMELEN(x) (x)->d_namlen
80 #if defined(USG) && !defined(_POSIX_VERSION) && !defined(DIRENT)
82 #define DP_NAMELEN(x) strlen((x)->d_name)
83 #endif /* USG and not _POSIX_VERSION and not DIRENT */
85 extern struct stat hstat
; /* Stat struct corresponding */
93 extern struct name
*gnu_list_name
;
96 * If there are no symbolic links, there is no lstat(). Use stat().
102 extern void print_header ();
104 union record
*start_header ();
105 void blank_name_list ();
106 int check_exclude ();
109 void clear_buffer ();
110 void close_archive ();
111 void collect_and_sort_names ();
113 int deal_with_sparse ();
114 void find_new_file_size ();
115 void finish_header ();
116 int finish_sparse_file ();
119 int is_dot_or_dotdot ();
120 void open_archive ();
125 void write_dir_file ();
129 /* This code moved from tar.h since create.c is the only file that cares
130 about 'struct link's. This means that other files might not have to
131 include sys/types.h any more. */
142 struct link
*linklist
; /* Points to first link in list */
144 static nolinks
; /* Gets set if we run out of RAM */
147 * "Scratch" space to store the information about a sparse file before
148 * writing the info into the header or extended header
150 /* struct sp_array *sparsearray;*/
152 /* number of elts storable in the sparsearray */
153 /*int sparse_array_size = 10;*/
159 char *name_from_list ();
161 open_archive (0); /* Open for writing */
165 char *buf
= ck_malloc (PATH_MAX
);
168 collect_and_sort_names ();
170 while (p
= name_from_list ())
171 dump_file (p
, -1, 1);
172 /* if(!f_dironly) { */
174 while (p
= name_from_list ())
177 if (p
[strlen (p
) - 1] != '/')
179 bufp
= buf
+ strlen (buf
);
180 for (q
= gnu_list_name
->dir_contents
; q
&& *q
; q
+= strlen (q
) + 1)
184 strcpy (bufp
, q
+ 1);
185 dump_file (buf
, -1, 1);
196 dump_file (p
, -1, 1);
197 while (p
= name_next (1));
208 * Dump a single file. If it's a directory, recurse.
209 * Result is 1 for success, 0 for failure.
210 * Sets global "hstat" to stat() output for this file.
213 dump_file (p
, curdev
, toplevel
)
214 char *p
; /* File name to dump */
215 int curdev
; /* Device our parent dir was on */
216 int toplevel
; /* Whether we are a toplevel call */
218 union record
*header
;
220 extern char *save_name
; /* JF for multi-volume support */
221 extern long save_totsize
;
222 extern long save_sizeleft
;
225 extern time_t new_time
;
226 int critical_error
= 0;
227 struct utimbuf restore_times
;
228 /* int sparse_ind = 0;*/
231 if (f_confirm
&& !confirm ("add", p
))
235 * Use stat if following (rather than dumping) 4.2BSD's
236 * symbolic links. Otherwise, use lstat (which, on non-4.2
237 * systems, is #define'd to stat anyway.
239 #ifdef STX_HIDDEN /* AIX */
240 if (0 != f_follow_links
?
241 statx (p
, &hstat
, STATSIZE
, STX_HIDDEN
) :
242 statx (p
, &hstat
, STATSIZE
, STX_HIDDEN
| STX_LINK
))
244 if (0 != f_follow_links
? stat (p
, &hstat
) : lstat (p
, &hstat
))
248 msg_perror ("can't add file %s", p
);
250 if (!f_ignore_failed_read
|| critical_error
)
255 restore_times
.actime
= hstat
.st_atime
;
256 restore_times
.modtime
= hstat
.st_mtime
;
259 if (S_ISHIDDEN (hstat
.st_mode
))
261 char *new = (char *) alloca (strlen (p
) + 2);
271 /* See if we only want new files, and check if this one is too old to
272 put in the archive. */
275 && new_time
> hstat
.st_mtime
276 && !S_ISDIR (hstat
.st_mode
)
277 && (f_new_files
> 1 || new_time
> hstat
.st_ctime
))
281 msg ("%s: is unchanged; not dumped", p
);
287 /* See if we are trying to dump the archive */
288 if (ar_dev
&& hstat
.st_dev
== ar_dev
&& hstat
.st_ino
== ar_ino
)
290 msg ("%s is the archive; not dumped", p
);
295 * Check for multiple links.
297 * We maintain a list of all such files that we've written so
298 * far. Any time we see another, we check the list and
299 * avoid dumping the data again if we've done it once already.
301 if (hstat
.st_nlink
> 1
302 && (S_ISREG (hstat
.st_mode
)
304 || S_ISCTG (hstat
.st_mode
)
307 || S_ISCHR (hstat
.st_mode
)
310 || S_ISBLK (hstat
.st_mode
)
313 || S_ISFIFO (hstat
.st_mode
)
317 register struct link
*lp
;
319 /* First quick and dirty. Hashing, etc later FIXME */
320 for (lp
= linklist
; lp
; lp
= lp
->next
)
322 if (lp
->ino
== hstat
.st_ino
&&
323 lp
->dev
== hstat
.st_dev
)
325 char *link_name
= lp
->name
;
327 /* We found a link. */
328 while (!f_absolute_paths
&& *link_name
== '/')
330 static int link_warn
= 0;
334 msg ("Removing leading / from absolute links");
339 if (link_name
- lp
->name
>= NAMSIZ
)
340 write_long (link_name
, LF_LONGLINK
);
341 current_link_name
= link_name
;
344 header
= start_header (p
, &hstat
);
350 strncpy (header
->header
.arch_linkname
,
353 /* Force null truncated */
354 header
->header
.arch_linkname
[NAMSIZ
- 1] = 0;
356 header
->header
.linkflag
= LF_LINK
;
357 finish_header (header
);
358 /* FIXME: Maybe remove from list after all links found? */
361 if (unlink (p
) == -1)
362 msg_perror ("cannot remove %s", p
);
364 return; /* We dumped it */
368 /* Not found. Add it to the list of possible links. */
369 lp
= (struct link
*) malloc ((unsigned) (sizeof (struct link
) + strlen (p
)));
375 "no memory for links, they will be dumped as separate files");
379 lp
->ino
= hstat
.st_ino
;
380 lp
->dev
= hstat
.st_dev
;
381 strcpy (lp
->name
, p
);
387 * This is not a link to a previously dumped file, so dump it.
389 if (S_ISREG (hstat
.st_mode
)
391 || S_ISCTG (hstat
.st_mode
)
395 int f
; /* File descriptor */
398 register union record
*start
;
402 /* int end_nulls = 0; */
410 * JK - This is the test for sparseness: whether the
411 * "size" of the file matches the number of blocks
412 * allocated for it. If there is a smaller number
413 * of blocks that would be necessary to accommodate
414 * a file of this size, we have a sparse file, i.e.,
415 * at least one of those records in the file is just
418 #ifdef hpux /* Nice of HPUX to gratuitiously change it, huh? - mib */
419 if (hstat
.st_size
- (hstat
.st_blocks
* 1024) > 1024)
421 if (hstat
.st_size
- (hstat
.st_blocks
* RECORDSIZE
) > RECORDSIZE
)
424 int filesize
= hstat
.st_size
;
427 header
= start_header (p
, &hstat
);
433 header
->header
.linkflag
= LF_SPARSE
;
437 * Call the routine that figures out the
438 * layout of the sparse file in question.
439 * UPPERBOUND is the index of the last
440 * element of the "sparsearray," i.e.,
441 * the number of elements it needed to
445 upperbound
= deal_with_sparse (p
, header
);
448 * See if we'll need an extended header
451 if (upperbound
> SPARSE_IN_HDR
- 1)
452 header
->header
.isextended
++;
454 * We store the "real" file size so
455 * we can show that in case someone wants
456 * to list the archive, i.e., tar tvf <file>.
457 * It might be kind of disconcerting if the
458 * shrunken file size was the one that showed
461 to_oct ((long) hstat
.st_size
, 1 + 12,
462 header
->header
.realsize
);
465 * This will be the new "size" of the
466 * file, i.e., the size of the file
467 * minus the records of holes that we're
471 find_new_file_size (&filesize
, upperbound
);
472 hstat
.st_size
= filesize
;
473 to_oct ((long) filesize
, 1 + 12,
474 header
->header
.size
);
475 /* to_oct((long) end_nulls, 1+12,
476 header->header.ending_blanks);*/
478 for (i
= 0; i
< SPARSE_IN_HDR
; i
++)
480 if (!sparsearray
[i
].numbytes
)
482 to_oct (sparsearray
[i
].offset
, 1 + 12,
483 header
->header
.sp
[i
].offset
);
484 to_oct (sparsearray
[i
].numbytes
, 1 + 12,
485 header
->header
.sp
[i
].numbytes
);
491 upperbound
= SPARSE_IN_HDR
- 1;
494 sizeleft
= hstat
.st_size
;
495 /* Don't bother opening empty, world readable files. */
496 if (sizeleft
> 0 || 0444 != (0444 & hstat
.st_mode
))
498 f
= open (p
, O_RDONLY
| O_BINARY
);
507 /* If the file is sparse, we've already taken care of this */
510 header
= start_header (p
, &hstat
);
520 /* Mark contiguous files, if we support them */
521 if (f_standard
&& S_ISCTG (hstat
.st_mode
))
523 header
->header
.linkflag
= LF_CONTIG
;
526 isextended
= header
->header
.isextended
;
527 save_linkflag
= header
->header
.linkflag
;
528 finish_header (header
);
533 /* register union record *exhdr;*/
534 /* int arraybound = SPARSE_EXT_HDR;*/
535 /* static */ int index_offset
= SPARSE_IN_HDR
;
537 extend
:exhdr
= findrec ();
544 bzero (exhdr
->charptr
, RECORDSIZE
);
545 for (i
= 0; i
< SPARSE_EXT_HDR
; i
++)
547 if (i
+ index_offset
> upperbound
)
549 to_oct ((long) sparsearray
[i
+ index_offset
].numbytes
,
551 exhdr
->ext_hdr
.sp
[i
].numbytes
);
552 to_oct ((long) sparsearray
[i
+ index_offset
].offset
,
554 exhdr
->ext_hdr
.sp
[i
].offset
);
558 if (sum < upperbound)
560 if (index_offset
+ i
< upperbound
)
563 exhdr
->ext_hdr
.isextended
++;
568 if (save_linkflag
== LF_SPARSE
)
570 if (finish_sparse_file (f
, &sizeleft
, hstat
.st_size
, p
))
580 save_sizeleft
= sizeleft
;
581 save_totsize
= hstat
.st_size
;
585 bufsize
= endofrecs ()->charptr
- start
->charptr
;
587 if (sizeleft
< bufsize
)
589 /* Last read -- zero out area beyond */
590 bufsize
= (int) sizeleft
;
591 count
= bufsize
% RECORDSIZE
;
593 bzero (start
->charptr
+ sizeleft
,
594 (int) (RECORDSIZE
- count
));
596 count
= read (f
, start
->charptr
, bufsize
);
599 msg_perror ("read error at byte %ld, reading\
600 %d bytes, in file %s", hstat
.st_size
- sizeleft
, bufsize
, p
);
605 /* This is nonportable (the type of userec's arg). */
606 userec (start
+ (count
- 1) / RECORDSIZE
);
608 if (count
== bufsize
)
610 msg ("file %s shrunk by %d bytes, padding with zeros.", p
, sizeleft
);
611 goto padit
; /* Short read */
622 if (unlink (p
) == -1)
623 msg_perror ("cannot remove %s", p
);
625 if (f_atime_preserve
)
626 utime (p
, &restore_times
);
630 * File shrunk or gave error, pad out tape to match
631 * the size we specified in the header.
636 save_sizeleft
= sizeleft
;
638 bzero (start
->charptr
, RECORDSIZE
);
640 sizeleft
-= RECORDSIZE
;
646 if (f_atime_preserve
)
647 utime (p
, &restore_times
);
652 else if (S_ISLNK (hstat
.st_mode
))
655 char *buf
= alloca (PATH_MAX
+ 1);
657 size
= readlink (p
, buf
, PATH_MAX
+ 1);
662 write_long (buf
, LF_LONGLINK
);
663 current_link_name
= buf
;
665 hstat
.st_size
= 0; /* Force 0 size on symlink */
666 header
= start_header (p
, &hstat
);
672 strncpy (header
->header
.arch_linkname
, buf
, NAMSIZ
);
673 header
->header
.arch_linkname
[NAMSIZ
- 1] = '\0';
674 header
->header
.linkflag
= LF_SYMLINK
;
675 finish_header (header
); /* Nothing more to do to it */
678 if (unlink (p
) == -1)
679 msg_perror ("cannot remove %s", p
);
685 else if (S_ISDIR (hstat
.st_mode
))
688 register struct direct
*d
;
692 int our_device
= hstat
.st_dev
;
694 /* Build new prototype name */
696 buflen
= len
+ NAMSIZ
;
697 namebuf
= ck_malloc (buflen
+ 1);
698 strncpy (namebuf
, p
, buflen
);
699 while (len
>= 1 && '/' == namebuf
[len
- 1])
700 len
--; /* Delete trailing slashes */
701 namebuf
[len
++] = '/'; /* Now add exactly one back */
702 namebuf
[len
] = '\0'; /* Make sure null-terminated */
705 * Output directory header record with permissions
706 * FIXME, do this AFTER files, to avoid R/O dir problems?
707 * If old archive format, don't write record at all.
711 hstat
.st_size
= 0; /* Force 0 size on dir */
713 * If people could really read standard archives,
714 * this should be: (FIXME)
715 header = start_header(f_standard? p: namebuf, &hstat);
716 * but since they'd interpret LF_DIR records as
717 * regular files, we'd better put the / on the name.
719 header
= start_header (namebuf
, &hstat
);
723 goto badfile
; /* eg name too long */
727 header
->header
.linkflag
= LF_DUMPDIR
;
729 header
->header
.linkflag
= LF_DIR
;
731 /* If we're gnudumping, we aren't done yet so don't close it. */
733 finish_header (header
); /* Done with directory header */
745 buf
= gnu_list_name
->dir_contents
; /* FOO */
747 for (p_buf
= buf
; p_buf
&& *p_buf
;)
751 tmp
= strlen (p_buf
) + 1;
756 to_oct ((long) totsize
, 1 + 12, header
->header
.size
);
757 finish_header (header
);
765 save_sizeleft
= sizeleft
;
766 save_totsize
= totsize
;
769 bufsize
= endofrecs ()->charptr
- start
->charptr
;
770 if (sizeleft
< bufsize
)
773 count
= bufsize
% RECORDSIZE
;
775 bzero (start
->charptr
+ sizeleft
, RECORDSIZE
- count
);
777 bcopy (p_buf
, start
->charptr
, bufsize
);
780 userec (start
+ (bufsize
- 1) / RECORDSIZE
);
784 if (f_atime_preserve
)
785 utime (p
, &restore_times
);
789 /* Now output all the files in the directory */
792 return; /* Unless the cmdline said not to */
795 * See if we are crossing from one file system to another,
796 * and avoid doing so if the user only wants to dump one file system.
798 if (f_local_filesys
&& !toplevel
&& curdev
!= hstat
.st_dev
)
801 msg ("%s: is on a different filesystem; not dumped", p
);
812 msg_perror ("can't open directory %s", p
);
816 msg ("error opening directory %s",
822 /* Hack to remove "./" from the front of all the file names */
823 if (len
== 2 && namebuf
[0] == '.' && namebuf
[1] == '/')
826 /* Should speed this up by cd-ing into the dir, FIXME */
827 while (NULL
!= (d
= readdir (dirp
)))
830 if (is_dot_or_dotdot (d
->d_name
))
833 if (DP_NAMELEN (d
) + len
>= buflen
)
835 buflen
= len
+ DP_NAMELEN (d
);
836 namebuf
= ck_realloc (namebuf
, buflen
+ 1);
837 /* namebuf[len]='\0';
838 msg("file name %s%s too long",
842 strcpy (namebuf
+ len
, d
->d_name
);
843 if (f_exclude
&& check_exclude (namebuf
))
845 dump_file (namebuf
, our_device
, 0);
850 if (f_atime_preserve
)
851 utime (p
, &restore_times
);
856 else if (S_ISCHR (hstat
.st_mode
))
863 else if (S_ISBLK (hstat
.st_mode
))
869 /* Avoid screwy apollo lossage where S_IFIFO == S_IFSOCK */
870 #if (_ISP__M68K == 0) && (_ISP__A88K == 0) && defined(S_ISFIFO)
871 else if (S_ISFIFO (hstat
.st_mode
))
878 else if (S_ISSOCK (hstat
.st_mode
))
889 hstat
.st_size
= 0; /* Force 0 size */
890 header
= start_header (p
, &hstat
);
894 goto badfile
; /* eg name too long */
897 header
->header
.linkflag
= type
;
898 #if defined(S_IFBLK) || defined(S_IFCHR)
901 to_oct ((long) major (hstat
.st_rdev
), 8,
902 header
->header
.devmajor
);
903 to_oct ((long) minor (hstat
.st_rdev
), 8,
904 header
->header
.devminor
);
908 finish_header (header
);
911 if (unlink (p
) == -1)
912 msg_perror ("cannot remove %s", p
);
917 msg ("%s: Unknown file type; file ignored.", p
);
921 finish_sparse_file (fd
, sizeleft
, fullsize
, name
)
923 long *sizeleft
, fullsize
;
927 char tempbuf
[RECORDSIZE
];
928 int bufsize
, sparse_ind
= 0, count
;
933 while (*sizeleft
> 0)
936 bzero (start
->charptr
, RECORDSIZE
);
937 bufsize
= sparsearray
[sparse_ind
].numbytes
;
939 { /* we blew it, maybe */
940 msg ("Wrote %ld of %ld bytes to file %s",
941 fullsize
- *sizeleft
, fullsize
, name
);
944 pos
= lseek (fd
, sparsearray
[sparse_ind
++].offset
, 0);
946 * If the number of bytes to be written here exceeds
947 * the size of the temporary buffer, do it in steps.
949 while (bufsize
> RECORDSIZE
)
952 count = read(fd, start->charptr+amt_read, RECORDSIZE-amt_read);
953 bufsize -= RECORDSIZE - amt_read;
957 bzero(start->charptr, RECORDSIZE);
960 count
= read (fd
, start
->charptr
, RECORDSIZE
);
963 msg_perror ("read error at byte %ld, reading %d bytes, in file %s",
964 fullsize
- *sizeleft
, bufsize
, name
);
970 nwritten
+= RECORDSIZE
; /* XXX */
972 bzero (start
->charptr
, RECORDSIZE
);
976 clear_buffer (tempbuf
);
977 count
= read (fd
, tempbuf
, bufsize
);
978 bcopy (tempbuf
, start
->charptr
, RECORDSIZE
);
981 msg_perror ("read error at byte %ld, reading %d bytes, in file %s",
982 fullsize
- *sizeleft
, bufsize
, name
);
985 /* if (amt_read >= RECORDSIZE) {
987 userec(start+(count-1)/RECORDSIZE);
988 if (count != bufsize) {
989 msg("file %s shrunk by %d bytes, padding with zeros.", name, sizeleft);
994 amt_read += bufsize;*/
995 nwritten
+= count
; /* XXX */
1001 /* printf ("Amount actually written is (I hope) %d.\n", nwritten); */
1002 /* userec(start+(count-1)/RECORDSIZE);*/
1014 * Make room for our scratch space -- initially is 10 elts long
1016 sparsearray
= (struct sp_array
*) malloc (sp_array_size
* sizeof (struct sp_array
));
1017 for (i
= 0; i
< sp_array_size
; i
++)
1019 sparsearray
[i
].offset
= 0;
1020 sparsearray
[i
].numbytes
= 0;
1027 * Okay, we've got a sparse file on our hands -- now, what we need to do is
1028 * make a pass through the file and carefully note where any data is, i.e.,
1029 * we want to find how far into the file each instance of data is, and how
1030 * many bytes are there. We store this information in the sparsearray,
1031 * which will later be translated into header information. For now, we use
1032 * the sparsearray as convenient storage.
1034 * As a side note, this routine is a mess. If I could have found a cleaner
1035 * way to do it, I would have. If anyone wants to find a nicer way to do
1039 /* There is little point in trimming small amounts of null data at the */
1040 /* head and tail of blocks -- it's ok if we only avoid dumping blocks */
1041 /* of complete null data */
1043 deal_with_sparse (name
, header
, nulls_at_end
)
1045 union record
*header
;
1050 /* long save_offset;*/
1052 /* int current_size = hstat.st_size;*/
1053 int sparse_ind
= 0, cc
;
1054 char buf
[RECORDSIZE
];
1056 int read_last_data
= 0; /* did we just read the last record? */
1058 int amidst_data
= 0;
1060 header
->header
.isextended
= 0;
1062 * Can't open the file -- this problem will be caught later on,
1065 if ((fd
= open (name
, O_RDONLY
)) < 0)
1068 init_sparsearray ();
1071 while ((cc
= read (fd
, buf
, sizeof buf
)) != 0)
1074 if (sparse_ind
> sp_array_size
- 1)
1078 * realloc the scratch area, since we've run out of room --
1080 sparsearray
= (struct sp_array
*)
1081 realloc (sparsearray
,
1082 2 * sp_array_size
* (sizeof (struct sp_array
)));
1085 if (cc
== sizeof buf
)
1087 if (zero_record (buf
))
1091 sparsearray
[sparse_ind
++].numbytes
1097 { /* !zero_record(buf) */
1104 sparsearray
[sparse_ind
].offset
1109 else if (cc
< sizeof buf
)
1111 /* This has to be the last bit of the file, so this */
1112 /* is somewhat shorter than the above. */
1113 if (!zero_record (buf
))
1119 sparsearray
[sparse_ind
].offset
1130 sparsearray
[sparse_ind
++].numbytes
= numbytes
;
1133 return sparse_ind
- 1;
1137 * Just zeroes out the buffer so we don't confuse ourselves with leftover
1146 for (i
= 0; i
< RECORDSIZE
; i
++)
1150 #if 0 /* I'm leaving this as a monument to Joy Kendall, who wrote it -mib */
1153 * This routine takes a character array, and tells where within that array
1154 * the data can be found. It skips over any zeros, and sets the first
1155 * non-zero point in the array to be the "start", and continues until it
1156 * finds non-data again, which is marked as the "end." This routine is
1157 * mainly for 1) seeing how far into a file we must lseek to data, given
1158 * that we have a sparse file, and 2) determining the "real size" of the
1159 * file, i.e., the number of bytes in the sparse file that are data, as
1160 * opposed to the zeros we are trying to skip.
1162 where_is_data (from
, to
, buffer
)
1167 register int save_to
= *to
;
1168 int amidst_data
= 0;
1175 if (*from
< 16) /* don't bother */
1177 /* keep going to make sure there isn't more real
1178 data in this record */
1179 while (i
< RECORDSIZE
)
1197 if (i
== RECORDSIZE
)
1206 /* Note that this routine is only called if zero_record returned true */
1207 #if 0 /* But we actually don't need it at all. */
1208 where_is_data (from
, to
, buffer
)
1214 for (fp
= buffer
; !*fp
; fp
++)
1216 for (tp
= buffer
+ RECORDSIZE
- 1; !*tp
; tp
--)
1218 *from
= fp
- buffer
;
1219 *to
= tp
- buffer
+ 1;
1227 * Takes a recordful of data and basically cruises through it to see if
1228 * it's made *entirely* of zeros, returning a 0 the instant it finds
1229 * something that is a non-zero, i.e., useful data.
1232 zero_record (buffer
)
1237 for (i
= 0; i
< RECORDSIZE
; i
++)
1238 if (buffer
[i
] != '\000')
1244 find_new_file_size (filesize
, highest_index
)
1251 for (i
= 0; sparsearray
[i
].numbytes
&& i
<= highest_index
; i
++)
1252 *filesize
+= sparsearray
[i
].numbytes
;
1256 * Make a header block for the file name whose stat info is st .
1257 * Return header pointer for success, NULL if the name is too long.
1260 start_header (name
, st
)
1262 register struct stat
*st
;
1264 register union record
*header
;
1266 if (strlen (name
) >= NAMSIZ
)
1267 write_long (name
, LF_LONGNAME
);
1269 header
= (union record
*) findrec ();
1270 bzero (header
->charptr
, sizeof (*header
)); /* XXX speed up */
1273 * Check the file name and put it in the record.
1275 if (!f_absolute_paths
)
1277 static int warned_once
= 0;
1283 msg ("Removing drive spec from names in the archive");
1286 while ('/' == *name
)
1288 name
++; /* Force relative path */
1290 msg ("Removing leading / from absolute path names in the archive.");
1293 current_file_name
= name
;
1294 strncpy (header
->header
.arch_name
, name
, NAMSIZ
);
1295 header
->header
.arch_name
[NAMSIZ
- 1] = '\0';
1297 to_oct ((long) (st
->st_mode
& 07777),
1298 8, header
->header
.mode
);
1299 to_oct ((long) st
->st_uid
, 8, header
->header
.uid
);
1300 to_oct ((long) st
->st_gid
, 8, header
->header
.gid
);
1301 to_oct ((long) st
->st_size
, 1 + 12, header
->header
.size
);
1302 to_oct ((long) st
->st_mtime
, 1 + 12, header
->header
.mtime
);
1303 /* header->header.linkflag is left as null */
1306 to_oct ((long) st
->st_atime
, 1 + 12, header
->header
.atime
);
1307 to_oct ((long) st
->st_ctime
, 1 + 12, header
->header
.ctime
);
1311 /* Fill in new Unix Standard fields if desired. */
1314 header
->header
.linkflag
= LF_NORMAL
; /* New default */
1315 strcpy (header
->header
.magic
, TMAGIC
); /* Mark as Unix Std */
1316 finduname (header
->header
.uname
, st
->st_uid
);
1317 findgname (header
->header
.gname
, st
->st_gid
);
1324 * Finish off a filled-in header block and write it out.
1325 * We also print the file name and/or full info if verbose is on.
1328 finish_header (header
)
1329 register union record
*header
;
1331 register int i
, sum
;
1334 bcopy (CHKBLANKS
, header
->header
.chksum
, sizeof (header
->header
.chksum
));
1337 p
= header
->charptr
;
1338 for (i
= sizeof (*header
); --i
>= 0;)
1341 * We can't use unsigned char here because of old compilers,
1348 * Fill in the checksum field. It's formatted differently
1349 * from the other fields: it has [6] digits, a null, then a
1350 * space -- rather than digits, a space, then a null.
1351 * We use to_oct then write the null in over to_oct's space.
1352 * The final space is already there, from checksumming, and
1353 * to_oct doesn't modify it.
1355 * This is a fast way to do:
1356 * (void) sprintf(header->header.chksum, "%6o", sum);
1358 to_oct ((long) sum
, 8, header
->header
.chksum
);
1359 header
->header
.chksum
[6] = '\0'; /* Zap the space */
1365 extern union record
*head
;/* Points to current tape header */
1366 extern int head_standard
; /* Tape header is in ANSI format */
1368 /* These globals are parameters to print_header, sigh */
1370 /* hstat is already set up */
1371 head_standard
= f_standard
;
1380 * Quick and dirty octal conversion.
1381 * Converts long "value" into a "digs"-digit field at "where",
1382 * including a trailing space and room for a null. "digs"==3 means
1383 * 1 digit, a space, and room for a null.
1385 * We assume the trailing null is already there and don't fill it in.
1386 * This fact is used by start_header and finish_header, so don't change it!
1388 * This should be equivalent to:
1389 * (void) sprintf(where, "%*lo ", digs-2, value);
1390 * except that sprintf fills in the trailing null and we don't.
1393 to_oct (value
, digs
, where
)
1394 register long value
;
1396 register char *where
;
1399 --digs
; /* Trailing null slot is left alone */
1400 where
[--digs
] = ' '; /* Put in the space, though */
1402 /* Produce the digits -- at least one */
1405 where
[--digs
] = '0' + (char) (value
& 7); /* one octal digit */
1408 while (digs
> 0 && value
!= 0);
1410 /* Leading spaces, if necessary */
1412 where
[--digs
] = ' ';
1418 * Write the EOT record(s).
1419 * We actually zero at least one record, through the end of the block.
1420 * Old tar writes garbage after two zeroed records -- and PDtar used to.
1431 bufsize
= endofrecs ()->charptr
- p
->charptr
;
1432 bzero (p
->charptr
, bufsize
);
1437 /* Write a LF_LONGLINK or LF_LONGNAME record. */
1438 write_long (p
, type
)
1442 int size
= strlen (p
) + 1;
1444 union record
*header
;
1448 bzero (&foo
, sizeof foo
);
1451 header
= start_header ("././@LongLink", &foo
);
1452 header
->header
.linkflag
= type
;
1453 finish_header (header
);
1455 header
= findrec ();
1457 bufsize
= endofrecs ()->charptr
- header
->charptr
;
1459 while (bufsize
< size
)
1461 bcopy (p
, header
->charptr
, bufsize
);
1464 userec (header
+ (bufsize
- 1) / RECORDSIZE
);
1465 header
= findrec ();
1466 bufsize
= endofrecs ()->charptr
- header
->charptr
;
1468 bcopy (p
, header
->charptr
, size
);
1469 bzero (header
->charptr
+ size
, bufsize
- size
);
1470 userec (header
+ (size
- 1) / RECORDSIZE
);
This page took 0.093916 seconds and 4 git commands to generate.