diff options
author | Rob Landley <rob@landley.net> | 2006-09-10 03:20:37 +0000 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2006-09-10 03:20:37 +0000 |
commit | 1bfca7bac7c379e7d42387851d6f689d60f749d8 (patch) | |
tree | 2757542ea9cc4297da57a5608b65617dc0f5bbdd | |
parent | 9852d5a1e65d23dece9bdfa2483409dda258748f (diff) | |
download | busybox-w32-1bfca7bac7c379e7d42387851d6f689d60f749d8.tar.gz busybox-w32-1bfca7bac7c379e7d42387851d6f689d60f749d8.tar.bz2 busybox-w32-1bfca7bac7c379e7d42387851d6f689d60f749d8.zip |
Cleanup headers and replace the zero padding with an even smaller version.
One of the pads turned out to be unnecessary: sizeof(struct TarHeader) is
TAR_BLOCK_SIZE, the padding's in the struct. The others could be done inline
with bb_common_bufsiz1.
This is a cleanup I did to Denis' patch long ago, but got sidetracked by
what turned into svn 15660.
-rw-r--r-- | archival/tar.c | 56 |
1 files changed, 10 insertions, 46 deletions
diff --git a/archival/tar.c b/archival/tar.c index 91232bcf3..5d9e870fa 100644 --- a/archival/tar.c +++ b/archival/tar.c | |||
@@ -23,21 +23,10 @@ | |||
23 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | 23 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
24 | */ | 24 | */ |
25 | 25 | ||
26 | #include <fcntl.h> | ||
27 | #include <getopt.h> | ||
28 | #include <search.h> | ||
29 | #include <stdio.h> | ||
30 | #include <stdlib.h> | ||
31 | #include <unistd.h> | ||
32 | #include <fnmatch.h> | ||
33 | #include <string.h> | ||
34 | #include <errno.h> | ||
35 | #include <signal.h> | ||
36 | #include <sys/wait.h> | ||
37 | #include <sys/socket.h> | ||
38 | #include <sys/sysmacros.h> /* major() and minor() */ | ||
39 | #include "unarchive.h" | ||
40 | #include "busybox.h" | 26 | #include "busybox.h" |
27 | #include "unarchive.h" | ||
28 | #include <fnmatch.h> | ||
29 | #include <getopt.h> | ||
41 | 30 | ||
42 | #ifdef CONFIG_FEATURE_TAR_CREATE | 31 | #ifdef CONFIG_FEATURE_TAR_CREATE |
43 | 32 | ||
@@ -192,26 +181,6 @@ static int putOctal(char *cp, int len, long value) | |||
192 | return TRUE; | 181 | return TRUE; |
193 | } | 182 | } |
194 | 183 | ||
195 | /* Pad file to TAR_BLOCK_SIZE with zeros */ | ||
196 | static void block_write_zeroes(int fd, size_t size) | ||
197 | { | ||
198 | char zerobuf[TAR_BLOCK_SIZE]; | ||
199 | memset(zerobuf, 0, size); | ||
200 | /* No point in trying to continue on error */ | ||
201 | if (full_write(fd, zerobuf, size) < 0) | ||
202 | bb_perror_msg_and_die("write"); | ||
203 | } | ||
204 | |||
205 | static size_t pad_block_write(int fd, size_t size) | ||
206 | { | ||
207 | size_t rem = (TAR_BLOCK_SIZE - size) & (TAR_BLOCK_SIZE-1); | ||
208 | if (rem) { | ||
209 | block_write_zeroes(fd, rem); | ||
210 | size += rem; | ||
211 | } | ||
212 | return size; | ||
213 | } | ||
214 | |||
215 | /* Write out a tar header for the specified file/directory/whatever */ | 184 | /* Write out a tar header for the specified file/directory/whatever */ |
216 | static int writeTarHeader(struct TarBallInfo *tbInfo, | 185 | static int writeTarHeader(struct TarBallInfo *tbInfo, |
217 | const char *header_name, const char *fileName, struct stat *statbuf) | 186 | const char *header_name, const char *fileName, struct stat *statbuf) |
@@ -221,7 +190,7 @@ static int writeTarHeader(struct TarBallInfo *tbInfo, | |||
221 | const unsigned char *cp = (const unsigned char *) &header; | 190 | const unsigned char *cp = (const unsigned char *) &header; |
222 | ssize_t size = sizeof(struct TarHeader); | 191 | ssize_t size = sizeof(struct TarHeader); |
223 | 192 | ||
224 | memset(&header, 0, size); | 193 | bzero(&header, size); |
225 | 194 | ||
226 | safe_strncpy(header.name, header_name, sizeof(header.name)); | 195 | safe_strncpy(header.name, header_name, sizeof(header.name)); |
227 | 196 | ||
@@ -288,14 +257,7 @@ static int writeTarHeader(struct TarBallInfo *tbInfo, | |||
288 | putOctal(header.chksum, 7, chksum); | 257 | putOctal(header.chksum, 7, chksum); |
289 | 258 | ||
290 | /* Now write the header out to disk */ | 259 | /* Now write the header out to disk */ |
291 | size = full_write(tbInfo->tarFd, (char *) &header, | 260 | xwrite(tbInfo->tarFd, &header, sizeof(struct TarHeader)); |
292 | sizeof(struct TarHeader)); | ||
293 | if (size < 0) { | ||
294 | bb_error_msg(bb_msg_io_error, fileName); | ||
295 | return FALSE; | ||
296 | } | ||
297 | /* Pad the header up to the tar block size */ | ||
298 | size = pad_block_write(tbInfo->tarFd, size); | ||
299 | 261 | ||
300 | /* Now do the verbose thing (or not) */ | 262 | /* Now do the verbose thing (or not) */ |
301 | 263 | ||
@@ -421,7 +383,9 @@ static int writeFileToTarball(const char *fileName, struct stat *statbuf, | |||
421 | close(inputFileFd); | 383 | close(inputFileFd); |
422 | 384 | ||
423 | /* Pad the file up to the tar block size */ | 385 | /* Pad the file up to the tar block size */ |
424 | readSize = pad_block_write(tbInfo->tarFd, readSize); | 386 | readSize = (TAR_BLOCK_SIZE - readSize) & (TAR_BLOCK_SIZE-1); |
387 | bzero(bb_common_bufsiz1, readSize); | ||
388 | xwrite(tbInfo->tarFd, bb_common_bufsiz1, readSize); | ||
425 | } | 389 | } |
426 | 390 | ||
427 | return TRUE; | 391 | return TRUE; |
@@ -516,8 +480,8 @@ static int writeTarFile(const int tar_fd, const int verboseFlag, | |||
516 | include = include->link; | 480 | include = include->link; |
517 | } | 481 | } |
518 | /* Write two empty blocks to the end of the archive */ | 482 | /* Write two empty blocks to the end of the archive */ |
519 | block_write_zeroes(tbInfo.tarFd, TAR_BLOCK_SIZE); | 483 | bzero(bb_common_bufsiz1, 2*TAR_BLOCK_SIZE); |
520 | block_write_zeroes(tbInfo.tarFd, TAR_BLOCK_SIZE); | 484 | xwrite(tbInfo.tarFd, bb_common_bufsiz1, 2*TAR_BLOCK_SIZE); |
521 | 485 | ||
522 | /* To be pedantically correct, we would check if the tarball | 486 | /* To be pedantically correct, we would check if the tarball |
523 | * is smaller than 20 tar blocks, and pad it if it was smaller, | 487 | * is smaller than 20 tar blocks, and pad it if it was smaller, |