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
|
/*
* Copyright (C) 2023 Patrick McDermott
*
* This file is part of opkg-opk.
*
* opkg-opk 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 3 of the License, or
* (at your option) any later version.
*
* opkg-opk 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 opkg-opk. If not, see <http://www.gnu.org/licenses/>.
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "defs.h"
#include "gzip.h"
#include "ustar.h"
#define OPKG_OPK_MAIN_FILE_BUFFER_SIZE_ 8192
struct _opkg_opk_main_file {
FILE *file;
char buffer[OPKG_OPK_MAIN_FILE_BUFFER_SIZE_];
};
static int
_opkg_opk_main_file_read(void *user_data, char **buffer, size_t *size)
{
struct _opkg_opk_main_file *file = user_data;
*buffer = file->buffer;
*size = fread(file->buffer, 1, OPKG_OPK_MAIN_FILE_BUFFER_SIZE_,
file->file);
if (feof(file->file)) {
return OPKG_OPK_END;
} else if (ferror(file->file) || *size == 0) {
return OPKG_OPK_ERROR;
} else {
return OPKG_OPK_OK;
}
}
static int
_opkg_opk_main_extract(const char *file_name, const char *outer_member,
int (*inner_action)(struct opkg_opk_ustar *))
{
struct _opkg_opk_main_file file;
struct opkg_opk_gzip *outer_gzip;
struct opkg_opk_ustar *outer_ustar;
struct opkg_opk_gzip *inner_gzip;
struct opkg_opk_ustar *inner_ustar;
file.file = fopen(file_name, "rb");
if (file.file == NULL) {
fprintf(stderr, "Error: Failed to open file \"%s\"\n",
file_name);
goto error0;
}
outer_gzip = opkg_opk_gzip_init(&_opkg_opk_main_file_read, &file);
if (outer_gzip == NULL) {
fputs("Error: Failed to initialize\n", stderr);
goto error1;
}
outer_ustar = opkg_opk_ustar_init(outer_gzip);
if (outer_ustar == NULL) {
fputs("Error: Failed to initialize\n", stderr);
goto error2;
}
if (opkg_opk_ustar_seek(outer_ustar, 1, outer_member) != OPKG_OPK_OK) {
fprintf(stderr, "Error: Failed to find \"%s\" in archive\n",
outer_member);
goto error3;
}
inner_gzip = opkg_opk_gzip_init(
(opkg_opk_gzip_read_func *) &opkg_opk_ustar_read,
outer_ustar);
if (inner_gzip == NULL) {
fputs("Error: Failed to initialize\n", stderr);
goto error3;
}
inner_ustar = opkg_opk_ustar_init(inner_gzip);
if (inner_ustar == NULL) {
fputs("Error: Failed to initialize\n", stderr);
goto error4;
}
if (inner_action(inner_ustar) != OPKG_OPK_OK) {
goto error5;
}
opkg_opk_ustar_free(inner_ustar);
opkg_opk_gzip_free(inner_gzip);
opkg_opk_ustar_free(outer_ustar);
opkg_opk_gzip_free(outer_gzip);
fclose(file.file);
return OPKG_OPK_OK;
error5:
opkg_opk_ustar_free(inner_ustar);
error4:
opkg_opk_gzip_free(inner_gzip);
error3:
opkg_opk_ustar_free(outer_ustar);
error2:
opkg_opk_gzip_free(outer_gzip);
error1:
fclose(file.file);
error0:
return OPKG_OPK_ERROR;
}
static int
_opkg_opk_main_read_control(struct opkg_opk_ustar *ustar)
{
char *buffer;
size_t size;
int ret;
if (opkg_opk_ustar_seek(ustar, 2, "control", "./control") !=
OPKG_OPK_OK) {
fputs("Error: Failed to find control file\n", stderr);
return OPKG_OPK_ERROR;
}
while ((ret = opkg_opk_ustar_read(ustar, &buffer, &size)) ==
OPKG_OPK_OK) {
fwrite(buffer, 1, size, stdout);
}
if (ret == OPKG_OPK_ERROR) {
fputs("Error: Failed to read control file\n", stderr);
return OPKG_OPK_ERROR;
}
return OPKG_OPK_OK;
}
static int
_opkg_opk_main_list_members(struct opkg_opk_ustar *ustar)
{
struct opkg_opk_ustar_member *head;
struct opkg_opk_ustar_member *tail;
struct opkg_opk_ustar_member *member;
int ret;
size_t uname_len;
size_t uname_len_max;
size_t gname_len;
size_t gname_len_max;
uint64_t size_max;
char fmt[29];
/* "%c%s %-32s/%-32s %11d %s %s\n" */
char mode[10];
char mtime[20];
head = NULL;
uname_len_max = 0;
gname_len_max = 0;
size_max = 0;
while ((ret = opkg_opk_ustar_list(ustar, &member)) == OPKG_OPK_OK) {
if (head == NULL) {
head = member;
} else {
tail->next = member;
}
tail = member;
uname_len = strlen(member->uname);
if (uname_len > uname_len_max) {
uname_len_max = uname_len;
}
gname_len = strlen(member->gname);
if (gname_len > gname_len_max) {
gname_len_max = gname_len;
}
if (member->size > size_max) {
size_max = member->size;
}
}
if (ret == OPKG_OPK_ERROR) {
fputs("Error: Failed to list data files\n", stderr);
return OPKG_OPK_ERROR;
}
tail->next = NULL;
snprintf(fmt, sizeof(fmt), "%%c%%s %%-%zus/%%-%zus %%%lid %%s %%s\n",
uname_len, gname_len, lrint(ceil(log10(size_max))));
for (member = head; member != NULL;) {
if (member->mode & 00400) mode[0] = 'r'; else mode[0] = '-';
if (member->mode & 00200) mode[1] = 'w'; else mode[1] = '-';
if (member->mode & 00100) mode[2] = 'x'; else mode[2] = '-';
if (member->mode & 04000) mode[2] = 's';
if (member->mode & 00040) mode[3] = 'r'; else mode[3] = '-';
if (member->mode & 00020) mode[4] = 'w'; else mode[4] = '-';
if (member->mode & 00010) mode[5] = 'x'; else mode[5] = '-';
if (member->mode & 02000) mode[5] = 's';
if (member->mode & 00004) mode[6] = 'r'; else mode[6] = '-';
if (member->mode & 00002) mode[7] = 'w'; else mode[7] = '-';
if (member->mode & 00001) mode[8] = 'x'; else mode[8] = '-';
if (member->mode & 01000) mode[8] = 't';
mode[9] = '\0';
strftime(mtime, sizeof(mtime), "%Y-%m-%d %H:%M:%S",
localtime(&member->mtime));
printf(fmt,
member->type, mode,
member->uname, member->gname,
member->size, mtime, member->name);
head = member;
member = member->next;
free(head);
}
return OPKG_OPK_OK;
}
int
main(int argc, char *argv[])
{
if (_opkg_opk_main_extract(argv[1], "control.tar.gz",
&_opkg_opk_main_read_control) != OPKG_OPK_OK) {
return EXIT_FAILURE;
}
puts("");
if (_opkg_opk_main_extract(argv[1], "data.tar.gz",
&_opkg_opk_main_list_members) != OPKG_OPK_OK) {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
|