forked from nih-at/libzip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoclose-archive.c
160 lines (130 loc) · 5.52 KB
/
autoclose-archive.c
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
152
153
154
155
156
157
158
159
160
/*
autoclose-archive.c -- automatically close archive when source is closed
Copyright (C) 2022-2023 Dieter Baron and Thomas Klausner
This file is part of libzip, a library to manipulate ZIP archives.
The authors can be contacted at <libzip@nih.at>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
3. The names of the authors may not be used to endorse or promote
products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
This example layered source takes ownership of a zip archive and discards it when the source is freed.
It can be used to add files from various zip archives without having to keep track of them yourself.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zip.h>
struct ctx {
zip_t* archive;
};
zip_int64_t callback(zip_source_t* src, void *ud, void* data, zip_uint64_t length, zip_source_cmd_t command) {
struct ctx* ctx = (struct ctx*)ud;
switch (command) {
case ZIP_SOURCE_FREE:
/* Close zip archive we took ownership of */
zip_discard(ctx->archive);
/* Free our own context */
free(ctx);
return 0;
default:
/* For all other commands, use default implementation */
return zip_source_pass_to_lower_layer(src, data, length, command);
}
}
zip_source_t* create_layered_autoclose(zip_source_t* source, zip_t *archive, zip_error_t *error) {
struct ctx* ctx = (struct ctx*)malloc(sizeof(*ctx));
zip_source_t *autoclose_source;
/* Allocate context. */
if (ctx == NULL) {
zip_error_set(error, ZIP_ER_MEMORY, 0);
return NULL;
}
/* Initialize context */
ctx->archive = archive;
/* Create layered source using our callback and context. */
autoclose_source = zip_source_layered_create(source, callback, ctx, error);
/* In case of error, free context. */
if (autoclose_source == NULL) {
free(ctx);
}
return autoclose_source;
}
int
main(int argc, char *argv[]) {
const char *destination_archive, *source_archive, *source_file;
zip_int64_t index;
zip_source_t *src, *src_autoclose;
zip_t *z_source, *z_destination;
int err;
if (argc != 4) {
fprintf(stderr, "usage: %s destination-archive source-archive source-file\n", argv[0]);
return 1;
}
destination_archive = argv[1];
source_archive = argv[2];
source_file = argv[3];
if ((z_source = zip_open(source_archive, 0, &err)) == NULL) {
zip_error_t error;
zip_error_init_with_code(&error, err);
fprintf(stderr, "%s: cannot open zip archive '%s': %s\n", argv[0], source_archive, zip_error_strerror(&error));
zip_error_fini(&error);
exit(1);
}
if ((index = zip_name_locate(z_source, source_file, 0)) < 0) {
fprintf(stderr, "%s: cannot find file '%s' in '%s': %s\n", argv[0], source_file, source_archive, zip_strerror(z_source));
zip_discard(z_source);
exit(1);
}
if ((src = zip_source_zip_file(z_source, z_source, index, 0, 0, -1, NULL)) == NULL) {
fprintf(stderr, "%s: cannot open file '%s' in '%s': %s\n", argv[0], source_file, source_archive, zip_strerror(z_source));
zip_discard(z_source);
exit(1);
}
zip_error_t error;
if ((src_autoclose = create_layered_autoclose(src, z_source, &error)) == NULL) {
fprintf(stderr, "%s: cannot create layered source: %s\n", argv[0], zip_error_strerror(&error));
zip_source_free(src);
zip_discard(z_source);
exit(1);
}
if ((z_destination = zip_open(destination_archive, ZIP_CREATE, &err)) == NULL) {
zip_error_init_with_code(&error, err);
fprintf(stderr, "%s: cannot open zip archive '%s': %s\n", argv[0], destination_archive, zip_error_strerror(&error));
zip_error_fini(&error);
zip_source_free(src_autoclose); /* freeing src_autoclose closes z_source */
exit(1);
}
if ((zip_file_add(z_destination, source_file, src_autoclose, 0)) < 0) {
fprintf(stderr, "%s: cannot add file: %s\n", argv[0], zip_strerror(z_source));
zip_source_free(src_autoclose);
zip_discard(z_destination);
exit(1);
}
if ((zip_close(z_destination)) < 0) {
fprintf(stderr, "%s: cannot close archive '%s': %s\n", argv[0], destination_archive, zip_strerror(z_source));
zip_discard(z_destination);
exit(1);
}
exit(0);
}