Skip to content

Commit 4de6590

Browse files
committed
Add a strextract.c tool, which is basically a sample program to use the
streaming extract API.
1 parent e83c4eb commit 4de6590

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

tools/Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
CFLAGS = -g `xml2-config --cflags`
2-
LDFLAGS = -lxar `xml2-config --libs` -lcrypto -lbz2 -lacl
2+
LDFLAGS = -lxar `xml2-config --libs` -lcrypto
33
all: xardiff vitoc toc_extract
44
xardiff: xardiff.o
5+
strextract: strextract.o
56
vitoc: vitoc.o
67
toc_extract: toc_extract.o
78

tools/strextract.c

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
#include <sys/types.h>
4+
#include <xar/xar.h>
5+
#include <sys/fcntl.h>
6+
7+
static int32_t err_callback(int32_t sev, int32_t err, xar_errctx_t ctx, void *usrctx) {
8+
fprintf(stderr, "err callback\n");
9+
}
10+
11+
int main(int argc, char *argv[]) {
12+
xar_t x;
13+
xar_iter_t i;
14+
xar_file_t f;
15+
xar_stream s;
16+
char buffer[8192];
17+
18+
x = xar_open(argv[1], READ);
19+
if( !x ) {
20+
fprintf(stderr, "Error opening archive\n");
21+
exit(1);
22+
}
23+
24+
xar_register_errhandler(x, err_callback, NULL);
25+
26+
i = xar_iter_new();
27+
if( !i ) {
28+
fprintf(stderr, "Error creating xar iterator\n");
29+
exit(1);
30+
}
31+
32+
for(f = xar_file_first(x, i); f; f = xar_file_next(i)) {
33+
char *path;
34+
const char *type;
35+
int32_t ret;
36+
int fd;
37+
38+
path = xar_get_path(f);
39+
40+
xar_prop_get(f, "type", &type);
41+
if( !type ) {
42+
fprintf(stderr, "File has no type %s\n", path);
43+
free(path);
44+
continue;
45+
}
46+
if( strcmp(type, "file") != 0 ) {
47+
fprintf(stderr, "Skipping %s\n", path);
48+
free(path);
49+
continue;
50+
}
51+
52+
fprintf(stderr, "Extracting %s\n", path);
53+
if( xar_extract_tostream_init(x, f, &s) != XAR_STREAM_OK ) {
54+
fprintf(stderr, "Error initializing stream %s\n", path);
55+
free(path);
56+
continue;
57+
}
58+
59+
fd = open(path, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR);
60+
if( !fd ) {
61+
fprintf(stderr, "Error opening output file %s\n", path);
62+
free(path);
63+
continue;
64+
}
65+
s.avail_out = sizeof(buffer);
66+
s.next_out = buffer;
67+
68+
while( (ret = xar_extract_tostream(&s)) != XAR_STREAM_END ) {
69+
if( ret == XAR_STREAM_ERR ) {
70+
fprintf(stderr, "Error extracting stream %s\n", path);
71+
exit(2);
72+
}
73+
74+
write(fd, buffer, sizeof(buffer)-s.avail_out);
75+
76+
s.avail_out = sizeof(buffer);
77+
s.next_out = buffer;
78+
}
79+
80+
if( xar_extract_tostream_end(&s) != XAR_STREAM_OK ) {
81+
fprintf(stderr, "Error ending stream %s\n", path);
82+
}
83+
84+
close(fd);
85+
free(path);
86+
}
87+
}

0 commit comments

Comments
 (0)