Skip to content

Commit 2c59db6

Browse files
committed
Rework minitar example to use archive_read_disk directory
traversal instead of a local copy of tree.c SVN-Revision: 2526
1 parent c938a22 commit 2c59db6

4 files changed

Lines changed: 62 additions & 534 deletions

File tree

examples/minitar/Makefile

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,21 @@
55
#
66
CFLAGS= \
77
-DNO_BZIP2_CREATE \
8-
-DNO_BZIP2_EXTRACT \
9-
-DNO_COMPRESS_EXTRACT \
10-
-DNO_CPIO_EXTRACT \
11-
-DNO_CREATE \
12-
-DNO_GZIP_CREATE \
13-
-DNO_GZIP_EXTRACT \
14-
-DNO_LOOKUP
8+
-I../../libarchive \
9+
-g
1510

16-
# Omit 'tree.o' if you're not including create support
17-
#OBJS= minitar.o tree.o
18-
OBJS= minitar.o
11+
# How to link against libarchive.
12+
LIBARCHIVE= ../../libarchive/libarchive.a
1913

2014
all: minitar
2115

22-
minitar: $(OBJS)
23-
cc -o minitar -static $(OBJS) -larchive -lz -lbz2
16+
minitar: minitar.o
17+
cc -g -o minitar minitar.o $(LIBARCHIVE) -lz -lbz2
2418
strip minitar
2519
ls -l minitar
2620

2721
minitar.o: minitar.c
2822

29-
tree.o: tree.c
30-
3123
clean::
3224
rm -f *.o
3325
rm -f minitar

examples/minitar/minitar.c

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@
4040
*/
4141

4242
#include <sys/types.h>
43-
__FBSDID("$FreeBSD$");
44-
4543
#include <sys/stat.h>
4644

4745
#include <archive.h>
@@ -52,10 +50,6 @@ __FBSDID("$FreeBSD$");
5250
#include <string.h>
5351
#include <unistd.h>
5452

55-
#ifndef NO_CREATE
56-
#include "tree.h"
57-
#endif
58-
5953
/*
6054
* NO_CREATE implies NO_BZIP2_CREATE and NO_GZIP_CREATE and NO_COMPRESS_CREATE.
6155
*/
@@ -264,28 +258,71 @@ create(const char *filename, int compress, const char **argv)
264258
archive_read_disk_set_standard_lookup(disk);
265259
#endif
266260
while (*argv != NULL) {
267-
struct tree *t = tree_open(*argv);
268-
while (tree_next(t)) {
261+
struct archive *disk = archive_read_disk_new();
262+
int r;
263+
264+
r = archive_read_disk_open(disk, *argv);
265+
if (r != ARCHIVE_OK) {
266+
errmsg(archive_error_string(disk));
267+
errmsg("\n");
268+
exit(1);
269+
}
270+
271+
for (;;) {
272+
int needcr = 0;
273+
269274
entry = archive_entry_new();
270-
archive_entry_set_pathname(entry, tree_current_path(t));
271-
archive_read_disk_entry_from_file(disk, entry, -1,
272-
tree_current_stat(t));
275+
r = archive_read_next_header2(disk, entry);
276+
if (r == ARCHIVE_EOF)
277+
break;
278+
if (r != ARCHIVE_OK) {
279+
errmsg(archive_error_string(disk));
280+
errmsg("\n");
281+
exit(1);
282+
}
283+
archive_read_disk_descend(disk);
273284
if (verbose) {
274285
msg("a ");
275-
msg(tree_current_path(t));
286+
msg(archive_entry_pathname(entry));
287+
needcr = 1;
288+
}
289+
r = archive_write_header(a, entry);
290+
if (r < ARCHIVE_OK) {
291+
errmsg(": ");
292+
errmsg(archive_error_string(a));
293+
needcr = 1;
276294
}
277-
archive_write_header(a, entry);
278-
fd = open(tree_current_access_path(t), O_RDONLY);
279-
len = read(fd, buff, sizeof(buff));
280-
while (len > 0) {
281-
archive_write_data(a, buff, len);
295+
if (r == ARCHIVE_FATAL)
296+
exit(1);
297+
if (r > ARCHIVE_FAILED) {
298+
#if 0
299+
/* Ideally, we would be able to use
300+
* the same code to copy a body from
301+
* an archive_read_disk to an
302+
* archive_write that we use for
303+
* copying data from an archive_read
304+
* to an archive_write_disk.
305+
* Unfortunately, this doesn't quite
306+
* work yet. */
307+
copy_data(disk, a);
308+
#else
309+
/* For now, we use a simpler loop to copy data
310+
* into the target archive. */
311+
fd = open(archive_entry_sourcepath(entry), O_RDONLY);
282312
len = read(fd, buff, sizeof(buff));
313+
while (len > 0) {
314+
archive_write_data(a, buff, len);
315+
len = read(fd, buff, sizeof(buff));
316+
}
317+
close(fd);
318+
#endif
283319
}
284-
close(fd);
285320
archive_entry_free(entry);
286-
if (verbose)
321+
if (needcr)
287322
msg("\n");
288323
}
324+
archive_read_close(disk);
325+
archive_read_free(disk);
289326
argv++;
290327
}
291328
archive_write_close(a);

0 commit comments

Comments
 (0)