aboutsummaryrefslogtreecommitdiff
path: root/tar.c
diff options
context:
space:
mode:
Diffstat (limited to 'tar.c')
-rw-r--r--tar.c125
1 files changed, 108 insertions, 17 deletions
diff --git a/tar.c b/tar.c
index 477487a50..37a28a3d0 100644
--- a/tar.c
+++ b/tar.c
@@ -1,10 +1,24 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* 2/*
3 * Mini tar implementation for busybox 3 *
4 * Mini tar implementation for busybox Note, that as of BusyBox 0.43 tar has
5 * been completely rewritten from the ground up. It still has remnents of the
6 * old code lying about, but it pretty different (i.e. cleaner, less global
7 * variables, etc)
4 * 8 *
5 * Copyright (C) 1999 by Lineo, inc. 9 * Copyright (C) 1999 by Lineo, inc.
6 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org> 10 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
7 * 11 *
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.
17 *
18 * Based in part on the tar implementation in busybox-0.28
19 * Copyright (C) 1995 Bruce Perens
20 * This is free software under the GNU General Public License.
21 *
8 * This program is free software; you can redistribute it and/or modify 22 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by 23 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or 24 * the Free Software Foundation; either version 2 of the License, or
@@ -147,17 +161,9 @@ static int readTarFile(const char* tarName, int extractFlag, int listFlag,
147 161
148 162
149#ifdef BB_FEATURE_TAR_CREATE 163#ifdef BB_FEATURE_TAR_CREATE
150/* 164/* Local procedures to save files into a tar file. */
151 * Local procedures to save files into a tar file. 165static int writeTarFile(const char* tarName, int extractFlag, int listFlag,
152 */ 166 int tostdoutFlag, int verboseFlag, int argc, char **argv);
153static void saveFile(const char *fileName, int seeLinks);
154static void saveRegularFile(const char *fileName,
155 const struct stat *statbuf);
156static void saveDirectory(const char *fileName,
157 const struct stat *statbuf);
158static void writeHeader(const char *fileName, const struct stat *statbuf);
159static void writeTarFile(int argc, char **argv);
160static void writeTarBlock(const char *buf, int len);
161static int putOctal(char *cp, int len, long value); 167static int putOctal(char *cp, int len, long value);
162 168
163#endif 169#endif
@@ -242,7 +248,7 @@ extern int tar_main(int argc, char **argv)
242#ifndef BB_FEATURE_TAR_CREATE 248#ifndef BB_FEATURE_TAR_CREATE
243 fatalError( "This version of tar was not compiled with tar creation support.\n"); 249 fatalError( "This version of tar was not compiled with tar creation support.\n");
244#else 250#else
245 exit(writeTarFile(argc, argv)); 251 exit(writeTarFile(tarName, extractFlag, listFlag, tostdoutFlag, verboseFlag, argc, argv));
246#endif 252#endif
247 } else { 253 } else {
248 exit(readTarFile(tarName, extractFlag, listFlag, tostdoutFlag, verboseFlag)); 254 exit(readTarFile(tarName, extractFlag, listFlag, tostdoutFlag, verboseFlag));
@@ -347,10 +353,9 @@ tarExtractDirectory(TarInfo *header, int extractFlag, int tostdoutFlag)
347 /* make the final component, just in case it was 353 /* make the final component, just in case it was
348 * omitted by createPath() (which will skip the 354 * omitted by createPath() (which will skip the
349 * directory if it doesn't have a terminating '/') */ 355 * directory if it doesn't have a terminating '/') */
350 mkdir(header->name, header->mode); 356 if (mkdir(header->name, header->mode) == 0) {
351 357 fixUpPermissions(header);
352 /* Now set permissions etc for the new directory */ 358 }
353 fixUpPermissions(header);
354} 359}
355 360
356static void 361static void
@@ -616,3 +621,89 @@ endgame:
616 return( FALSE); 621 return( FALSE);
617} 622}
618 623
624
625#ifdef BB_FEATURE_TAR_CREATE
626
627/* Put an octal string into the specified buffer.
628 * The number is zero and space padded and possibly null padded.
629 * Returns TRUE if successful. */
630static int putOctal (char *cp, int len, long value)
631{
632 int tempLength;
633 char *tempString;
634 char tempBuffer[32];
635
636 /* Create a string of the specified length with an initial space,
637 * leading zeroes and the octal number, and a trailing null. */
638 tempString = tempBuffer;
639
640 sprintf (tempString, " %0*lo", len - 2, value);
641
642 tempLength = strlen (tempString) + 1;
643
644 /* If the string is too large, suppress the leading space. */
645 if (tempLength > len) {
646 tempLength--;
647 tempString++;
648 }
649
650 /* If the string is still too large, suppress the trailing null. */
651 if (tempLength > len)
652 tempLength--;
653
654 /* If the string is still too large, fail. */
655 if (tempLength > len)
656 return FALSE;
657
658 /* Copy the string to the field. */
659 memcpy (cp, tempString, len);
660
661 return TRUE;
662}
663
664static int fileAction(const char *fileName, struct stat *statbuf)
665{
666 fprintf(stdout, "%s\n", fileName);
667 return (TRUE);
668}
669
670static int writeTarFile(const char* tarName, int extractFlag, int listFlag,
671 int tostdoutFlag, int verboseFlag, int argc, char **argv)
672{
673 int tarFd=0;
674 //int errorFlag=FALSE;
675 //TarHeader rawHeader;
676 //TarInfo header;
677 //int alreadyWarned=FALSE;
678 char *directory = ".";
679 //int skipFileFlag=FALSE;
680
681 /* Open the tar file for writing. */
682 if (!strcmp(tarName, "-"))
683 tarFd = fileno(stdout);
684 else
685 tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644);
686 if (tarFd < 0) {
687 errorMsg( "Error opening '%s': %s\n", tarName, strerror(errno));
688 return ( FALSE);
689 }
690
691 /* Set the umask for this process so it doesn't
692 * screw up permission setting for us later. */
693 umask(0);
694
695 /* Read the directory/files and iterate over them one at a time */
696 if (recursiveAction(directory, TRUE, FALSE, FALSE,
697 fileAction, fileAction) == FALSE) {
698 exit(FALSE);
699 }
700
701
702 // TODO: DO STUFF HERE
703 close(tarFd);
704 return( TRUE);
705}
706
707
708#endif
709