Allow to print also content of tar file
authorKrzysztof Opasiak <k.opasiak@samsung.com>
Mon, 16 May 2016 16:41:24 +0000 (18:41 +0200)
committerSeung-Woo Kim <sw0312.kim@samsung.com>
Mon, 25 Jul 2016 05:39:49 +0000 (14:39 +0900)
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 <k.opasiak@samsung.com>
libthor/thor.h
libthor/thor_raw_file.c
libthor/thor_tar.c
lthor.c

index 9bc99300988ed08bb00561da368a1cd5ac71f5f1..130f505dcc5650eaf5d71b0493a2baad2de19c1f 100644 (file)
@@ -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);
 };
 
index d4177bb433ebb0684953890f4d26bf0da28a9bd2..e357c68e3075671521d5607109df857fcc37ba9e 100644 (file)
@@ -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;
index b91d102e94dcddca2f72904ba265742f67d9a567..f9508a4f053b69bd76082190600496dfa35706e3 100644 (file)
 #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)
@@ -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 4c5f99911c8b3b7b34154b07c4967ed8b98c3ac7..03ee33eb5285900c4eeb7a87bf150ae24ddaee21 100644 (file)
--- 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;
        }