Initial original import from: fuse-2.4.2-2.fc4
[captive.git] / src / libcaptive / client / file-parent.c
index 20b46e8..038bdc5 100644 (file)
@@ -25,6 +25,8 @@
 #include "vfs-parent.h"
 #include "reactos/ntos/types.h"        /* for ULONG */
 #include "parent-connector.h"
+#include "captive/client.h"    /* for captive_path_normalize() */
+#include <ctype.h>
 
 
 static gpointer captive_file_parent_object_parent_class=NULL;
@@ -180,6 +182,8 @@ GnomeVFSResult captive_file_parent_new_open(CaptiveFileObject **captive_file_obj
 {
 CaptiveFileParentObject *captive_file_parent_object;
 GnomeVFSResult r;
+gint retried=0;
+gint retried_commit=0;
 
        g_return_val_if_fail(captive_file_object_return!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
        g_return_val_if_fail(CAPTIVE_VFS_PARENT_IS_OBJECT(captive_vfs_object),GNOME_VFS_ERROR_BAD_PARAMETERS);
@@ -193,18 +197,30 @@ GnomeVFSResult r;
 
        captive_file_parent_init(captive_file_parent_object,captive_vfs_object);
 
-       if (GNOME_VFS_OK!=(r=captive_parent_connector_connect(CAPTIVE_PARENT_CONNECTOR(captive_file_parent_object))))
-               return r;
-
-       if (GNOME_VFS_OK!=(r=captive_sandbox_parent_file_new_open(captive_file_parent_object))) {
-               g_object_unref(captive_file_parent_object);
-               *captive_file_object_return=NULL;
-               return r;
-               }
-
-       *captive_file_object_return=CAPTIVE_FILE_OBJECT(captive_file_parent_object);
-       return (*captive_file_parent_object_captive_parent_connector_open_orig)
-                       (CAPTIVE_PARENT_CONNECTOR(captive_file_parent_object));
+       do {
+               if (GNOME_VFS_OK!=(r=captive_parent_connector_connect(CAPTIVE_PARENT_CONNECTOR(captive_file_parent_object))))
+                       return r;
+               if (GNOME_VFS_ERROR_SERVICE_NOT_AVAILABLE
+                               !=(r=captive_sandbox_parent_file_new_open(captive_file_parent_object))) {
+                       if (GNOME_VFS_ERROR_SERVICE_OBSOLETE==r) {
+                               if (!retried_commit++) {
+                                       if (GNOME_VFS_OK!=(r=captive_vfs_commit(captive_vfs_object)))
+                                               return r;
+                                       retried=0;
+                                       continue;
+                                       }
+                               }
+                       if (GNOME_VFS_OK!=r) {
+                               g_object_unref(captive_file_parent_object);
+                               *captive_file_object_return=NULL;
+                               return r;
+                               }
+                       *captive_file_object_return=CAPTIVE_FILE_OBJECT(captive_file_parent_object);
+                       return (*captive_file_parent_object_captive_parent_connector_open_orig)
+                                       (CAPTIVE_PARENT_CONNECTOR(captive_file_parent_object));
+                       }
+               } while (!retried++);
+       return r;
 }
 
 
@@ -213,6 +229,8 @@ GnomeVFSResult captive_file_parent_new_create(CaptiveFileObject **captive_file_o
 {
 CaptiveFileParentObject *captive_file_parent_object;
 GnomeVFSResult r;
+gint retried=0;
+gint retried_commit=0;
 
        g_return_val_if_fail(captive_file_object_return!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
        g_return_val_if_fail(CAPTIVE_VFS_PARENT_IS_OBJECT(captive_vfs_object),GNOME_VFS_ERROR_BAD_PARAMETERS);
@@ -229,16 +247,31 @@ GnomeVFSResult r;
        if (GNOME_VFS_OK!=(r=captive_parent_connector_connect(CAPTIVE_PARENT_CONNECTOR(captive_file_parent_object))))
                return r;
 
-       if (GNOME_VFS_OK!=(r=captive_sandbox_parent_file_new_create(captive_file_parent_object,exclusive,perm))) {
-               g_object_unref(captive_file_parent_object);
-               *captive_file_object_return=NULL;
-               return r;
-               }
-       captive_parent_connector_set_dirty(CAPTIVE_PARENT_CONNECTOR(captive_file_parent_object));
-
-       *captive_file_object_return=CAPTIVE_FILE_OBJECT(captive_file_parent_object);
-       return (*captive_file_parent_object_captive_parent_connector_open_orig)
-                       (CAPTIVE_PARENT_CONNECTOR(captive_file_parent_object));
+       do {
+               if (GNOME_VFS_OK!=(r=captive_parent_connector_connect(CAPTIVE_PARENT_CONNECTOR(captive_file_parent_object))))
+                       return r;
+               if (GNOME_VFS_ERROR_SERVICE_NOT_AVAILABLE
+                               !=(r=captive_sandbox_parent_file_new_create(captive_file_parent_object,exclusive,perm))) {
+                       if (GNOME_VFS_ERROR_SERVICE_OBSOLETE==r) {
+                               if (!retried_commit++) {
+                                       if (GNOME_VFS_OK!=(r=captive_vfs_commit(captive_vfs_object)))
+                                               return r;
+                                       retried=0;
+                                       continue;
+                                       }
+                               }
+                       if (GNOME_VFS_OK!=r) {
+                               g_object_unref(captive_file_parent_object);
+                               *captive_file_object_return=NULL;
+                               return r;
+                               }
+                       captive_parent_connector_set_dirty(CAPTIVE_PARENT_CONNECTOR(captive_file_parent_object));
+                       *captive_file_object_return=CAPTIVE_FILE_OBJECT(captive_file_parent_object);
+                       return (*captive_file_parent_object_captive_parent_connector_open_orig)
+                                       (CAPTIVE_PARENT_CONNECTOR(captive_file_parent_object));
+                       }
+               } while (!retried++);
+       return r;
 }
 
 
@@ -355,7 +388,7 @@ gint retried_commit=0;
                if (GNOME_VFS_ERROR_SERVICE_NOT_AVAILABLE
                                !=(r=captive_sandbox_parent_file_write(captive_file_parent_object,buffer,num_bytes,bytes_written_return))) {
                        /* Occured: ExRaiseStatus(STATUS_LOG_FILE_FULL); */
-                       if (GNOME_VFS_ERROR_LAUNCH==r) {
+                       if (GNOME_VFS_ERROR_SERVICE_OBSOLETE==r) {
                                if (!retried_commit++) {
                                        if (GNOME_VFS_OK!=(r=captive_vfs_commit(captive_file_object->vfs)))
                                                return r;
@@ -363,7 +396,7 @@ gint retried_commit=0;
                                        continue;
                                        }
                                }
-                       g_assert(*bytes_written_return==num_bytes);     /* Not GNOME_VFS_ERROR_LAUNCH. */
+                       g_assert(*bytes_written_return==num_bytes);     /* Not GNOME_VFS_ERROR_SERVICE_OBSOLETE. */
                        if (GNOME_VFS_OK==r)
                                captive_parent_connector_set_dirty(CAPTIVE_PARENT_CONNECTOR(captive_file_parent_object));
                        return r;
@@ -520,19 +553,48 @@ static GnomeVFSResult captive_file_parent_move
 CaptiveFileParentObject *captive_file_parent_object_old;
 GnomeVFSResult r;
 gint retried=0;
+gchar *chksub_pathname_old_cased,*chksub_pathname_new_cased;   /* case-sensitive version */
+gchar *chksub_pathname_old,*chksub_pathname_new,*chksub_s_old,*chksub_s_new;
 
        g_return_val_if_fail(CAPTIVE_FILE_PARENT_IS_OBJECT(captive_file_object_old),GNOME_VFS_ERROR_BAD_PARAMETERS);
        g_return_val_if_fail(pathname_new!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
 
        captive_file_parent_object_old=CAPTIVE_FILE_PARENT_OBJECT(captive_file_object_old);
 
+       /* Prevent "mv dir dir/subdir" as it is not catched by ntfs.sys of NT-5.1sp1. */
+       /* FIXME: Move to 'CaptiveFileSlaveObject' but it has no '->pathname' stored now! */
+       /* FIXME: UTF8 may not be compared correctly - we should use g_utf8_collate() */
+       chksub_pathname_old_cased=captive_path_normalize(captive_file_parent_object_old->pathname);
+       chksub_pathname_new_cased=captive_path_normalize(pathname_new);
+       chksub_pathname_old=g_utf8_casefold(chksub_pathname_old_cased,
+                       -1);    /* len; '\0'-terminated */
+       chksub_pathname_new=g_utf8_casefold(chksub_pathname_new_cased,
+                       -1);    /* len; '\0'-terminated */
+       g_free(chksub_pathname_old_cased);
+       g_free(chksub_pathname_new_cased);
+       for (
+                       chksub_s_old=chksub_pathname_old,chksub_s_new=chksub_pathname_new;
+                       *chksub_s_old && *chksub_s_new && tolower(*chksub_s_old)==tolower(*chksub_s_new);
+                       chksub_s_old++,chksub_s_new++);
+       g_assert(chksub_s_old>chksub_pathname_old);
+       g_assert(chksub_s_new>chksub_pathname_new);
+       if (!*chksub_s_old && (!*chksub_s_new || *chksub_s_new=='/'))
+               return GNOME_VFS_ERROR_DIRECTORY_BUSY;
+       if (!*chksub_s_new && (!*chksub_s_old || *chksub_s_old=='/'))
+               return GNOME_VFS_ERROR_DIRECTORY_BUSY;
+       g_free(chksub_pathname_old);
+       g_free(chksub_pathname_new);
+
        do {
                if (GNOME_VFS_OK!=(r=captive_parent_connector_open(CAPTIVE_PARENT_CONNECTOR(captive_file_parent_object_old))))
                        return r;
                if (GNOME_VFS_ERROR_SERVICE_NOT_AVAILABLE
                                !=(r=captive_sandbox_parent_file_move(captive_file_parent_object_old,pathname_new,force_replace))) {
-                       if (GNOME_VFS_OK==r)
+                       if (GNOME_VFS_OK==r) {
+                               g_free(captive_file_parent_object_old->pathname);
+                               captive_file_parent_object_old->pathname=g_strdup(pathname_new);
                                captive_parent_connector_set_dirty(CAPTIVE_PARENT_CONNECTOR(captive_file_parent_object_old));
+                               }
                        return r;
                        }
                } while (!retried++);