]>
Dogcows Code - chaz/tar/blob - src/create.c
1 /* Create a tar archive.
2 Copyright (C) 1985, 1992, 1993 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 extern struct stat hstat
; /* Stat struct corresponding */
71 extern struct name
*gnu_list_name
;
74 * If there are no symbolic links, there is no lstat(). Use stat().
80 extern void print_header ();
82 union record
*start_header ();
83 void blank_name_list ();
88 void close_archive ();
89 void collect_and_sort_names ();
91 int deal_with_sparse ();
92 void find_new_file_size ();
93 void finish_header ();
94 int finish_sparse_file ();
97 int is_dot_or_dotdot ();
103 void write_dir_file ();
108 /* This code moved from tar.h since create.c is the only file that cares
109 about 'struct link's. This means that other files might not have to
110 include sys/types.h any more. */
121 struct link
*linklist
; /* Points to first link in list */
123 static nolinks
; /* Gets set if we run out of RAM */
126 * "Scratch" space to store the information about a sparse file before
127 * writing the info into the header or extended header
129 /* struct sp_array *sparsearray;*/
131 /* number of elts storable in the sparsearray */
132 /*int sparse_array_size = 10;*/
138 char *name_from_list ();
140 open_archive (0); /* Open for writing */
144 char *buf
= ck_malloc (PATH_MAX
);
147 collect_and_sort_names ();
149 while (p
= name_from_list ())
150 dump_file (p
, -1, 1);
151 /* if(!f_dironly) { */
153 while (p
= name_from_list ())
156 if (p
[strlen (p
) - 1] != '/')
158 bufp
= buf
+ strlen (buf
);
159 for (q
= gnu_list_name
->dir_contents
; q
&& *q
; q
+= strlen (q
) + 1)
163 strcpy (bufp
, q
+ 1);
164 dump_file (buf
, -1, 1);
173 while (p
= name_next (1))
174 dump_file (p
, -1, 1);
185 * Dump a single file. If it's a directory, recurse.
186 * Result is 1 for success, 0 for failure.
187 * Sets global "hstat" to stat() output for this file.
190 dump_file (p
, curdev
, toplevel
)
191 char *p
; /* File name to dump */
192 int curdev
; /* Device our parent dir was on */
193 int toplevel
; /* Whether we are a toplevel call */
195 union record
*header
;
197 extern char *save_name
; /* JF for multi-volume support */
198 extern long save_totsize
;
199 extern long save_sizeleft
;
202 extern time_t new_time
;
203 int critical_error
= 0;
204 struct utimbuf restore_times
;
205 /* int sparse_ind = 0;*/
208 if (f_confirm
&& !confirm ("add", p
))
212 * Use stat if following (rather than dumping) 4.2BSD's
213 * symbolic links. Otherwise, use lstat (which, on non-4.2
214 * systems, is #define'd to stat anyway.
216 #ifdef STX_HIDDEN /* AIX */
217 if (0 != f_follow_links
?
218 statx (p
, &hstat
, STATSIZE
, STX_HIDDEN
) :
219 statx (p
, &hstat
, STATSIZE
, STX_HIDDEN
| STX_LINK
))
221 if (0 != f_follow_links
? stat (p
, &hstat
) : lstat (p
, &hstat
))
225 msg_perror ("can't add file %s", p
);
227 if (!f_ignore_failed_read
|| critical_error
)
232 restore_times
.actime
= hstat
.st_atime
;
233 restore_times
.modtime
= hstat
.st_mtime
;
236 if (S_ISHIDDEN (hstat
.st_mode
))
238 char *new = (char *) alloca (strlen (p
) + 2);
248 /* See if we only want new files, and check if this one is too old to
249 put in the archive. */
252 && new_time
> hstat
.st_mtime
253 && !S_ISDIR (hstat
.st_mode
)
254 && (f_new_files
> 1 || new_time
> hstat
.st_ctime
))
258 msg ("%s: is unchanged; not dumped", p
);
264 /* See if we are trying to dump the archive */
265 if (ar_dev
&& hstat
.st_dev
== ar_dev
&& hstat
.st_ino
== ar_ino
)
267 msg ("%s is the archive; not dumped", p
);
272 * Check for multiple links.
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.
278 if (hstat
.st_nlink
> 1
279 && (S_ISREG (hstat
.st_mode
)
281 || S_ISCTG (hstat
.st_mode
)
284 || S_ISCHR (hstat
.st_mode
)
287 || S_ISBLK (hstat
.st_mode
)
290 || S_ISFIFO (hstat
.st_mode
)
294 register struct link
*lp
;
296 /* First quick and dirty. Hashing, etc later FIXME */
297 for (lp
= linklist
; lp
; lp
= lp
->next
)
299 if (lp
->ino
== hstat
.st_ino
&&
300 lp
->dev
== hstat
.st_dev
)
302 char *link_name
= lp
->name
;
304 /* We found a link. */
305 while (!f_absolute_paths
&& *link_name
== '/')
307 static int link_warn
= 0;
311 msg ("Removing leading / from absolute links");
316 if (link_name
- lp
->name
>= NAMSIZ
)
317 write_long (link_name
, LF_LONGLINK
);
318 current_link_name
= link_name
;
321 header
= start_header (p
, &hstat
);
327 strncpy (header
->header
.arch_linkname
,
330 /* Force null truncated */
331 header
->header
.arch_linkname
[NAMSIZ
- 1] = 0;
333 header
->header
.linkflag
= LF_LINK
;
334 finish_header (header
);
335 /* FIXME: Maybe remove from list after all links found? */
338 if (unlink (p
) == -1)
339 msg_perror ("cannot remove %s", p
);
341 return; /* We dumped it */
345 /* Not found. Add it to the list of possible links. */
346 lp
= (struct link
*) ck_malloc ((unsigned) (sizeof (struct link
) + strlen (p
)));
352 "no memory for links, they will be dumped as separate files");
356 lp
->ino
= hstat
.st_ino
;
357 lp
->dev
= hstat
.st_dev
;
358 strcpy (lp
->name
, p
);
364 * This is not a link to a previously dumped file, so dump it.
366 if (S_ISREG (hstat
.st_mode
)
368 || S_ISCTG (hstat
.st_mode
)
372 int f
; /* File descriptor */
375 register union record
*start
;
379 /* int end_nulls = 0; */
387 * JK - This is the test for sparseness: whether the
388 * "size" of the file matches the number of blocks
389 * allocated for it. If there is a smaller number
390 * of blocks that would be necessary to accommodate
391 * a file of this size, we have a sparse file, i.e.,
392 * at least one of those records in the file is just
395 #ifdef hpux /* Nice of HPUX to gratuitiously change it, huh? - mib */
396 if (hstat
.st_size
- (hstat
.st_blocks
* 1024) > 1024)
398 if (hstat
.st_size
- (hstat
.st_blocks
* RECORDSIZE
) > RECORDSIZE
)
401 int filesize
= hstat
.st_size
;
404 header
= start_header (p
, &hstat
);
410 header
->header
.linkflag
= LF_SPARSE
;
414 * Call the routine that figures out the
415 * layout of the sparse file in question.
416 * UPPERBOUND is the index of the last
417 * element of the "sparsearray," i.e.,
418 * the number of elements it needed to
422 upperbound
= deal_with_sparse (p
, header
);
425 * See if we'll need an extended header
428 if (upperbound
> SPARSE_IN_HDR
- 1)
429 header
->header
.isextended
++;
431 * We store the "real" file size so
432 * we can show that in case someone wants
433 * to list the archive, i.e., tar tvf <file>.
434 * It might be kind of disconcerting if the
435 * shrunken file size was the one that showed
438 to_oct ((long) hstat
.st_size
, 1 + 12,
439 header
->header
.realsize
);
442 * This will be the new "size" of the
443 * file, i.e., the size of the file
444 * minus the records of holes that we're
448 find_new_file_size (&filesize
, upperbound
);
449 hstat
.st_size
= filesize
;
450 to_oct ((long) filesize
, 1 + 12,
451 header
->header
.size
);
452 /* to_oct((long) end_nulls, 1+12,
453 header->header.ending_blanks);*/
455 for (i
= 0; i
< SPARSE_IN_HDR
; i
++)
457 if (!sparsearray
[i
].numbytes
)
459 to_oct (sparsearray
[i
].offset
, 1 + 12,
460 header
->header
.sp
[i
].offset
);
461 to_oct (sparsearray
[i
].numbytes
, 1 + 12,
462 header
->header
.sp
[i
].numbytes
);
468 upperbound
= SPARSE_IN_HDR
- 1;
471 sizeleft
= hstat
.st_size
;
472 /* Don't bother opening empty, world readable files. */
473 if (sizeleft
> 0 || 0444 != (0444 & hstat
.st_mode
))
475 f
= open (p
, O_RDONLY
| O_BINARY
);
484 /* If the file is sparse, we've already taken care of this */
487 header
= start_header (p
, &hstat
);
497 /* Mark contiguous files, if we support them */
498 if (f_standard
&& S_ISCTG (hstat
.st_mode
))
500 header
->header
.linkflag
= LF_CONTIG
;
503 isextended
= header
->header
.isextended
;
504 save_linkflag
= header
->header
.linkflag
;
505 finish_header (header
);
510 /* register union record *exhdr;*/
511 /* int arraybound = SPARSE_EXT_HDR;*/
512 /* static */ int index_offset
= SPARSE_IN_HDR
;
514 extend
:exhdr
= findrec ();
521 bzero (exhdr
->charptr
, RECORDSIZE
);
522 for (i
= 0; i
< SPARSE_EXT_HDR
; i
++)
524 if (i
+ index_offset
> upperbound
)
526 to_oct ((long) sparsearray
[i
+ index_offset
].numbytes
,
528 exhdr
->ext_hdr
.sp
[i
].numbytes
);
529 to_oct ((long) sparsearray
[i
+ index_offset
].offset
,
531 exhdr
->ext_hdr
.sp
[i
].offset
);
535 if (sum < upperbound)
537 if (index_offset
+ i
<= upperbound
)
540 exhdr
->ext_hdr
.isextended
++;
545 if (save_linkflag
== LF_SPARSE
)
547 if (finish_sparse_file (f
, &sizeleft
, hstat
.st_size
, p
))
557 save_sizeleft
= sizeleft
;
558 save_totsize
= hstat
.st_size
;
562 bufsize
= endofrecs ()->charptr
- start
->charptr
;
564 if (sizeleft
< bufsize
)
566 /* Last read -- zero out area beyond */
567 bufsize
= (int) sizeleft
;
568 count
= bufsize
% RECORDSIZE
;
570 bzero (start
->charptr
+ sizeleft
,
571 (int) (RECORDSIZE
- count
));
573 count
= read (f
, start
->charptr
, bufsize
);
576 msg_perror ("read error at byte %ld, reading\
577 %d bytes, in file %s", hstat
.st_size
- sizeleft
, bufsize
, p
);
582 /* This is nonportable (the type of userec's arg). */
583 userec (start
+ (count
- 1) / RECORDSIZE
);
585 if (count
== bufsize
)
587 msg ("file %s shrunk by %d bytes, padding with zeros.", p
, sizeleft
);
588 goto padit
; /* Short read */
599 if (unlink (p
) == -1)
600 msg_perror ("cannot remove %s", p
);
602 if (f_atime_preserve
)
603 utime (p
, &restore_times
);
607 * File shrunk or gave error, pad out tape to match
608 * the size we specified in the header.
613 save_sizeleft
= sizeleft
;
615 bzero (start
->charptr
, RECORDSIZE
);
617 sizeleft
-= RECORDSIZE
;
623 if (f_atime_preserve
)
624 utime (p
, &restore_times
);
629 else if (S_ISLNK (hstat
.st_mode
))
632 char *buf
= alloca (PATH_MAX
+ 1);
634 size
= readlink (p
, buf
, PATH_MAX
+ 1);
639 write_long (buf
, LF_LONGLINK
);
640 current_link_name
= buf
;
642 hstat
.st_size
= 0; /* Force 0 size on symlink */
643 header
= start_header (p
, &hstat
);
649 strncpy (header
->header
.arch_linkname
, buf
, NAMSIZ
);
650 header
->header
.arch_linkname
[NAMSIZ
- 1] = '\0';
651 header
->header
.linkflag
= LF_SYMLINK
;
652 finish_header (header
); /* Nothing more to do to it */
655 if (unlink (p
) == -1)
656 msg_perror ("cannot remove %s", p
);
662 else if (S_ISDIR (hstat
.st_mode
))
665 register struct dirent
*d
;
669 int our_device
= hstat
.st_dev
;
671 /* Build new prototype name */
673 buflen
= len
+ NAMSIZ
;
674 namebuf
= ck_malloc (buflen
+ 1);
675 strncpy (namebuf
, p
, buflen
);
676 while (len
>= 1 && '/' == namebuf
[len
- 1])
677 len
--; /* Delete trailing slashes */
678 namebuf
[len
++] = '/'; /* Now add exactly one back */
679 namebuf
[len
] = '\0'; /* Make sure null-terminated */
682 * Output directory header record with permissions
683 * FIXME, do this AFTER files, to avoid R/O dir problems?
684 * If old archive format, don't write record at all.
688 hstat
.st_size
= 0; /* Force 0 size on dir */
690 * If people could really read standard archives,
691 * this should be: (FIXME)
692 header = start_header(f_standard? p: namebuf, &hstat);
693 * but since they'd interpret LF_DIR records as
694 * regular files, we'd better put the / on the name.
696 header
= start_header (namebuf
, &hstat
);
700 goto badfile
; /* eg name too long */
704 header
->header
.linkflag
= LF_DUMPDIR
;
706 header
->header
.linkflag
= LF_DIR
;
708 /* If we're gnudumping, we aren't done yet so don't close it. */
710 finish_header (header
); /* Done with directory header */
722 buf
= gnu_list_name
->dir_contents
; /* FOO */
724 for (p_buf
= buf
; p_buf
&& *p_buf
;)
728 tmp
= strlen (p_buf
) + 1;
733 to_oct ((long) totsize
, 1 + 12, header
->header
.size
);
734 finish_header (header
);
742 save_sizeleft
= sizeleft
;
743 save_totsize
= totsize
;
746 bufsize
= endofrecs ()->charptr
- start
->charptr
;
747 if (sizeleft
< bufsize
)
750 count
= bufsize
% RECORDSIZE
;
752 bzero (start
->charptr
+ sizeleft
, RECORDSIZE
- count
);
754 bcopy (p_buf
, start
->charptr
, bufsize
);
757 userec (start
+ (bufsize
- 1) / RECORDSIZE
);
761 if (f_atime_preserve
)
762 utime (p
, &restore_times
);
766 /* Now output all the files in the directory */
769 return; /* Unless the cmdline said not to */
772 * See if we are crossing from one file system to another,
773 * and avoid doing so if the user only wants to dump one file system.
775 if (f_local_filesys
&& !toplevel
&& curdev
!= hstat
.st_dev
)
778 msg ("%s: is on a different filesystem; not dumped", p
);
789 msg_perror ("can't open directory %s", p
);
793 msg ("error opening directory %s",
799 /* Hack to remove "./" from the front of all the file names */
800 if (len
== 2 && namebuf
[0] == '.' && namebuf
[1] == '/')
803 /* Should speed this up by cd-ing into the dir, FIXME */
804 while (NULL
!= (d
= readdir (dirp
)))
807 if (is_dot_or_dotdot (d
->d_name
))
810 if (NLENGTH (d
) + len
>= buflen
)
812 buflen
= len
+ NLENGTH (d
);
813 namebuf
= ck_realloc (namebuf
, buflen
+ 1);
814 /* namebuf[len]='\0';
815 msg("file name %s%s too long",
819 strcpy (namebuf
+ len
, d
->d_name
);
820 if (f_exclude
&& check_exclude (namebuf
))
822 dump_file (namebuf
, our_device
, 0);
827 if (f_atime_preserve
)
828 utime (p
, &restore_times
);
833 else if (S_ISCHR (hstat
.st_mode
))
840 else if (S_ISBLK (hstat
.st_mode
))
846 /* Avoid screwy apollo lossage where S_IFIFO == S_IFSOCK */
847 #if (_ISP__M68K == 0) && (_ISP__A88K == 0) && defined(S_ISFIFO)
848 else if (S_ISFIFO (hstat
.st_mode
))
855 else if (S_ISSOCK (hstat
.st_mode
))
866 hstat
.st_size
= 0; /* Force 0 size */
867 header
= start_header (p
, &hstat
);
871 goto badfile
; /* eg name too long */
874 header
->header
.linkflag
= type
;
875 #if defined(S_IFBLK) || defined(S_IFCHR)
878 to_oct ((long) major (hstat
.st_rdev
), 8,
879 header
->header
.devmajor
);
880 to_oct ((long) minor (hstat
.st_rdev
), 8,
881 header
->header
.devminor
);
885 finish_header (header
);
888 if (unlink (p
) == -1)
889 msg_perror ("cannot remove %s", p
);
894 msg ("%s: Unknown file type; file ignored.", p
);
898 finish_sparse_file (fd
, sizeleft
, fullsize
, name
)
900 long *sizeleft
, fullsize
;
904 char tempbuf
[RECORDSIZE
];
905 int bufsize
, sparse_ind
= 0, count
;
910 while (*sizeleft
> 0)
913 bzero (start
->charptr
, RECORDSIZE
);
914 bufsize
= sparsearray
[sparse_ind
].numbytes
;
916 { /* we blew it, maybe */
917 msg ("Wrote %ld of %ld bytes to file %s",
918 fullsize
- *sizeleft
, fullsize
, name
);
921 pos
= lseek (fd
, sparsearray
[sparse_ind
++].offset
, 0);
923 * If the number of bytes to be written here exceeds
924 * the size of the temporary buffer, do it in steps.
926 while (bufsize
> RECORDSIZE
)
929 count = read(fd, start->charptr+amt_read, RECORDSIZE-amt_read);
930 bufsize -= RECORDSIZE - amt_read;
934 bzero(start->charptr, RECORDSIZE);
937 count
= read (fd
, start
->charptr
, RECORDSIZE
);
940 msg_perror ("read error at byte %ld, reading %d bytes, in file %s",
941 fullsize
- *sizeleft
, bufsize
, name
);
947 nwritten
+= RECORDSIZE
; /* XXX */
949 bzero (start
->charptr
, RECORDSIZE
);
953 clear_buffer (tempbuf
);
954 count
= read (fd
, tempbuf
, bufsize
);
955 bcopy (tempbuf
, start
->charptr
, RECORDSIZE
);
958 msg_perror ("read error at byte %ld, reading %d bytes, in file %s",
959 fullsize
- *sizeleft
, bufsize
, name
);
962 /* if (amt_read >= RECORDSIZE) {
964 userec(start+(count-1)/RECORDSIZE);
965 if (count != bufsize) {
966 msg("file %s shrunk by %d bytes, padding with zeros.", name, sizeleft);
971 amt_read += bufsize;*/
972 nwritten
+= count
; /* XXX */
978 /* printf ("Amount actually written is (I hope) %d.\n", nwritten); */
979 /* userec(start+(count-1)/RECORDSIZE);*/
991 * Make room for our scratch space -- initially is 10 elts long
993 sparsearray
= (struct sp_array
*) ck_malloc (sp_array_size
* sizeof (struct sp_array
));
994 for (i
= 0; i
< sp_array_size
; i
++)
996 sparsearray
[i
].offset
= 0;
997 sparsearray
[i
].numbytes
= 0;
1004 * Okay, we've got a sparse file on our hands -- now, what we need to do is
1005 * make a pass through the file and carefully note where any data is, i.e.,
1006 * we want to find how far into the file each instance of data is, and how
1007 * many bytes are there. We store this information in the sparsearray,
1008 * which will later be translated into header information. For now, we use
1009 * the sparsearray as convenient storage.
1011 * As a side note, this routine is a mess. If I could have found a cleaner
1012 * way to do it, I would have. If anyone wants to find a nicer way to do
1016 /* There is little point in trimming small amounts of null data at the */
1017 /* head and tail of blocks -- it's ok if we only avoid dumping blocks */
1018 /* of complete null data */
1020 deal_with_sparse (name
, header
, nulls_at_end
)
1022 union record
*header
;
1027 /* long save_offset;*/
1029 /* int current_size = hstat.st_size;*/
1030 int sparse_ind
= 0, cc
;
1031 char buf
[RECORDSIZE
];
1033 int read_last_data
= 0; /* did we just read the last record? */
1035 int amidst_data
= 0;
1037 header
->header
.isextended
= 0;
1039 * Can't open the file -- this problem will be caught later on,
1042 if ((fd
= open (name
, O_RDONLY
)) < 0)
1045 init_sparsearray ();
1048 while ((cc
= read (fd
, buf
, sizeof buf
)) != 0)
1051 if (sparse_ind
> sp_array_size
- 1)
1055 * realloc the scratch area, since we've run out of room --
1057 sparsearray
= (struct sp_array
*)
1058 ck_realloc (sparsearray
,
1059 2 * sp_array_size
* (sizeof (struct sp_array
)));
1062 if (cc
== sizeof buf
)
1064 if (zero_record (buf
))
1068 sparsearray
[sparse_ind
++].numbytes
1074 { /* !zero_record(buf) */
1081 sparsearray
[sparse_ind
].offset
1086 else if (cc
< sizeof buf
)
1088 /* This has to be the last bit of the file, so this */
1089 /* is somewhat shorter than the above. */
1090 if (!zero_record (buf
))
1096 sparsearray
[sparse_ind
].offset
1107 sparsearray
[sparse_ind
++].numbytes
= numbytes
;
1110 sparsearray
[sparse_ind
].offset
= offset
-1;
1111 sparsearray
[sparse_ind
++].numbytes
= 1;
1115 return sparse_ind
- 1;
1119 * Just zeroes out the buffer so we don't confuse ourselves with leftover
1128 for (i
= 0; i
< RECORDSIZE
; i
++)
1132 #if 0 /* I'm leaving this as a monument to Joy Kendall, who wrote it -mib */
1135 * This routine takes a character array, and tells where within that array
1136 * the data can be found. It skips over any zeros, and sets the first
1137 * non-zero point in the array to be the "start", and continues until it
1138 * finds non-data again, which is marked as the "end." This routine is
1139 * mainly for 1) seeing how far into a file we must lseek to data, given
1140 * that we have a sparse file, and 2) determining the "real size" of the
1141 * file, i.e., the number of bytes in the sparse file that are data, as
1142 * opposed to the zeros we are trying to skip.
1144 where_is_data (from
, to
, buffer
)
1149 register int save_to
= *to
;
1150 int amidst_data
= 0;
1157 if (*from
< 16) /* don't bother */
1159 /* keep going to make sure there isn't more real
1160 data in this record */
1161 while (i
< RECORDSIZE
)
1179 if (i
== RECORDSIZE
)
1188 /* Note that this routine is only called if zero_record returned true */
1189 #if 0 /* But we actually don't need it at all. */
1190 where_is_data (from
, to
, buffer
)
1196 for (fp
= buffer
; !*fp
; fp
++)
1198 for (tp
= buffer
+ RECORDSIZE
- 1; !*tp
; tp
--)
1200 *from
= fp
- buffer
;
1201 *to
= tp
- buffer
+ 1;
1209 * Takes a recordful of data and basically cruises through it to see if
1210 * it's made *entirely* of zeros, returning a 0 the instant it finds
1211 * something that is a non-zero, i.e., useful data.
1214 zero_record (buffer
)
1219 for (i
= 0; i
< RECORDSIZE
; i
++)
1220 if (buffer
[i
] != '\000')
1226 find_new_file_size (filesize
, highest_index
)
1233 for (i
= 0; sparsearray
[i
].numbytes
&& i
<= highest_index
; i
++)
1234 *filesize
+= sparsearray
[i
].numbytes
;
1238 * Make a header block for the file name whose stat info is st .
1239 * Return header pointer for success, NULL if the name is too long.
1242 start_header (name
, st
)
1244 register struct stat
*st
;
1246 register union record
*header
;
1248 if (strlen (name
) >= NAMSIZ
)
1249 write_long (name
, LF_LONGNAME
);
1251 header
= (union record
*) findrec ();
1252 bzero (header
->charptr
, sizeof (*header
)); /* XXX speed up */
1255 * Check the file name and put it in the record.
1257 if (!f_absolute_paths
)
1259 static int warned_once
= 0;
1265 msg ("Removing drive spec from names in the archive");
1268 while ('/' == *name
)
1270 name
++; /* Force relative path */
1272 msg ("Removing leading / from absolute path names in the archive.");
1275 current_file_name
= name
;
1276 strncpy (header
->header
.arch_name
, name
, NAMSIZ
);
1277 header
->header
.arch_name
[NAMSIZ
- 1] = '\0';
1279 to_oct ((long) (f_oldarch
? (st
->st_mode
& 07777) : st
->st_mode
),
1280 8, header
->header
.mode
);
1281 to_oct ((long) st
->st_uid
, 8, header
->header
.uid
);
1282 to_oct ((long) st
->st_gid
, 8, header
->header
.gid
);
1283 to_oct ((long) st
->st_size
, 1 + 12, header
->header
.size
);
1284 to_oct ((long) st
->st_mtime
, 1 + 12, header
->header
.mtime
);
1285 /* header->header.linkflag is left as null */
1288 to_oct ((long) st
->st_atime
, 1 + 12, header
->header
.atime
);
1289 to_oct ((long) st
->st_ctime
, 1 + 12, header
->header
.ctime
);
1293 /* Fill in new Unix Standard fields if desired. */
1296 header
->header
.linkflag
= LF_NORMAL
; /* New default */
1297 strcpy (header
->header
.magic
, TMAGIC
); /* Mark as Unix Std */
1298 finduname (header
->header
.uname
, st
->st_uid
);
1299 findgname (header
->header
.gname
, st
->st_gid
);
1306 * Finish off a filled-in header block and write it out.
1307 * We also print the file name and/or full info if verbose is on.
1310 finish_header (header
)
1311 register union record
*header
;
1313 register int i
, sum
;
1316 bcopy (CHKBLANKS
, header
->header
.chksum
, sizeof (header
->header
.chksum
));
1319 p
= header
->charptr
;
1320 for (i
= sizeof (*header
); --i
>= 0;)
1323 * We can't use unsigned char here because of old compilers,
1330 * Fill in the checksum field. It's formatted differently
1331 * from the other fields: it has [6] digits, a null, then a
1332 * space -- rather than digits, a space, then a null.
1333 * We use to_oct then write the null in over to_oct's space.
1334 * The final space is already there, from checksumming, and
1335 * to_oct doesn't modify it.
1337 * This is a fast way to do:
1338 * (void) sprintf(header->header.chksum, "%6o", sum);
1340 to_oct ((long) sum
, 8, header
->header
.chksum
);
1341 header
->header
.chksum
[6] = '\0'; /* Zap the space */
1347 extern union record
*head
;/* Points to current tape header */
1348 extern int head_standard
; /* Tape header is in ANSI format */
1350 /* These globals are parameters to print_header, sigh */
1352 /* hstat is already set up */
1353 head_standard
= f_standard
;
1362 * Quick and dirty octal conversion.
1363 * Converts long "value" into a "digs"-digit field at "where",
1364 * including a trailing space and room for a null. "digs"==3 means
1365 * 1 digit, a space, and room for a null.
1367 * We assume the trailing null is already there and don't fill it in.
1368 * This fact is used by start_header and finish_header, so don't change it!
1370 * This should be equivalent to:
1371 * (void) sprintf(where, "%*lo ", digs-2, value);
1372 * except that sprintf fills in the trailing null and we don't.
1375 to_oct (value
, digs
, where
)
1376 register long value
;
1378 register char *where
;
1381 --digs
; /* Trailing null slot is left alone */
1382 where
[--digs
] = ' '; /* Put in the space, though */
1384 /* Produce the digits -- at least one */
1387 where
[--digs
] = '0' + (char) (value
& 7); /* one octal digit */
1390 while (digs
> 0 && value
!= 0);
1392 /* Leading spaces, if necessary */
1394 where
[--digs
] = ' ';
1400 * Write the EOT record(s).
1401 * We actually zero at least one record, through the end of the block.
1402 * Old tar writes garbage after two zeroed records -- and PDtar used to.
1413 bufsize
= endofrecs ()->charptr
- p
->charptr
;
1414 bzero (p
->charptr
, bufsize
);
1419 /* Write a LF_LONGLINK or LF_LONGNAME record. */
1421 write_long (p
, type
)
1425 int size
= strlen (p
) + 1;
1427 union record
*header
;
1431 bzero (&foo
, sizeof foo
);
1434 header
= start_header ("././@LongLink", &foo
);
1435 header
->header
.linkflag
= type
;
1436 finish_header (header
);
1438 header
= findrec ();
1440 bufsize
= endofrecs ()->charptr
- header
->charptr
;
1442 while (bufsize
< size
)
1444 bcopy (p
, header
->charptr
, bufsize
);
1447 userec (header
+ (bufsize
- 1) / RECORDSIZE
);
1448 header
= findrec ();
1449 bufsize
= endofrecs ()->charptr
- header
->charptr
;
1451 bcopy (p
, header
->charptr
, size
);
1452 bzero (header
->charptr
+ size
, bufsize
- size
);
1453 userec (header
+ (size
- 1) / RECORDSIZE
);
This page took 0.094772 seconds and 4 git commands to generate.