]>
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)
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);
175 dump_file (p
, -1, 1);
176 while (p
= name_next (1));
187 * Dump a single file. If it's a directory, recurse.
188 * Result is 1 for success, 0 for failure.
189 * Sets global "hstat" to stat() output for this file.
192 dump_file (p
, curdev
, toplevel
)
193 char *p
; /* File name to dump */
194 int curdev
; /* Device our parent dir was on */
195 int toplevel
; /* Whether we are a toplevel call */
197 union record
*header
;
199 extern char *save_name
; /* JF for multi-volume support */
200 extern long save_totsize
;
201 extern long save_sizeleft
;
204 extern time_t new_time
;
205 int critical_error
= 0;
206 struct utimbuf restore_times
;
207 /* int sparse_ind = 0;*/
210 if (f_confirm
&& !confirm ("add", p
))
214 * Use stat if following (rather than dumping) 4.2BSD's
215 * symbolic links. Otherwise, use lstat (which, on non-4.2
216 * systems, is #define'd to stat anyway.
218 #ifdef STX_HIDDEN /* AIX */
219 if (0 != f_follow_links
?
220 statx (p
, &hstat
, STATSIZE
, STX_HIDDEN
) :
221 statx (p
, &hstat
, STATSIZE
, STX_HIDDEN
| STX_LINK
))
223 if (0 != f_follow_links
? stat (p
, &hstat
) : lstat (p
, &hstat
))
227 msg_perror ("can't add file %s", p
);
229 if (!f_ignore_failed_read
|| critical_error
)
234 restore_times
.actime
= hstat
.st_atime
;
235 restore_times
.modtime
= hstat
.st_mtime
;
238 if (S_ISHIDDEN (hstat
.st_mode
))
240 char *new = (char *) alloca (strlen (p
) + 2);
250 /* See if we only want new files, and check if this one is too old to
251 put in the archive. */
254 && new_time
> hstat
.st_mtime
255 && !S_ISDIR (hstat
.st_mode
)
256 && (f_new_files
> 1 || new_time
> hstat
.st_ctime
))
260 msg ("%s: is unchanged; not dumped", p
);
266 /* See if we are trying to dump the archive */
267 if (ar_dev
&& hstat
.st_dev
== ar_dev
&& hstat
.st_ino
== ar_ino
)
269 msg ("%s is the archive; not dumped", p
);
274 * Check for multiple links.
276 * We maintain a list of all such files that we've written so
277 * far. Any time we see another, we check the list and
278 * avoid dumping the data again if we've done it once already.
280 if (hstat
.st_nlink
> 1
281 && (S_ISREG (hstat
.st_mode
)
283 || S_ISCTG (hstat
.st_mode
)
286 || S_ISCHR (hstat
.st_mode
)
289 || S_ISBLK (hstat
.st_mode
)
292 || S_ISFIFO (hstat
.st_mode
)
296 register struct link
*lp
;
298 /* First quick and dirty. Hashing, etc later FIXME */
299 for (lp
= linklist
; lp
; lp
= lp
->next
)
301 if (lp
->ino
== hstat
.st_ino
&&
302 lp
->dev
== hstat
.st_dev
)
304 char *link_name
= lp
->name
;
306 /* We found a link. */
307 while (!f_absolute_paths
&& *link_name
== '/')
309 static int link_warn
= 0;
313 msg ("Removing leading / from absolute links");
318 if (link_name
- lp
->name
>= NAMSIZ
)
319 write_long (link_name
, LF_LONGLINK
);
320 current_link_name
= link_name
;
323 header
= start_header (p
, &hstat
);
329 strncpy (header
->header
.arch_linkname
,
332 /* Force null truncated */
333 header
->header
.arch_linkname
[NAMSIZ
- 1] = 0;
335 header
->header
.linkflag
= LF_LINK
;
336 finish_header (header
);
337 /* FIXME: Maybe remove from list after all links found? */
340 if (unlink (p
) == -1)
341 msg_perror ("cannot remove %s", p
);
343 return; /* We dumped it */
347 /* Not found. Add it to the list of possible links. */
348 lp
= (struct link
*) malloc ((unsigned) (sizeof (struct link
) + strlen (p
)));
354 "no memory for links, they will be dumped as separate files");
358 lp
->ino
= hstat
.st_ino
;
359 lp
->dev
= hstat
.st_dev
;
360 strcpy (lp
->name
, p
);
366 * This is not a link to a previously dumped file, so dump it.
368 if (S_ISREG (hstat
.st_mode
)
370 || S_ISCTG (hstat
.st_mode
)
374 int f
; /* File descriptor */
377 register union record
*start
;
381 /* int end_nulls = 0; */
389 * JK - This is the test for sparseness: whether the
390 * "size" of the file matches the number of blocks
391 * allocated for it. If there is a smaller number
392 * of blocks that would be necessary to accommodate
393 * a file of this size, we have a sparse file, i.e.,
394 * at least one of those records in the file is just
397 #ifdef hpux /* Nice of HPUX to gratuitiously change it, huh? - mib */
398 if (hstat
.st_size
- (hstat
.st_blocks
* 1024) > 1024)
400 if (hstat
.st_size
- (hstat
.st_blocks
* RECORDSIZE
) > RECORDSIZE
)
403 int filesize
= hstat
.st_size
;
406 header
= start_header (p
, &hstat
);
412 header
->header
.linkflag
= LF_SPARSE
;
416 * Call the routine that figures out the
417 * layout of the sparse file in question.
418 * UPPERBOUND is the index of the last
419 * element of the "sparsearray," i.e.,
420 * the number of elements it needed to
424 upperbound
= deal_with_sparse (p
, header
);
427 * See if we'll need an extended header
430 if (upperbound
> SPARSE_IN_HDR
- 1)
431 header
->header
.isextended
++;
433 * We store the "real" file size so
434 * we can show that in case someone wants
435 * to list the archive, i.e., tar tvf <file>.
436 * It might be kind of disconcerting if the
437 * shrunken file size was the one that showed
440 to_oct ((long) hstat
.st_size
, 1 + 12,
441 header
->header
.realsize
);
444 * This will be the new "size" of the
445 * file, i.e., the size of the file
446 * minus the records of holes that we're
450 find_new_file_size (&filesize
, upperbound
);
451 hstat
.st_size
= filesize
;
452 to_oct ((long) filesize
, 1 + 12,
453 header
->header
.size
);
454 /* to_oct((long) end_nulls, 1+12,
455 header->header.ending_blanks);*/
457 for (i
= 0; i
< SPARSE_IN_HDR
; i
++)
459 if (!sparsearray
[i
].numbytes
)
461 to_oct (sparsearray
[i
].offset
, 1 + 12,
462 header
->header
.sp
[i
].offset
);
463 to_oct (sparsearray
[i
].numbytes
, 1 + 12,
464 header
->header
.sp
[i
].numbytes
);
470 upperbound
= SPARSE_IN_HDR
- 1;
473 sizeleft
= hstat
.st_size
;
474 /* Don't bother opening empty, world readable files. */
475 if (sizeleft
> 0 || 0444 != (0444 & hstat
.st_mode
))
477 f
= open (p
, O_RDONLY
| O_BINARY
);
486 /* If the file is sparse, we've already taken care of this */
489 header
= start_header (p
, &hstat
);
499 /* Mark contiguous files, if we support them */
500 if (f_standard
&& S_ISCTG (hstat
.st_mode
))
502 header
->header
.linkflag
= LF_CONTIG
;
505 isextended
= header
->header
.isextended
;
506 save_linkflag
= header
->header
.linkflag
;
507 finish_header (header
);
512 /* register union record *exhdr;*/
513 /* int arraybound = SPARSE_EXT_HDR;*/
514 /* static */ int index_offset
= SPARSE_IN_HDR
;
516 extend
:exhdr
= findrec ();
523 bzero (exhdr
->charptr
, RECORDSIZE
);
524 for (i
= 0; i
< SPARSE_EXT_HDR
; i
++)
526 if (i
+ index_offset
> upperbound
)
528 to_oct ((long) sparsearray
[i
+ index_offset
].numbytes
,
530 exhdr
->ext_hdr
.sp
[i
].numbytes
);
531 to_oct ((long) sparsearray
[i
+ index_offset
].offset
,
533 exhdr
->ext_hdr
.sp
[i
].offset
);
537 if (sum < upperbound)
539 if (index_offset
+ i
< upperbound
)
542 exhdr
->ext_hdr
.isextended
++;
547 if (save_linkflag
== LF_SPARSE
)
549 if (finish_sparse_file (f
, &sizeleft
, hstat
.st_size
, p
))
559 save_sizeleft
= sizeleft
;
560 save_totsize
= hstat
.st_size
;
564 bufsize
= endofrecs ()->charptr
- start
->charptr
;
566 if (sizeleft
< bufsize
)
568 /* Last read -- zero out area beyond */
569 bufsize
= (int) sizeleft
;
570 count
= bufsize
% RECORDSIZE
;
572 bzero (start
->charptr
+ sizeleft
,
573 (int) (RECORDSIZE
- count
));
575 count
= read (f
, start
->charptr
, bufsize
);
578 msg_perror ("read error at byte %ld, reading\
579 %d bytes, in file %s", hstat
.st_size
- sizeleft
, bufsize
, p
);
584 /* This is nonportable (the type of userec's arg). */
585 userec (start
+ (count
- 1) / RECORDSIZE
);
587 if (count
== bufsize
)
589 msg ("file %s shrunk by %d bytes, padding with zeros.", p
, sizeleft
);
590 goto padit
; /* Short read */
601 if (unlink (p
) == -1)
602 msg_perror ("cannot remove %s", p
);
604 if (f_atime_preserve
)
605 utime (p
, &restore_times
);
609 * File shrunk or gave error, pad out tape to match
610 * the size we specified in the header.
615 save_sizeleft
= sizeleft
;
617 bzero (start
->charptr
, RECORDSIZE
);
619 sizeleft
-= RECORDSIZE
;
625 if (f_atime_preserve
)
626 utime (p
, &restore_times
);
631 else if (S_ISLNK (hstat
.st_mode
))
634 char *buf
= alloca (PATH_MAX
+ 1);
636 size
= readlink (p
, buf
, PATH_MAX
+ 1);
641 write_long (buf
, LF_LONGLINK
);
642 current_link_name
= buf
;
644 hstat
.st_size
= 0; /* Force 0 size on symlink */
645 header
= start_header (p
, &hstat
);
651 strncpy (header
->header
.arch_linkname
, buf
, NAMSIZ
);
652 header
->header
.arch_linkname
[NAMSIZ
- 1] = '\0';
653 header
->header
.linkflag
= LF_SYMLINK
;
654 finish_header (header
); /* Nothing more to do to it */
657 if (unlink (p
) == -1)
658 msg_perror ("cannot remove %s", p
);
664 else if (S_ISDIR (hstat
.st_mode
))
667 register struct dirent
*d
;
671 int our_device
= hstat
.st_dev
;
673 /* Build new prototype name */
675 buflen
= len
+ NAMSIZ
;
676 namebuf
= ck_malloc (buflen
+ 1);
677 strncpy (namebuf
, p
, buflen
);
678 while (len
>= 1 && '/' == namebuf
[len
- 1])
679 len
--; /* Delete trailing slashes */
680 namebuf
[len
++] = '/'; /* Now add exactly one back */
681 namebuf
[len
] = '\0'; /* Make sure null-terminated */
684 * Output directory header record with permissions
685 * FIXME, do this AFTER files, to avoid R/O dir problems?
686 * If old archive format, don't write record at all.
690 hstat
.st_size
= 0; /* Force 0 size on dir */
692 * If people could really read standard archives,
693 * this should be: (FIXME)
694 header = start_header(f_standard? p: namebuf, &hstat);
695 * but since they'd interpret LF_DIR records as
696 * regular files, we'd better put the / on the name.
698 header
= start_header (namebuf
, &hstat
);
702 goto badfile
; /* eg name too long */
706 header
->header
.linkflag
= LF_DUMPDIR
;
708 header
->header
.linkflag
= LF_DIR
;
710 /* If we're gnudumping, we aren't done yet so don't close it. */
712 finish_header (header
); /* Done with directory header */
724 buf
= gnu_list_name
->dir_contents
; /* FOO */
726 for (p_buf
= buf
; p_buf
&& *p_buf
;)
730 tmp
= strlen (p_buf
) + 1;
735 to_oct ((long) totsize
, 1 + 12, header
->header
.size
);
736 finish_header (header
);
744 save_sizeleft
= sizeleft
;
745 save_totsize
= totsize
;
748 bufsize
= endofrecs ()->charptr
- start
->charptr
;
749 if (sizeleft
< bufsize
)
752 count
= bufsize
% RECORDSIZE
;
754 bzero (start
->charptr
+ sizeleft
, RECORDSIZE
- count
);
756 bcopy (p_buf
, start
->charptr
, bufsize
);
759 userec (start
+ (bufsize
- 1) / RECORDSIZE
);
763 if (f_atime_preserve
)
764 utime (p
, &restore_times
);
768 /* Now output all the files in the directory */
771 return; /* Unless the cmdline said not to */
774 * See if we are crossing from one file system to another,
775 * and avoid doing so if the user only wants to dump one file system.
777 if (f_local_filesys
&& !toplevel
&& curdev
!= hstat
.st_dev
)
780 msg ("%s: is on a different filesystem; not dumped", p
);
791 msg_perror ("can't open directory %s", p
);
795 msg ("error opening directory %s",
801 /* Hack to remove "./" from the front of all the file names */
802 if (len
== 2 && namebuf
[0] == '.' && namebuf
[1] == '/')
805 /* Should speed this up by cd-ing into the dir, FIXME */
806 while (NULL
!= (d
= readdir (dirp
)))
809 if (is_dot_or_dotdot (d
->d_name
))
812 if (NLENGTH (d
) + len
>= buflen
)
814 buflen
= len
+ NLENGTH (d
);
815 namebuf
= ck_realloc (namebuf
, buflen
+ 1);
816 /* namebuf[len]='\0';
817 msg("file name %s%s too long",
821 strcpy (namebuf
+ len
, d
->d_name
);
822 if (f_exclude
&& check_exclude (namebuf
))
824 dump_file (namebuf
, our_device
, 0);
829 if (f_atime_preserve
)
830 utime (p
, &restore_times
);
835 else if (S_ISCHR (hstat
.st_mode
))
842 else if (S_ISBLK (hstat
.st_mode
))
848 /* Avoid screwy apollo lossage where S_IFIFO == S_IFSOCK */
849 #if (_ISP__M68K == 0) && (_ISP__A88K == 0) && defined(S_ISFIFO)
850 else if (S_ISFIFO (hstat
.st_mode
))
857 else if (S_ISSOCK (hstat
.st_mode
))
868 hstat
.st_size
= 0; /* Force 0 size */
869 header
= start_header (p
, &hstat
);
873 goto badfile
; /* eg name too long */
876 header
->header
.linkflag
= type
;
877 #if defined(S_IFBLK) || defined(S_IFCHR)
880 to_oct ((long) major (hstat
.st_rdev
), 8,
881 header
->header
.devmajor
);
882 to_oct ((long) minor (hstat
.st_rdev
), 8,
883 header
->header
.devminor
);
887 finish_header (header
);
890 if (unlink (p
) == -1)
891 msg_perror ("cannot remove %s", p
);
896 msg ("%s: Unknown file type; file ignored.", p
);
900 finish_sparse_file (fd
, sizeleft
, fullsize
, name
)
902 long *sizeleft
, fullsize
;
906 char tempbuf
[RECORDSIZE
];
907 int bufsize
, sparse_ind
= 0, count
;
912 while (*sizeleft
> 0)
915 bzero (start
->charptr
, RECORDSIZE
);
916 bufsize
= sparsearray
[sparse_ind
].numbytes
;
918 { /* we blew it, maybe */
919 msg ("Wrote %ld of %ld bytes to file %s",
920 fullsize
- *sizeleft
, fullsize
, name
);
923 pos
= lseek (fd
, sparsearray
[sparse_ind
++].offset
, 0);
925 * If the number of bytes to be written here exceeds
926 * the size of the temporary buffer, do it in steps.
928 while (bufsize
> RECORDSIZE
)
931 count = read(fd, start->charptr+amt_read, RECORDSIZE-amt_read);
932 bufsize -= RECORDSIZE - amt_read;
936 bzero(start->charptr, RECORDSIZE);
939 count
= read (fd
, start
->charptr
, RECORDSIZE
);
942 msg_perror ("read error at byte %ld, reading %d bytes, in file %s",
943 fullsize
- *sizeleft
, bufsize
, name
);
949 nwritten
+= RECORDSIZE
; /* XXX */
951 bzero (start
->charptr
, RECORDSIZE
);
955 clear_buffer (tempbuf
);
956 count
= read (fd
, tempbuf
, bufsize
);
957 bcopy (tempbuf
, start
->charptr
, RECORDSIZE
);
960 msg_perror ("read error at byte %ld, reading %d bytes, in file %s",
961 fullsize
- *sizeleft
, bufsize
, name
);
964 /* if (amt_read >= RECORDSIZE) {
966 userec(start+(count-1)/RECORDSIZE);
967 if (count != bufsize) {
968 msg("file %s shrunk by %d bytes, padding with zeros.", name, sizeleft);
973 amt_read += bufsize;*/
974 nwritten
+= count
; /* XXX */
980 /* printf ("Amount actually written is (I hope) %d.\n", nwritten); */
981 /* userec(start+(count-1)/RECORDSIZE);*/
993 * Make room for our scratch space -- initially is 10 elts long
995 sparsearray
= (struct sp_array
*) malloc (sp_array_size
* sizeof (struct sp_array
));
996 for (i
= 0; i
< sp_array_size
; i
++)
998 sparsearray
[i
].offset
= 0;
999 sparsearray
[i
].numbytes
= 0;
1006 * Okay, we've got a sparse file on our hands -- now, what we need to do is
1007 * make a pass through the file and carefully note where any data is, i.e.,
1008 * we want to find how far into the file each instance of data is, and how
1009 * many bytes are there. We store this information in the sparsearray,
1010 * which will later be translated into header information. For now, we use
1011 * the sparsearray as convenient storage.
1013 * As a side note, this routine is a mess. If I could have found a cleaner
1014 * way to do it, I would have. If anyone wants to find a nicer way to do
1018 /* There is little point in trimming small amounts of null data at the */
1019 /* head and tail of blocks -- it's ok if we only avoid dumping blocks */
1020 /* of complete null data */
1022 deal_with_sparse (name
, header
, nulls_at_end
)
1024 union record
*header
;
1029 /* long save_offset;*/
1031 /* int current_size = hstat.st_size;*/
1032 int sparse_ind
= 0, cc
;
1033 char buf
[RECORDSIZE
];
1035 int read_last_data
= 0; /* did we just read the last record? */
1037 int amidst_data
= 0;
1039 header
->header
.isextended
= 0;
1041 * Can't open the file -- this problem will be caught later on,
1044 if ((fd
= open (name
, O_RDONLY
)) < 0)
1047 init_sparsearray ();
1050 while ((cc
= read (fd
, buf
, sizeof buf
)) != 0)
1053 if (sparse_ind
> sp_array_size
- 1)
1057 * realloc the scratch area, since we've run out of room --
1059 sparsearray
= (struct sp_array
*)
1060 realloc (sparsearray
,
1061 2 * sp_array_size
* (sizeof (struct sp_array
)));
1064 if (cc
== sizeof buf
)
1066 if (zero_record (buf
))
1070 sparsearray
[sparse_ind
++].numbytes
1076 { /* !zero_record(buf) */
1083 sparsearray
[sparse_ind
].offset
1088 else if (cc
< sizeof buf
)
1090 /* This has to be the last bit of the file, so this */
1091 /* is somewhat shorter than the above. */
1092 if (!zero_record (buf
))
1098 sparsearray
[sparse_ind
].offset
1109 sparsearray
[sparse_ind
++].numbytes
= numbytes
;
1112 return sparse_ind
- 1;
1116 * Just zeroes out the buffer so we don't confuse ourselves with leftover
1125 for (i
= 0; i
< RECORDSIZE
; i
++)
1129 #if 0 /* I'm leaving this as a monument to Joy Kendall, who wrote it -mib */
1132 * This routine takes a character array, and tells where within that array
1133 * the data can be found. It skips over any zeros, and sets the first
1134 * non-zero point in the array to be the "start", and continues until it
1135 * finds non-data again, which is marked as the "end." This routine is
1136 * mainly for 1) seeing how far into a file we must lseek to data, given
1137 * that we have a sparse file, and 2) determining the "real size" of the
1138 * file, i.e., the number of bytes in the sparse file that are data, as
1139 * opposed to the zeros we are trying to skip.
1141 where_is_data (from
, to
, buffer
)
1146 register int save_to
= *to
;
1147 int amidst_data
= 0;
1154 if (*from
< 16) /* don't bother */
1156 /* keep going to make sure there isn't more real
1157 data in this record */
1158 while (i
< RECORDSIZE
)
1176 if (i
== RECORDSIZE
)
1185 /* Note that this routine is only called if zero_record returned true */
1186 #if 0 /* But we actually don't need it at all. */
1187 where_is_data (from
, to
, buffer
)
1193 for (fp
= buffer
; !*fp
; fp
++)
1195 for (tp
= buffer
+ RECORDSIZE
- 1; !*tp
; tp
--)
1197 *from
= fp
- buffer
;
1198 *to
= tp
- buffer
+ 1;
1206 * Takes a recordful of data and basically cruises through it to see if
1207 * it's made *entirely* of zeros, returning a 0 the instant it finds
1208 * something that is a non-zero, i.e., useful data.
1211 zero_record (buffer
)
1216 for (i
= 0; i
< RECORDSIZE
; i
++)
1217 if (buffer
[i
] != '\000')
1223 find_new_file_size (filesize
, highest_index
)
1230 for (i
= 0; sparsearray
[i
].numbytes
&& i
<= highest_index
; i
++)
1231 *filesize
+= sparsearray
[i
].numbytes
;
1235 * Make a header block for the file name whose stat info is st .
1236 * Return header pointer for success, NULL if the name is too long.
1239 start_header (name
, st
)
1241 register struct stat
*st
;
1243 register union record
*header
;
1245 if (strlen (name
) >= NAMSIZ
)
1246 write_long (name
, LF_LONGNAME
);
1248 header
= (union record
*) findrec ();
1249 bzero (header
->charptr
, sizeof (*header
)); /* XXX speed up */
1252 * Check the file name and put it in the record.
1254 if (!f_absolute_paths
)
1256 static int warned_once
= 0;
1262 msg ("Removing drive spec from names in the archive");
1265 while ('/' == *name
)
1267 name
++; /* Force relative path */
1269 msg ("Removing leading / from absolute path names in the archive.");
1272 current_file_name
= name
;
1273 strncpy (header
->header
.arch_name
, name
, NAMSIZ
);
1274 header
->header
.arch_name
[NAMSIZ
- 1] = '\0';
1276 to_oct ((long) (st
->st_mode
& 07777),
1277 8, header
->header
.mode
);
1278 to_oct ((long) st
->st_uid
, 8, header
->header
.uid
);
1279 to_oct ((long) st
->st_gid
, 8, header
->header
.gid
);
1280 to_oct ((long) st
->st_size
, 1 + 12, header
->header
.size
);
1281 to_oct ((long) st
->st_mtime
, 1 + 12, header
->header
.mtime
);
1282 /* header->header.linkflag is left as null */
1285 to_oct ((long) st
->st_atime
, 1 + 12, header
->header
.atime
);
1286 to_oct ((long) st
->st_ctime
, 1 + 12, header
->header
.ctime
);
1290 /* Fill in new Unix Standard fields if desired. */
1293 header
->header
.linkflag
= LF_NORMAL
; /* New default */
1294 strcpy (header
->header
.magic
, TMAGIC
); /* Mark as Unix Std */
1295 finduname (header
->header
.uname
, st
->st_uid
);
1296 findgname (header
->header
.gname
, st
->st_gid
);
1303 * Finish off a filled-in header block and write it out.
1304 * We also print the file name and/or full info if verbose is on.
1307 finish_header (header
)
1308 register union record
*header
;
1310 register int i
, sum
;
1313 bcopy (CHKBLANKS
, header
->header
.chksum
, sizeof (header
->header
.chksum
));
1316 p
= header
->charptr
;
1317 for (i
= sizeof (*header
); --i
>= 0;)
1320 * We can't use unsigned char here because of old compilers,
1327 * Fill in the checksum field. It's formatted differently
1328 * from the other fields: it has [6] digits, a null, then a
1329 * space -- rather than digits, a space, then a null.
1330 * We use to_oct then write the null in over to_oct's space.
1331 * The final space is already there, from checksumming, and
1332 * to_oct doesn't modify it.
1334 * This is a fast way to do:
1335 * (void) sprintf(header->header.chksum, "%6o", sum);
1337 to_oct ((long) sum
, 8, header
->header
.chksum
);
1338 header
->header
.chksum
[6] = '\0'; /* Zap the space */
1344 extern union record
*head
;/* Points to current tape header */
1345 extern int head_standard
; /* Tape header is in ANSI format */
1347 /* These globals are parameters to print_header, sigh */
1349 /* hstat is already set up */
1350 head_standard
= f_standard
;
1359 * Quick and dirty octal conversion.
1360 * Converts long "value" into a "digs"-digit field at "where",
1361 * including a trailing space and room for a null. "digs"==3 means
1362 * 1 digit, a space, and room for a null.
1364 * We assume the trailing null is already there and don't fill it in.
1365 * This fact is used by start_header and finish_header, so don't change it!
1367 * This should be equivalent to:
1368 * (void) sprintf(where, "%*lo ", digs-2, value);
1369 * except that sprintf fills in the trailing null and we don't.
1372 to_oct (value
, digs
, where
)
1373 register long value
;
1375 register char *where
;
1378 --digs
; /* Trailing null slot is left alone */
1379 where
[--digs
] = ' '; /* Put in the space, though */
1381 /* Produce the digits -- at least one */
1384 where
[--digs
] = '0' + (char) (value
& 7); /* one octal digit */
1387 while (digs
> 0 && value
!= 0);
1389 /* Leading spaces, if necessary */
1391 where
[--digs
] = ' ';
1397 * Write the EOT record(s).
1398 * We actually zero at least one record, through the end of the block.
1399 * Old tar writes garbage after two zeroed records -- and PDtar used to.
1410 bufsize
= endofrecs ()->charptr
- p
->charptr
;
1411 bzero (p
->charptr
, bufsize
);
1416 /* Write a LF_LONGLINK or LF_LONGNAME record. */
1418 write_long (p
, type
)
1422 int size
= strlen (p
) + 1;
1424 union record
*header
;
1428 bzero (&foo
, sizeof foo
);
1431 header
= start_header ("././@LongLink", &foo
);
1432 header
->header
.linkflag
= type
;
1433 finish_header (header
);
1435 header
= findrec ();
1437 bufsize
= endofrecs ()->charptr
- header
->charptr
;
1439 while (bufsize
< size
)
1441 bcopy (p
, header
->charptr
, bufsize
);
1444 userec (header
+ (bufsize
- 1) / RECORDSIZE
);
1445 header
= findrec ();
1446 bufsize
= endofrecs ()->charptr
- header
->charptr
;
1448 bcopy (p
, header
->charptr
, size
);
1449 bzero (header
->charptr
+ size
, bufsize
- size
);
1450 userec (header
+ (size
- 1) / RECORDSIZE
);
This page took 0.097553 seconds and 4 git commands to generate.