[Spice-devel] [phodav PATCH 6/7 v3] spice-webdavd-windows: Check for mapped shared folder

Lukas Venhoda lvenhoda at redhat.com
Thu Mar 17 13:47:50 UTC 2016


Check whether Spice Folder is already mapped.
If yes, don't try to map another one.
---
Changes since v2:
 - New patch
---
 spice/spice-webdavd.c | 58 ++++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 51 insertions(+), 7 deletions(-)

diff --git a/spice/spice-webdavd.c b/spice/spice-webdavd.c
index 49b1884..8f9b06b 100644
--- a/spice/spice-webdavd.c
+++ b/spice/spice-webdavd.c
@@ -767,6 +767,42 @@ get_free_drive_letter(void)
   return 0;
 }

+static gboolean
+is_spice_folder_mapped(void)
+{
+  const guint32 max_mask = 1 << 25;
+  gchar local_name[3];
+  gchar remote_name[32];
+  guint32 size = sizeof(remote_name);
+  guint32 drives;
+  guint32 retval;
+  guint32 mask;
+  gint i;
+
+  local_name[0] = 'z';
+  local_name[1] = ':';
+  local_name[2] = 0;
+
+  drives = GetLogicalDrives ();
+
+  for (i = 0; i < 26; i++)
+  {
+    mask = max_mask >> i;
+    if (drives & mask)
+    {
+      local_name[0] = 'z' - i;
+      retval = WNetGetConnection (local_name, remote_name, (LPDWORD)&size);
+      if (retval == NO_ERROR)
+      {
+        if (strncmp (remote_name, "\\\\localhost at 9843\\DavWWWRoot", 28) == 0)
+          return TRUE;
+      }
+    }
+  }
+
+  return FALSE;
+}
+
 static map_drive_enum
 map_drive(void)
 {
@@ -842,11 +878,16 @@ run_service (void)
   g_debug ("Run service");

 #ifdef G_OS_WIN32
-  GCancellable * cancel_map = g_cancellable_new ();
-  GTask * map_drive_task = g_task_new (NULL, NULL, NULL, NULL);
-  g_task_set_task_data (map_drive_task, cancel_map, NULL);
-  g_task_run_in_thread (map_drive_task, map_drive_cb);
-  g_object_unref (map_drive_task);
+  gboolean spice_folder_mapped = is_spice_folder_mapped();
+  GCancellable * cancel_map;
+  if (!spice_folder_mapped)
+  {
+    cancel_map = g_cancellable_new ();
+    GTask * map_drive_task = g_task_new (NULL, NULL, NULL, NULL);
+    g_task_set_task_data (map_drive_task, cancel_map, NULL);
+    g_task_run_in_thread (map_drive_task, map_drive_cb);
+    g_object_unref (map_drive_task);
+  }
 #endif

   g_socket_service_start (socket_service);
@@ -883,8 +924,11 @@ run_service (void)
   g_main_loop_unref (loop);

 #ifdef G_OS_WIN32
-  g_cancellable_cancel (cancel_map);
-  g_object_unref (cancel_map);
+  if (!spice_folder_mapped)
+  {
+    g_cancellable_cancel (cancel_map);
+    g_object_unref (cancel_map);
+  }
 #endif

   g_cancellable_cancel (cancel);
--
2.5.0



More information about the Spice-devel mailing list