2 * captive vfs utility functions for interface to reactos
3 * Copyright (C) 2002-2003 Jan Kratochvil <project-captive@jankratochvil.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; exactly version 2 of June 1991 is required
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "lib.h" /* self */
23 #include <libgnomevfs/gnome-vfs-result.h>
24 #include <glib/gmessages.h>
25 #include "captive/macros.h"
26 #include "reactos/ntos/rtl.h" /* for InitializeObjectAttributes() */
27 #include "captive/unicode.h"
28 #include <libgnomevfs/gnome-vfs-utils.h> /* for gnome_vfs_unescape_string() */
29 #include "captive/options.h"
30 #include <glib/gstrfuncs.h>
33 /* Returns: g_malloc() allocated absolute pathname string */
34 gchar *captive_path_normalize(const gchar *raw_pathname)
36 gchar *r,*r_end,*s,*d;
38 g_return_val_if_fail(raw_pathname!=NULL,NULL);
39 g_return_val_if_fail(g_path_is_absolute(raw_pathname),NULL);
41 r=g_strdup(raw_pathname);
45 if (*s==G_DIR_SEPARATOR && d>r && d[-1]==G_DIR_SEPARATOR)
50 if (d>(r+1) && d[-1]==G_DIR_SEPARATOR)
55 /* 'r' is NOT NULL-terminated here! */
57 for (d=s=r+1;s<r_end;) {
58 if (!strncmp(s-1,"/./",3)) {
62 if (!strncmp(s-1,"/../",4)) {
64 g_assert(d[-1]==G_DIR_SEPARATOR);
68 } while (d[-1]!=G_DIR_SEPARATOR);
74 g_assert(d[-1]==G_DIR_SEPARATOR); /* trailing '/' */
75 if (d>r+1) /* leave at least "/" */
79 g_assert(g_path_is_absolute(r));
84 /* function will leave g_malloc()ed 'ObjectAttributes->ObjectName'!
86 GnomeVFSResult captive_ObjectAttributes_init(const gchar *pathname,OBJECT_ATTRIBUTES *ObjectAttributes)
88 gchar *w32_path,*s,*d;
89 const gchar *media_root;
90 gchar *pathname_normalized;
92 g_return_val_if_fail(pathname!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
93 g_return_val_if_fail(ObjectAttributes!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
95 /* Do not open "\Cdfs"(anything) as it is just the filesystem implementation.
96 * ntoskrnl/io/fs.c/IoMountVolume() will map
97 * FILE_DEVICE_CD_ROM -> FILE_DEVICE_CD_ROM_FILE_SYSTEM
98 * for us automatically when opening the device itself.
99 * If you put some trailing content (such as "\\.") there
100 * IoCreateFile()->ObCreateObject()->ObFindObject()
101 * will not leave 'ObCreateObject::RemainingPath' as NULL
102 * and later IopCreateFile() would not consider it FO_DIRECT_DEVICE_OPEN (e.g. w/o any direct mount!).
103 * On the other side it will somehow get managed automatically and it works even
104 * without the trailing "\\." for root directory open - don't ask me why. :-)
105 * Tested: \\Device\\CaptiveHarddisk0\\. FAIL
106 * \\Device\\CaptiveHarddisk0\\ OK
108 switch (captive_options->media) {
109 case CAPTIVE_OPTION_MEDIA_CDROM: media_root="\\Device\\CdRom0"; break; /* libcaptive/storage/cdrom.c */
110 case CAPTIVE_OPTION_MEDIA_DISK: media_root="\\Device\\CaptiveHarddisk0"; break; /* libcaptive/storage/disk.c */
112 g_assert_not_reached();
116 pathname_normalized=captive_path_normalize(pathname);
117 w32_path=(gchar *)/* de-const it as we can modify it but we must not free() it */
118 captive_printf_alloca("%s\\%s",media_root,pathname_normalized);
119 g_free(pathname_normalized);
120 /* translate '/' -> '\' */
121 for (s=w32_path;(s=strchr(s,'/'));s++)
123 /* collapse multiple sequences of '\' as it is a no-no for W32 */
124 for (s=d=w32_path;*s;s++)
125 if (d==w32_path || !(*s=='\\' && d[-1]=='\\'))
128 InitializeObjectAttributes(
129 ObjectAttributes, /* InitializedAttributes */
130 captive_utf8_to_UnicodeString_malloc(w32_path), /* ObjectName */
131 0, /* Attributes; I hope no OBJ_KERNEL_HANDLE as we are 'system process' */
132 NULL, /* RootDirectory */
133 NULL); /* SecurityDescriptor; ignored */