]>
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) || defined(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)
62 #define DP_NAMELEN(x) (x)->d_namlen
63 #endif /* not _POSIX_VERSION and BSD42 */
66 #define DP_NAMELEN(x) (x)->d_namlen
69 #if defined(USG) && !defined(_POSIX_VERSION) && !defined(DIRENT)
71 #define DP_NAMELEN(x) strlen((x)->d_name)
72 #endif /* USG and not _POSIX_VERSION and not DIRENT */
74 extern struct stat hstat
; /* Stat struct corresponding */
82 extern struct name
*gnu_list_name
;
85 * If there are no symbolic links, there is no lstat(). Use stat().
91 extern void print_header();
93 union record
*start_header();
94 void blank_name_list();
100 void collect_and_sort_names();
102 int deal_with_sparse();
103 void find_new_file_size();
104 void finish_header();
105 int finish_sparse_file();
108 int is_dot_or_dotdot();
114 void write_dir_file();
118 /* This code moved from tar.h since create.c is the only file that cares
119 about 'struct link's. This means that other files might not have to
120 include sys/types.h any more. */
130 struct link
*linklist
; /* Points to first link in list */
132 static nolinks
; /* Gets set if we run out of RAM */
135 * "Scratch" space to store the information about a sparse file before
136 * writing the info into the header or extended header
138 /* struct sp_array *sparsearray;*/
140 /* number of elts storable in the sparsearray */
141 /*int sparse_array_size = 10;*/
147 char *name_from_list();
149 open_archive(0); /* Open for writing */
152 char *buf
= ck_malloc(PATH_MAX
);
155 collect_and_sort_names();
157 while(p
=name_from_list())
159 /* if(!f_dironly) { */
161 while(p
=name_from_list()) {
163 if(p
[strlen(p
)-1]!='/')
165 bufp
=buf
+strlen(buf
);
166 for(q
=gnu_list_name
->dir_contents
;q
&& *q
;q
+=strlen(q
)+1) {
169 dump_file(buf
,-1, 1);
179 while (p
= name_next(1));
190 * Dump a single file. If it's a directory, recurse.
191 * Result is 1 for success, 0 for failure.
192 * Sets global "hstat" to stat() output for this file.
195 dump_file (p
, curdev
, toplevel
)
196 char *p
; /* File name to dump */
197 int curdev
; /* Device our parent dir was on */
198 int toplevel
; /* Whether we are a toplevel call */
200 union record
*header
;
202 extern char *save_name
; /* JF for multi-volume support */
203 extern long save_totsize
;
204 extern long save_sizeleft
;
207 extern time_t new_time
;
208 int critical_error
= 0;
209 time_t restore_times
[2];
210 /* int sparse_ind = 0;*/
213 if(f_confirm
&& !confirm("add",p
))
217 * Use stat if following (rather than dumping) 4.2BSD's
218 * symbolic links. Otherwise, use lstat (which, on non-4.2
219 * systems, is #define'd to stat anyway.
221 #ifdef STX_HIDDEN /* AIX */
222 if (0 != f_follow_links
?
223 statx (p
, &hstat
, STATSIZE
, STX_HIDDEN
):
224 statx (p
, &hstat
, STATSIZE
, STX_HIDDEN
|STX_LINK
))
226 if (0 != f_follow_links
? stat(p
, &hstat
): lstat(p
, &hstat
))
230 msg_perror("can't add file %s",p
);
232 if (!f_ignore_failed_read
|| critical_error
)
237 restore_times
[0] = hstat
.st_atime
;
238 restore_times
[1] = hstat
.st_mtime
;
241 if (S_ISHIDDEN (hstat
.st_mode
)) {
242 char *new = (char *)alloca (strlen (p
) + 2);
251 /* See if we only want new files, and check if this one is too old to
252 put in the archive. */
255 && new_time
>hstat
.st_mtime
256 && !S_ISDIR(hstat
.st_mode
)
257 && (f_new_files
>1 || new_time
>hstat
.st_ctime
)) {
259 msg("%s: is unchanged; not dumped",p
);
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
);
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
)
293 register struct link
*lp
;
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
;
301 /* We found a link. */
302 while(!f_absolute_paths
&& *link_name
== '/') {
303 static int link_warn
= 0;
306 msg("Removing leading / from absolute links");
311 if (link_name
- lp
->name
>= NAMSIZ
)
312 write_long (link_name
, LF_LONGLINK
);
313 current_link_name
= link_name
;
316 header
= start_header(p
, &hstat
);
322 strncpy(header
->header
.arch_linkname
,
325 /* Force null truncated */
326 header
->header
.arch_linkname
[NAMSIZ
-1] = 0;
328 header
->header
.linkflag
= LF_LINK
;
329 finish_header(header
);
330 /* FIXME: Maybe remove from list after all links found? */
333 if (unlink (p
) == -1)
334 msg_perror ("cannot remove %s", p
);
336 return; /* We dumped it */
340 /* Not found. Add it to the list of possible links. */
341 lp
= (struct link
*)malloc((unsigned)(sizeof(struct link
)+strlen(p
)));
345 "no memory for links, they will be dumped as separate files");
349 lp
->ino
= hstat
.st_ino
;
350 lp
->dev
= hstat
.st_dev
;
357 * This is not a link to a previously dumped file, so dump it.
359 if (S_ISREG(hstat
.st_mode
)
361 || S_ISCTG(hstat
.st_mode
)
365 int f
; /* File descriptor */
368 register union record
*start
;
372 /* int end_nulls = 0; */
377 if (f_sparse_files
) {
379 * JK - This is the test for sparseness: whether the
380 * "size" of the file matches the number of blocks
381 * allocated for it. If there is a smaller number
382 * of blocks that would be necessary to accommodate
383 * a file of this size, we have a sparse file, i.e.,
384 * at least one of those records in the file is just
387 #ifdef hpux /* Nice of HPUX to gratuitiously change it, huh? - mib */
388 if (hstat
.st_size
- (hstat
.st_blocks
* 1024) > 1024 )
390 if (hstat
.st_size
- (hstat
.st_blocks
* RECORDSIZE
) > RECORDSIZE
)
393 int filesize
= hstat
.st_size
;
396 header
= start_header(p
, &hstat
);
402 header
->header
.linkflag
= LF_SPARSE
;
406 * Call the routine that figures out the
407 * layout of the sparse file in question.
408 * UPPERBOUND is the index of the last
409 * element of the "sparsearray," i.e.,
410 * the number of elements it needed to
414 upperbound
= deal_with_sparse(p
, header
);
417 * See if we'll need an extended header
420 if (upperbound
> SPARSE_IN_HDR
-1)
421 header
->header
.isextended
++;
423 * We store the "real" file size so
424 * we can show that in case someone wants
425 * to list the archive, i.e., tar tvf <file>.
426 * It might be kind of disconcerting if the
427 * shrunken file size was the one that showed
430 to_oct((long) hstat
.st_size
, 1+12,
431 header
->header
.realsize
);
434 * This will be the new "size" of the
435 * file, i.e., the size of the file
436 * minus the records of holes that we're
440 find_new_file_size(&filesize
, upperbound
);
441 hstat
.st_size
= filesize
;
442 to_oct((long) filesize
, 1+12,
443 header
->header
.size
);
444 /* to_oct((long) end_nulls, 1+12,
445 header->header.ending_blanks);*/
447 for (i
= 0; i
< SPARSE_IN_HDR
; i
++) {
448 if (!sparsearray
[i
].numbytes
)
450 to_oct(sparsearray
[i
].offset
, 1+12,
451 header
->header
.sp
[i
].offset
);
452 to_oct(sparsearray
[i
].numbytes
, 1+12,
453 header
->header
.sp
[i
].numbytes
);
459 upperbound
=SPARSE_IN_HDR
-1;
462 sizeleft
= hstat
.st_size
;
463 /* Don't bother opening empty, world readable files. */
464 if (sizeleft
> 0 || 0444 != (0444 & hstat
.st_mode
)) {
465 f
= open(p
, O_RDONLY
|O_BINARY
);
466 if (f
< 0) goto badperror
;
471 /* If the file is sparse, we've already taken care of this */
473 header
= start_header(p
, &hstat
);
474 if (header
== NULL
) {
482 /* Mark contiguous files, if we support them */
483 if (f_standard
&& S_ISCTG(hstat
.st_mode
)) {
484 header
->header
.linkflag
= LF_CONTIG
;
487 isextended
= header
->header
.isextended
;
488 save_linkflag
= header
->header
.linkflag
;
489 finish_header(header
);
493 /* register union record *exhdr;*/
494 /* int arraybound = SPARSE_EXT_HDR;*/
495 /* static */ int index_offset
= SPARSE_IN_HDR
;
497 extend
: exhdr
= findrec();
504 bzero(exhdr
->charptr
, RECORDSIZE
);
505 for (i
= 0; i
< SPARSE_EXT_HDR
; i
++) {
506 if (i
+index_offset
> upperbound
)
508 to_oct((long) sparsearray
[i
+index_offset
].numbytes
,
510 exhdr
->ext_hdr
.sp
[i
].numbytes
);
511 to_oct((long) sparsearray
[i
+index_offset
].offset
,
513 exhdr
->ext_hdr
.sp
[i
].offset
);
517 if (sum < upperbound)
519 if (index_offset
+i
< upperbound
) {
521 exhdr
->ext_hdr
.isextended
++;
526 if (save_linkflag
== LF_SPARSE
) {
527 if (finish_sparse_file(f
, &sizeleft
, hstat
.st_size
, p
))
531 while (sizeleft
> 0) {
535 save_sizeleft
= sizeleft
;
536 save_totsize
= hstat
.st_size
;
540 bufsize
= endofrecs()->charptr
- start
->charptr
;
542 if (sizeleft
< bufsize
) {
543 /* Last read -- zero out area beyond */
544 bufsize
= (int)sizeleft
;
545 count
= bufsize
% RECORDSIZE
;
547 bzero(start
->charptr
+ sizeleft
,
548 (int)(RECORDSIZE
- count
));
550 count
= read(f
, start
->charptr
, bufsize
);
552 msg_perror("read error at byte %ld, reading\
553 %d bytes, in file %s", hstat
.st_size
- sizeleft
, bufsize
,p
);
558 /* This is nonportable (the type of userec's arg). */
559 userec(start
+(count
-1)/RECORDSIZE
);
561 if (count
== bufsize
) continue;
562 msg( "file %s shrunk by %d bytes, padding with zeros.", p
, sizeleft
);
563 goto padit
; /* Short read */
574 if (unlink (p
) == -1)
575 msg_perror ("cannot remove %s", p
);
577 if (f_atime_preserve
)
578 utime (p
, restore_times
);
582 * File shrunk or gave error, pad out tape to match
583 * the size we specified in the header.
587 save_sizeleft
=sizeleft
;
589 bzero(start
->charptr
,RECORDSIZE
);
591 sizeleft
-=RECORDSIZE
;
597 if (f_atime_preserve
)
598 utime (p
, restore_times
);
603 else if(S_ISLNK(hstat
.st_mode
))
606 char *buf
= alloca (PATH_MAX
+ 1);
608 size
= readlink (p
, buf
, PATH_MAX
+ 1);
613 write_long (buf
, LF_LONGLINK
);
614 current_link_name
= buf
;
616 hstat
.st_size
= 0; /* Force 0 size on symlink */
617 header
= start_header(p
, &hstat
);
623 strncpy (header
->header
.arch_linkname
, buf
, NAMSIZ
);
624 header
->header
.arch_linkname
[NAMSIZ
- 1] = '\0';
625 header
->header
.linkflag
= LF_SYMLINK
;
626 finish_header(header
); /* Nothing more to do to it */
629 if (unlink (p
) == -1)
630 msg_perror ("cannot remove %s", p
);
636 else if (S_ISDIR(hstat
.st_mode
))
639 register struct direct
*d
;
643 int our_device
= hstat
.st_dev
;
645 /* Build new prototype name */
648 namebuf
=ck_malloc(buflen
+1);
649 strncpy(namebuf
, p
, buflen
);
650 while (len
>= 1 && '/' == namebuf
[len
-1])
651 len
--; /* Delete trailing slashes */
652 namebuf
[len
++] = '/'; /* Now add exactly one back */
653 namebuf
[len
] = '\0'; /* Make sure null-terminated */
656 * Output directory header record with permissions
657 * FIXME, do this AFTER files, to avoid R/O dir problems?
658 * If old archive format, don't write record at all.
661 hstat
.st_size
= 0; /* Force 0 size on dir */
663 * If people could really read standard archives,
664 * this should be: (FIXME)
665 header = start_header(f_standard? p: namebuf, &hstat);
666 * but since they'd interpret LF_DIR records as
667 * regular files, we'd better put the / on the name.
669 header
= start_header(namebuf
, &hstat
);
673 goto badfile
; /* eg name too long */
677 header
->header
.linkflag
= LF_DUMPDIR
;
679 header
->header
.linkflag
= LF_DIR
;
681 /* If we're gnudumping, we aren't done yet so don't close it. */
683 finish_header(header
); /* Done with directory header */
694 buf
=gnu_list_name
->dir_contents
; /* FOO */
696 for(p_buf
=buf
;p_buf
&& *p_buf
;) {
704 to_oct((long)totsize
,1+12,header
->header
.size
);
705 finish_header(header
);
711 save_sizeleft
=sizeleft
;
712 save_totsize
=totsize
;
715 bufsize
=endofrecs()->charptr
- start
->charptr
;
716 if(sizeleft
<bufsize
) {
718 count
=bufsize%RECORDSIZE
;
720 bzero(start
->charptr
+sizeleft
,RECORDSIZE
-count
);
722 bcopy(p_buf
,start
->charptr
,bufsize
);
725 userec(start
+(bufsize
-1)/RECORDSIZE
);
729 if (f_atime_preserve
)
730 utime (p
, restore_times
);
734 /* Now output all the files in the directory */
737 return; /* Unless the cmdline said not to */
740 * See if we are crossing from one file system to another,
741 * and avoid doing so if the user only wants to dump one file system.
743 if (f_local_filesys
&& !toplevel
&& curdev
!= hstat
.st_dev
) {
745 msg("%s: is on a different filesystem; not dumped",p
);
754 msg_perror ("can't open directory %s",p
);
756 msg("error opening directory %s",
762 /* Hack to remove "./" from the front of all the file names */
763 if (len
== 2 && namebuf
[0] == '.' && namebuf
[1]=='/')
766 /* Should speed this up by cd-ing into the dir, FIXME */
767 while (NULL
!= (d
=readdir(dirp
))) {
769 if(is_dot_or_dotdot(d
->d_name
))
772 if (DP_NAMELEN(d
) + len
>= buflen
) {
773 buflen
=len
+DP_NAMELEN(d
);
774 namebuf
=ck_realloc(namebuf
,buflen
+1);
775 /* namebuf[len]='\0';
776 msg("file name %s%s too long",
780 strcpy(namebuf
+len
, d
->d_name
);
781 if(f_exclude
&& check_exclude(namebuf
))
783 dump_file(namebuf
, our_device
, 0);
788 if (f_atime_preserve
)
789 utime (p
, restore_times
);
794 else if (S_ISCHR(hstat
.st_mode
)) {
800 else if (S_ISBLK(hstat
.st_mode
)) {
805 /* Avoid screwy apollo lossage where S_IFIFO == S_IFSOCK */
806 #if (_ISP__M68K == 0) && (_ISP__A88K == 0) && defined(S_ISFIFO)
807 else if (S_ISFIFO(hstat
.st_mode
)) {
813 else if (S_ISSOCK(hstat
.st_mode
)) {
820 if (!f_standard
) goto unknown
;
822 hstat
.st_size
= 0; /* Force 0 size */
823 header
= start_header(p
, &hstat
);
827 goto badfile
; /* eg name too long */
830 header
->header
.linkflag
= type
;
831 #if defined(S_IFBLK) || defined(S_IFCHR)
832 if (type
!= LF_FIFO
) {
833 to_oct((long) major(hstat
.st_rdev
), 8,
834 header
->header
.devmajor
);
835 to_oct((long) minor(hstat
.st_rdev
), 8,
836 header
->header
.devminor
);
840 finish_header(header
);
843 if (unlink (p
) == -1)
844 msg_perror ("cannot remove %s", p
);
849 msg("%s: Unknown file type; file ignored.", p
);
853 finish_sparse_file(fd
, sizeleft
, fullsize
, name
)
860 char tempbuf
[RECORDSIZE
];
868 while (*sizeleft
> 0) {
870 bzero(start
->charptr
, RECORDSIZE
);
871 bufsize
= sparsearray
[sparse_ind
].numbytes
;
872 if (!bufsize
) { /* we blew it, maybe */
873 msg("Wrote %ld of %ld bytes to file %s",
874 fullsize
- *sizeleft
, fullsize
, name
);
877 pos
= lseek(fd
, sparsearray
[sparse_ind
++].offset
, 0);
879 * If the number of bytes to be written here exceeds
880 * the size of the temporary buffer, do it in steps.
882 while (bufsize
> RECORDSIZE
) {
884 count = read(fd, start->charptr+amt_read, RECORDSIZE-amt_read);
885 bufsize -= RECORDSIZE - amt_read;
889 bzero(start->charptr, RECORDSIZE);
892 count
= read(fd
, start
->charptr
, RECORDSIZE
);
894 msg_perror("read error at byte %ld, reading %d bytes, in file %s",
895 fullsize
- *sizeleft
, bufsize
, name
);
901 nwritten
+= RECORDSIZE
; /* XXX */
903 bzero(start
->charptr
, RECORDSIZE
);
907 clear_buffer(tempbuf
);
908 count
= read(fd
, tempbuf
, bufsize
);
909 bcopy(tempbuf
, start
->charptr
, RECORDSIZE
);
911 msg_perror("read error at byte %ld, reading %d bytes, in file %s",
912 fullsize
- *sizeleft
, bufsize
, name
);
915 /* if (amt_read >= RECORDSIZE) {
917 userec(start+(count-1)/RECORDSIZE);
918 if (count != bufsize) {
919 msg("file %s shrunk by %d bytes, padding with zeros.", name, sizeleft);
924 amt_read += bufsize;*/
925 nwritten
+= count
; /* XXX */
931 /* printf ("Amount actually written is (I hope) %d.\n", nwritten); */
932 /* userec(start+(count-1)/RECORDSIZE);*/
944 * Make room for our scratch space -- initially is 10 elts long
946 sparsearray
= (struct sp_array
*) malloc(sp_array_size
* sizeof(struct sp_array
));
947 for (i
= 0; i
< sp_array_size
; i
++) {
948 sparsearray
[i
].offset
= 0;
949 sparsearray
[i
].numbytes
= 0;
956 * Okay, we've got a sparse file on our hands -- now, what we need to do is
957 * make a pass through the file and carefully note where any data is, i.e.,
958 * we want to find how far into the file each instance of data is, and how
959 * many bytes are there. We store this information in the sparsearray,
960 * which will later be translated into header information. For now, we use
961 * the sparsearray as convenient storage.
963 * As a side note, this routine is a mess. If I could have found a cleaner
964 * way to do it, I would have. If anyone wants to find a nicer way to do
968 /* There is little point in trimming small amounts of null data at the */
969 /* head and tail of blocks -- it's ok if we only avoid dumping blocks */
970 /* of complete null data */
972 deal_with_sparse(name
, header
, nulls_at_end
)
974 union record
*header
;
979 /* long save_offset;*/
981 /* int current_size = hstat.st_size;*/
984 char buf
[RECORDSIZE
];
986 int read_last_data
= 0; /* did we just read the last record? */
990 header
->header
.isextended
= 0;
992 * Can't open the file -- this problem will be caught later on,
995 if ((fd
= open(name
, O_RDONLY
)) < 0)
1001 while ((cc
= read(fd
, buf
, sizeof buf
)) != 0) {
1003 if (sparse_ind
> sp_array_size
-1) {
1006 * realloc the scratch area, since we've run out of room --
1008 sparsearray
= (struct sp_array
*)
1009 realloc(sparsearray
,
1010 2 * sp_array_size
* (sizeof(struct sp_array
)));
1013 if (cc
== sizeof buf
) {
1014 if (zero_record(buf
)) {
1016 sparsearray
[sparse_ind
++].numbytes
1020 } else { /* !zero_record(buf) */
1026 sparsearray
[sparse_ind
].offset
1030 } else if (cc
< sizeof buf
) {
1031 /* This has to be the last bit of the file, so this */
1032 /* is somewhat shorter than the above. */
1033 if (!zero_record(buf
)) {
1037 sparsearray
[sparse_ind
].offset
1047 sparsearray
[sparse_ind
++].numbytes
= numbytes
;
1050 return sparse_ind
- 1;
1054 * Just zeroes out the buffer so we don't confuse ourselves with leftover
1063 for (i
= 0; i
< RECORDSIZE
; i
++)
1067 #if 0 /* I'm leaving this as a monument to Joy Kendall, who wrote it -mib */
1070 * This routine takes a character array, and tells where within that array
1071 * the data can be found. It skips over any zeros, and sets the first
1072 * non-zero point in the array to be the "start", and continues until it
1073 * finds non-data again, which is marked as the "end." This routine is
1074 * mainly for 1) seeing how far into a file we must lseek to data, given
1075 * that we have a sparse file, and 2) determining the "real size" of the
1076 * file, i.e., the number of bytes in the sparse file that are data, as
1077 * opposed to the zeros we are trying to skip.
1079 where_is_data(from
, to
, buffer
)
1085 register int save_to
= *to
;
1086 int amidst_data
= 0;
1093 if (*from
< 16) /* don't bother */
1095 /* keep going to make sure there isn't more real
1096 data in this record */
1097 while (i
< RECORDSIZE
) {
1105 else if (buffer
[i
]) {
1111 if (i
== RECORDSIZE
)
1119 /* Note that this routine is only called if zero_record returned true */
1120 #if 0 /* But we actually don't need it at all. */
1121 where_is_data (from
, to
, buffer
)
1127 for (fp
= buffer
; ! *fp
; fp
++)
1129 for (tp
= buffer
+ RECORDSIZE
- 1; ! *tp
; tp
--)
1131 *from
= fp
- buffer
;
1132 *to
= tp
- buffer
+ 1;
1139 * Takes a recordful of data and basically cruises through it to see if
1140 * it's made *entirely* of zeros, returning a 0 the instant it finds
1141 * something that is a non-zero, i.e., useful data.
1149 for (i
= 0; i
< RECORDSIZE
; i
++)
1150 if (buffer
[i
] != '\000')
1156 find_new_file_size(filesize
, highest_index
)
1163 for (i
= 0; sparsearray
[i
].numbytes
&& i
<= highest_index
; i
++)
1164 *filesize
+= sparsearray
[i
].numbytes
;
1168 * Make a header block for the file name whose stat info is st .
1169 * Return header pointer for success, NULL if the name is too long.
1172 start_header(name
, st
)
1174 register struct stat
*st
;
1176 register union record
*header
;
1178 if (strlen (name
) >= NAMSIZ
)
1179 write_long (name
, LF_LONGNAME
);
1181 header
= (union record
*) findrec();
1182 bzero(header
->charptr
, sizeof(*header
)); /* XXX speed up */
1185 * Check the file name and put it in the record.
1187 if(!f_absolute_paths
) {
1188 static int warned_once
= 0;
1193 msg("Removing drive spec from names in the archive");
1196 while ('/' == *name
) {
1197 name
++; /* Force relative path */
1199 msg("Removing leading / from absolute path names in the archive.");
1202 current_file_name
= name
;
1203 strncpy(header
->header
.arch_name
, name
, NAMSIZ
);
1204 header
->header
.arch_name
[NAMSIZ
-1] = '\0';
1206 to_oct((long) (st
->st_mode
& 07777),
1207 8, header
->header
.mode
);
1208 to_oct((long) st
->st_uid
, 8, header
->header
.uid
);
1209 to_oct((long) st
->st_gid
, 8, header
->header
.gid
);
1210 to_oct((long) st
->st_size
, 1+12, header
->header
.size
);
1211 to_oct((long) st
->st_mtime
, 1+12, header
->header
.mtime
);
1212 /* header->header.linkflag is left as null */
1214 to_oct((long) st
->st_atime
, 1+12, header
->header
.atime
);
1215 to_oct((long) st
->st_ctime
, 1+12, header
->header
.ctime
);
1219 /* Fill in new Unix Standard fields if desired. */
1221 header
->header
.linkflag
= LF_NORMAL
; /* New default */
1222 strcpy(header
->header
.magic
, TMAGIC
); /* Mark as Unix Std */
1223 finduname(header
->header
.uname
, st
->st_uid
);
1224 findgname(header
->header
.gname
, st
->st_gid
);
1231 * Finish off a filled-in header block and write it out.
1232 * We also print the file name and/or full info if verbose is on.
1235 finish_header(header
)
1236 register union record
*header
;
1238 register int i
, sum
;
1241 bcopy(CHKBLANKS
, header
->header
.chksum
, sizeof(header
->header
.chksum
));
1244 p
= header
->charptr
;
1245 for (i
= sizeof(*header
); --i
>= 0; ) {
1247 * We can't use unsigned char here because of old compilers,
1254 * Fill in the checksum field. It's formatted differently
1255 * from the other fields: it has [6] digits, a null, then a
1256 * space -- rather than digits, a space, then a null.
1257 * We use to_oct then write the null in over to_oct's space.
1258 * The final space is already there, from checksumming, and
1259 * to_oct doesn't modify it.
1261 * This is a fast way to do:
1262 * (void) sprintf(header->header.chksum, "%6o", sum);
1264 to_oct((long) sum
, 8, header
->header
.chksum
);
1265 header
->header
.chksum
[6] = '\0'; /* Zap the space */
1270 extern union record
*head
; /* Points to current tape header */
1271 extern int head_standard
; /* Tape header is in ANSI format */
1273 /* These globals are parameters to print_header, sigh */
1275 /* hstat is already set up */
1276 head_standard
= f_standard
;
1285 * Quick and dirty octal conversion.
1286 * Converts long "value" into a "digs"-digit field at "where",
1287 * including a trailing space and room for a null. "digs"==3 means
1288 * 1 digit, a space, and room for a null.
1290 * We assume the trailing null is already there and don't fill it in.
1291 * This fact is used by start_header and finish_header, so don't change it!
1293 * This should be equivalent to:
1294 * (void) sprintf(where, "%*lo ", digs-2, value);
1295 * except that sprintf fills in the trailing null and we don't.
1298 to_oct(value
, digs
, where
)
1299 register long value
;
1301 register char *where
;
1304 --digs
; /* Trailing null slot is left alone */
1305 where
[--digs
] = ' '; /* Put in the space, though */
1307 /* Produce the digits -- at least one */
1309 where
[--digs
] = '0' + (char)(value
& 7); /* one octal digit */
1311 } while (digs
> 0 && value
!= 0);
1313 /* Leading spaces, if necessary */
1315 where
[--digs
] = ' ';
1321 * Write the EOT record(s).
1322 * We actually zero at least one record, through the end of the block.
1323 * Old tar writes garbage after two zeroed records -- and PDtar used to.
1334 bufsize
= endofrecs()->charptr
- p
->charptr
;
1335 bzero(p
->charptr
, bufsize
);
1340 /* Write a LF_LONGLINK or LF_LONGNAME record. */
1341 write_long (p
, type
)
1345 int size
= strlen (p
) + 1;
1347 union record
*header
;
1351 bzero (&foo
, sizeof foo
);
1354 header
= start_header ("././@LongLink", &foo
);
1355 header
->header
.linkflag
= type
;
1356 finish_header (header
);
1358 header
= findrec ();
1360 bufsize
= endofrecs ()->charptr
- header
->charptr
;
1362 while (bufsize
< size
)
1364 bcopy (p
, header
->charptr
, bufsize
);
1367 userec (header
+ (bufsize
- 1)/RECORDSIZE
);
1368 header
= findrec ();
1369 bufsize
= endofrecs ()->charptr
- header
->charptr
;
1371 bcopy (p
, header
->charptr
, size
);
1372 bzero (header
->charptr
+ size
, bufsize
- size
);
1373 userec (header
+ (size
- 1)/RECORDSIZE
);
This page took 0.090528 seconds and 5 git commands to generate.