summaryrefslogtreecommitdiffstats
path: root/lib/dpkg/t/c-tarextract.c
blob: 882253d2700886a3ed08e2559fa3ec2e32078134 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * libdpkg - Debian packaging suite library routines
 * c-tarextract.c - test tar extractor
 *
 * Copyright © 2014 Guillem Jover <guillem@debian.org>
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2,
 * or (at your option) any later version.
 *
 * This is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <compat.h>

#include <sys/types.h>
#if HAVE_SYS_SYSMACROS_H
#include <sys/sysmacros.h> /* Needed on AIX for major()/minor(). */
#endif

#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <dpkg/ehandle.h>
#include <dpkg/fdio.h>
#include <dpkg/buffer.h>
#include <dpkg/tarfn.h>

struct tar_context {
	int tar_fd;
};

static int
tar_read(struct tar_archive *tar, char *buffer, int size)
{
	struct tar_context *tc = tar->ctx;

	return fd_read(tc->tar_fd, buffer, size);
}

static int
tar_object_skip(struct tar_archive *tar, struct tar_entry *te)
{
	struct tar_context *tc = tar->ctx;
	off_t size;

	size = (te->size + TARBLKSZ - 1) / TARBLKSZ * TARBLKSZ;
	if (size == 0)
		return 0;

	return fd_skip(tc->tar_fd, size, NULL);
}

static int
tar_object(struct tar_archive *tar, struct tar_entry *te)
{
	printf("%s mode=%o time=%jd.%.9d uid=%d gid=%d", te->name,
	       te->stat.mode, te->mtime, 0, te->stat.uid, te->stat.gid);
	if (te->stat.uname)
		printf(" uname=%s", te->stat.uname);
	if (te->stat.gname)
		printf(" gname=%s", te->stat.gname);

	switch (te->type) {
	case TAR_FILETYPE_FILE0:
	case TAR_FILETYPE_FILE:
		tar_object_skip(tar, te);
		printf(" type=file size=%jd", (intmax_t)te->size);
		break;
	case TAR_FILETYPE_HARDLINK:
		printf(" type=hardlink linkto=%s size=%jd",
		       te->linkname, (intmax_t)te->size);
		break;
	case TAR_FILETYPE_SYMLINK:
		printf(" type=symlink linkto=%s size=%jd",
		       te->linkname, (intmax_t)te->size);
		break;
	case TAR_FILETYPE_DIR:
		printf(" type=dir");
		break;
	case TAR_FILETYPE_CHARDEV:
	case TAR_FILETYPE_BLOCKDEV:
		printf(" type=device id=%d.%d", major(te->dev), minor(te->dev));
		break;
	case TAR_FILETYPE_FIFO:
		printf(" type=fifo");
		break;
	default:
		ohshit("unexpected tar entry type '%c'", te->type);
	}

	printf("\n");

	return 0;
}

struct tar_operations tar_ops = {
	.read = tar_read,
	.extract_file = tar_object,
	.link = tar_object,
	.symlink = tar_object,
	.mkdir = tar_object,
	.mknod = tar_object,
};

int
main(int argc, char **argv)
{
	struct tar_archive tar;
	struct tar_context tar_ctx;
	const char *tar_name = argv[1];

	setvbuf(stdout, NULL, _IOLBF, 0);

	push_error_context();

	if (tar_name) {
		tar_ctx.tar_fd = open(tar_name, O_RDONLY);
		if (tar_ctx.tar_fd < 0)
			ohshite("cannot open file '%s'", tar_name);
	} else {
		tar_ctx.tar_fd = STDIN_FILENO;
	}

	tar.err = DPKG_ERROR_OBJECT;
	tar.ctx = &tar_ctx;
	tar.ops = &tar_ops;

	if (tar_extractor(&tar))
		ohshite("extracting tar");

	dpkg_error_destroy(&tar.err);

	if (tar_name)
		close(tar_ctx.tar_fd);

	pop_error_context(ehflag_normaltidy);

	return 0;
}