[phodav: 11/18] wip: move delete method



commit 132c33cb334aae35579e93cae3b76597de8bb375
Author: Marc-André Lureau <marcandre lureau gmail com>
Date:   Thu Apr 10 19:01:42 2014 +0200

    wip: move delete method

 Makefile.am                      |    3 +-
 libphodav/phodav-method-delete.c |  114 ++++++++++++++++++++++++++++++++++++++
 libphodav/phodav-priv.h          |    6 ++
 libphodav/phodav-server.c        |   98 +--------------------------------
 4 files changed, 124 insertions(+), 97 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index 07fd6ad..4e63829 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -34,8 +34,9 @@ libphodav_1_0_la_SOURCES =                    \
        libphodav/phodav-lock.c                 \
        libphodav/phodav-lock.h                 \
        libphodav/phodav-if.c                   \
+       libphodav/phodav-method-delete.c        \
        libphodav/phodav-method-get.c           \
-       libphodav/phodav-method-mkcol.c \
+       libphodav/phodav-method-mkcol.c         \
        libphodav/phodav-method-propfind.c      \
        libphodav/phodav-method-proppatch.c     \
        libphodav/phodav-multistatus.c          \
diff --git a/libphodav/phodav-method-delete.c b/libphodav/phodav-method-delete.c
new file mode 100644
index 0000000..21069b7
--- /dev/null
+++ b/libphodav/phodav-method-delete.c
@@ -0,0 +1,114 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
+/*
+ * Copyright (C) 2013 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "phodav-priv.h"
+#include "phodav-multistatus.h"
+
+static gint
+error_to_status (GError *err)
+{
+  if (g_error_matches (err, G_FILE_ERROR, G_FILE_ERROR_NOENT))
+    return SOUP_STATUS_NOT_FOUND;
+  if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
+    return SOUP_STATUS_NOT_FOUND;
+
+  return SOUP_STATUS_FORBIDDEN;
+}
+
+gint
+phodav_delete_file (const gchar *path, GFile *file,
+                    GHashTable *mstatus,
+                    GCancellable *cancellable)
+{
+  GError *error = NULL;
+  GFileEnumerator *e;
+  gint status = SOUP_STATUS_NO_CONTENT;
+
+  e = g_file_enumerate_children (file, "standard::*", G_FILE_QUERY_INFO_NONE,
+                                 cancellable, NULL);
+  if (e)
+    {
+      while (1)
+        {
+          GFileInfo *info = g_file_enumerator_next_file (e, cancellable, &error);
+          if (!info)
+            break;
+          GFile *del = g_file_get_child (file, g_file_info_get_name (info));
+          gchar *escape = g_markup_escape_text (g_file_info_get_name (info), -1);
+          gchar *del_path = g_build_path ("/", path, escape, NULL);
+          phodav_delete_file (del_path, del, mstatus, cancellable);
+          g_object_unref (del);
+          g_object_unref (info);
+          g_free (escape);
+          g_free (del_path);
+        }
+
+      g_file_enumerator_close (e, cancellable, NULL);
+      g_clear_object (&e);
+    }
+
+  if (!g_file_delete (file, cancellable, &error) && mstatus)
+    {
+      status = error_to_status (error);
+
+      g_hash_table_insert (mstatus, g_strdup (path),
+                           response_new (NULL, status));
+    }
+
+  if (error)
+    {
+      g_debug ("ignored del error: %s", error->message);
+      g_clear_error (&error);
+    }
+
+  return status;
+}
+
+gint
+phodav_method_delete (PathHandler *handler, SoupMessage *msg,
+                      const char *path, GError **err)
+{
+  GCancellable *cancellable = handler_get_cancellable (handler);
+  GFile *file = NULL;
+  GHashTable *mstatus = NULL;
+  gint status;
+  GList *submitted = NULL;
+
+  /* depth = depth_from_string(soup_message_headers_get_one (msg->request_headers, "Depth")); */
+  /* must be == infinity with collection */
+
+  status = phodav_check_if (handler, msg, path, &submitted);
+  if (status != SOUP_STATUS_OK)
+    goto end;
+
+  file = g_file_get_child (handler_get_file (handler), path + 1);
+  mstatus = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
+                                   (GDestroyNotify) response_free);
+
+  status = phodav_delete_file (path, file, mstatus, cancellable);
+  if (status == SOUP_STATUS_NO_CONTENT)
+    if (g_hash_table_size (mstatus) > 0)
+      status = set_response_multistatus (msg, mstatus);
+
+end:
+  if (mstatus)
+    g_hash_table_unref (mstatus);
+  g_clear_object (&file);
+
+  return status;
+}
diff --git a/libphodav/phodav-priv.h b/libphodav/phodav-priv.h
index 11e3a40..d8c4e2f 100644
--- a/libphodav/phodav-priv.h
+++ b/libphodav/phodav-priv.h
@@ -99,6 +99,10 @@ gboolean                server_path_has_other_locks          (PhodavServer *self
 gint                    phodav_check_if                      (PathHandler *handler, SoupMessage *msg,
                                                               const gchar *path, GList **locks);
 
+gint                    phodav_delete_file                   (const gchar *path, GFile *file,
+                                                              GHashTable *mstatus,
+                                                              GCancellable *cancellable);
+
 gint                    phodav_method_get                    (PathHandler *handler, SoupMessage *msg,
                                                               const char *path, GError **err);
 gint                    phodav_method_propfind               (PathHandler *handler, SoupMessage *msg,
@@ -107,6 +111,8 @@ gint                    phodav_method_proppatch              (PathHandler *handl
                                                               const char *path, GError **err);
 gint                    phodav_method_mkcol                  (PathHandler *handler, SoupMessage *msg,
                                                               const char *path, GError **err);
+gint                    phodav_method_delete                 (PathHandler *handler, SoupMessage *msg,
+                                                              const char *path, GError **err);
 
 
 G_END_DECLS
diff --git a/libphodav/phodav-server.c b/libphodav/phodav-server.c
index 1fc19ed..13cb969 100644
--- a/libphodav/phodav-server.c
+++ b/libphodav/phodav-server.c
@@ -461,100 +461,6 @@ server_path_has_other_locks (PhodavServer *self, const gchar *path, GList *locks
   return !server_foreach_parent_path (self, path, other_lock_exists, locks);
 }
 
-static gint
-error_to_status (GError *err)
-{
-  if (g_error_matches (err, G_FILE_ERROR, G_FILE_ERROR_NOENT))
-    return SOUP_STATUS_NOT_FOUND;
-  if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
-    return SOUP_STATUS_NOT_FOUND;
-
-  return SOUP_STATUS_FORBIDDEN;
-}
-
-static gint
-do_delete_file (const gchar *path, GFile *file,
-                GHashTable *mstatus,
-                GCancellable *cancellable)
-{
-  GError *error = NULL;
-  GFileEnumerator *e;
-  gint status = SOUP_STATUS_NO_CONTENT;
-
-  e = g_file_enumerate_children (file, "standard::*", G_FILE_QUERY_INFO_NONE,
-                                 cancellable, NULL);
-  if (e)
-    {
-      while (1)
-        {
-          GFileInfo *info = g_file_enumerator_next_file (e, cancellable, &error);
-          if (!info)
-            break;
-          GFile *del = g_file_get_child (file, g_file_info_get_name (info));
-          gchar *escape = g_markup_escape_text (g_file_info_get_name (info), -1);
-          gchar *del_path = g_build_path ("/", path, escape, NULL);
-          do_delete_file (del_path, del, mstatus, cancellable);
-          g_object_unref (del);
-          g_object_unref (info);
-          g_free (escape);
-          g_free (del_path);
-        }
-
-      g_file_enumerator_close (e, cancellable, NULL);
-      g_clear_object (&e);
-    }
-
-  if (!g_file_delete (file, cancellable, &error) && mstatus)
-    {
-      status = error_to_status (error);
-
-      g_hash_table_insert (mstatus, g_strdup (path),
-                           response_new (NULL, status));
-    }
-
-  if (error)
-    {
-      g_debug ("ignored del error: %s", error->message);
-      g_clear_error (&error);
-    }
-
-  return status;
-}
-
-static gint
-method_delete (PathHandler *handler, SoupMessage *msg,
-               const char *path, GError **err)
-{
-  PhodavServer *self = handler->self;
-  GFile *file = NULL;
-  GHashTable *mstatus = NULL;
-  gint status;
-  GList *submitted = NULL;
-
-  /* depth = depth_from_string(soup_message_headers_get_one (msg->request_headers, "Depth")); */
-  /* must be == infinity with collection */
-
-  status = phodav_check_if (handler, msg, path, &submitted);
-  if (status != SOUP_STATUS_OK)
-    goto end;
-
-  file = g_file_get_child (handler->file, path + 1);
-  mstatus = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
-                                   (GDestroyNotify) response_free);
-
-  status = do_delete_file (path, file, mstatus, self->cancellable);
-  if (status == SOUP_STATUS_NO_CONTENT)
-    if (g_hash_table_size (mstatus) > 0)
-      status = set_response_multistatus (msg, mstatus);
-
-end:
-  if (mstatus)
-    g_hash_table_unref (mstatus);
-  g_clear_object (&file);
-
-  return status;
-}
-
 static gboolean
 do_copy_r (GFile *src, GFile *dest, GFileCopyFlags flags,
            GCancellable *cancellable, GError **err)
@@ -638,7 +544,7 @@ again:
         if (overwrite && !retry &&
             (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY) ||
              g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE)) &&
-            do_delete_file (dest_path, dest, NULL, cancellable) == SOUP_STATUS_NO_CONTENT)
+            phodav_delete_file (dest_path, dest, NULL, cancellable) == SOUP_STATUS_NO_CONTENT)
           {
             g_clear_error (&error);
             retry = TRUE;
@@ -1102,7 +1008,7 @@ server_callback (SoupServer *server, SoupMessage *msg,
   else if (msg->method == SOUP_METHOD_MKCOL)
     status = phodav_method_mkcol (handler, msg, path, &err);
   else if (msg->method == SOUP_METHOD_DELETE)
-    status = method_delete (handler, msg, path, &err);
+    status = phodav_method_delete (handler, msg, path, &err);
   else if (msg->method == SOUP_METHOD_MOVE ||
            msg->method == SOUP_METHOD_COPY)
     status = method_movecopy (handler, msg, path, &err);


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