[gnome-boxes] util: Add libarchive helper functions



commit fa2a4097305bfd54a7fdd1f8bdaea98a32761080
Author: Lasse Schuirmann <lasse schuirmann gmail com>
Date:   Tue Jul 1 10:36:36 2014 +0200

    util: Add libarchive helper functions
    
    These functions will be used by the following patches to introduce a
    libarchive wrapper. They provide the capability to handle the
    Archive.Result type.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=730640

 src/util.vala |   55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 55 insertions(+), 0 deletions(-)
---
diff --git a/src/util.vala b/src/util.vala
index cf8a9dd..8ffab7f 100644
--- a/src/util.vala
+++ b/src/util.vala
@@ -1,4 +1,5 @@
 // This file is part of GNOME Boxes. License: LGPLv2+
+using Archive;
 using Config;
 
 public errordomain Boxes.Error {
@@ -365,6 +366,60 @@ namespace Boxes {
         return indented;
     }
 
+    public delegate Archive.Result LibarchiveFunction ();
+
+    public static void execute_libarchive_function (Archive.Archive    archive,
+                                                    LibarchiveFunction function,
+                                                    uint               num_retries = 1)
+                                                    throws GLib.IOError {
+        switch (function ()) {
+        case Archive.Result.OK:
+            return;
+
+        case Archive.Result.EOF:
+            throw new GLib.IOError.CLOSED ("End of file reached.");
+
+        case Archive.Result.RETRY:
+            if (num_retries < 1)
+                break;
+
+            execute_libarchive_function (archive, function, num_retries - 1);
+            return;
+
+        case Archive.Result.WARN:
+            warning ("%s", archive.error_string ());
+            return;
+
+        default:
+            break;
+        }
+
+        var msg = archive.error_string ();
+        if (msg == "Unrecognized archive format")
+            throw new GLib.IOError.NOT_SUPPORTED ("%s", msg);
+
+        throw new GLib.IOError.FAILED ("%s", msg);
+    }
+
+    public static bool get_next_header (Archive.Read              archive,
+                                        out unowned Archive.Entry iterator,
+                                        uint                      num_retries = 1)
+                                        throws GLib.IOError {
+        // Create own iterator that can be captured by a lambda
+        unowned Archive.Entry local_iterator = null;
+        try {
+            execute_libarchive_function (archive, () => {
+                return archive.next_header (out local_iterator);
+            }, num_retries);
+
+            iterator = local_iterator;
+            return true;
+        } catch (GLib.IOError.CLOSED e) {
+            iterator = local_iterator;
+            return false;
+        }
+    }
+
     // shamelessly copied form gnome-contacts
     private static unichar strip_char (unichar ch) {
         switch (ch.type ()) {


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