[network-manager-openvpn/nm-1-0: 2/4] import: handle line splitting and utf8 conversion inside do_import()



commit 0fae95b9e4618176a902af96d81ea3d095952de1
Author: Beniamino Galvani <bgalvani redhat com>
Date:   Mon Jan 18 16:36:33 2016 +0100

    import: handle line splitting and utf8 conversion inside do_import()
    
    The text parsing logic belongs to do_import(). No functional change.
    
    (cherry picked from commit d936de40f33a5814330b61fbd7621d727759ae31)

 properties/import-export.c |   35 +++++++++++++++++++++++++++++++++--
 properties/import-export.h |    2 +-
 properties/nm-openvpn.c    |   30 +-----------------------------
 3 files changed, 35 insertions(+), 32 deletions(-)
---
diff --git a/properties/import-export.c b/properties/import-export.c
index 37c1684..e63cb81 100644
--- a/properties/import-export.c
+++ b/properties/import-export.c
@@ -390,19 +390,20 @@ parse_ip (const char *str, const char *line, guint32 *out_ip)
 }
 
 NMConnection *
-do_import (const char *path, char **lines, GError **error)
+do_import (const char *path, const char *contents, GError **error)
 {
        NMConnection *connection = NULL;
        NMSettingConnection *s_con;
        NMSettingIP4Config *s_ip4;
        NMSettingVPN *s_vpn;
        char *last_dot;
-       char **line;
+       char **line, **lines = NULL;
        gboolean have_client = FALSE, have_remote = FALSE;
        gboolean have_pass = FALSE, have_sk = FALSE;
        const char *ctype = NULL;
        char *basename;
        char *default_path, *tmp, *tmp2;
+       char *new_contents = NULL;
        gboolean http_proxy = FALSE, socks_proxy = FALSE, proxy_set = FALSE;
        int nitems;
 
@@ -435,6 +436,31 @@ do_import (const char *path, char **lines, GError **error)
                *last_dot = '\0';
        g_object_set (s_con, NM_SETTING_CONNECTION_ID, basename, NULL);
 
+       if (!g_utf8_validate (contents, -1, NULL)) {
+               GError *conv_error = NULL;
+
+               new_contents = g_locale_to_utf8 (contents, -1, NULL, NULL, &conv_error);
+               if (conv_error) {
+                       /* ignore the error, we tried at least. */
+                       g_error_free (conv_error);
+                       g_free (new_contents);
+               } else {
+                       g_assert (new_contents);
+                       contents = new_contents;  /* update contents with the UTF-8 safe text */
+               }
+       }
+
+       lines = g_strsplit_set (contents, "\r\n", 0);
+       if (g_strv_length (lines) <= 1) {
+               g_set_error_literal (error,
+                                    OPENVPN_PLUGIN_UI_ERROR,
+                                    OPENVPN_PLUGIN_UI_ERROR_FILE_NOT_READABLE,
+                                    _("not a valid OpenVPN configuration file"));
+               g_object_unref (connection);
+               connection = NULL;
+               goto out;
+       }
+
        for (line = lines; *line; line++) {
                char *comment, **items = NULL, *leftover = NULL;
 
@@ -930,6 +956,7 @@ route_fail:
                }
        }
 
+out:
        g_free (default_path);
        g_free (basename);
 
@@ -938,6 +965,10 @@ route_fail:
        else if (s_vpn)
                g_object_unref (s_vpn);
 
+       g_free (new_contents);
+       if (lines)
+               g_strfreev (lines);
+
        return connection;
 }
 
diff --git a/properties/import-export.h b/properties/import-export.h
index 1c5fbc9..9887a4c 100644
--- a/properties/import-export.h
+++ b/properties/import-export.h
@@ -25,7 +25,7 @@
 #include <glib.h>
 #include <nm-connection.h>
 
-NMConnection *do_import (const char *path, char **lines, GError **error);
+NMConnection *do_import (const char *path, const char *contents, GError **error);
 
 gboolean do_export (const char *path, NMConnection *connection, GError **error);
 
diff --git a/properties/nm-openvpn.c b/properties/nm-openvpn.c
index b49fece..e7cb89a 100644
--- a/properties/nm-openvpn.c
+++ b/properties/nm-openvpn.c
@@ -655,7 +655,6 @@ import (NMVpnPluginUiInterface *iface, const char *path, GError **error)
 {
        NMConnection *connection = NULL;
        char *contents = NULL;
-       char **lines = NULL;
        char *ext;
 
        ext = strrchr (path, '.');
@@ -681,36 +680,9 @@ import (NMVpnPluginUiInterface *iface, const char *path, GError **error)
        if (!g_file_get_contents (path, &contents, NULL, error))
                return NULL;
 
-       if (!g_utf8_validate (contents, -1, NULL)) {
-               char *tmp;
-               GError *conv_error = NULL;
-
-               tmp = g_locale_to_utf8 (contents, -1, NULL, NULL, &conv_error);
-               if (conv_error) {
-                       /* ignore the error, we tried at least. */
-                       g_error_free (conv_error);
-                       g_free (tmp);
-               } else {
-                       g_assert (tmp);
-                       g_free (contents);
-                       contents = tmp;  /* update contents with the UTF-8 safe text */
-               }
-       }
-
-       lines = g_strsplit_set (contents, "\r\n", 0);
-       if (g_strv_length (lines) <= 1) {
-               g_set_error (error,
-                            OPENVPN_PLUGIN_UI_ERROR,
-                            OPENVPN_PLUGIN_UI_ERROR_FILE_NOT_READABLE,
-                            "not a valid OpenVPN configuration file");
-               goto out;
-       }
-
-       connection = do_import (path, lines, error);
+       connection = do_import (path, contents, error);
 
 out:
-       if (lines)
-               g_strfreev (lines);
        g_free (contents);
        return connection;
 }


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