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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
/*
* libthor - Tizen Thor communication protocol
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <archive.h>
#include <archive_entry.h>
#include <errno.h>
#include <string.h>
#include <sys/queue.h>
#include "thor.h"
#include "thor_internal.h"
struct entry_container {
struct thor_data_src_entry entry;
STAILQ_ENTRY(entry_container) node;
};
struct tar_data_src {
struct thor_data_src src;
struct archive *ar;
struct archive_entry *ae;
off_t total_size;
struct thor_data_src_entry **entries;
STAILQ_HEAD(ent, entry_container) ent;
};
static off_t tar_get_file_length(struct thor_data_src *src)
{
struct tar_data_src *tardata =
container_of(src, struct tar_data_src, src);
return archive_entry_size(tardata->ae);
}
static off_t tar_get_size(struct thor_data_src *src)
{
struct tar_data_src *tardata =
container_of(src, struct tar_data_src, src);
return tardata->total_size;
}
static off_t tar_get_data_block(struct thor_data_src *src,
void *data, off_t len)
{
struct tar_data_src *tardata =
container_of(src, struct tar_data_src, src);
return archive_read_data(tardata->ar, data, len);
}
static const char *tar_get_file_name(struct thor_data_src *src)
{
struct tar_data_src *tardata =
container_of(src, struct tar_data_src, src);
return archive_entry_pathname(tardata->ae);
}
static int tar_next_file(struct thor_data_src *src)
{
struct tar_data_src *tardata =
container_of(src, struct tar_data_src, src);
int ret;
ret = archive_read_next_header2(tardata->ar, tardata->ae);
if (ret == ARCHIVE_OK)
return 1;
if (ret == ARCHIVE_EOF)
return 0;
return -EINVAL;
}
static void tar_release(struct thor_data_src *src)
{
struct tar_data_src *tardata =
container_of(src, struct tar_data_src, src);
struct entry_container *container;
while (!STAILQ_EMPTY(&tardata->ent)) {
container = STAILQ_FIRST(&tardata->ent);
STAILQ_REMOVE_HEAD(&tardata->ent, node);
free(container);
}
free(tardata->entries);
archive_read_close(tardata->ar);
archive_read_finish(tardata->ar);
archive_entry_free(tardata->ae);
free(tardata);
}
static int tar_prep_read(const char *path, struct archive **archive,
struct archive_entry **aentry)
{
struct archive *ar;
struct archive_entry *ae;
int ret;
ar = archive_read_new();
if (!ar)
return -ENOMEM;
ae = archive_entry_new();
if (!ae)
goto read_finish;
archive_read_support_format_tar(ar);
archive_read_support_compression_gzip(ar);
archive_read_support_compression_bzip2(ar);
if (!strcmp(path, "-"))
ret = archive_read_open_FILE(ar, stdin);
else
ret = archive_read_open_filename(ar, path, 512);
if (ret)
goto cleanup;
*archive = ar;
*aentry = ae;
return 0;
cleanup:
archive_entry_free(ae);
read_finish:
archive_read_finish(ar);
return ret;
}
static int tar_calculate_total(const char *path, struct tar_data_src *tardata)
{
struct archive *ar;
struct archive_entry *ae;
char *name;
off_t size;
struct entry_container *container;
int i;
int ret;
STAILQ_INIT(&tardata->ent);
tardata->entries = NULL;
/*
* Yes this is very ugly but libarchive doesn't
* allow to reset position :(
*/
ret = tar_prep_read(path, &ar, &ae);
if (ret)
goto out;
tardata->total_size = 0;
for (i = 0; 1; ++i) {
ret = archive_read_next_header2(ar, ae);
if (ret == ARCHIVE_EOF) {
break;
} else if (ret != ARCHIVE_OK) {
tardata->total_size = -EINVAL;
goto cleanup;
}
name = strdup(archive_entry_pathname(ae));
if (!name) {
ret = -ENOMEM;
goto cleanup;
}
size = archive_entry_size(ae);
tardata->total_size += size;
container = calloc(1, sizeof(*container));
if (!container) {
free(name);
ret = -ENOMEM;
goto cleanup;
}
container->entry.name = name;
container->entry.size = size;
STAILQ_INSERT_TAIL(&tardata->ent, container, node);
}
tardata->entries = calloc(i + 1, sizeof(*(tardata->entries)));
if (!tardata->entries) {
ret = -ENOMEM;
goto cleanup;
}
i = 0;
STAILQ_FOREACH(container, &tardata->ent, node)
tardata->entries[i++] = &container->entry;
ret = 0;
cleanup:
if (ret) {
while (!STAILQ_EMPTY(&tardata->ent)) {
container = STAILQ_FIRST(&tardata->ent);
STAILQ_REMOVE_HEAD(&tardata->ent, node);
free(container);
}
}
archive_read_close(ar);
archive_read_finish(ar);
archive_entry_free(ae);
out:
return ret;
}
static struct thor_data_src_entry **tar_get_entries(struct thor_data_src *src)
{
struct tar_data_src *tardata =
container_of(src, struct tar_data_src, src);
return tardata->entries;
}
int t_tar_get_data_src(const char *path, struct thor_data_src **data)
{
struct tar_data_src *tdata;
int ret;
tdata = calloc(1, sizeof(*tdata));
if (!tdata)
return -ENOMEM;
/* open the tar archive */
ret = tar_prep_read(path, &tdata->ar, &tdata->ae);
if (ret)
goto free_tdata;
tdata->src.get_file_length = tar_get_file_length;
tdata->src.get_size = tar_get_size;
tdata->src.get_block = tar_get_data_block;
tdata->src.get_name = tar_get_file_name;
tdata->src.next_file = tar_next_file;
tdata->src.get_entries = tar_get_entries;
tdata->src.release = tar_release;
ret = tar_calculate_total(path, tdata);
if (ret < 0)
goto read_close;
*data = &tdata->src;
return 0;
read_close:
archive_read_close(tdata->ar);
archive_entry_free(tdata->ae);
free_tdata:
free(tdata);
return ret;
}
|