summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPatrick McDermott <patrick.mcdermott@libiquity.com>2023-04-14 15:43:48 (EDT)
committer Patrick McDermott <patrick.mcdermott@libiquity.com>2023-04-14 15:43:48 (EDT)
commitf36acc8cfe90ff98ad39e9d84b0270890f442273 (patch)
treeb0132b00ef250ea42b79fee794f423b1ef9c2793 /src
parent20cb252b483c6797a77219be3faa6513e35adf51 (diff)
[WIP] main: Print control file and list data files
Diffstat (limited to 'src')
-rw-r--r--src/main.c110
1 files changed, 92 insertions, 18 deletions
diff --git a/src/main.c b/src/main.c
index 25659b6..700cc36 100644
--- a/src/main.c
+++ b/src/main.c
@@ -47,36 +47,110 @@ _opkg_opk_main_file_read(void *user_data, char **buffer, size_t *size)
}
}
-int
-main(int argc, char *argv[])
+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 *gzip;
- struct opkg_opk_ustar *ustar;
- struct opkg_opk_ustar_member member;
- int ret;
+ 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(argv[1], "rb");
+ file.file = fopen(file_name, "rb");
if (file.file == NULL) {
- return EXIT_FAILURE;
+ return OPKG_OPK_ERROR;
}
- gzip = opkg_opk_gzip_init(&_opkg_opk_main_file_read, &file);
- if (gzip == NULL) {
+
+ outer_gzip = opkg_opk_gzip_init(&_opkg_opk_main_file_read, &file);
+ if (outer_gzip == NULL) {
fclose(file.file);
- return EXIT_FAILURE;
+ return OPKG_OPK_ERROR;
}
- ustar = opkg_opk_ustar_init(gzip);
- if (ustar == NULL) {
- opkg_opk_gzip_free(gzip);
+
+ outer_ustar = opkg_opk_ustar_init(outer_gzip);
+ if (outer_ustar == NULL) {
+ opkg_opk_gzip_free(outer_gzip);
fclose(file.file);
- return EXIT_FAILURE;
+ return OPKG_OPK_ERROR;
+ }
+
+ opkg_opk_ustar_seek(outer_ustar, outer_member);
+ inner_gzip = opkg_opk_gzip_init(&opkg_opk_ustar_read, outer_ustar);
+ if (inner_gzip == NULL) {
+ opkg_opk_ustar_free(outer_ustar);
+ opkg_opk_gzip_free(outer_gzip);
+ fclose(file.file);
+ return OPKG_OPK_ERROR;
+ }
+
+ inner_ustar = opkg_opk_ustar_init(inner_gzip);
+ if (inner_ustar == NULL) {
+ opkg_opk_gzip_free(inner_gzip);
+ opkg_opk_ustar_free(outer_ustar);
+ opkg_opk_gzip_free(outer_gzip);
+ fclose(file.file);
+ return OPKG_OPK_ERROR;
+ }
+
+ inner_action(inner_ustar);
+
+ 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;
+}
+
+static int
+_opkg_opk_main_read_control(struct opkg_opk_ustar *ustar)
+{
+ char buffer[OPKG_OPK_USTAR_RECORD_SIZE];
+ size_t size;
+ int ret;
+
+ opkg_opk_ustar_seek(ustar, "control");
+ while ((ret = opkg_opk_ustar_read(ustar, &buffer, &size)) ==
+ OPKG_OPK_OK) {
+ fwrite(buffer, 1, size, stdout);
+ }
+ if (ret == OPKG_OPK_ERROR) {
+ 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 member;
+ int ret;
+
while ((ret = opkg_opk_ustar_list(ustar, &member)) == OPKG_OPK_OK) {
puts(member.name);
}
- opkg_opk_gzip_free(gzip);
- opkg_opk_ustar_free(ustar);
- fclose(file.file);
+ if (ret == OPKG_OPK_ERROR) {
+ return OPKG_OPK_ERROR;
+ }
+
+ 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;
}