[PATCH 2/6] nm_utils: parse mount uris



---
 libnm-util/nm-utils.c             |   49 +++++
 libnm-util/nm-utils.h             |    2 +
 libnm-util/tests/test-general.c   |   55 ++++++


diff --git a/libnm-util/nm-utils.c b/libnm-util/nm-utils.c
index a880620..4c3ae7f 100644
--- a/libnm-util/nm-utils.c
+++ b/libnm-util/nm-utils.c
@@ -2568,3 +2568,52 @@ nm_utils_hwaddr_ntoa (gconstpointer addr, int type)

     return g_string_free (out, FALSE);
 }
+
+/**
+ * nm_utils_parse_mount_uri:
+ * @uri: the uri containing a network mount to parse (e.g. smb://host/folder) + * @scheme: a pointer receiving the char* with the parsed scheme (e.g. "smb") or NULL if it is not of interest + * @host: a pointer receiving the char* with the parsed host (e.g. "host") or NULL if it is not of interest + * @folder: a pointer receiving the char* with the parsed folder (e.g. "folder") or NULL if it is not of interest
+ *
+ * Parses a uri identifying a gvfs-mountable network share.
+ * Username/Password, no folders & deep folders are currently not supported.
+ *
+ * The received strings must be g_free'd after use.
+ *
+ * Return value: TRUE if the uri was parsed successfully, FALSE otherwise
+ */
+gboolean
+nm_utils_parse_mount_uri (const char *uri, char **scheme, char **host, char **folder)
+{
+    char *tmp_scheme, *tmp_host, *tmp_folder, *rest;
+
+    // Check if uri meets minimum requirements
+    if ((uri == NULL) || (strlen(uri) < 9)) return FALSE;
+
+    // Parse scheme
+    rest = g_strstr_len(uri, strlen(uri), "://");
+    if (rest == NULL) return FALSE;
+    tmp_scheme = (char *) g_malloc0 (rest - uri + 1);
+    strncpy(tmp_scheme, uri, rest - uri);
+
+    // Parse host
+    rest = rest + strlen("://");
+    tmp_folder = g_strstr_len(rest, strlen(rest), "/");
+    if (tmp_folder == NULL) return FALSE;
+    tmp_host = (char *) g_malloc0 (tmp_folder - rest + 1);
+    strncpy(tmp_host, rest, tmp_folder - rest);
+
+    // Rest is folder, just check if valid
+    rest = tmp_folder + strlen("/");
+    if (strchr(rest, '/') != NULL) return FALSE;
+    if (strchr(rest, '?') != NULL) return FALSE;
+    tmp_folder = g_strdup(rest);
+
+    // Return requested data, free the others
+    if (scheme != NULL) *scheme = tmp_scheme; else g_free(tmp_scheme);
+    if (host != NULL) *host = tmp_host; else g_free(tmp_host);
+    if (folder != NULL) *folder = tmp_folder; else g_free(tmp_folder);
+
+    return TRUE;
+}
diff --git a/libnm-util/nm-utils.h b/libnm-util/nm-utils.h
index 7bc536a..4a081a3 100644
--- a/libnm-util/nm-utils.h
+++ b/libnm-util/nm-utils.h
@@ -134,6 +134,8 @@ char *nm_utils_hwaddr_ntoa (gconstpointer addr, int type);
 GByteArray *nm_utils_hwaddr_atoba (const char *asc, int type);
guint8 *nm_utils_hwaddr_aton (const char *asc, int type, gpointer buffer);

+gboolean nm_utils_parse_mount_uri (const char *uri, char **scheme, char **host, char **folder);
+
 G_END_DECLS

 #endif /* NM_UTILS_H */
diff --git a/libnm-util/tests/test-general.c b/libnm-util/tests/test-general.c
index 1eed888..ac89f12 100644
--- a/libnm-util/tests/test-general.c
+++ b/libnm-util/tests/test-general.c
@@ -1361,6 +1361,59 @@ test_hwaddr_aton_malformed (void)
g_assert (nm_utils_hwaddr_aton ("0:1a:2B:3:a@%%", ARPHRD_ETHER, buf) == NULL);
 }

+static void
+test_parse_mount_uri (void)
+{
+    char *valid_uris[] = {
+            "smb://host/folder",
+            "sftp://host/";,
+            NULL
+    };
+    // corresponding array parts for valid_uris[]
+    char *valid_schemes[] = {
+            "smb",
+            "sftp"
+    };
+    char *valid_hosts[] = {
+            "host",
+            "host"
+    };
+    char *valid_folders[] = {
+            "folder",
+            ""
+    };
+
+    char *invalid_uris[] = {
+            "http://host/folder?param";,
+            "justastring",
+            "file://usr/local/bin",
+            "smb://host",
+            NULL
+    };
+
+    char *scheme, *host, *folder;
+    int i = 0;
+
+    while (valid_uris[i] != NULL)
+    {
+ g_assert(nm_utils_parse_mount_uri(valid_uris[i], &scheme, &host, &folder) == TRUE);
+        g_assert(strcmp(scheme, valid_schemes[i]) == 0);
+        g_assert(strcmp(host, valid_hosts[i]) == 0);
+        g_assert(strcmp(folder, valid_folders[i]) == 0);
+        i++;
+    }
+
+    i = 0; scheme = NULL; host = NULL; folder = NULL;
+    while (invalid_uris[i] != NULL)
+    {
+ g_assert(nm_utils_parse_mount_uri(invalid_uris[i], &scheme, &host, &folder) == FALSE);
+        g_assert(scheme == NULL);
+        g_assert(host == NULL);
+        g_assert(folder == NULL);
+        i++;
+    }
+}
+
 int main (int argc, char **argv)
 {
     GError *error = NULL;
@@ -1408,6 +1461,8 @@ int main (int argc, char **argv)
     test_hwaddr_aton_no_leading_zeros ();
     test_hwaddr_aton_malformed ();

+    test_parse_mount_uri ();
+
     base = g_path_get_basename (argv[0]);
     fprintf (stdout, "%s: SUCCESS\n", base);
     g_free (base);


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