1 /* vi: set sw=4 ts=4: */
3 * Mini tar implementation for busybox
5 * Note, that as of BusyBox-0.43, tar has been completely rewritten from the
6 * ground up. It still has remnents of the old code lying about, but it is
7 * very different now (i.e. cleaner, less global variables, etc)
9 * Copyright (C) 2000 by Lineo, inc.
10 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
12 * Based in part in the tar implementation in sash
13 * Copyright (c) 1999 by David I. Bell
14 * Permission is granted to use, distribute, or modify this source,
15 * provided that this copyright notice remains intact.
16 * Permission to distribute sash derived code under the GPL has been granted.
18 * Based in part on the tar implementation from busybox-0.28
19 * Copyright (C) 1995 Bruce Perens
20 * This is free software under the GNU General Public License.
22 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation; either version 2 of the License, or
25 * (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30 * General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
40 #define BB_DECLARE_EXTERN
41 #define bb_need_io_error
42 #define bb_need_name_longer_than_foo
51 #include <sys/types.h>
52 #include <sys/sysmacros.h>
55 #ifdef BB_FEATURE_TAR_GZIP
56 extern int unzip(int in, int out);
57 extern int gunzip_init();
60 /* Tar file constants */
62 #define MAJOR(dev) (((dev)>>8)&0xff)
63 #define MINOR(dev) ((dev)&0xff)
68 /* POSIX tar Header Block, from POSIX 1003.1-1990 */
72 char name[NAME_SIZE]; /* 0-99 */
73 char mode[8]; /* 100-107 */
74 char uid[8]; /* 108-115 */
75 char gid[8]; /* 116-123 */
76 char size[12]; /* 124-135 */
77 char mtime[12]; /* 136-147 */
78 char chksum[8]; /* 148-155 */
79 char typeflag; /* 156-156 */
80 char linkname[NAME_SIZE]; /* 157-256 */
81 char magic[6]; /* 257-262 */
82 char version[2]; /* 263-264 */
83 char uname[32]; /* 265-296 */
84 char gname[32]; /* 297-328 */
85 char devmajor[8]; /* 329-336 */
86 char devminor[8]; /* 337-344 */
87 char prefix[155]; /* 345-499 */
88 char padding[12]; /* 500-512 (pad to exactly the TAR_BLOCK_SIZE) */
90 typedef struct TarHeader TarHeader;
93 /* A few useful constants */
94 #define TAR_MAGIC "ustar" /* ustar and a null */
95 #define TAR_VERSION " " /* Be compatable with GNU tar format */
96 #define TAR_MAGIC_LEN 6
97 #define TAR_VERSION_LEN 2
98 #define TAR_BLOCK_SIZE 512
100 /* A nice enum with all the possible tar file content types */
103 REGTYPE = '0', /* regular file */
104 REGTYPE0 = '\0', /* regular file (ancient bug compat)*/
105 LNKTYPE = '1', /* hard link */
106 SYMTYPE = '2', /* symbolic link */
107 CHRTYPE = '3', /* character special */
108 BLKTYPE = '4', /* block special */
109 DIRTYPE = '5', /* directory */
110 FIFOTYPE = '6', /* FIFO special */
111 CONTTYPE = '7', /* reserved */
112 GNULONGLINK = 'K', /* GNU long (>100 chars) link name */
113 GNULONGNAME = 'L', /* GNU long (>100 chars) file name */
115 typedef enum TarFileType TarFileType;
117 /* This struct ignores magic, non-numeric user name,
118 * non-numeric group name, and the checksum, since
119 * these are all ignored by BusyBox tar. */
122 int tarFd; /* An open file descriptor for reading from the tarball */
123 char * name; /* File name */
124 mode_t mode; /* Unix mode, including device bits. */
125 uid_t uid; /* Numeric UID */
126 gid_t gid; /* Numeric GID */
127 size_t size; /* Size of file */
128 time_t mtime; /* Last-modified time */
129 enum TarFileType type; /* Regular, directory, link, etc */
130 char * linkname; /* Name for symbolic and hard links */
131 long devmajor; /* Major number for special device */
132 long devminor; /* Minor number for special device */
134 typedef struct TarInfo TarInfo;
136 /* Local procedures to restore files from a tar file. */
137 extern int readTarFile(int tarFd, int extractFlag, int listFlag,
138 int tostdoutFlag, int verboseFlag, char** extractList,
141 #ifdef BB_FEATURE_TAR_CREATE
142 /* Local procedures to save files into a tar file. */
143 static int writeTarFile(const char* tarName, int verboseFlag, char **argv,
147 #ifdef BB_FEATURE_TAR_GZIP
148 /* Signal handler for when child gzip process dies... */
156 extern int tar_unzip_init(int tarFd)
159 static int unzip_pipe[2];
160 /* Cope if child dies... Otherwise we block forever in read()... */
161 signal(SIGCHLD, child_died);
163 if (pipe(unzip_pipe)!=0)
164 error_msg_and_die("pipe error\n");
166 if ( (child_pid = fork()) == -1)
167 error_msg_and_die("fork failure\n");
172 unzip(tarFd, unzip_pipe[1]);
176 /* return fd of uncompressed data to parent process */
177 return(unzip_pipe[0]);
181 extern int tar_main(int argc, char **argv)
183 char** excludeList=NULL;
184 char** extractList=NULL;
185 const char *tarName="-";
186 #if defined BB_FEATURE_TAR_EXCLUDE
187 int excludeListSize=0;
188 char *excludeFileName ="-";
192 #if defined BB_FEATURE_TAR_GZIP
193 int unzipFlag = FALSE;
195 int listFlag = FALSE;
196 int extractFlag = FALSE;
197 int createFlag = FALSE;
198 int verboseFlag = FALSE;
199 int tostdoutFlag = FALSE;
207 while (*(++argv) && (**argv == '-' || firstOpt == TRUE)) {
210 while (stopIt==FALSE && **argv) {
211 switch (*((*argv)++)) {
213 if (extractFlag == TRUE || listFlag == TRUE)
218 if (listFlag == TRUE || createFlag == TRUE)
223 if (extractFlag == TRUE || createFlag == TRUE)
227 #ifdef BB_FEATURE_TAR_GZIP
240 error_msg_and_die( "Only one 'f' option allowed\n");
243 error_msg_and_die( "Option requires an argument: No file specified\n");
246 #if defined BB_FEATURE_TAR_EXCLUDE
248 if (strcmp(*argv, "xclude")==0) {
249 excludeList=xrealloc( excludeList,
250 sizeof(char *) * (excludeListSize+2));
251 excludeList[excludeListSize] = *(++argv);
252 if (excludeList[excludeListSize] == NULL)
253 error_msg_and_die( "Option requires an argument: No file specified\n");
254 /* Remove leading "/"s */
255 while (*excludeList[excludeListSize] =='/')
256 excludeList[excludeListSize]++;
257 /* Tack a NULL onto the end of the list */
258 excludeList[++excludeListSize] = NULL;
263 if (*excludeFileName != '-')
264 error_msg_and_die("Only one 'X' option allowed\n");
265 excludeFileName = *(++argv);
266 if (excludeFileName == NULL)
267 error_msg_and_die("Option requires an argument: No file specified\n");
268 fileList = fopen (excludeFileName, "r");
270 error_msg_and_die("Exclude file: file not found\n");
271 while (fgets(file, sizeof(file), fileList) != NULL) {
272 excludeList = xrealloc(excludeList,
273 sizeof(char *) * (excludeListSize+2));
274 if (file[strlen(file)-1] == '\n')
275 file[strlen(file)-1] = '\0';
276 excludeList[excludeListSize] = xstrdup(file);
277 /* Remove leading "/"s */
278 while (*excludeList[excludeListSize] == '/')
279 excludeList[excludeListSize]++;
280 /* Tack a NULL onto the end of the list */
281 excludeList[++excludeListSize] = NULL;
296 * Do the correct type of action supplying the rest of the
297 * command line arguments as the list of files to process.
299 if (createFlag == TRUE) {
300 #ifndef BB_FEATURE_TAR_CREATE
301 error_msg_and_die( "This version of tar was not compiled with tar creation support.\n");
303 #ifdef BB_FEATURE_TAR_GZIP
305 error_msg_and_die("Creation of compressed not internally support by tar, pipe to busybox gunzip\n");
307 status = writeTarFile(tarName, verboseFlag, argv, excludeList);
310 if (listFlag == TRUE || extractFlag == TRUE) {
314 /* Open the tar file for reading. */
315 if (!strcmp(tarName, "-"))
316 tarFd = fileno(stdin);
318 tarFd = open(tarName, O_RDONLY);
320 perror_msg_and_die("Error opening '%s'", tarName);
322 #ifdef BB_FEATURE_TAR_GZIP
323 /* unzip tarFd in a seperate process */
324 if (unzipFlag == TRUE)
325 tarFd = tar_unzip_init(tarFd);
327 status = readTarFile(tarFd, extractFlag, listFlag, tostdoutFlag,
328 verboseFlag, extractList, excludeList);
337 error_msg_and_die( "Exactly one of 'c', 'x' or 't' must be specified\n");
341 fixUpPermissions(TarInfo *header)
344 /* Now set permissions etc for the new file */
345 chown(header->name, header->uid, header->gid);
346 chmod(header->name, header->mode);
349 t.modtime = header->mtime;
350 utime(header->name, &t);
354 tarExtractRegularFile(TarInfo *header, int extractFlag, int tostdoutFlag)
358 size_t actualWriteSz;
360 size_t size = header->size;
361 int outFd=fileno(stdout);
363 /* Open the file to be written, if a file is supposed to be written */
364 if (extractFlag==TRUE && tostdoutFlag==FALSE) {
365 /* Create the path to the file, just in case it isn't there...
366 * This should not screw up path permissions or anything. */
367 create_path(header->name, 0777);
368 if ((outFd=open(header->name, O_CREAT|O_TRUNC|O_WRONLY,
369 header->mode & ~S_IFMT)) < 0) {
370 error_msg(io_error, header->name, strerror(errno));
375 /* Write out the file, if we are supposed to be doing that */
378 if ( size > sizeof(buffer) )
379 writeSize = readSize = sizeof(buffer);
381 int mod = size % 512;
383 readSize = size + (512 - mod);
388 if ( (readSize = full_read(header->tarFd, buffer, readSize)) <= 0 ) {
389 /* Tarball seems to have a problem */
390 error_msg("Unexpected EOF in archive\n");
393 if ( readSize < writeSize )
394 writeSize = readSize;
396 /* Write out the file, if we are supposed to be doing that */
397 if (extractFlag==TRUE) {
399 if ((actualWriteSz=full_write(outFd, buffer, writeSize)) != writeSize ) {
400 /* Output file seems to have a problem */
401 error_msg(io_error, header->name, strerror(errno));
405 actualWriteSz=writeSize;
408 size -= actualWriteSz;
411 /* Now we are done writing the file out, so try
412 * and fix up the permissions and whatnot */
413 if (extractFlag==TRUE && tostdoutFlag==FALSE) {
415 fixUpPermissions(header);
421 tarExtractDirectory(TarInfo *header, int extractFlag, int tostdoutFlag)
424 if (extractFlag==FALSE || tostdoutFlag==TRUE)
427 if (create_path(header->name, header->mode) != TRUE) {
428 perror_msg("%s: Cannot mkdir", header->name);
431 /* make the final component, just in case it was
432 * omitted by create_path() (which will skip the
433 * directory if it doesn't have a terminating '/') */
434 if (mkdir(header->name, header->mode) == 0) {
435 fixUpPermissions(header);
441 tarExtractHardLink(TarInfo *header, int extractFlag, int tostdoutFlag)
443 if (extractFlag==FALSE || tostdoutFlag==TRUE)
446 if (link(header->linkname, header->name) < 0) {
447 perror_msg("%s: Cannot create hard link to '%s'", header->name,
452 /* Now set permissions etc for the new directory */
453 fixUpPermissions(header);
458 tarExtractSymLink(TarInfo *header, int extractFlag, int tostdoutFlag)
460 if (extractFlag==FALSE || tostdoutFlag==TRUE)
464 if (symlink(header->linkname, header->name) < 0) {
465 perror_msg("%s: Cannot create symlink to '%s'", header->name,
469 /* Try to change ownership of the symlink.
470 * If libs doesn't support that, don't bother.
471 * Changing the pointed-to-file is the Wrong Thing(tm).
473 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
474 lchown(header->name, header->uid, header->gid);
477 /* Do not change permissions or date on symlink,
478 * since it changes the pointed to file instead. duh. */
480 error_msg("%s: Cannot create symlink to '%s': %s\n",
481 header->name, header->linkname,
482 "symlinks not supported");
488 tarExtractSpecial(TarInfo *header, int extractFlag, int tostdoutFlag)
490 if (extractFlag==FALSE || tostdoutFlag==TRUE)
493 if (S_ISCHR(header->mode) || S_ISBLK(header->mode) || S_ISSOCK(header->mode)) {
494 if (mknod(header->name, header->mode, makedev(header->devmajor, header->devminor)) < 0) {
495 perror_msg("%s: Cannot mknod", header->name);
498 } else if (S_ISFIFO(header->mode)) {
499 if (mkfifo(header->name, header->mode) < 0) {
500 perror_msg("%s: Cannot mkfifo", header->name);
505 /* Now set permissions etc for the new directory */
506 fixUpPermissions(header);
510 /* Read an octal value in a field of the specified width, with optional
511 * spaces on both sides of the number and with an optional null character
512 * at the end. Returns -1 on an illegal format. */
513 static long getOctal(const char *cp, int size)
517 for(;(size > 0) && (*cp == ' '); cp++, size--);
518 if ((size == 0) || !is_octal(*cp))
520 for(; (size > 0) && is_octal(*cp); size--) {
521 val = val * 8 + *cp++ - '0';
523 for (;(size > 0) && (*cp == ' '); cp++, size--);
524 if ((size > 0) && *cp)
530 /* Parse the tar header and fill in the nice struct with the details */
532 readTarHeader(struct TarHeader *rawHeader, struct TarInfo *header)
536 unsigned char *s = (unsigned char *)rawHeader;
538 header->name = rawHeader->name;
539 /* Check for and relativify any absolute paths */
540 if ( *(header->name) == '/' ) {
541 static int alreadyWarned=FALSE;
543 while (*(header->name) == '/')
546 if (alreadyWarned == FALSE) {
547 error_msg("Removing leading '/' from member names\n");
548 alreadyWarned = TRUE;
552 header->mode = getOctal(rawHeader->mode, sizeof(rawHeader->mode));
553 header->uid = getOctal(rawHeader->uid, sizeof(rawHeader->uid));
554 header->gid = getOctal(rawHeader->gid, sizeof(rawHeader->gid));
555 header->size = getOctal(rawHeader->size, sizeof(rawHeader->size));
556 header->mtime = getOctal(rawHeader->mtime, sizeof(rawHeader->mtime));
557 chksum = getOctal(rawHeader->chksum, sizeof(rawHeader->chksum));
558 header->type = rawHeader->typeflag;
559 header->linkname = rawHeader->linkname;
560 header->devmajor = getOctal(rawHeader->devmajor, sizeof(rawHeader->devmajor));
561 header->devminor = getOctal(rawHeader->devminor, sizeof(rawHeader->devminor));
563 /* Check the checksum */
564 for (i = sizeof(*rawHeader); i-- != 0;) {
567 /* Remove the effects of the checksum field (replace
568 * with blanks for the purposes of the checksum) */
569 s = rawHeader->chksum;
570 for (i = sizeof(rawHeader->chksum) ; i-- != 0;) {
573 sum += ' ' * sizeof(rawHeader->chksum);
581 * Read a tar file and extract or list the specified files within it.
582 * If the list is empty than all files are extracted or listed.
584 extern int readTarFile(int tarFd, int extractFlag, int listFlag,
585 int tostdoutFlag, int verboseFlag, char** extractList,
590 int skipNextHeaderFlag=FALSE;
595 /* Set the umask for this process so it doesn't
596 * screw up permission setting for us later. */
599 /* Read the tar file, and iterate over it one file at a time */
600 while ( (status = full_read(tarFd, (char*)&rawHeader, TAR_BLOCK_SIZE)) == TAR_BLOCK_SIZE ) {
602 /* Try to read the header */
603 if ( readTarHeader(&rawHeader, &header) == FALSE ) {
604 if ( *(header.name) == '\0' ) {
608 error_msg("Bad tar header, skipping\n");
612 if ( *(header.name) == '\0' )
614 header.tarFd = tarFd;
616 /* Skip funky extra GNU headers that precede long files */
617 if ( (header.type == GNULONGNAME) || (header.type == GNULONGLINK) ) {
618 skipNextHeaderFlag=TRUE;
619 if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE)
623 if ( skipNextHeaderFlag == TRUE ) {
624 skipNextHeaderFlag=FALSE;
625 error_msg(name_longer_than_foo, NAME_SIZE);
626 if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE)
631 #if defined BB_FEATURE_TAR_EXCLUDE
634 /* Check for excluded files.... */
635 for (tmpList=excludeList; tmpList && *tmpList; tmpList++) {
636 /* Do some extra hoop jumping for when directory names
637 * end in '/' but the entry in tmpList doesn't */
638 if (strncmp( *tmpList, header.name, strlen(*tmpList))==0 || (
639 header.name[strlen(header.name)-1]=='/'
640 && strncmp( *tmpList, header.name,
641 MIN(strlen(header.name)-1, strlen(*tmpList)))==0)) {
642 /* If it is a regular file, pretend to extract it with
643 * the extractFlag set to FALSE, so the junk in the tarball
644 * is properly skipped over */
645 if ( header.type==REGTYPE || header.type==REGTYPE0 ) {
646 if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE)
653 /* There are not the droids you're looking for, move along */
658 if (extractList != NULL) {
660 for (tmpList = extractList; *tmpList != NULL; tmpList++) {
661 if (strncmp( *tmpList, header.name, strlen(*tmpList))==0 || (
662 header.name[strlen(header.name)-1]=='/'
663 && strncmp( *tmpList, header.name,
664 MIN(strlen(header.name)-1, strlen(*tmpList)))==0)) {
665 /* If it is a regular file, pretend to extract it with
666 * the extractFlag set to FALSE, so the junk in the tarball
667 * is properly skipped over */
669 memmove(extractList+1, extractList,
670 sizeof(*extractList)*(tmpList-extractList));
675 /* There are not the droids you're looking for, move along */
676 if (skipFlag == TRUE) {
677 if ( header.type==REGTYPE || header.type==REGTYPE0 )
678 if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE)
684 if (listFlag == TRUE) {
685 /* Special treatment if the list (-t) flag is on */
686 if (verboseFlag == TRUE) {
689 struct tm *tm = localtime (&(header.mtime));
691 len=printf("%s ", mode_string(header.mode));
692 my_getpwuid(buf, header.uid);
694 len+=printf("%d", header.uid);
696 len+=printf("%s", buf);
697 my_getgrgid(buf, header.gid);
699 len+=printf("/%-d ", header.gid);
701 len+=printf("/%-s ", buf);
703 if (header.type==CHRTYPE || header.type==BLKTYPE) {
704 len1=snprintf(buf, sizeof(buf), "%ld,%-ld ",
705 header.devmajor, header.devminor);
707 len1=snprintf(buf, sizeof(buf), "%lu ", (long)header.size);
709 /* Jump through some hoops to make the columns match up */
710 for(;(len+len1)<31;len++)
714 /* Use ISO 8610 time format */
716 printf ("%04d-%02d-%02d %02d:%02d:%02d ",
717 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
718 tm->tm_hour, tm->tm_min, tm->tm_sec);
721 printf("%s", header.name);
722 if (verboseFlag == TRUE) {
723 if (header.type==LNKTYPE) /* If this is a link, say so */
724 printf(" link to %s", header.linkname);
725 else if (header.type==SYMTYPE)
726 printf(" -> %s", header.linkname);
731 /* List contents if we are supposed to do that */
732 if (verboseFlag == TRUE && extractFlag == TRUE) {
733 /* Now the normal listing */
735 if (tostdoutFlag == TRUE) // If the archive goes to stdout, verbose to stderr
737 fprintf(vbFd, "%s\n", header.name);
740 /* Remove files if we would overwrite them */
741 if (extractFlag == TRUE && tostdoutFlag == FALSE)
744 /* If we got here, we can be certain we have a legitimate
745 * header to work with. So work with it. */
746 switch ( header.type ) {
749 /* If the name ends in a '/' then assume it is
750 * supposed to be a directory, and fall through */
751 if (header.name[strlen(header.name)-1] != '/') {
752 if (tarExtractRegularFile(&header, extractFlag, tostdoutFlag)==FALSE)
757 if (tarExtractDirectory( &header, extractFlag, tostdoutFlag)==FALSE)
761 if (tarExtractHardLink( &header, extractFlag, tostdoutFlag)==FALSE)
765 if (tarExtractSymLink( &header, extractFlag, tostdoutFlag)==FALSE)
771 if (tarExtractSpecial( &header, extractFlag, tostdoutFlag)==FALSE)
775 /* Handled earlier */
778 skipNextHeaderFlag=TRUE;
782 error_msg("Unknown file type '%c' in tar file\n", header.type);
789 /* Bummer - we read a partial header */
790 perror_msg("Error reading tar file");
793 else if (errorFlag==TRUE) {
794 error_msg( "Error exit delayed from previous errors\n");
799 /* Stuff to do when we are done */
802 if (extractList != NULL) {
803 for (; *extractList != NULL; extractList++) {
804 error_msg("%s: Not found in archive\n", *extractList);
808 if ( *(header.name) == '\0' ) {
810 error_msg( "Error exit delayed from previous errors\n");
818 #ifdef BB_FEATURE_TAR_CREATE
821 ** writeTarFile(), writeFileToTarball(), and writeTarHeader() are
822 ** the only functions that deal with the HardLinkInfo structure.
823 ** Even these functions use the xxxHardLinkInfo() functions.
825 typedef struct HardLinkInfo HardLinkInfo;
828 HardLinkInfo *next; /* Next entry in list */
829 dev_t dev; /* Device number */
830 ino_t ino; /* Inode number */
831 short linkCount; /* (Hard) Link Count */
832 char name[1]; /* Start of filename (must be last) */
835 /* Some info to be carried along when creating a new tarball */
838 char* fileName; /* File name of the tarball */
839 int tarFd; /* Open-for-write file descriptor
841 struct stat statBuf; /* Stat info for the tarball, letting
842 us know the inode and device that the
843 tarball lives, so we can avoid trying
844 to include the tarball into itself */
845 int verboseFlag; /* Whether to print extra stuff or not */
846 char** excludeList; /* List of files to not include */
847 HardLinkInfo *hlInfoHead; /* Hard Link Tracking Information */
848 HardLinkInfo *hlInfo; /* Hard Link Info for the current file */
850 typedef struct TarBallInfo TarBallInfo;
853 /* Might be faster (and bigger) if the dev/ino were stored in numeric order;) */
855 addHardLinkInfo (HardLinkInfo **hlInfoHeadPtr, dev_t dev, ino_t ino,
856 short linkCount, const char *name)
858 /* Note: hlInfoHeadPtr can never be NULL! */
859 HardLinkInfo *hlInfo;
861 hlInfo = (HardLinkInfo *)xmalloc(sizeof(HardLinkInfo)+strlen(name)+1);
863 hlInfo->next = *hlInfoHeadPtr;
864 *hlInfoHeadPtr = hlInfo;
867 hlInfo->linkCount = linkCount;
868 strcpy(hlInfo->name, name);
874 freeHardLinkInfo (HardLinkInfo **hlInfoHeadPtr)
876 HardLinkInfo *hlInfo = NULL;
877 HardLinkInfo *hlInfoNext = NULL;
880 hlInfo = *hlInfoHeadPtr;
882 hlInfoNext = hlInfo->next;
886 *hlInfoHeadPtr = NULL;
891 /* Might be faster (and bigger) if the dev/ino were stored in numeric order;) */
892 static HardLinkInfo *
893 findHardLinkInfo (HardLinkInfo *hlInfo, dev_t dev, ino_t ino)
896 if ((ino == hlInfo->ino) && (dev == hlInfo->dev))
898 hlInfo = hlInfo->next;
903 /* Put an octal string into the specified buffer.
904 * The number is zero and space padded and possibly null padded.
905 * Returns TRUE if successful. */
906 static int putOctal (char *cp, int len, long value)
910 char *tempString = tempBuffer;
912 /* Create a string of the specified length with an initial space,
913 * leading zeroes and the octal number, and a trailing null. */
914 sprintf (tempString, "%0*lo", len - 1, value);
916 /* If the string is too large, suppress the leading space. */
917 tempLength = strlen (tempString) + 1;
918 if (tempLength > len) {
923 /* If the string is still too large, suppress the trailing null. */
924 if (tempLength > len)
927 /* If the string is still too large, fail. */
928 if (tempLength > len)
931 /* Copy the string to the field. */
932 memcpy (cp, tempString, len);
937 /* Write out a tar header for the specified file/directory/whatever */
939 writeTarHeader(struct TarBallInfo *tbInfo, const char *header_name,
940 const char *real_name, struct stat *statbuf)
943 struct TarHeader header;
944 const unsigned char *cp = (const unsigned char *) &header;
945 ssize_t size = sizeof(struct TarHeader);
947 memset( &header, 0, size);
949 strncpy(header.name, header_name, sizeof(header.name));
951 putOctal(header.mode, sizeof(header.mode), statbuf->st_mode);
952 putOctal(header.uid, sizeof(header.uid), statbuf->st_uid);
953 putOctal(header.gid, sizeof(header.gid), statbuf->st_gid);
954 putOctal(header.size, sizeof(header.size), 0); /* Regular file size is handled later */
955 putOctal(header.mtime, sizeof(header.mtime), statbuf->st_mtime);
956 strncpy(header.magic, TAR_MAGIC TAR_VERSION,
957 TAR_MAGIC_LEN + TAR_VERSION_LEN );
959 /* Enter the user and group names (default to root if it fails) */
960 my_getpwuid(header.uname, statbuf->st_uid);
962 strcpy(header.uname, "root");
963 my_getgrgid(header.gname, statbuf->st_gid);
965 strcpy(header.uname, "root");
967 if (tbInfo->hlInfo) {
968 /* This is a hard link */
969 header.typeflag = LNKTYPE;
970 strncpy(header.linkname, tbInfo->hlInfo->name, sizeof(header.linkname));
971 } else if (S_ISLNK(statbuf->st_mode)) {
974 header.typeflag = SYMTYPE;
975 link_size = readlink(real_name, buffer, sizeof(buffer) - 1);
976 if ( link_size < 0) {
977 perror_msg("Error reading symlink '%s'", header.name);
980 buffer[link_size] = '\0';
981 strncpy(header.linkname, buffer, sizeof(header.linkname));
982 } else if (S_ISDIR(statbuf->st_mode)) {
983 header.typeflag = DIRTYPE;
984 strncat(header.name, "/", sizeof(header.name));
985 } else if (S_ISCHR(statbuf->st_mode)) {
986 header.typeflag = CHRTYPE;
987 putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev));
988 putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev));
989 } else if (S_ISBLK(statbuf->st_mode)) {
990 header.typeflag = BLKTYPE;
991 putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev));
992 putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev));
993 } else if (S_ISFIFO(statbuf->st_mode)) {
994 header.typeflag = FIFOTYPE;
995 } else if (S_ISREG(statbuf->st_mode)) {
996 header.typeflag = REGTYPE;
997 putOctal(header.size, sizeof(header.size), statbuf->st_size);
999 error_msg("%s: Unknown file type\n", real_name);
1003 /* Calculate and store the checksum (i.e. the sum of all of the bytes of
1004 * the header). The checksum field must be filled with blanks for the
1005 * calculation. The checksum field is formatted differently from the
1006 * other fields: it has [6] digits, a null, then a space -- rather than
1007 * digits, followed by a null like the other fields... */
1008 memset(header.chksum, ' ', sizeof(header.chksum));
1009 cp = (const unsigned char *) &header;
1012 putOctal(header.chksum, 7, chksum);
1014 /* Now write the header out to disk */
1015 if ((size=full_write(tbInfo->tarFd, (char*)&header, sizeof(struct TarHeader))) < 0) {
1016 error_msg(io_error, real_name, strerror(errno));
1019 /* Pad the header up to the tar block size */
1020 for (; size<TAR_BLOCK_SIZE; size++) {
1021 write(tbInfo->tarFd, "\0", 1);
1023 /* Now do the verbose thing (or not) */
1024 if (tbInfo->verboseFlag==TRUE) {
1025 FILE *vbFd = stdout;
1026 if (tbInfo->tarFd == fileno(stdout)) // If the archive goes to stdout, verbose to stderr
1028 fprintf(vbFd, "%s\n", header.name);
1035 static int writeFileToTarball(const char *fileName, struct stat *statbuf, void* userData)
1037 struct TarBallInfo *tbInfo = (struct TarBallInfo *)userData;
1038 const char *header_name;
1039 #if defined BB_FEATURE_TAR_EXCLUDE
1044 ** Check to see if we are dealing with a hard link.
1046 ** Treat the first occurance of a given dev/inode as a file while
1047 ** treating any additional occurances as hard links. This is done
1048 ** by adding the file information to the HardLinkInfo linked list.
1050 tbInfo->hlInfo = NULL;
1051 if (statbuf->st_nlink > 1) {
1052 tbInfo->hlInfo = findHardLinkInfo(tbInfo->hlInfoHead, statbuf->st_dev,
1054 if (tbInfo->hlInfo == NULL)
1055 addHardLinkInfo (&tbInfo->hlInfoHead, statbuf->st_dev,
1056 statbuf->st_ino, statbuf->st_nlink, fileName);
1059 /* It is against the rules to archive a socket */
1060 if (S_ISSOCK(statbuf->st_mode)) {
1061 error_msg("%s: socket ignored\n", fileName);
1065 /* It is a bad idea to store the archive we are in the process of creating,
1066 * so check the device and inode to be sure that this particular file isn't
1067 * the new tarball */
1068 if (tbInfo->statBuf.st_dev == statbuf->st_dev &&
1069 tbInfo->statBuf.st_ino == statbuf->st_ino) {
1070 error_msg("%s: file is the archive; skipping\n", fileName);
1074 header_name = fileName;
1075 while (header_name[0] == '/') {
1076 static int alreadyWarned=FALSE;
1077 if (alreadyWarned==FALSE) {
1078 error_msg("Removing leading '/' from member names\n");
1084 if (strlen(fileName) >= NAME_SIZE) {
1085 error_msg(name_longer_than_foo, NAME_SIZE);
1089 if (header_name[0] == '\0')
1092 #if defined BB_FEATURE_TAR_EXCLUDE
1093 /* Check for excluded files.... */
1094 for (tmpList=tbInfo->excludeList; tmpList && *tmpList; tmpList++) {
1095 /* Do some extra hoop jumping for when directory names
1096 * end in '/' but the entry in tmpList doesn't */
1097 if (strncmp( *tmpList, header_name, strlen(*tmpList))==0 || (
1098 fileName[strlen(fileName)-1]=='/'
1099 && strncmp( *tmpList, fileName,
1100 MIN(strlen(fileName)-1, strlen(*tmpList)))==0)) {
1106 if (writeTarHeader(tbInfo, header_name, fileName, statbuf)==FALSE) {
1110 /* Now, if the file is a regular file, copy it out to the tarball */
1111 if ((tbInfo->hlInfo == NULL)
1112 && (S_ISREG(statbuf->st_mode))) {
1114 char buffer[BUFSIZ];
1115 ssize_t size=0, readSize=0;
1117 /* open the file we want to archive, and make sure all is well */
1118 if ((inputFileFd = open(fileName, O_RDONLY)) < 0) {
1119 error_msg("%s: Cannot open: %s\n", fileName, strerror(errno));
1123 /* write the file to the archive */
1124 while ( (size = full_read(inputFileFd, buffer, sizeof(buffer))) > 0 ) {
1125 if (full_write(tbInfo->tarFd, buffer, size) != size ) {
1126 /* Output file seems to have a problem */
1127 error_msg(io_error, fileName, strerror(errno));
1133 error_msg(io_error, fileName, strerror(errno));
1136 /* Pad the file up to the tar block size */
1137 for (; (readSize%TAR_BLOCK_SIZE) != 0; readSize++) {
1138 write(tbInfo->tarFd, "\0", 1);
1140 close( inputFileFd);
1146 static int writeTarFile(const char* tarName, int verboseFlag, char **argv,
1150 int errorFlag=FALSE;
1152 struct TarBallInfo tbInfo;
1153 tbInfo.verboseFlag = verboseFlag;
1154 tbInfo.hlInfoHead = NULL;
1156 /* Make sure there is at least one file to tar up. */
1158 error_msg_and_die("Cowardly refusing to create an empty archive\n");
1160 /* Open the tar file for writing. */
1161 if (!strcmp(tarName, "-"))
1162 tbInfo.tarFd = fileno(stdout);
1164 tbInfo.tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1165 if (tbInfo.tarFd < 0) {
1166 perror_msg( "Error opening '%s'", tarName);
1167 freeHardLinkInfo(&tbInfo.hlInfoHead);
1170 tbInfo.excludeList=excludeList;
1171 /* Store the stat info for the tarball's file, so
1172 * can avoid including the tarball into itself.... */
1173 if (fstat(tbInfo.tarFd, &tbInfo.statBuf) < 0)
1174 error_msg_and_die(io_error, tarName, strerror(errno));
1176 /* Set the umask for this process so it doesn't
1177 * screw up permission setting for us later. */
1180 /* Read the directory/files and iterate over them one at a time */
1181 while (*argv != NULL) {
1182 if (recursive_action(*argv++, TRUE, FALSE, FALSE,
1183 writeFileToTarball, writeFileToTarball,
1184 (void*) &tbInfo) == FALSE) {
1188 /* Write two empty blocks to the end of the archive */
1189 for (size=0; size<(2*TAR_BLOCK_SIZE); size++) {
1190 write(tbInfo.tarFd, "\0", 1);
1193 /* To be pedantically correct, we would check if the tarball
1194 * is smaller than 20 tar blocks, and pad it if it was smaller,
1195 * but that isn't necessary for GNU tar interoperability, and
1196 * so is considered a waste of space */
1198 /* Hang up the tools, close up shop, head home */
1200 if (errorFlag == TRUE) {
1201 error_msg("Error exit delayed from previous errors\n");
1202 freeHardLinkInfo(&tbInfo.hlInfoHead);
1205 freeHardLinkInfo(&tbInfo.hlInfoHead);