2
* Copyright (c) 2013 Vojtech Horky
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
9
* - Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* - Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
14
* - The name of the author may not be used to endorse or promote products
15
* derived from this software without specific prior written permission.
17
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39
#include <str_error.h>
42
static size_t get_block_count(size_t bytes) {
43
return (bytes + TAR_BLOCK_SIZE - 1) / TAR_BLOCK_SIZE;
46
static int skip_blocks(FILE *tarfile, size_t valid_data_size)
48
size_t blocks_to_read = get_block_count(valid_data_size);
49
while (blocks_to_read > 0) {
50
uint8_t block[TAR_BLOCK_SIZE];
51
size_t actually_read = fread(block, TAR_BLOCK_SIZE, 1, tarfile);
52
if (actually_read != 1) {
60
static int handle_normal_file(const tar_header_t *header, FILE *tarfile)
62
// FIXME: create the directory first
64
FILE *file = fopen(header->filename, "wb");
66
fprintf(stderr, "Failed to create %s: %s.\n", header->filename,
72
size_t bytes_remaining = header->size;
73
size_t blocks = get_block_count(bytes_remaining);
75
uint8_t block[TAR_BLOCK_SIZE];
76
size_t actually_read = fread(block, 1, TAR_BLOCK_SIZE, tarfile);
77
if (actually_read != TAR_BLOCK_SIZE) {
79
fprintf(stderr, "Failed to read block for %s: %s.\n",
80
header->filename, str_error(rc));
83
size_t to_write = TAR_BLOCK_SIZE;
84
if (bytes_remaining < TAR_BLOCK_SIZE) {
85
to_write = bytes_remaining;
87
size_t actually_written = fwrite(block, 1, to_write, file);
88
if (actually_written != to_write) {
90
fprintf(stderr, "Failed to write to %s: %s.\n",
91
header->filename, str_error(rc));
95
bytes_remaining -= TAR_BLOCK_SIZE;
103
static int handle_directory(const tar_header_t *header, FILE *tarfile)
105
int rc = mkdir(header->filename, 0755);
106
if ((rc == EEXIST) || (rc == EEXISTS)) {
107
// printf("Note: directory %s already exists.\n", header->filename);
111
fprintf(stderr, "Failed to create directory %s: %s.\n",
112
header->filename, str_error(rc));
116
return skip_blocks(tarfile, header->size);
119
int main(int argc, char *argv[])
122
fprintf(stderr, "Usage: %s tar-file\n", argv[0]);
126
const char *filename = argv[1];
128
FILE *tarfile = fopen(filename, "rb");
129
if (tarfile == NULL) {
130
fprintf(stderr, "Failed to open `%s': %s.\n", filename, str_error(errno));
136
tar_header_raw_t header_raw;
138
header_ok = fread(&header_raw, sizeof(header_raw), 1, tarfile);
139
if (header_ok != 1) {
142
int rc = tar_header_parse(&header, &header_raw);
147
fprintf(stderr, "Failed parsing TAR header: %s.\n", str_error(rc));
151
//printf(" ==> %s (%zuB, type %s)\n", header.filename,
152
// header.size, tar_type_str(header.type));
154
switch (header.type) {
155
case TAR_TYPE_DIRECTORY:
156
rc = handle_directory(&header, tarfile);
158
case TAR_TYPE_NORMAL:
159
rc = handle_normal_file(&header, tarfile);
162
rc = skip_blocks(tarfile, header.size);