[gcab] trivial: Split up the code creating a GOutputStream for a GCabFile



commit f596a0afe5648e41c8ad9fe34d017b7e76dea17f
Author: Richard Hughes <richard hughsie com>
Date:   Fri Dec 15 10:01:08 2017 +0000

    trivial: Split up the code creating a GOutputStream for a GCabFile
    
    No functional changes.

 libgcab/gcab-file.c   |   40 ++++++++++++++++++++++++++++++++++++++++
 libgcab/gcab-folder.c |   44 +++++---------------------------------------
 libgcab/gcab-priv.h   |    4 ++++
 3 files changed, 49 insertions(+), 39 deletions(-)
---
diff --git a/libgcab/gcab-file.c b/libgcab/gcab-file.c
index ee8adb4..e2e24e9 100644
--- a/libgcab/gcab-file.c
+++ b/libgcab/gcab-file.c
@@ -389,6 +389,46 @@ gcab_file_get_input_stream (GCabFile *self, GCancellable *cancellable, GError **
     return NULL;
 }
 
+G_GNUC_INTERNAL GOutputStream *
+gcab_file_get_output_stream (GCabFile *self,
+                             GFile *path_extract,
+                             GCancellable *cancellable,
+                             GError **error)
+{
+    /* make path have UNIX directory slashes */
+    g_autofree gchar *fname = g_strdup (gcab_file_get_extract_name (self));
+    g_strdelimit (fname, "\\", '/');
+
+    /* "Rebase" the file in the given path, to ensure we never escape it */
+    g_autoptr(GFile) file = g_file_resolve_relative_path (path_extract, fname);
+    if (!g_file_has_prefix (file, path_extract)) {
+        g_autofree gchar *rawpath = g_file_get_path (file);
+        if (rawpath != NULL) {
+            char *newpath = rawpath;
+            while (*newpath != 0 && *newpath == G_DIR_SEPARATOR) {
+                newpath++;
+            }
+            g_autoptr(GFile) newfile = g_file_resolve_relative_path (path_extract, newpath);
+            g_set_object (&file, newfile);
+        }
+    }
+
+    /* create parent directories */
+    g_autoptr(GFile) parent = g_file_get_parent (file);
+    g_autoptr(GError) error_local = NULL;
+    if (!g_file_make_directory_with_parents (parent, cancellable, &error_local)) {
+        if (!g_error_matches (error_local, G_IO_ERROR, G_IO_ERROR_EXISTS)) {
+            g_propagate_error (error, g_steal_pointer (&error_local));
+            return NULL;
+        }
+    }
+
+    /* write to a file */
+    return G_OUTPUT_STREAM (g_file_replace (file, NULL, FALSE,
+                                            G_FILE_CREATE_REPLACE_DESTINATION,
+                                            cancellable, error));
+}
+
 /**
  * gcab_file_get_extract_name:
  * @file: a #GCabFile
diff --git a/libgcab/gcab-folder.c b/libgcab/gcab-folder.c
index 61bff20..1b3be1c 100644
--- a/libgcab/gcab-folder.c
+++ b/libgcab/gcab-folder.c
@@ -378,7 +378,7 @@ sort_by_offset (GCabFile *a, GCabFile *b)
 G_GNUC_INTERNAL gboolean
 gcab_folder_extract (GCabFolder *self,
                      GDataInputStream *data,
-                     GFile *path,
+                     GFile *path_extract,
                      guint8 res_data,
                      GCabFileCallback file_callback,
                      GFileProgressCallback progress_callback,
@@ -386,8 +386,6 @@ gcab_folder_extract (GCabFolder *self,
                      GCancellable *cancellable,
                      GError **error)
 {
-    GError *my_error = NULL;
-    g_autoptr(GFileOutputStream) out = NULL;
     GSList *f = NULL;
     g_autoptr(GSList) files = NULL;
     g_autoptr(cdata_t) cdata = g_new0 (cdata_t, 1);
@@ -407,41 +405,9 @@ gcab_folder_extract (GCabFolder *self,
         if (file_callback && !file_callback (file, callback_data))
             continue;
 
-        g_autofree gchar *fname = g_strdup (gcab_file_get_extract_name (file));
-        int i = 0, len = strlen (fname);
-        for (i = 0; i < len; i++)
-            if (fname[i] == '\\')
-                fname[i] = '/';
-
-        g_autoptr(GFile) gfile = g_file_resolve_relative_path (path, fname);
-
-        if (!g_file_has_prefix (gfile, path)) {
-            // "Rebase" the file in the given path, to ensure we never escape it
-            g_autofree gchar *rawpath = g_file_get_path (gfile);
-            if (rawpath != NULL) {
-                char *newpath = rawpath;
-                while (*newpath != 0 && *newpath == G_DIR_SEPARATOR) {
-                    newpath++;
-                }
-                g_autoptr(GFile) newgfile = g_file_resolve_relative_path (path, newpath);
-                g_set_object (&gfile, newgfile);
-            }
-        }
-
-        g_autoptr(GFile) parent = g_file_get_parent (gfile);
-
-        if (!g_file_make_directory_with_parents (parent, cancellable, &my_error)) {
-            if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
-                g_clear_error (&my_error);
-            else {
-                g_propagate_error (error, my_error);
-                return FALSE;
-            }
-        }
-
-        g_autoptr(GFileOutputStream) out2 = NULL;
-        out2 = g_file_replace (gfile, NULL, FALSE, G_FILE_CREATE_REPLACE_DESTINATION, cancellable, error);
-        if (!out2)
+        g_autoptr(GOutputStream) out = NULL;
+        out = gcab_file_get_output_stream (file, path_extract, cancellable, error);
+        if (out == NULL)
             return FALSE;
 
         guint32 usize = gcab_file_get_usize (file);
@@ -468,7 +434,7 @@ gcab_folder_extract (GCabFolder *self,
                     gcab_file_get_uoffset (file) - nubytes : 0;
                 const void *p = &cdata->out[offset];
                 gsize count = MIN (usize, cdata->nubytes - offset);
-                if (!g_output_stream_write_all (G_OUTPUT_STREAM (out2), p, count,
+                if (!g_output_stream_write_all (G_OUTPUT_STREAM (out), p, count,
                                                 NULL, cancellable, error))
                     return FALSE;
                 usize -= count;
diff --git a/libgcab/gcab-priv.h b/libgcab/gcab-priv.h
index a4efab6..6d200e5 100644
--- a/libgcab/gcab-priv.h
+++ b/libgcab/gcab-priv.h
@@ -64,5 +64,9 @@ gboolean         gcab_folder_extract                 (GCabFolder *self,
 GInputStream    *gcab_file_get_input_stream          (GCabFile *file,
                                                       GCancellable *cancellable,
                                                       GError **error);
+GOutputStream   *gcab_file_get_output_stream         (GCabFile *cabfile,
+                                                      GFile *path_extract,
+                                                      GCancellable *cancellable,
+                                                      GError **error);
 
 #endif /* GCAB_PRIV_H */


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