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;
91 g_return_val_if_fail(pathname!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
92 g_return_val_if_fail(ObjectAttributes!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
94 /* Do not open "\Cdfs"(anything) as it is just the filesystem implementation.
95 * ntoskrnl/io/fs.c/IoMountVolume() will map
96 * FILE_DEVICE_CD_ROM -> FILE_DEVICE_CD_ROM_FILE_SYSTEM
97 * for us automatically when opening the device itself.
98 * If you put some trailing content (such as "\\.") there
99 * IoCreateFile()->ObCreateObject()->ObFindObject()
100 * will not leave 'ObCreateObject::RemainingPath' as NULL
101 * and later IopCreateFile() would not consider it FO_DIRECT_DEVICE_OPEN (e.g. w/o any direct mount!).
102 * On the other side it will somehow get managed automatically and it works even
103 * without the trailing "\\." for root directory open - don't ask me why. :-)
105 switch (captive_options->media) {
106 case CAPTIVE_OPTION_MEDIA_CDROM: media_root="\\Device\\CdRom0"; break; /* libcaptive/storage/cdrom.c */
107 case CAPTIVE_OPTION_MEDIA_DISK: media_root="\\Device\\CaptiveHarddisk0"; break; /* libcaptive/storage/disk.c */
109 g_assert_not_reached();
113 w32_path=(gchar *)/* de-const it as we can modify it but we must not free() it */
114 captive_printf_alloca("%s\\%s",media_root,pathname);
115 /* translate '/' -> '\' */
116 for (s=w32_path;(s=strchr(s,'/'));s++)
118 /* collapse multiple sequences of '\' as it is a no-no for W32 */
119 for (s=d=w32_path;*s;s++)
120 if (d==w32_path || !(*s=='\\' && d[-1]=='\\'))
123 InitializeObjectAttributes(
124 ObjectAttributes, /* InitializedAttributes */
125 captive_utf8_to_UnicodeString_malloc(w32_path), /* ObjectName */
126 0, /* Attributes; I hope no OBJ_KERNEL_HANDLE as we are 'system process' */
127 NULL, /* RootDirectory */
128 NULL); /* SecurityDescriptor; ignored */