]>
Dogcows Code - chaz/tar/blob - src/create.c
639507c3da1d711a605dca6212af155456836987
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 ();
130 /* This code moved from tar.h since create.c is the only file that cares
131 about 'struct link's. This means that other files might not have to
132 include sys/types.h any more. */
143 struct link
*linklist
; /* Points to first link in list */
145 static nolinks
; /* Gets set if we run out of RAM */
148 * "Scratch" space to store the information about a sparse file before
149 * writing the info into the header or extended header
151 /* struct sp_array *sparsearray;*/
153 /* number of elts storable in the sparsearray */
154 /*int sparse_array_size = 10;*/
160 char *name_from_list ();
162 open_archive (0); /* Open for writing */
166 char *buf
= ck_malloc (PATH_MAX
);
169 collect_and_sort_names ();
171 while (p
= name_from_list ())
172 dump_file (p
, -1, 1);
173 /* if(!f_dironly) { */
175 while (p
= name_from_list ())
178 if (p
[strlen (p
) - 1] != '/')
180 bufp
= buf
+ strlen (buf
);
181 for (q
= gnu_list_name
->dir_contents
; q
&& *q
; q
+= strlen (q
) + 1)
185 strcpy (bufp
, q
+ 1);
186 dump_file (buf
, -1, 1);
197 dump_file (p
, -1, 1);
198 while (p
= name_next (1));
209 * Dump a single file. If it's a directory, recurse.
210 * Result is 1 for success, 0 for failure.
211 * Sets global "hstat" to stat() output for this file.
214 dump_file (p
, curdev
, toplevel
)
215 char *p
; /* File name to dump */
216 int curdev
; /* Device our parent dir was on */
217 int toplevel
; /* Whether we are a toplevel call */
219 union record
*header
;
221 extern char *save_name
; /* JF for multi-volume support */
222 extern long save_totsize
;
223 extern long save_sizeleft
;
226 extern time_t new_time
;
227 int critical_error
= 0;
228 struct utimbuf restore_times
;
229 /* int sparse_ind = 0;*/
232 if (f_confirm
&& !confirm ("add", p
))
236 * Use stat if following (rather than dumping) 4.2BSD's
237 * symbolic links. Otherwise, use lstat (which, on non-4.2
238 * systems, is #define'd to stat anyway.
240 #ifdef STX_HIDDEN /* AIX */
241 if (0 != f_follow_links
?
242 statx (p
, &hstat
, STATSIZE
, STX_HIDDEN
) :
243 statx (p
, &hstat
, STATSIZE
, STX_HIDDEN
| STX_LINK
))
245 if (0 != f_follow_links
? stat (p
, &hstat
) : lstat (p
, &hstat
))
249 msg_perror ("can't add file %s", p
);
251 if (!f_ignore_failed_read
|| critical_error
)
256 restore_times
.actime
= hstat
.st_atime
;
257 restore_times
.modtime
= hstat
.st_mtime
;
260 if (S_ISHIDDEN (hstat
.st_mode
))
262 char *new = (char *) alloca (strlen (p
) + 2);
272 /* See if we only want new files, and check if this one is too old to
273 put in the archive. */
276 && new_time
> hstat
.st_mtime
277 && !S_ISDIR (hstat
.st_mode
)
278 && (f_new_files
> 1 || new_time
> hstat
.st_ctime
))
282 msg ("%s: is unchanged; not dumped", p
);
288 /* See if we are trying to dump the archive */
289 if (ar_dev
&& hstat
.st_dev
== ar_dev
&& hstat
.st_ino
== ar_ino
)
291 msg ("%s is the archive; not dumped", p
);
296 * Check for multiple links.
298 * We maintain a list of all such files that we've written so
299 * far. Any time we see another, we check the list and
300 * avoid dumping the data again if we've done it once already.
302 if (hstat
.st_nlink
> 1
303 && (S_ISREG (hstat
.st_mode
)
305 || S_ISCTG (hstat
.st_mode
)
308 || S_ISCHR (hstat
.st_mode
)
311 || S_ISBLK (hstat
.st_mode
)
314 || S_ISFIFO (hstat
.st_mode
)
318 register struct link
*lp
;
320 /* First quick and dirty. Hashing, etc later FIXME */
321 for (lp
= linklist
; lp
; lp
= lp
->next
)
323 if (lp
->ino
== hstat
.st_ino
&&
324 lp
->dev
== hstat
.st_dev
)
326 char *link_name
= lp
->name
;
328 /* We found a link. */
329 while (!f_absolute_paths
&& *link_name
== '/')
331 static int link_warn
= 0;
335 msg ("Removing leading / from absolute links");
340 if (link_name
- lp
->name
>= NAMSIZ
)
341 write_long (link_name
, LF_LONGLINK
);
342 current_link_name
= link_name
;
345 header
= start_header (p
, &hstat
);
351 strncpy (header
->header
.arch_linkname
,
354 /* Force null truncated */
355 header
->header
.arch_linkname
[NAMSIZ
- 1] = 0;
357 header
->header
.linkflag
= LF_LINK
;
358 finish_header (header
);
359 /* FIXME: Maybe remove from list after all links found? */
362 if (unlink (p
) == -1)
363 msg_perror ("cannot remove %s", p
);
365 return; /* We dumped it */
369 /* Not found. Add it to the list of possible links. */
370 lp
= (struct link
*) malloc ((unsigned) (sizeof (struct link
) + strlen (p
)));
376 "no memory for links, they will be dumped as separate files");
380 lp
->ino
= hstat
.st_ino
;
381 lp
->dev
= hstat
.st_dev
;
382 strcpy (lp
->name
, p
);
388 * This is not a link to a previously dumped file, so dump it.
390 if (S_ISREG (hstat
.st_mode
)
392 || S_ISCTG (hstat
.st_mode
)
396 int f
; /* File descriptor */
399 register union record
*start
;
403 /* int end_nulls = 0; */
411 * JK - This is the test for sparseness: whether the
412 * "size" of the file matches the number of blocks
413 * allocated for it. If there is a smaller number
414 * of blocks that would be necessary to accommodate
415 * a file of this size, we have a sparse file, i.e.,
416 * at least one of those records in the file is just
419 #ifdef hpux /* Nice of HPUX to gratuitiously change it, huh? - mib */
420 if (hstat
.st_size
- (hstat
.st_blocks
* 1024) > 1024)
422 if (hstat
.st_size
- (hstat
.st_blocks
* RECORDSIZE
) > RECORDSIZE
)
425 int filesize
= hstat
.st_size
;
428 header
= start_header (p
, &hstat
);
434 header
->header
.linkflag
= LF_SPARSE
;
438 * Call the routine that figures out the
439 * layout of the sparse file in question.
440 * UPPERBOUND is the index of the last
441 * element of the "sparsearray," i.e.,
442 * the number of elements it needed to
446 upperbound
= deal_with_sparse (p
, header
);
449 * See if we'll need an extended header
452 if (upperbound
> SPARSE_IN_HDR
- 1)
453 header
->header
.isextended
++;
455 * We store the "real" file size so
456 * we can show that in case someone wants
457 * to list the archive, i.e., tar tvf <file>.
458 * It might be kind of disconcerting if the
459 * shrunken file size was the one that showed
462 to_oct ((long) hstat
.st_size
, 1 + 12,
463 header
->header
.realsize
);
466 * This will be the new "size" of the
467 * file, i.e., the size of the file
468 * minus the records of holes that we're
472 find_new_file_size (&filesize
, upperbound
);
473 hstat
.st_size
= filesize
;
474 to_oct ((long) filesize
, 1 + 12,
475 header
->header
.size
);
476 /* to_oct((long) end_nulls, 1+12,
477 header->header.ending_blanks);*/
479 for (i
= 0; i
< SPARSE_IN_HDR
; i
++)
481 if (!sparsearray
[i
].numbytes
)
483 to_oct (sparsearray
[i
].offset
, 1 + 12,
484 header
->header
.sp
[i
].offset
);
485 to_oct (sparsearray
[i
].numbytes
, 1 + 12,
486 header
->header
.sp
[i
].numbytes
);
492 upperbound
= SPARSE_IN_HDR
- 1;
495 sizeleft
= hstat
.st_size
;
496 /* Don't bother opening empty, world readable files. */
497 if (sizeleft
> 0 || 0444 != (0444 & hstat
.st_mode
))
499 f
= open (p
, O_RDONLY
| O_BINARY
);
508 /* If the file is sparse, we've already taken care of this */
511 header
= start_header (p
, &hstat
);
521 /* Mark contiguous files, if we support them */
522 if (f_standard
&& S_ISCTG (hstat
.st_mode
))
524 header
->header
.linkflag
= LF_CONTIG
;
527 isextended
= header
->header
.isextended
;
528 save_linkflag
= header
->header
.linkflag
;
529 finish_header (header
);
534 /* register union record *exhdr;*/
535 /* int arraybound = SPARSE_EXT_HDR;*/
536 /* static */ int index_offset
= SPARSE_IN_HDR
;
538 extend
:exhdr
= findrec ();
545 bzero (exhdr
->charptr
, RECORDSIZE
);
546 for (i
= 0; i
< SPARSE_EXT_HDR
; i
++)
548 if (i
+ index_offset
> upperbound
)
550 to_oct ((long) sparsearray
[i
+ index_offset
].numbytes
,
552 exhdr
->ext_hdr
.sp
[i
].numbytes
);
553 to_oct ((long) sparsearray
[i
+ index_offset
].offset
,
555 exhdr
->ext_hdr
.sp
[i
].offset
);
559 if (sum < upperbound)
561 if (index_offset
+ i
< upperbound
)
564 exhdr
->ext_hdr
.isextended
++;
569 if (save_linkflag
== LF_SPARSE
)
571 if (finish_sparse_file (f
, &sizeleft
, hstat
.st_size
, p
))
581 save_sizeleft
= sizeleft
;
582 save_totsize
= hstat
.st_size
;
586 bufsize
= endofrecs ()->charptr
- start
->charptr
;
588 if (sizeleft
< bufsize
)
590 /* Last read -- zero out area beyond */
591 bufsize
= (int) sizeleft
;
592 count
= bufsize
% RECORDSIZE
;
594 bzero (start
->charptr
+ sizeleft
,
595 (int) (RECORDSIZE
- count
));
597 count
= read (f
, start
->charptr
, bufsize
);
600 msg_perror ("read error at byte %ld, reading\
601 %d bytes, in file %s", hstat
.st_size
- sizeleft
, bufsize
, p
);
606 /* This is nonportable (the type of userec's arg). */
607 userec (start
+ (count
- 1) / RECORDSIZE
);
609 if (count
== bufsize
)
611 msg ("file %s shrunk by %d bytes, padding with zeros.", p
, sizeleft
);
612 goto padit
; /* Short read */
623 if (unlink (p
) == -1)
624 msg_perror ("cannot remove %s", p
);
626 if (f_atime_preserve
)
627 utime (p
, &restore_times
);
631 * File shrunk or gave error, pad out tape to match
632 * the size we specified in the header.
637 save_sizeleft
= sizeleft
;
639 bzero (start
->charptr
, RECORDSIZE
);
641 sizeleft
-= RECORDSIZE
;
647 if (f_atime_preserve
)
648 utime (p
, &restore_times
);
653 else if (S_ISLNK (hstat
.st_mode
))
656 char *buf
= alloca (PATH_MAX
+ 1);
658 size
= readlink (p
, buf
, PATH_MAX
+ 1);
663 write_long (buf
, LF_LONGLINK
);
664 current_link_name
= buf
;
666 hstat
.st_size
= 0; /* Force 0 size on symlink */
667 header
= start_header (p
, &hstat
);
673 strncpy (header
->header
.arch_linkname
, buf
, NAMSIZ
);
674 header
->header
.arch_linkname
[NAMSIZ
- 1] = '\0';
675 header
->header
.linkflag
= LF_SYMLINK
;
676 finish_header (header
); /* Nothing more to do to it */
679 if (unlink (p
) == -1)
680 msg_perror ("cannot remove %s", p
);
686 else if (S_ISDIR (hstat
.st_mode
))
689 register struct direct
*d
;
693 int our_device
= hstat
.st_dev
;
695 /* Build new prototype name */
697 buflen
= len
+ NAMSIZ
;
698 namebuf
= ck_malloc (buflen
+ 1);
699 strncpy (namebuf
, p
, buflen
);
700 while (len
>= 1 && '/' == namebuf
[len
- 1])
701 len
--; /* Delete trailing slashes */
702 namebuf
[len
++] = '/'; /* Now add exactly one back */
703 namebuf
[len
] = '\0'; /* Make sure null-terminated */
706 * Output directory header record with permissions
707 * FIXME, do this AFTER files, to avoid R/O dir problems?
708 * If old archive format, don't write record at all.
712 hstat
.st_size
= 0; /* Force 0 size on dir */
714 * If people could really read standard archives,
715 * this should be: (FIXME)
716 header = start_header(f_standard? p: namebuf, &hstat);
717 * but since they'd interpret LF_DIR records as
718 * regular files, we'd better put the / on the name.
720 header
= start_header (namebuf
, &hstat
);
724 goto badfile
; /* eg name too long */
728 header
->header
.linkflag
= LF_DUMPDIR
;
730 header
->header
.linkflag
= LF_DIR
;
732 /* If we're gnudumping, we aren't done yet so don't close it. */
734 finish_header (header
); /* Done with directory header */
746 buf
= gnu_list_name
->dir_contents
; /* FOO */
748 for (p_buf
= buf
; p_buf
&& *p_buf
;)
752 tmp
= strlen (p_buf
) + 1;
757 to_oct ((long) totsize
, 1 + 12, header
->header
.size
);
758 finish_header (header
);
766 save_sizeleft
= sizeleft
;
767 save_totsize
= totsize
;
770 bufsize
= endofrecs ()->charptr
- start
->charptr
;
771 if (sizeleft
< bufsize
)
774 count
= bufsize
% RECORDSIZE
;
776 bzero (start
->charptr
+ sizeleft
, RECORDSIZE
- count
);
778 bcopy (p_buf
, start
->charptr
, bufsize
);
781 userec (start
+ (bufsize
- 1) / RECORDSIZE
);
785 if (f_atime_preserve
)
786 utime (p
, &restore_times
);
790 /* Now output all the files in the directory */
793 return; /* Unless the cmdline said not to */
796 * See if we are crossing from one file system to another,
797 * and avoid doing so if the user only wants to dump one file system.
799 if (f_local_filesys
&& !toplevel
&& curdev
!= hstat
.st_dev
)
802 msg ("%s: is on a different filesystem; not dumped", p
);
813 msg_perror ("can't open directory %s", p
);
817 msg ("error opening directory %s",
823 /* Hack to remove "./" from the front of all the file names */
824 if (len
== 2 && namebuf
[0] == '.' && namebuf
[1] == '/')
827 /* Should speed this up by cd-ing into the dir, FIXME */
828 while (NULL
!= (d
= readdir (dirp
)))
831 if (is_dot_or_dotdot (d
->d_name
))
834 if (DP_NAMELEN (d
) + len
>= buflen
)
836 buflen
= len
+ DP_NAMELEN (d
);
837 namebuf
= ck_realloc (namebuf
, buflen
+ 1);
838 /* namebuf[len]='\0';
839 msg("file name %s%s too long",
843 strcpy (namebuf
+ len
, d
->d_name
);
844 if (f_exclude
&& check_exclude (namebuf
))
846 dump_file (namebuf
, our_device
, 0);
851 if (f_atime_preserve
)
852 utime (p
, &restore_times
);
857 else if (S_ISCHR (hstat
.st_mode
))
864 else if (S_ISBLK (hstat
.st_mode
))
870 /* Avoid screwy apollo lossage where S_IFIFO == S_IFSOCK */
871 #if (_ISP__M68K == 0) && (_ISP__A88K == 0) && defined(S_ISFIFO)
872 else if (S_ISFIFO (hstat
.st_mode
))
879 else if (S_ISSOCK (hstat
.st_mode
))
890 hstat
.st_size
= 0; /* Force 0 size */
891 header
= start_header (p
, &hstat
);
895 goto badfile
; /* eg name too long */
898 header
->header
.linkflag
= type
;
899 #if defined(S_IFBLK) || defined(S_IFCHR)
902 to_oct ((long) major (hstat
.st_rdev
), 8,
903 header
->header
.devmajor
);
904 to_oct ((long) minor (hstat
.st_rdev
), 8,
905 header
->header
.devminor
);
909 finish_header (header
);
912 if (unlink (p
) == -1)
913 msg_perror ("cannot remove %s", p
);
918 msg ("%s: Unknown file type; file ignored.", p
);
922 finish_sparse_file (fd
, sizeleft
, fullsize
, name
)
924 long *sizeleft
, fullsize
;
928 char tempbuf
[RECORDSIZE
];
929 int bufsize
, sparse_ind
= 0, count
;
934 while (*sizeleft
> 0)
937 bzero (start
->charptr
, RECORDSIZE
);
938 bufsize
= sparsearray
[sparse_ind
].numbytes
;
940 { /* we blew it, maybe */
941 msg ("Wrote %ld of %ld bytes to file %s",
942 fullsize
- *sizeleft
, fullsize
, name
);
945 pos
= lseek (fd
, sparsearray
[sparse_ind
++].offset
, 0);
947 * If the number of bytes to be written here exceeds
948 * the size of the temporary buffer, do it in steps.
950 while (bufsize
> RECORDSIZE
)
953 count = read(fd, start->charptr+amt_read, RECORDSIZE-amt_read);
954 bufsize -= RECORDSIZE - amt_read;
958 bzero(start->charptr, RECORDSIZE);
961 count
= read (fd
, start
->charptr
, RECORDSIZE
);
964 msg_perror ("read error at byte %ld, reading %d bytes, in file %s",
965 fullsize
- *sizeleft
, bufsize
, name
);
971 nwritten
+= RECORDSIZE
; /* XXX */
973 bzero (start
->charptr
, RECORDSIZE
);
977 clear_buffer (tempbuf
);
978 count
= read (fd
, tempbuf
, bufsize
);
979 bcopy (tempbuf
, start
->charptr
, RECORDSIZE
);
982 msg_perror ("read error at byte %ld, reading %d bytes, in file %s",
983 fullsize
- *sizeleft
, bufsize
, name
);
986 /* if (amt_read >= RECORDSIZE) {
988 userec(start+(count-1)/RECORDSIZE);
989 if (count != bufsize) {
990 msg("file %s shrunk by %d bytes, padding with zeros.", name, sizeleft);
995 amt_read += bufsize;*/
996 nwritten
+= count
; /* XXX */
1002 /* printf ("Amount actually written is (I hope) %d.\n", nwritten); */
1003 /* userec(start+(count-1)/RECORDSIZE);*/
1015 * Make room for our scratch space -- initially is 10 elts long
1017 sparsearray
= (struct sp_array
*) malloc (sp_array_size
* sizeof (struct sp_array
));
1018 for (i
= 0; i
< sp_array_size
; i
++)
1020 sparsearray
[i
].offset
= 0;
1021 sparsearray
[i
].numbytes
= 0;
1028 * Okay, we've got a sparse file on our hands -- now, what we need to do is
1029 * make a pass through the file and carefully note where any data is, i.e.,
1030 * we want to find how far into the file each instance of data is, and how
1031 * many bytes are there. We store this information in the sparsearray,
1032 * which will later be translated into header information. For now, we use
1033 * the sparsearray as convenient storage.
1035 * As a side note, this routine is a mess. If I could have found a cleaner
1036 * way to do it, I would have. If anyone wants to find a nicer way to do
1040 /* There is little point in trimming small amounts of null data at the */
1041 /* head and tail of blocks -- it's ok if we only avoid dumping blocks */
1042 /* of complete null data */
1044 deal_with_sparse (name
, header
, nulls_at_end
)
1046 union record
*header
;
1051 /* long save_offset;*/
1053 /* int current_size = hstat.st_size;*/
1054 int sparse_ind
= 0, cc
;
1055 char buf
[RECORDSIZE
];
1057 int read_last_data
= 0; /* did we just read the last record? */
1059 int amidst_data
= 0;
1061 header
->header
.isextended
= 0;
1063 * Can't open the file -- this problem will be caught later on,
1066 if ((fd
= open (name
, O_RDONLY
)) < 0)
1069 init_sparsearray ();
1072 while ((cc
= read (fd
, buf
, sizeof buf
)) != 0)
1075 if (sparse_ind
> sp_array_size
- 1)
1079 * realloc the scratch area, since we've run out of room --
1081 sparsearray
= (struct sp_array
*)
1082 realloc (sparsearray
,
1083 2 * sp_array_size
* (sizeof (struct sp_array
)));
1086 if (cc
== sizeof buf
)
1088 if (zero_record (buf
))
1092 sparsearray
[sparse_ind
++].numbytes
1098 { /* !zero_record(buf) */
1105 sparsearray
[sparse_ind
].offset
1110 else if (cc
< sizeof buf
)
1112 /* This has to be the last bit of the file, so this */
1113 /* is somewhat shorter than the above. */
1114 if (!zero_record (buf
))
1120 sparsearray
[sparse_ind
].offset
1131 sparsearray
[sparse_ind
++].numbytes
= numbytes
;
1134 return sparse_ind
- 1;
1138 * Just zeroes out the buffer so we don't confuse ourselves with leftover
1147 for (i
= 0; i
< RECORDSIZE
; i
++)
1151 #if 0 /* I'm leaving this as a monument to Joy Kendall, who wrote it -mib */
1154 * This routine takes a character array, and tells where within that array
1155 * the data can be found. It skips over any zeros, and sets the first
1156 * non-zero point in the array to be the "start", and continues until it
1157 * finds non-data again, which is marked as the "end." This routine is
1158 * mainly for 1) seeing how far into a file we must lseek to data, given
1159 * that we have a sparse file, and 2) determining the "real size" of the
1160 * file, i.e., the number of bytes in the sparse file that are data, as
1161 * opposed to the zeros we are trying to skip.
1163 where_is_data (from
, to
, buffer
)
1168 register int save_to
= *to
;
1169 int amidst_data
= 0;
1176 if (*from
< 16) /* don't bother */
1178 /* keep going to make sure there isn't more real
1179 data in this record */
1180 while (i
< RECORDSIZE
)
1198 if (i
== RECORDSIZE
)
1207 /* Note that this routine is only called if zero_record returned true */
1208 #if 0 /* But we actually don't need it at all. */
1209 where_is_data (from
, to
, buffer
)
1215 for (fp
= buffer
; !*fp
; fp
++)
1217 for (tp
= buffer
+ RECORDSIZE
- 1; !*tp
; tp
--)
1219 *from
= fp
- buffer
;
1220 *to
= tp
- buffer
+ 1;
1228 * Takes a recordful of data and basically cruises through it to see if
1229 * it's made *entirely* of zeros, returning a 0 the instant it finds
1230 * something that is a non-zero, i.e., useful data.
1233 zero_record (buffer
)
1238 for (i
= 0; i
< RECORDSIZE
; i
++)
1239 if (buffer
[i
] != '\000')
1245 find_new_file_size (filesize
, highest_index
)
1252 for (i
= 0; sparsearray
[i
].numbytes
&& i
<= highest_index
; i
++)
1253 *filesize
+= sparsearray
[i
].numbytes
;
1257 * Make a header block for the file name whose stat info is st .
1258 * Return header pointer for success, NULL if the name is too long.
1261 start_header (name
, st
)
1263 register struct stat
*st
;
1265 register union record
*header
;
1267 if (strlen (name
) >= NAMSIZ
)
1268 write_long (name
, LF_LONGNAME
);
1270 header
= (union record
*) findrec ();
1271 bzero (header
->charptr
, sizeof (*header
)); /* XXX speed up */
1274 * Check the file name and put it in the record.
1276 if (!f_absolute_paths
)
1278 static int warned_once
= 0;
1284 msg ("Removing drive spec from names in the archive");
1287 while ('/' == *name
)
1289 name
++; /* Force relative path */
1291 msg ("Removing leading / from absolute path names in the archive.");
1294 current_file_name
= name
;
1295 strncpy (header
->header
.arch_name
, name
, NAMSIZ
);
1296 header
->header
.arch_name
[NAMSIZ
- 1] = '\0';
1298 to_oct ((long) (st
->st_mode
& 07777),
1299 8, header
->header
.mode
);
1300 to_oct ((long) st
->st_uid
, 8, header
->header
.uid
);
1301 to_oct ((long) st
->st_gid
, 8, header
->header
.gid
);
1302 to_oct ((long) st
->st_size
, 1 + 12, header
->header
.size
);
1303 to_oct ((long) st
->st_mtime
, 1 + 12, header
->header
.mtime
);
1304 /* header->header.linkflag is left as null */
1307 to_oct ((long) st
->st_atime
, 1 + 12, header
->header
.atime
);
1308 to_oct ((long) st
->st_ctime
, 1 + 12, header
->header
.ctime
);
1312 /* Fill in new Unix Standard fields if desired. */
1315 header
->header
.linkflag
= LF_NORMAL
; /* New default */
1316 strcpy (header
->header
.magic
, TMAGIC
); /* Mark as Unix Std */
1317 finduname (header
->header
.uname
, st
->st_uid
);
1318 findgname (header
->header
.gname
, st
->st_gid
);
1325 * Finish off a filled-in header block and write it out.
1326 * We also print the file name and/or full info if verbose is on.
1329 finish_header (header
)
1330 register union record
*header
;
1332 register int i
, sum
;
1335 bcopy (CHKBLANKS
, header
->header
.chksum
, sizeof (header
->header
.chksum
));
1338 p
= header
->charptr
;
1339 for (i
= sizeof (*header
); --i
>= 0;)
1342 * We can't use unsigned char here because of old compilers,
1349 * Fill in the checksum field. It's formatted differently
1350 * from the other fields: it has [6] digits, a null, then a
1351 * space -- rather than digits, a space, then a null.
1352 * We use to_oct then write the null in over to_oct's space.
1353 * The final space is already there, from checksumming, and
1354 * to_oct doesn't modify it.
1356 * This is a fast way to do:
1357 * (void) sprintf(header->header.chksum, "%6o", sum);
1359 to_oct ((long) sum
, 8, header
->header
.chksum
);
1360 header
->header
.chksum
[6] = '\0'; /* Zap the space */
1366 extern union record
*head
;/* Points to current tape header */
1367 extern int head_standard
; /* Tape header is in ANSI format */
1369 /* These globals are parameters to print_header, sigh */
1371 /* hstat is already set up */
1372 head_standard
= f_standard
;
1381 * Quick and dirty octal conversion.
1382 * Converts long "value" into a "digs"-digit field at "where",
1383 * including a trailing space and room for a null. "digs"==3 means
1384 * 1 digit, a space, and room for a null.
1386 * We assume the trailing null is already there and don't fill it in.
1387 * This fact is used by start_header and finish_header, so don't change it!
1389 * This should be equivalent to:
1390 * (void) sprintf(where, "%*lo ", digs-2, value);
1391 * except that sprintf fills in the trailing null and we don't.
1394 to_oct (value
, digs
, where
)
1395 register long value
;
1397 register char *where
;
1400 --digs
; /* Trailing null slot is left alone */
1401 where
[--digs
] = ' '; /* Put in the space, though */
1403 /* Produce the digits -- at least one */
1406 where
[--digs
] = '0' + (char) (value
& 7); /* one octal digit */
1409 while (digs
> 0 && value
!= 0);
1411 /* Leading spaces, if necessary */
1413 where
[--digs
] = ' ';
1419 * Write the EOT record(s).
1420 * We actually zero at least one record, through the end of the block.
1421 * Old tar writes garbage after two zeroed records -- and PDtar used to.
1432 bufsize
= endofrecs ()->charptr
- p
->charptr
;
1433 bzero (p
->charptr
, bufsize
);
1438 /* Write a LF_LONGLINK or LF_LONGNAME record. */
1440 write_long (p
, type
)
1444 int size
= strlen (p
) + 1;
1446 union record
*header
;
1450 bzero (&foo
, sizeof foo
);
1453 header
= start_header ("././@LongLink", &foo
);
1454 header
->header
.linkflag
= type
;
1455 finish_header (header
);
1457 header
= findrec ();
1459 bufsize
= endofrecs ()->charptr
- header
->charptr
;
1461 while (bufsize
< size
)
1463 bcopy (p
, header
->charptr
, bufsize
);
1466 userec (header
+ (bufsize
- 1) / RECORDSIZE
);
1467 header
= findrec ();
1468 bufsize
= endofrecs ()->charptr
- header
->charptr
;
1470 bcopy (p
, header
->charptr
, size
);
1471 bzero (header
->charptr
+ size
, bufsize
- size
);
1472 userec (header
+ (size
- 1) / RECORDSIZE
);
This page took 0.103651 seconds and 4 git commands to generate.