]>
Dogcows Code - chaz/tar/blob - src/create.c
1 /* Create a tar archive.
2 Copyright (C) 1988 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();
95 void add_symlink_mangle();
96 void blank_name_list();
101 void close_archive();
102 void collect_and_sort_names();
104 int deal_with_sparse();
105 void find_new_file_size();
106 void finish_header();
107 int finish_sparse_file();
110 int is_dot_or_dotdot();
116 void write_dir_file();
118 void write_mangled();
121 /* This code moved from tar.h since create.c is the only file that cares
122 about 'struct link's. This means that other files might not have to
123 include sys/types.h any more. */
133 struct link
*linklist
; /* Points to first link in list */
135 static nolinks
; /* Gets set if we run out of RAM */
138 * "Scratch" space to store the information about a sparse file before
139 * writing the info into the header or extended header
141 /* struct sp_array *sparsearray;*/
143 /* number of elts storable in the sparsearray */
144 /*int sparse_array_size = 10;*/
150 char *name_from_list();
152 open_archive(0); /* Open for writing */
155 char *buf
= ck_malloc(NAME_MAX
);
158 collect_and_sort_names();
160 while(p
=name_from_list())
162 /* if(!f_dironly) { */
164 while(p
=name_from_list()) {
166 if(p
[strlen(p
)-1]!='/')
168 bufp
=buf
+strlen(buf
);
169 for(q
=gnu_list_name
->dir_contents
;q
&& *q
;q
+=strlen(q
)+1) {
184 while (p
= name_next(1));
197 * Dump a single file. If it's a directory, recurse.
198 * Result is 1 for success, 0 for failure.
199 * Sets global "hstat" to stat() output for this file.
202 dump_file (p
, curdev
)
203 char *p
; /* File name to dump */
204 int curdev
; /* Device our parent dir was on */
206 union record
*header
;
208 extern char *save_name
; /* JF for multi-volume support */
209 extern long save_totsize
;
210 extern long save_sizeleft
;
213 extern time_t new_time
;
214 /* int sparse_ind = 0;*/
217 if(f_confirm
&& !confirm("add",p
))
221 * Use stat if following (rather than dumping) 4.2BSD's
222 * symbolic links. Otherwise, use lstat (which, on non-4.2
223 * systems, is #define'd to stat anyway.
225 #ifdef STX_HIDDEN /* AIX */
226 if (0 != f_follow_links
?
227 statx (p
, &hstat
, STATSIZE
, STX_HIDDEN
):
228 statx (p
, &hstat
, STATSIZE
, STX_HIDDEN
|STX_LINK
))
230 if (0 != f_follow_links
? stat(p
, &hstat
): lstat(p
, &hstat
))
234 msg_perror("can't add file %s",p
);
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. */
303 header
= start_header(p
, &hstat
);
304 if (header
== NULL
) goto badfile
;
305 while(!f_absolute_paths
&& *link_name
== '/') {
306 static int link_warn
= 0;
309 msg("Removing leading / from absolute links");
314 strncpy(header
->header
.linkname
,
316 if(header
->header
.linkname
[NAMSIZ
-1]) {
318 extern char *find_mangled();
320 mangled
=find_mangled(link_name
);
321 msg("%s: link name too long: mangled to %s",link_name
,mangled
);
322 strncpy(header
->header
.linkname
,mangled
,NAMSIZ
);
324 header
->header
.linkflag
= LF_LINK
;
325 finish_header(header
);
326 /* FIXME: Maybe remove from list after all links found? */
327 return; /* We dumped it */
331 /* Not found. Add it to the list of possible links. */
332 lp
= (struct link
*)malloc((unsigned)(sizeof(struct link
)+strlen(p
)));
336 "no memory for links, they will be dumped as separate files");
340 lp
->ino
= hstat
.st_ino
;
341 lp
->dev
= hstat
.st_dev
;
348 * This is not a link to a previously dumped file, so dump it.
350 if (S_ISREG(hstat
.st_mode
)
352 || S_ISCTG(hstat
.st_mode
)
356 int f
; /* File descriptor */
359 register union record
*start
;
363 /* int end_nulls = 0; */
368 if (f_sparse_files
) {
370 * JK - This is the test for sparseness: whether the
371 * "size" of the file matches the number of blocks
372 * allocated for it. If there is a smaller number
373 * of blocks that would be necessary to accommodate
374 * a file of this size, we have a sparse file, i.e.,
375 * at least one of those records in the file is just
378 #ifdef hpux /* Nice of HPUX to gratuitiously change it, huh? - mib */
379 if (hstat
.st_size
- (hstat
.st_blocks
* 1024) > 1024 )
381 if (hstat
.st_size
- (hstat
.st_blocks
* RECORDSIZE
) > RECORDSIZE
)
384 int filesize
= hstat
.st_size
;
387 header
= start_header(p
, &hstat
);
390 header
->header
.linkflag
= LF_SPARSE
;
394 * Call the routine that figures out the
395 * layout of the sparse file in question.
396 * UPPERBOUND is the index of the last
397 * element of the "sparsearray," i.e.,
398 * the number of elements it needed to
402 upperbound
= deal_with_sparse(p
, header
);
405 * See if we'll need an extended header
408 if (upperbound
> SPARSE_IN_HDR
-1)
409 header
->header
.isextended
++;
411 * We store the "real" file size so
412 * we can show that in case someone wants
413 * to list the archive, i.e., tar tvf <file>.
414 * It might be kind of disconcerting if the
415 * shrunken file size was the one that showed
418 to_oct((long) hstat
.st_size
, 1+12,
419 header
->header
.realsize
);
422 * This will be the new "size" of the
423 * file, i.e., the size of the file
424 * minus the records of holes that we're
428 find_new_file_size(&filesize
, upperbound
);
429 hstat
.st_size
= filesize
;
430 to_oct((long) filesize
, 1+12,
431 header
->header
.size
);
432 /* to_oct((long) end_nulls, 1+12,
433 header->header.ending_blanks);*/
435 for (i
= 0; i
< SPARSE_IN_HDR
; i
++) {
436 if (!sparsearray
[i
].numbytes
)
438 to_oct(sparsearray
[i
].offset
, 1+12,
439 header
->header
.sp
[i
].offset
);
440 to_oct(sparsearray
[i
].numbytes
, 1+12,
441 header
->header
.sp
[i
].numbytes
);
447 upperbound
=SPARSE_IN_HDR
-1;
450 sizeleft
= hstat
.st_size
;
451 /* Don't bother opening empty, world readable files. */
452 if (sizeleft
> 0 || 0444 != (0444 & hstat
.st_mode
)) {
453 f
= open(p
, O_RDONLY
|O_BINARY
);
454 if (f
< 0) goto badperror
;
459 /* If the file is sparse, we've already taken care of this */
461 header
= start_header(p
, &hstat
);
462 if (header
== NULL
) {
469 /* Mark contiguous files, if we support them */
470 if (f_standard
&& S_ISCTG(hstat
.st_mode
)) {
471 header
->header
.linkflag
= LF_CONTIG
;
474 isextended
= header
->header
.isextended
;
475 save_linkflag
= header
->header
.linkflag
;
476 finish_header(header
);
480 /* register union record *exhdr;*/
481 /* int arraybound = SPARSE_EXT_HDR;*/
482 /* static */ int index_offset
= SPARSE_IN_HDR
;
484 extend
: exhdr
= findrec();
486 if (exhdr
== NULL
) goto badfile
;
487 bzero(exhdr
->charptr
, RECORDSIZE
);
488 for (i
= 0; i
< SPARSE_EXT_HDR
; i
++) {
489 if (i
+index_offset
> upperbound
)
491 to_oct((long) sparsearray
[i
+index_offset
].numbytes
,
493 exhdr
->ext_hdr
.sp
[i
].numbytes
);
494 to_oct((long) sparsearray
[i
+index_offset
].offset
,
496 exhdr
->ext_hdr
.sp
[i
].offset
);
500 if (sum < upperbound)
502 if (index_offset
+i
< upperbound
) {
504 exhdr
->ext_hdr
.isextended
++;
509 if (save_linkflag
== LF_SPARSE
) {
510 if (finish_sparse_file(f
, &sizeleft
, hstat
.st_size
, p
))
514 while (sizeleft
> 0) {
517 save_name
= header
->header
.name
;
518 save_sizeleft
= sizeleft
;
519 save_totsize
= hstat
.st_size
;
523 bufsize
= endofrecs()->charptr
- start
->charptr
;
525 if (sizeleft
< bufsize
) {
526 /* Last read -- zero out area beyond */
527 bufsize
= (int)sizeleft
;
528 count
= bufsize
% RECORDSIZE
;
530 bzero(start
->charptr
+ sizeleft
,
531 (int)(RECORDSIZE
- count
));
533 count
= read(f
, start
->charptr
, bufsize
);
535 msg_perror("read error at byte %ld, reading\
536 %d bytes, in file %s", hstat
.st_size
- sizeleft
, bufsize
,p
);
541 /* This is nonportable (the type of userec's arg). */
542 userec(start
+(count
-1)/RECORDSIZE
);
544 if (count
== bufsize
) continue;
545 msg( "file %s shrunk by %d bytes, padding with zeros.", p
, sizeleft
);
546 goto padit
; /* Short read */
558 * File shrunk or gave error, pad out tape to match
559 * the size we specified in the header.
563 save_sizeleft
=sizeleft
;
565 bzero(start
->charptr
,RECORDSIZE
);
567 sizeleft
-=RECORDSIZE
;
577 else if(S_ISLNK(hstat
.st_mode
))
581 hstat
.st_size
= 0; /* Force 0 size on symlink */
582 header
= start_header(p
, &hstat
);
583 if (header
== NULL
) goto badfile
;
584 size
= readlink(p
, header
->header
.linkname
, NAMSIZ
);
585 if (size
< 0) goto badperror
;
586 if (size
== NAMSIZ
) {
587 char *buf
= ck_malloc(PATH_MAX
);
589 readlink(p
,buf
,PATH_MAX
);
590 /* next_mangle(header->header.linkname); */
591 add_symlink_mangle(buf
,p
,header
->header
.linkname
);
592 msg("symbolic link %s too long: mangling to %s",p
, header
->header
.linkname
);
593 /* size=strlen(header->header.linkname); */
596 header
->header
.linkname
[size
] = '\0';
597 header
->header
.linkflag
= LF_SYMLINK
;
598 finish_header(header
); /* Nothing more to do to it */
603 else if (S_ISDIR(hstat
.st_mode
))
606 register struct direct
*d
;
610 int our_device
= hstat
.st_dev
;
612 /* Build new prototype name */
615 namebuf
=ck_malloc(buflen
+1);
616 strncpy(namebuf
, p
, buflen
);
617 while (len
>= 1 && '/' == namebuf
[len
-1])
618 len
--; /* Delete trailing slashes */
619 namebuf
[len
++] = '/'; /* Now add exactly one back */
620 namebuf
[len
] = '\0'; /* Make sure null-terminated */
623 * Output directory header record with permissions
624 * FIXME, do this AFTER files, to avoid R/O dir problems?
625 * If old archive format, don't write record at all.
628 hstat
.st_size
= 0; /* Force 0 size on dir */
630 * If people could really read standard archives,
631 * this should be: (FIXME)
632 header = start_header(f_standard? p: namebuf, &hstat);
633 * but since they'd interpret LF_DIR records as
634 * regular files, we'd better put the / on the name.
636 header
= start_header(namebuf
, &hstat
);
638 goto badfile
; /* eg name too long */
641 header
->header
.linkflag
= LF_DUMPDIR
;
643 header
->header
.linkflag
= LF_DIR
;
645 /* If we're gnudumping, we aren't done yet so don't close it. */
647 finish_header(header
); /* Done with directory header */
658 buf
=gnu_list_name
->dir_contents
; /* FOO */
660 for(p_buf
=buf
;p_buf
&& *p_buf
;) {
668 to_oct((long)totsize
,1+12,header
->header
.size
);
669 finish_header(header
);
675 save_sizeleft
=sizeleft
;
676 save_totsize
=totsize
;
679 bufsize
=endofrecs()->charptr
- start
->charptr
;
680 if(sizeleft
<bufsize
) {
682 count
=bufsize%RECORDSIZE
;
684 bzero(start
->charptr
+sizeleft
,RECORDSIZE
-count
);
686 bcopy(p_buf
,start
->charptr
,bufsize
);
689 userec(start
+(bufsize
-1)/RECORDSIZE
);
696 /* Now output all the files in the directory */
699 return; /* Unless the cmdline said not to */
702 * See if we are crossing from one file system to another,
703 * and avoid doing so if the user only wants to dump one file system.
705 if (f_local_filesys
&& curdev
>= 0 && curdev
!= hstat
.st_dev
) {
707 msg("%s: is on a different filesystem; not dumped",p
);
716 msg_perror ("can't open directory %s",p
);
718 msg("error opening directory %s",
724 /* Hack to remove "./" from the front of all the file names */
725 if (len
== 2 && namebuf
[0] == '.' && namebuf
[1]=='/')
728 /* Should speed this up by cd-ing into the dir, FIXME */
729 while (NULL
!= (d
=readdir(dirp
))) {
731 if(is_dot_or_dotdot(d
->d_name
))
734 if (DP_NAMELEN(d
) + len
>= buflen
) {
735 buflen
=len
+DP_NAMELEN(d
);
736 namebuf
=ck_realloc(namebuf
,buflen
+1);
737 /* namebuf[len]='\0';
738 msg("file name %s%s too long",
742 strcpy(namebuf
+len
, d
->d_name
);
743 if(f_exclude
&& check_exclude(namebuf
))
745 dump_file(namebuf
, our_device
);
754 else if (S_ISCHR(hstat
.st_mode
)) {
760 else if (S_ISBLK(hstat
.st_mode
)) {
765 /* Avoid screwy apollo lossage where S_IFIFO == S_IFSOCK */
766 #if (_ISP__M68K == 0) && (_ISP__A88K == 0) && defined(S_ISFIFO)
767 else if (S_ISFIFO(hstat
.st_mode
)) {
773 else if (S_ISSOCK(hstat
.st_mode
)) {
780 if (!f_standard
) goto unknown
;
782 hstat
.st_size
= 0; /* Force 0 size */
783 header
= start_header(p
, &hstat
);
784 if (header
== NULL
) goto badfile
; /* eg name too long */
786 header
->header
.linkflag
= type
;
787 #if defined(S_IFBLK) || defined(S_IFCHR)
788 if (type
!= LF_FIFO
) {
789 to_oct((long) major(hstat
.st_rdev
), 8,
790 header
->header
.devmajor
);
791 to_oct((long) minor(hstat
.st_rdev
), 8,
792 header
->header
.devminor
);
796 finish_header(header
);
800 msg("%s: Unknown file type; file ignored.", p
);
804 finish_sparse_file(fd
, sizeleft
, fullsize
, name
)
811 char tempbuf
[RECORDSIZE
];
819 while (*sizeleft
> 0) {
821 bzero(start
->charptr
, RECORDSIZE
);
822 bufsize
= sparsearray
[sparse_ind
].numbytes
;
823 if (!bufsize
) { /* we blew it, maybe */
824 msg("Wrote %ld of %ld bytes to file %s",
825 fullsize
- *sizeleft
, fullsize
, name
);
828 pos
= lseek(fd
, sparsearray
[sparse_ind
++].offset
, 0);
830 * If the number of bytes to be written here exceeds
831 * the size of the temporary buffer, do it in steps.
833 while (bufsize
> RECORDSIZE
) {
835 count = read(fd, start->charptr+amt_read, RECORDSIZE-amt_read);
836 bufsize -= RECORDSIZE - amt_read;
840 bzero(start->charptr, RECORDSIZE);
843 count
= read(fd
, start
->charptr
, RECORDSIZE
);
845 msg_perror("read error at byte %ld, reading %d bytes, in file %s",
846 fullsize
- *sizeleft
, bufsize
, name
);
852 nwritten
+= RECORDSIZE
; /* XXX */
854 bzero(start
->charptr
, RECORDSIZE
);
858 clear_buffer(tempbuf
);
859 count
= read(fd
, tempbuf
, bufsize
);
860 bcopy(tempbuf
, start
->charptr
, RECORDSIZE
);
862 msg_perror("read error at byte %ld, reading %d bytes, in file %s",
863 fullsize
- *sizeleft
, bufsize
, name
);
866 /* if (amt_read >= RECORDSIZE) {
868 userec(start+(count-1)/RECORDSIZE);
869 if (count != bufsize) {
870 msg("file %s shrunk by %d bytes, padding with zeros.", name, sizeleft);
875 amt_read += bufsize;*/
876 nwritten
+= count
; /* XXX */
882 printf ("Amount actually written is (I hope) %d.\n", nwritten
);
883 /* userec(start+(count-1)/RECORDSIZE);*/
895 * Make room for our scratch space -- initially is 10 elts long
897 sparsearray
= (struct sp_array
*) malloc(sp_array_size
* sizeof(struct sp_array
));
898 for (i
= 0; i
< sp_array_size
; i
++) {
899 sparsearray
[i
].offset
= 0;
900 sparsearray
[i
].numbytes
= 0;
907 * Okay, we've got a sparse file on our hands -- now, what we need to do is
908 * make a pass through the file and carefully note where any data is, i.e.,
909 * we want to find how far into the file each instance of data is, and how
910 * many bytes are there. We store this information in the sparsearray,
911 * which will later be translated into header information. For now, we use
912 * the sparsearray as convenient storage.
914 * As a side note, this routine is a mess. If I could have found a cleaner
915 * way to do it, I would have. If anyone wants to find a nicer way to do
919 /* There is little point in trimming small amounts of null data at the */
920 /* head and tail of blocks -- it's ok if we only avoid dumping blocks */
921 /* of complete null data */
923 deal_with_sparse(name
, header
, nulls_at_end
)
925 union record
*header
;
930 /* long save_offset;*/
932 /* int current_size = hstat.st_size;*/
935 char buf
[RECORDSIZE
];
937 int read_last_data
= 0; /* did we just read the last record? */
941 header
->header
.isextended
= 0;
943 * Can't open the file -- this problem will be caught later on,
946 if ((fd
= open(name
, O_RDONLY
)) < 0)
952 while ((cc
= read(fd
, buf
, sizeof buf
)) != 0) {
954 if (sparse_ind
> sp_array_size
-1) {
957 * realloc the scratch area, since we've run out of room --
959 sparsearray
= (struct sp_array
*)
961 2 * sp_array_size
* (sizeof(struct sp_array
)));
964 if (cc
== sizeof buf
) {
965 if (zero_record(buf
)) {
967 sparsearray
[sparse_ind
++].numbytes
971 } else { /* !zero_record(buf) */
977 sparsearray
[sparse_ind
].offset
981 } else if (cc
< sizeof buf
) {
982 /* This has to be the last bit of the file, so this */
983 /* is somewhat shorter than the above. */
984 if (!zero_record(buf
)) {
988 sparsearray
[sparse_ind
].offset
998 sparsearray
[sparse_ind
++].numbytes
= numbytes
;
1001 return sparse_ind
- 1;
1005 * Just zeroes out the buffer so we don't confuse ourselves with leftover
1014 for (i
= 0; i
< RECORDSIZE
; i
++)
1018 #if 0 /* I'm leaving this as a monument to Joy Kendall, who wrote it -mib */
1021 * This routine takes a character array, and tells where within that array
1022 * the data can be found. It skips over any zeros, and sets the first
1023 * non-zero point in the array to be the "start", and continues until it
1024 * finds non-data again, which is marked as the "end." This routine is
1025 * mainly for 1) seeing how far into a file we must lseek to data, given
1026 * that we have a sparse file, and 2) determining the "real size" of the
1027 * file, i.e., the number of bytes in the sparse file that are data, as
1028 * opposed to the zeros we are trying to skip.
1030 where_is_data(from
, to
, buffer
)
1036 register int save_to
= *to
;
1037 int amidst_data
= 0;
1044 if (*from
< 16) /* don't bother */
1046 /* keep going to make sure there isn't more real
1047 data in this record */
1048 while (i
< RECORDSIZE
) {
1056 else if (buffer
[i
]) {
1062 if (i
== RECORDSIZE
)
1070 /* Note that this routine is only called if zero_record returned true */
1071 #if 0 /* But we actually don't need it at all. */
1072 where_is_data (from
, to
, buffer
)
1078 for (fp
= buffer
; ! *fp
; fp
++)
1080 for (tp
= buffer
+ RECORDSIZE
- 1; ! *tp
; tp
--)
1082 *from
= fp
- buffer
;
1083 *to
= tp
- buffer
+ 1;
1090 * Takes a recordful of data and basically cruises through it to see if
1091 * it's made *entirely* of zeros, returning a 0 the instant it finds
1092 * something that is a non-zero, i.e., useful data.
1100 for (i
= 0; i
< RECORDSIZE
; i
++)
1101 if (buffer
[i
] != '\000')
1107 find_new_file_size(filesize
, highest_index
)
1114 for (i
= 0; sparsearray
[i
].numbytes
&& i
<= highest_index
; i
++)
1115 *filesize
+= sparsearray
[i
].numbytes
;
1119 * Make a header block for the file name whose stat info is st .
1120 * Return header pointer for success, NULL if the name is too long.
1123 start_header(name
, st
)
1125 register struct stat
*st
;
1127 register union record
*header
;
1129 header
= (union record
*) findrec();
1130 bzero(header
->charptr
, sizeof(*header
)); /* XXX speed up */
1133 * Check the file name and put it in the record.
1135 if(!f_absolute_paths
) {
1136 static int warned_once
= 0;
1141 msg("Removing drive spec from names in the archive");
1144 while ('/' == *name
) {
1145 name
++; /* Force relative path */
1147 msg("Removing leading / from absolute path names in the archive.");
1150 strncpy(header
->header
.name
, name
, NAMSIZ
);
1151 if (header
->header
.name
[NAMSIZ
-1]) {
1154 /* next_mangle(header->header.name); */
1155 add_mangle(name
,header
->header
.name
);
1156 msg("%s: is too long: mangling to %s", name
, header
->header
.name
);
1159 to_oct((long) (st
->st_mode
& 07777),
1160 8, header
->header
.mode
);
1161 to_oct((long) st
->st_uid
, 8, header
->header
.uid
);
1162 to_oct((long) st
->st_gid
, 8, header
->header
.gid
);
1163 to_oct((long) st
->st_size
, 1+12, header
->header
.size
);
1164 to_oct((long) st
->st_mtime
, 1+12, header
->header
.mtime
);
1165 /* header->header.linkflag is left as null */
1167 to_oct((long) st
->st_atime
, 1+12, header
->header
.atime
);
1168 to_oct((long) st
->st_ctime
, 1+12, header
->header
.ctime
);
1172 /* Fill in new Unix Standard fields if desired. */
1174 header
->header
.linkflag
= LF_NORMAL
; /* New default */
1175 strcpy(header
->header
.magic
, TMAGIC
); /* Mark as Unix Std */
1176 finduname(header
->header
.uname
, st
->st_uid
);
1177 findgname(header
->header
.gname
, st
->st_gid
);
1184 * Finish off a filled-in header block and write it out.
1185 * We also print the file name and/or full info if verbose is on.
1188 finish_header(header
)
1189 register union record
*header
;
1191 register int i
, sum
;
1194 bcopy(CHKBLANKS
, header
->header
.chksum
, sizeof(header
->header
.chksum
));
1197 p
= header
->charptr
;
1198 for (i
= sizeof(*header
); --i
>= 0; ) {
1200 * We can't use unsigned char here because of old compilers,
1207 * Fill in the checksum field. It's formatted differently
1208 * from the other fields: it has [6] digits, a null, then a
1209 * space -- rather than digits, a space, then a null.
1210 * We use to_oct then write the null in over to_oct's space.
1211 * The final space is already there, from checksumming, and
1212 * to_oct doesn't modify it.
1214 * This is a fast way to do:
1215 * (void) sprintf(header->header.chksum, "%6o", sum);
1217 to_oct((long) sum
, 8, header
->header
.chksum
);
1218 header
->header
.chksum
[6] = '\0'; /* Zap the space */
1223 extern union record
*head
; /* Points to current tape header */
1224 extern int head_standard
; /* Tape header is in ANSI format */
1226 /* These globals are parameters to print_header, sigh */
1228 /* hstat is already set up */
1229 head_standard
= f_standard
;
1238 * Quick and dirty octal conversion.
1239 * Converts long "value" into a "digs"-digit field at "where",
1240 * including a trailing space and room for a null. "digs"==3 means
1241 * 1 digit, a space, and room for a null.
1243 * We assume the trailing null is already there and don't fill it in.
1244 * This fact is used by start_header and finish_header, so don't change it!
1246 * This should be equivalent to:
1247 * (void) sprintf(where, "%*lo ", digs-2, value);
1248 * except that sprintf fills in the trailing null and we don't.
1251 to_oct(value
, digs
, where
)
1252 register long value
;
1254 register char *where
;
1257 --digs
; /* Trailing null slot is left alone */
1258 where
[--digs
] = ' '; /* Put in the space, though */
1260 /* Produce the digits -- at least one */
1262 where
[--digs
] = '0' + (char)(value
& 7); /* one octal digit */
1264 } while (digs
> 0 && value
!= 0);
1266 /* Leading spaces, if necessary */
1268 where
[--digs
] = ' ';
1274 * Write the EOT record(s).
1275 * We actually zero at least one record, through the end of the block.
1276 * Old tar writes garbage after two zeroed records -- and PDtar used to.
1287 bufsize
= endofrecs()->charptr
- p
->charptr
;
1288 bzero(p
->charptr
, bufsize
);
This page took 0.088483 seconds and 5 git commands to generate.