]>
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();
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(PATH_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) {
172 dump_file(buf
,-1, 1);
182 while (p
= name_next(1));
194 * Dump a single file. If it's a directory, recurse.
195 * Result is 1 for success, 0 for failure.
196 * Sets global "hstat" to stat() output for this file.
199 dump_file (p
, curdev
, toplevel
)
200 char *p
; /* File name to dump */
201 int curdev
; /* Device our parent dir was on */
202 int toplevel
; /* Whether we are a toplevel call */
204 union record
*header
;
206 extern char *save_name
; /* JF for multi-volume support */
207 extern long save_totsize
;
208 extern long save_sizeleft
;
211 extern time_t new_time
;
212 int critical_error
= 0;
213 time_t restore_times
[2];
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
);
236 if (!f_ignore_failed_read
|| critical_error
)
241 restore_times
[0] = hstat
.st_atime
;
242 restore_times
[1] = hstat
.st_utime
;
245 if (S_ISHIDDEN (hstat
.st_mode
)) {
246 char *new = (char *)alloca (strlen (p
) + 2);
255 /* See if we only want new files, and check if this one is too old to
256 put in the archive. */
259 && new_time
>hstat
.st_mtime
260 && !S_ISDIR(hstat
.st_mode
)
261 && (f_new_files
>1 || new_time
>hstat
.st_ctime
)) {
263 msg("%s: is unchanged; not dumped",p
);
269 /* See if we are trying to dump the archive */
270 if(ar_dev
&& hstat
.st_dev
==ar_dev
&& hstat
.st_ino
==ar_ino
) {
271 msg("%s is the archive; not dumped",p
);
276 * Check for multiple links.
278 * We maintain a list of all such files that we've written so
279 * far. Any time we see another, we check the list and
280 * avoid dumping the data again if we've done it once already.
282 if (hstat
.st_nlink
> 1
283 && (S_ISREG(hstat
.st_mode
)
285 || S_ISCTG(hstat
.st_mode
)
288 || S_ISCHR(hstat
.st_mode
)
291 || S_ISBLK(hstat
.st_mode
)
294 || S_ISFIFO(hstat
.st_mode
)
297 register struct link
*lp
;
299 /* First quick and dirty. Hashing, etc later FIXME */
300 for (lp
= linklist
; lp
; lp
= lp
->next
) {
301 if (lp
->ino
== hstat
.st_ino
&&
302 lp
->dev
== hstat
.st_dev
) {
303 char *link_name
= lp
->name
;
305 /* We found a link. */
307 header
= start_header(p
, &hstat
);
313 while(!f_absolute_paths
&& *link_name
== '/') {
314 static int link_warn
= 0;
317 msg("Removing leading / from absolute links");
322 strncpy(header
->header
.linkname
,
324 if(header
->header
.linkname
[NAMSIZ
-1]) {
326 extern char *find_mangled();
328 mangled
=find_mangled(link_name
);
329 msg("%s: link name too long: mangled to %s",link_name
,mangled
);
330 strncpy(header
->header
.linkname
,mangled
,NAMSIZ
);
332 header
->header
.linkflag
= LF_LINK
;
333 finish_header(header
);
334 /* FIXME: Maybe remove from list after all links found? */
337 if (unlink (p
) == -1)
338 msg_perror ("cannot remove %s", p
);
340 return; /* We dumped it */
344 /* Not found. Add it to the list of possible links. */
345 lp
= (struct link
*)malloc((unsigned)(sizeof(struct link
)+strlen(p
)));
349 "no memory for links, they will be dumped as separate files");
353 lp
->ino
= hstat
.st_ino
;
354 lp
->dev
= hstat
.st_dev
;
361 * This is not a link to a previously dumped file, so dump it.
363 if (S_ISREG(hstat
.st_mode
)
365 || S_ISCTG(hstat
.st_mode
)
369 int f
; /* File descriptor */
372 register union record
*start
;
376 /* int end_nulls = 0; */
381 if (f_sparse_files
) {
383 * JK - This is the test for sparseness: whether the
384 * "size" of the file matches the number of blocks
385 * allocated for it. If there is a smaller number
386 * of blocks that would be necessary to accommodate
387 * a file of this size, we have a sparse file, i.e.,
388 * at least one of those records in the file is just
391 #ifdef hpux /* Nice of HPUX to gratuitiously change it, huh? - mib */
392 if (hstat
.st_size
- (hstat
.st_blocks
* 1024) > 1024 )
394 if (hstat
.st_size
- (hstat
.st_blocks
* RECORDSIZE
) > RECORDSIZE
)
397 int filesize
= hstat
.st_size
;
400 header
= start_header(p
, &hstat
);
406 header
->header
.linkflag
= LF_SPARSE
;
410 * Call the routine that figures out the
411 * layout of the sparse file in question.
412 * UPPERBOUND is the index of the last
413 * element of the "sparsearray," i.e.,
414 * the number of elements it needed to
418 upperbound
= deal_with_sparse(p
, header
);
421 * See if we'll need an extended header
424 if (upperbound
> SPARSE_IN_HDR
-1)
425 header
->header
.isextended
++;
427 * We store the "real" file size so
428 * we can show that in case someone wants
429 * to list the archive, i.e., tar tvf <file>.
430 * It might be kind of disconcerting if the
431 * shrunken file size was the one that showed
434 to_oct((long) hstat
.st_size
, 1+12,
435 header
->header
.realsize
);
438 * This will be the new "size" of the
439 * file, i.e., the size of the file
440 * minus the records of holes that we're
444 find_new_file_size(&filesize
, upperbound
);
445 hstat
.st_size
= filesize
;
446 to_oct((long) filesize
, 1+12,
447 header
->header
.size
);
448 /* to_oct((long) end_nulls, 1+12,
449 header->header.ending_blanks);*/
451 for (i
= 0; i
< SPARSE_IN_HDR
; i
++) {
452 if (!sparsearray
[i
].numbytes
)
454 to_oct(sparsearray
[i
].offset
, 1+12,
455 header
->header
.sp
[i
].offset
);
456 to_oct(sparsearray
[i
].numbytes
, 1+12,
457 header
->header
.sp
[i
].numbytes
);
463 upperbound
=SPARSE_IN_HDR
-1;
466 sizeleft
= hstat
.st_size
;
467 /* Don't bother opening empty, world readable files. */
468 if (sizeleft
> 0 || 0444 != (0444 & hstat
.st_mode
)) {
469 f
= open(p
, O_RDONLY
|O_BINARY
);
470 if (f
< 0) goto badperror
;
475 /* If the file is sparse, we've already taken care of this */
477 header
= start_header(p
, &hstat
);
478 if (header
== NULL
) {
486 /* Mark contiguous files, if we support them */
487 if (f_standard
&& S_ISCTG(hstat
.st_mode
)) {
488 header
->header
.linkflag
= LF_CONTIG
;
491 isextended
= header
->header
.isextended
;
492 save_linkflag
= header
->header
.linkflag
;
493 finish_header(header
);
497 /* register union record *exhdr;*/
498 /* int arraybound = SPARSE_EXT_HDR;*/
499 /* static */ int index_offset
= SPARSE_IN_HDR
;
501 extend
: exhdr
= findrec();
508 bzero(exhdr
->charptr
, RECORDSIZE
);
509 for (i
= 0; i
< SPARSE_EXT_HDR
; i
++) {
510 if (i
+index_offset
> upperbound
)
512 to_oct((long) sparsearray
[i
+index_offset
].numbytes
,
514 exhdr
->ext_hdr
.sp
[i
].numbytes
);
515 to_oct((long) sparsearray
[i
+index_offset
].offset
,
517 exhdr
->ext_hdr
.sp
[i
].offset
);
521 if (sum < upperbound)
523 if (index_offset
+i
< upperbound
) {
525 exhdr
->ext_hdr
.isextended
++;
530 if (save_linkflag
== LF_SPARSE
) {
531 if (finish_sparse_file(f
, &sizeleft
, hstat
.st_size
, p
))
535 while (sizeleft
> 0) {
539 save_sizeleft
= sizeleft
;
540 save_totsize
= hstat
.st_size
;
544 bufsize
= endofrecs()->charptr
- start
->charptr
;
546 if (sizeleft
< bufsize
) {
547 /* Last read -- zero out area beyond */
548 bufsize
= (int)sizeleft
;
549 count
= bufsize
% RECORDSIZE
;
551 bzero(start
->charptr
+ sizeleft
,
552 (int)(RECORDSIZE
- count
));
554 count
= read(f
, start
->charptr
, bufsize
);
556 msg_perror("read error at byte %ld, reading\
557 %d bytes, in file %s", hstat
.st_size
- sizeleft
, bufsize
,p
);
562 /* This is nonportable (the type of userec's arg). */
563 userec(start
+(count
-1)/RECORDSIZE
);
565 if (count
== bufsize
) continue;
566 msg( "file %s shrunk by %d bytes, padding with zeros.", p
, sizeleft
);
567 goto padit
; /* Short read */
578 if (unlink (p
) == -1)
579 msg_perror ("cannot remove %s", p
);
581 if (f_atime_preserve
)
582 utime (p
, restore_times
);
586 * File shrunk or gave error, pad out tape to match
587 * the size we specified in the header.
591 save_sizeleft
=sizeleft
;
593 bzero(start
->charptr
,RECORDSIZE
);
595 sizeleft
-=RECORDSIZE
;
601 if (f_atime_preserve
)
602 utime (p
, restore_times
);
607 else if(S_ISLNK(hstat
.st_mode
))
611 hstat
.st_size
= 0; /* Force 0 size on symlink */
612 header
= start_header(p
, &hstat
);
618 size
= readlink(p
, header
->header
.linkname
, NAMSIZ
);
619 if (size
< 0) goto badperror
;
620 if (size
== NAMSIZ
) {
621 char *buf
= ck_malloc(PATH_MAX
);
623 readlink(p
,buf
,PATH_MAX
);
624 /* next_mangle(header->header.linkname); */
625 add_symlink_mangle(buf
,p
,header
->header
.linkname
);
626 msg("symbolic link %s too long: mangling to %s",p
, header
->header
.linkname
);
627 /* size=strlen(header->header.linkname); */
630 header
->header
.linkname
[size
] = '\0';
631 header
->header
.linkflag
= LF_SYMLINK
;
632 finish_header(header
); /* Nothing more to do to it */
635 if (unlink (p
) == -1)
636 msg_perror ("cannot remove %s", p
);
642 else if (S_ISDIR(hstat
.st_mode
))
645 register struct direct
*d
;
649 int our_device
= hstat
.st_dev
;
651 /* Build new prototype name */
654 namebuf
=ck_malloc(buflen
+1);
655 strncpy(namebuf
, p
, buflen
);
656 while (len
>= 1 && '/' == namebuf
[len
-1])
657 len
--; /* Delete trailing slashes */
658 namebuf
[len
++] = '/'; /* Now add exactly one back */
659 namebuf
[len
] = '\0'; /* Make sure null-terminated */
662 * Output directory header record with permissions
663 * FIXME, do this AFTER files, to avoid R/O dir problems?
664 * If old archive format, don't write record at all.
667 hstat
.st_size
= 0; /* Force 0 size on dir */
669 * If people could really read standard archives,
670 * this should be: (FIXME)
671 header = start_header(f_standard? p: namebuf, &hstat);
672 * but since they'd interpret LF_DIR records as
673 * regular files, we'd better put the / on the name.
675 header
= start_header(namebuf
, &hstat
);
679 goto badfile
; /* eg name too long */
683 header
->header
.linkflag
= LF_DUMPDIR
;
685 header
->header
.linkflag
= LF_DIR
;
687 /* If we're gnudumping, we aren't done yet so don't close it. */
689 finish_header(header
); /* Done with directory header */
700 buf
=gnu_list_name
->dir_contents
; /* FOO */
702 for(p_buf
=buf
;p_buf
&& *p_buf
;) {
710 to_oct((long)totsize
,1+12,header
->header
.size
);
711 finish_header(header
);
717 save_sizeleft
=sizeleft
;
718 save_totsize
=totsize
;
721 bufsize
=endofrecs()->charptr
- start
->charptr
;
722 if(sizeleft
<bufsize
) {
724 count
=bufsize%RECORDSIZE
;
726 bzero(start
->charptr
+sizeleft
,RECORDSIZE
-count
);
728 bcopy(p_buf
,start
->charptr
,bufsize
);
731 userec(start
+(bufsize
-1)/RECORDSIZE
);
735 if (f_atime_preserve
)
736 utime (p
, restore_times
);
740 /* Now output all the files in the directory */
743 return; /* Unless the cmdline said not to */
746 * See if we are crossing from one file system to another,
747 * and avoid doing so if the user only wants to dump one file system.
749 if (f_local_filesys
&& !toplevel
&& curdev
!= hstat
.st_dev
) {
751 msg("%s: is on a different filesystem; not dumped",p
);
760 msg_perror ("can't open directory %s",p
);
762 msg("error opening directory %s",
768 /* Hack to remove "./" from the front of all the file names */
769 if (len
== 2 && namebuf
[0] == '.' && namebuf
[1]=='/')
772 /* Should speed this up by cd-ing into the dir, FIXME */
773 while (NULL
!= (d
=readdir(dirp
))) {
775 if(is_dot_or_dotdot(d
->d_name
))
778 if (DP_NAMELEN(d
) + len
>= buflen
) {
779 buflen
=len
+DP_NAMELEN(d
);
780 namebuf
=ck_realloc(namebuf
,buflen
+1);
781 /* namebuf[len]='\0';
782 msg("file name %s%s too long",
786 strcpy(namebuf
+len
, d
->d_name
);
787 if(f_exclude
&& check_exclude(namebuf
))
789 dump_file(namebuf
, our_device
, 0);
794 if (f_atime_preserve
)
795 utime (p
, restore_times
);
800 else if (S_ISCHR(hstat
.st_mode
)) {
806 else if (S_ISBLK(hstat
.st_mode
)) {
811 /* Avoid screwy apollo lossage where S_IFIFO == S_IFSOCK */
812 #if (_ISP__M68K == 0) && (_ISP__A88K == 0) && defined(S_ISFIFO)
813 else if (S_ISFIFO(hstat
.st_mode
)) {
819 else if (S_ISSOCK(hstat
.st_mode
)) {
826 if (!f_standard
) goto unknown
;
828 hstat
.st_size
= 0; /* Force 0 size */
829 header
= start_header(p
, &hstat
);
833 goto badfile
; /* eg name too long */
836 header
->header
.linkflag
= type
;
837 #if defined(S_IFBLK) || defined(S_IFCHR)
838 if (type
!= LF_FIFO
) {
839 to_oct((long) major(hstat
.st_rdev
), 8,
840 header
->header
.devmajor
);
841 to_oct((long) minor(hstat
.st_rdev
), 8,
842 header
->header
.devminor
);
846 finish_header(header
);
849 if (unlink (p
) == -1)
850 msg_perror ("cannot remove %s", p
);
855 msg("%s: Unknown file type; file ignored.", p
);
859 finish_sparse_file(fd
, sizeleft
, fullsize
, name
)
866 char tempbuf
[RECORDSIZE
];
874 while (*sizeleft
> 0) {
876 bzero(start
->charptr
, RECORDSIZE
);
877 bufsize
= sparsearray
[sparse_ind
].numbytes
;
878 if (!bufsize
) { /* we blew it, maybe */
879 msg("Wrote %ld of %ld bytes to file %s",
880 fullsize
- *sizeleft
, fullsize
, name
);
883 pos
= lseek(fd
, sparsearray
[sparse_ind
++].offset
, 0);
885 * If the number of bytes to be written here exceeds
886 * the size of the temporary buffer, do it in steps.
888 while (bufsize
> RECORDSIZE
) {
890 count = read(fd, start->charptr+amt_read, RECORDSIZE-amt_read);
891 bufsize -= RECORDSIZE - amt_read;
895 bzero(start->charptr, RECORDSIZE);
898 count
= read(fd
, start
->charptr
, RECORDSIZE
);
900 msg_perror("read error at byte %ld, reading %d bytes, in file %s",
901 fullsize
- *sizeleft
, bufsize
, name
);
907 nwritten
+= RECORDSIZE
; /* XXX */
909 bzero(start
->charptr
, RECORDSIZE
);
913 clear_buffer(tempbuf
);
914 count
= read(fd
, tempbuf
, bufsize
);
915 bcopy(tempbuf
, start
->charptr
, RECORDSIZE
);
917 msg_perror("read error at byte %ld, reading %d bytes, in file %s",
918 fullsize
- *sizeleft
, bufsize
, name
);
921 /* if (amt_read >= RECORDSIZE) {
923 userec(start+(count-1)/RECORDSIZE);
924 if (count != bufsize) {
925 msg("file %s shrunk by %d bytes, padding with zeros.", name, sizeleft);
930 amt_read += bufsize;*/
931 nwritten
+= count
; /* XXX */
937 /* printf ("Amount actually written is (I hope) %d.\n", nwritten); */
938 /* userec(start+(count-1)/RECORDSIZE);*/
950 * Make room for our scratch space -- initially is 10 elts long
952 sparsearray
= (struct sp_array
*) malloc(sp_array_size
* sizeof(struct sp_array
));
953 for (i
= 0; i
< sp_array_size
; i
++) {
954 sparsearray
[i
].offset
= 0;
955 sparsearray
[i
].numbytes
= 0;
962 * Okay, we've got a sparse file on our hands -- now, what we need to do is
963 * make a pass through the file and carefully note where any data is, i.e.,
964 * we want to find how far into the file each instance of data is, and how
965 * many bytes are there. We store this information in the sparsearray,
966 * which will later be translated into header information. For now, we use
967 * the sparsearray as convenient storage.
969 * As a side note, this routine is a mess. If I could have found a cleaner
970 * way to do it, I would have. If anyone wants to find a nicer way to do
974 /* There is little point in trimming small amounts of null data at the */
975 /* head and tail of blocks -- it's ok if we only avoid dumping blocks */
976 /* of complete null data */
978 deal_with_sparse(name
, header
, nulls_at_end
)
980 union record
*header
;
985 /* long save_offset;*/
987 /* int current_size = hstat.st_size;*/
990 char buf
[RECORDSIZE
];
992 int read_last_data
= 0; /* did we just read the last record? */
996 header
->header
.isextended
= 0;
998 * Can't open the file -- this problem will be caught later on,
1001 if ((fd
= open(name
, O_RDONLY
)) < 0)
1007 while ((cc
= read(fd
, buf
, sizeof buf
)) != 0) {
1009 if (sparse_ind
> sp_array_size
-1) {
1012 * realloc the scratch area, since we've run out of room --
1014 sparsearray
= (struct sp_array
*)
1015 realloc(sparsearray
,
1016 2 * sp_array_size
* (sizeof(struct sp_array
)));
1019 if (cc
== sizeof buf
) {
1020 if (zero_record(buf
)) {
1022 sparsearray
[sparse_ind
++].numbytes
1026 } else { /* !zero_record(buf) */
1032 sparsearray
[sparse_ind
].offset
1036 } else if (cc
< sizeof buf
) {
1037 /* This has to be the last bit of the file, so this */
1038 /* is somewhat shorter than the above. */
1039 if (!zero_record(buf
)) {
1043 sparsearray
[sparse_ind
].offset
1053 sparsearray
[sparse_ind
++].numbytes
= numbytes
;
1056 return sparse_ind
- 1;
1060 * Just zeroes out the buffer so we don't confuse ourselves with leftover
1069 for (i
= 0; i
< RECORDSIZE
; i
++)
1073 #if 0 /* I'm leaving this as a monument to Joy Kendall, who wrote it -mib */
1076 * This routine takes a character array, and tells where within that array
1077 * the data can be found. It skips over any zeros, and sets the first
1078 * non-zero point in the array to be the "start", and continues until it
1079 * finds non-data again, which is marked as the "end." This routine is
1080 * mainly for 1) seeing how far into a file we must lseek to data, given
1081 * that we have a sparse file, and 2) determining the "real size" of the
1082 * file, i.e., the number of bytes in the sparse file that are data, as
1083 * opposed to the zeros we are trying to skip.
1085 where_is_data(from
, to
, buffer
)
1091 register int save_to
= *to
;
1092 int amidst_data
= 0;
1099 if (*from
< 16) /* don't bother */
1101 /* keep going to make sure there isn't more real
1102 data in this record */
1103 while (i
< RECORDSIZE
) {
1111 else if (buffer
[i
]) {
1117 if (i
== RECORDSIZE
)
1125 /* Note that this routine is only called if zero_record returned true */
1126 #if 0 /* But we actually don't need it at all. */
1127 where_is_data (from
, to
, buffer
)
1133 for (fp
= buffer
; ! *fp
; fp
++)
1135 for (tp
= buffer
+ RECORDSIZE
- 1; ! *tp
; tp
--)
1137 *from
= fp
- buffer
;
1138 *to
= tp
- buffer
+ 1;
1145 * Takes a recordful of data and basically cruises through it to see if
1146 * it's made *entirely* of zeros, returning a 0 the instant it finds
1147 * something that is a non-zero, i.e., useful data.
1155 for (i
= 0; i
< RECORDSIZE
; i
++)
1156 if (buffer
[i
] != '\000')
1162 find_new_file_size(filesize
, highest_index
)
1169 for (i
= 0; sparsearray
[i
].numbytes
&& i
<= highest_index
; i
++)
1170 *filesize
+= sparsearray
[i
].numbytes
;
1174 * Make a header block for the file name whose stat info is st .
1175 * Return header pointer for success, NULL if the name is too long.
1178 start_header(name
, st
)
1180 register struct stat
*st
;
1182 register union record
*header
;
1184 header
= (union record
*) findrec();
1185 bzero(header
->charptr
, sizeof(*header
)); /* XXX speed up */
1188 * Check the file name and put it in the record.
1190 if(!f_absolute_paths
) {
1191 static int warned_once
= 0;
1196 msg("Removing drive spec from names in the archive");
1199 while ('/' == *name
) {
1200 name
++; /* Force relative path */
1202 msg("Removing leading / from absolute path names in the archive.");
1205 strncpy(header
->header
.name
, name
, NAMSIZ
);
1206 if (header
->header
.name
[NAMSIZ
-1]) {
1209 /* next_mangle(header->header.name); */
1210 add_mangle(name
,header
->header
.name
);
1211 msg("%s: is too long: mangling to %s", name
, header
->header
.name
);
1214 to_oct((long) (st
->st_mode
& 07777),
1215 8, header
->header
.mode
);
1216 to_oct((long) st
->st_uid
, 8, header
->header
.uid
);
1217 to_oct((long) st
->st_gid
, 8, header
->header
.gid
);
1218 to_oct((long) st
->st_size
, 1+12, header
->header
.size
);
1219 to_oct((long) st
->st_mtime
, 1+12, header
->header
.mtime
);
1220 /* header->header.linkflag is left as null */
1222 to_oct((long) st
->st_atime
, 1+12, header
->header
.atime
);
1223 to_oct((long) st
->st_ctime
, 1+12, header
->header
.ctime
);
1227 /* Fill in new Unix Standard fields if desired. */
1229 header
->header
.linkflag
= LF_NORMAL
; /* New default */
1230 strcpy(header
->header
.magic
, TMAGIC
); /* Mark as Unix Std */
1231 finduname(header
->header
.uname
, st
->st_uid
);
1232 findgname(header
->header
.gname
, st
->st_gid
);
1239 * Finish off a filled-in header block and write it out.
1240 * We also print the file name and/or full info if verbose is on.
1243 finish_header(header
)
1244 register union record
*header
;
1246 register int i
, sum
;
1249 bcopy(CHKBLANKS
, header
->header
.chksum
, sizeof(header
->header
.chksum
));
1252 p
= header
->charptr
;
1253 for (i
= sizeof(*header
); --i
>= 0; ) {
1255 * We can't use unsigned char here because of old compilers,
1262 * Fill in the checksum field. It's formatted differently
1263 * from the other fields: it has [6] digits, a null, then a
1264 * space -- rather than digits, a space, then a null.
1265 * We use to_oct then write the null in over to_oct's space.
1266 * The final space is already there, from checksumming, and
1267 * to_oct doesn't modify it.
1269 * This is a fast way to do:
1270 * (void) sprintf(header->header.chksum, "%6o", sum);
1272 to_oct((long) sum
, 8, header
->header
.chksum
);
1273 header
->header
.chksum
[6] = '\0'; /* Zap the space */
1278 extern union record
*head
; /* Points to current tape header */
1279 extern int head_standard
; /* Tape header is in ANSI format */
1281 /* These globals are parameters to print_header, sigh */
1283 /* hstat is already set up */
1284 head_standard
= f_standard
;
1293 * Quick and dirty octal conversion.
1294 * Converts long "value" into a "digs"-digit field at "where",
1295 * including a trailing space and room for a null. "digs"==3 means
1296 * 1 digit, a space, and room for a null.
1298 * We assume the trailing null is already there and don't fill it in.
1299 * This fact is used by start_header and finish_header, so don't change it!
1301 * This should be equivalent to:
1302 * (void) sprintf(where, "%*lo ", digs-2, value);
1303 * except that sprintf fills in the trailing null and we don't.
1306 to_oct(value
, digs
, where
)
1307 register long value
;
1309 register char *where
;
1312 --digs
; /* Trailing null slot is left alone */
1313 where
[--digs
] = ' '; /* Put in the space, though */
1315 /* Produce the digits -- at least one */
1317 where
[--digs
] = '0' + (char)(value
& 7); /* one octal digit */
1319 } while (digs
> 0 && value
!= 0);
1321 /* Leading spaces, if necessary */
1323 where
[--digs
] = ' ';
1329 * Write the EOT record(s).
1330 * We actually zero at least one record, through the end of the block.
1331 * Old tar writes garbage after two zeroed records -- and PDtar used to.
1342 bufsize
= endofrecs()->charptr
- p
->charptr
;
1343 bzero(p
->charptr
, bufsize
);
This page took 0.087823 seconds and 5 git commands to generate.