From 9a233e06cb17d6ae0978eb59f9678bdb86003a07 Mon Sep 17 00:00:00 2001 From: Krzysztof Opasiak Date: Mon, 16 May 2016 18:41:24 +0200 Subject: Allow to print also content of tar file Old implementation of lthor was printing not only name of tar file and it's size but also name of each entry and it's size. This patch allows also new implementation to do this. Change-Id: I05c12fb5368bcaaaeeb93a3116d9322ecc4349b1 Signed-off-by: Krzysztof Opasiak --- libthor/thor.h | 6 +++++ libthor/thor_raw_file.c | 15 +++++++++++ libthor/thor_tar.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++-- lthor.c | 24 ++++++++--------- 4 files changed, 100 insertions(+), 14 deletions(-) diff --git a/libthor/thor.h b/libthor/thor.h index 9bc9930..130f505 100644 --- a/libthor/thor.h +++ b/libthor/thor.h @@ -36,12 +36,18 @@ enum thor_data_type { THOR_PIT_DATA, }; +struct thor_data_src_entry { + char *name; + off_t size; +}; + struct thor_data_src { off_t (*get_file_length)(struct thor_data_src *src); off_t (*get_size)(struct thor_data_src *src); off_t (*get_block)(struct thor_data_src *src, void *data, off_t len); const char* (*get_name)(struct thor_data_src *src); int (*next_file)(struct thor_data_src *src); + struct thor_data_src_entry **(*get_entries)(struct thor_data_src *src); void (*release)(struct thor_data_src *src); }; diff --git a/libthor/thor_raw_file.c b/libthor/thor_raw_file.c index d4177bb..e357c68 100644 --- a/libthor/thor_raw_file.c +++ b/libthor/thor_raw_file.c @@ -33,6 +33,8 @@ struct file_data_src { const char* filename; off_t filesize; int pos; + struct thor_data_src_entry entry; + struct thor_data_src_entry *ent[2]; }; static off_t file_get_file_length(struct thor_data_src *src) @@ -78,6 +80,14 @@ static int file_next_file(struct thor_data_src *src) return !filedata->pos ? ++filedata->pos : 0; } +static struct thor_data_src_entry **file_get_entries(struct thor_data_src *src) +{ + struct file_data_src *filedata = + container_of(src, struct file_data_src, src); + + return filedata->ent; +} + int t_file_get_data_src(const char *path, struct thor_data_src **data) { int ret; @@ -110,12 +120,17 @@ int t_file_get_data_src(const char *path, struct thor_data_src **data) fdata->filesize = lseek(fdata->fd, 0, SEEK_END); lseek(fdata->fd, 0, SEEK_SET); + fdata->entry.name = (char *)fdata->filename; + fdata->entry.size = fdata->filesize; + fdata->ent[0] = &fdata->entry; + fdata->ent[1] = NULL; fdata->src.get_file_length = file_get_file_length; fdata->src.get_size = file_get_file_length; fdata->src.get_block = file_get_data_block; fdata->src.get_name = file_get_file_name; fdata->src.release = file_release; fdata->src.next_file = file_next_file; + fdata->src.get_entries = file_get_entries; fdata->pos = 0; *data = &fdata->src; diff --git a/libthor/thor_tar.c b/libthor/thor_tar.c index b91d102..f9508a4 100644 --- a/libthor/thor_tar.c +++ b/libthor/thor_tar.c @@ -20,15 +20,23 @@ #include #include #include +#include #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) @@ -84,7 +92,13 @@ 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); + } archive_read_close(tardata->ar); archive_read_finish(tardata->ar); archive_entry_free(tardata->ae); @@ -132,8 +146,15 @@ 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 :( @@ -143,7 +164,7 @@ static int tar_calculate_total(const char *path, struct tar_data_src *tardata) goto out; tardata->total_size = 0; - while (1) { + for (i = 0; 1; ++i) { ret = archive_read_next_header2(ar, ae); if (ret == ARCHIVE_EOF) { break; @@ -152,11 +173,46 @@ static int tar_calculate_total(const char *path, struct tar_data_src *tardata) goto cleanup; } - tardata->total_size += archive_entry_size(ae); + 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); @@ -164,6 +220,14 @@ 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; @@ -183,6 +247,7 @@ int t_tar_get_data_src(const char *path, struct thor_data_src **data) 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); diff --git a/lthor.c b/lthor.c index 4c5f999..03ee33e 100644 --- a/lthor.c +++ b/lthor.c @@ -117,9 +117,8 @@ static int init_data_parts(const char *pitfile, char **tarfilelist, } while (*tarfilelist) { - printf(TERM_YELLOW "%s :" TERM_NORMAL "\n" , *tarfilelist); data_parts[entry].type = THOR_NORMAL_DATA; - data_parts[0].name = *tarfilelist; + data_parts[entry].name = *tarfilelist; ret = thor_get_data_src(*tarfilelist, THOR_FORMAT_TAR, &(data_parts[entry].data)); if (ret) { @@ -290,17 +289,18 @@ static int process_download(struct thor_device_id *dev_id, const char *pitfile, /* Count the total size of data */ for (i = 0; i < entries; ++i) { - off_t size = data_parts[i].data->get_size(data_parts[i].data); + struct thor_data_src *dsrc = data_parts[i].data; + off_t size = dsrc->get_size(dsrc); + struct thor_data_src_entry **ent; + + printf(TERM_YELLOW "%s :\n" TERM_NORMAL, data_parts[i].name); + + for (ent = dsrc->get_entries(dsrc); ent && *ent; ++ent) + printf("[" TERM_LIGHT_GREEN "%s" TERM_NORMAL "]" + "\t %jdk\n", + (*ent)->name, + (intmax_t)((*ent)->size/KB)); - switch (data_parts[i].type) { - case THOR_PIT_DATA: - printf(TERM_YELLOW "%s :" TERM_NORMAL "%jdk\n", - data_parts[i].name, (intmax_t)(size/KB)); - break; - case THOR_NORMAL_DATA: - default: - break; - } total_size += size; } -- cgit v1.2.3