[gcab/wip/hughsie/bzero: 4/5] Add a helper executable to create massive compressed archives



commit d530be420c61be3755f26f1471ca1d843af831ad
Author: Richard Hughes <richard hughsie com>
Date:   Fri Dec 29 20:36:40 2017 +0000

    Add a helper executable to create massive compressed archives

 src/gcab-bzero.c |  101 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/meson.build  |   18 ++++++++++
 2 files changed, 119 insertions(+), 0 deletions(-)
---
diff --git a/src/gcab-bzero.c b/src/gcab-bzero.c
new file mode 100644
index 0000000..eca4b89
--- /dev/null
+++ b/src/gcab-bzero.c
@@ -0,0 +1,101 @@
+/*
+ * LibGCab
+ * Copyright (c) 2017, Richard Hughes <richard hughsie com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301 USA
+ */
+
+#include "config.h"
+
+#include <stdlib.h>
+#include <libgcab.h>
+
+int
+main (int argc, char *argv[])
+{
+    guint64 bufsz = 1024;
+    guint64 nfiles = 1;
+    g_autofree guint8 *buf = NULL;
+    g_autoptr(GBytes) bytes = NULL;
+    g_autoptr(GCabCabinet) cabinet = NULL;
+    g_autoptr(GCabFolder) folder = NULL;
+    g_autoptr(GError) error = NULL;
+    g_autoptr(GFile) opfile = NULL;
+    g_autoptr(GOutputStream) ostream = NULL;
+
+    /* check args */
+    if (argc < 2) {
+        g_printerr ("Incorrect args, expected FILENAME [SIZE-IN-MB] [NR-FILES]\n");
+        return EXIT_FAILURE;
+    }
+
+    /* get size */
+    if (argc > 2) {
+        bufsz = g_ascii_strtoull (argv[2], NULL, 10);
+        if (bufsz == 0) {
+            g_printerr ("Failed to parse base-10 integer: %s\n", argv[2]);
+            return EXIT_FAILURE;
+        }
+    }
+    bufsz *= 0x100000;
+
+    /* get number of files */
+    if (argc > 3) {
+        nfiles = g_ascii_strtoull (argv[3], NULL, 10);
+        if (nfiles == 0) {
+            g_printerr ("Failed to parse base-10 integer: %s\n", argv[3]);
+            return EXIT_FAILURE;
+        }
+    }
+
+    /* create archive */
+    cabinet = gcab_cabinet_new ();
+    folder = gcab_folder_new (GCAB_COMPRESSION_MSZIP);
+    if (!gcab_cabinet_add_folder (cabinet, folder, &error)) {
+        g_printerr ("%s\n", error->message);
+        return EXIT_FAILURE;
+    }
+
+    /* allocate huge buffer */
+    buf = g_malloc0 (bufsz);
+    bytes = g_bytes_new_take (g_steal_pointer (&buf), bufsz);
+
+    /* add multiple files to the archive */
+    for (guint64 i = 0; i < nfiles; i++) {
+        g_autofree gchar *name = g_strdup_printf ("%04x", (guint) i);
+        g_autoptr(GCabFile) file = gcab_file_new_with_bytes (name, bytes);
+        if (!gcab_folder_add_file (folder, file, FALSE, NULL, &error)) {
+            g_printerr ("%s\n", error->message);
+            return EXIT_FAILURE;
+        }
+    }
+
+    /* save file */
+    opfile = g_file_new_for_path (argv[1]);
+    ostream = G_OUTPUT_STREAM (g_file_replace (opfile, NULL, FALSE,
+                                               0, NULL, &error));
+    if (ostream == NULL) {
+        g_printerr ("%s\n", error->message);
+        return EXIT_FAILURE;
+    }
+    if (!gcab_cabinet_write_simple (cabinet, ostream, NULL, NULL, NULL, &error)) {
+        g_printerr ("%s\n", error->message);
+        return EXIT_FAILURE;
+    }
+
+    /* success */
+    return EXIT_SUCCESS;
+}
diff --git a/src/meson.build b/src/meson.build
index bd6f517..4149fbc 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -43,3 +43,21 @@ executable(
   ],
   c_args : cargs,
 )
+
+executable(
+  'gcab-bzero',
+  sources : [
+    'gcab-bzero.c',
+  ],
+  include_directories : [
+    include_directories('..'),
+    include_directories('../libgcab'),
+  ],
+  dependencies : [
+    gio,
+  ],
+  link_with : [
+    libgcab,
+  ],
+  c_args : cargs,
+)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]