[network-manager-openvpn: 3/10] properties: consider file size while importing from file



commit 6ee5cc616223eb808edb157da743d5df674feb5e
Author: Thomas Haller <thaller redhat com>
Date:   Fri Jan 22 14:41:24 2016 +0100

    properties: consider file size while importing from file
    
    Pass the number of read bytes to do_import(). As we allow for the
    file to be in a different encoding, we need to consider all
    bytes read, not only the chunk until the first '\0'.

 properties/import-export.c |    8 +++++---
 properties/import-export.h |    2 +-
 properties/nm-openvpn.c    |    5 +++--
 3 files changed, 9 insertions(+), 6 deletions(-)
---
diff --git a/properties/import-export.c b/properties/import-export.c
index f89d42b..b25e6e2 100644
--- a/properties/import-export.c
+++ b/properties/import-export.c
@@ -505,7 +505,7 @@ parse_ip (const char *str, const char *line, guint32 *out_ip)
 }
 
 NMConnection *
-do_import (const char *path, const char *contents, GError **error)
+do_import (const char *path, const char *contents, gsize contents_len, GError **error)
 {
        NMConnection *connection = NULL;
        NMSettingConnection *s_con;
@@ -552,10 +552,11 @@ do_import (const char *path, const char *contents, GError **error)
                *last_dot = '\0';
        g_object_set (s_con, NM_SETTING_CONNECTION_ID, basename, NULL);
 
-       if (!g_utf8_validate (contents, -1, NULL)) {
+       if (!g_utf8_validate (contents, contents_len, NULL)) {
                GError *conv_error = NULL;
+               gsize bytes_written;
 
-               new_contents = g_locale_to_utf8 (contents, -1, NULL, NULL, &conv_error);
+               new_contents = g_locale_to_utf8 (contents, contents_len, NULL, &bytes_written, &conv_error);
                if (conv_error) {
                        /* ignore the error, we tried at least. */
                        g_error_free (conv_error);
@@ -563,6 +564,7 @@ do_import (const char *path, const char *contents, GError **error)
                } else {
                        g_assert (new_contents);
                        contents = new_contents;  /* update contents with the UTF-8 safe text */
+                       contents_len = bytes_written + 1;
                }
        }
 
diff --git a/properties/import-export.h b/properties/import-export.h
index 0668353..9a1e1cc 100644
--- a/properties/import-export.h
+++ b/properties/import-export.h
@@ -30,7 +30,7 @@
 #include <NetworkManager.h>
 #endif
 
-NMConnection *do_import (const char *path, const char *contents, GError **error);
+NMConnection *do_import (const char *path, const char *contents, gsize contents_len, 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 68ce85f..18183d1 100644
--- a/properties/nm-openvpn.c
+++ b/properties/nm-openvpn.c
@@ -636,6 +636,7 @@ import (NMVpnEditorPlugin *iface, const char *path, GError **error)
        NMConnection *connection = NULL;
        char *contents = NULL;
        char *ext;
+       gsize contents_len;
 
        ext = strrchr (path, '.');
 
@@ -650,10 +651,10 @@ import (NMVpnEditorPlugin *iface, const char *path, GError **error)
                goto out;
        }
 
-       if (!g_file_get_contents (path, &contents, NULL, error))
+       if (!g_file_get_contents (path, &contents, &contents_len, error))
                return NULL;
 
-       connection = do_import (path, contents, error);
+       connection = do_import (path, contents, contents_len, error);
 
 out:
        g_free (contents);


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