First phase of multifilesystem enhancement
[captive.git] / src / libcaptive / client / lib.c
1 /* $Id$
2  * captive vfs utility functions for interface to reactos
3  * Copyright (C) 2002-2003 Jan Kratochvil <project-captive@jankratochvil.net>
4  * 
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
8  * 
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.
13  * 
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
17  */
18
19
20 #include "config.h"
21
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
31
32 /* function will leave g_malloc()ed 'ObjectAttributes->ObjectName'!
33  */
34 GnomeVFSResult captive_ObjectAttributes_init(const gchar *pathname,OBJECT_ATTRIBUTES *ObjectAttributes)
35 {
36 gchar *w32_path,*s,*d;
37 const gchar *media_root;
38
39         g_return_val_if_fail(pathname!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
40         g_return_val_if_fail(ObjectAttributes!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
41
42         /* Do not open "\Cdfs"(anything) as it is just the filesystem implementation.
43          * ntoskrnl/io/fs.c/IoMountVolume() will map
44          *  FILE_DEVICE_CD_ROM -> FILE_DEVICE_CD_ROM_FILE_SYSTEM
45          * for us automatically when opening the device itself.
46          * If you put some trailing content (such as "\\.") there
47          *  IoCreateFile()->ObCreateObject()->ObFindObject()
48          * will not leave 'ObCreateObject::RemainingPath' as NULL
49          * and later IopCreateFile() would not consider it FO_DIRECT_DEVICE_OPEN (e.g. w/o any direct mount!).
50          * On the other side it will somehow get managed automatically and it works even
51          * without the trailing "\\." for root directory open - don't ask me why. :-)
52          */
53         switch (captive_options->media) {
54                 case CAPTIVE_OPTION_MEDIA_CDROM: media_root="\\Device\\CdRom0";           break;        /* libcaptive/storage/cdrom.c */
55                 case CAPTIVE_OPTION_MEDIA_DISK:  media_root="\\Device\\CaptiveHarddisk0"; break;        /* libcaptive/storage/disk.c */
56                 default:
57                         g_assert_not_reached();
58                         media_root="";
59                 }
60
61         w32_path=(gchar *)/* de-const it as we can modify it but we must not free() it */
62                         captive_printf_alloca("%s\\%s",media_root,pathname);
63         /* translate '/' -> '\' */
64         for (s=w32_path;(s=strchr(s,'/'));s++)
65                 *s='\\';
66         /* collapse multiple sequences of '\' as it is a no-no for W32 */
67         for (s=d=w32_path;*s;s++)
68                 if (d==w32_path || !(*s=='\\' && d[-1]=='\\'))
69                         *d++=*s;
70         *d=0;
71         InitializeObjectAttributes(
72                         ObjectAttributes,       /* InitializedAttributes */
73                         captive_utf8_to_UnicodeString_malloc(w32_path), /* ObjectName */
74                         0,      /* Attributes; I hope no OBJ_KERNEL_HANDLE as we are 'system process' */
75                         NULL,   /* RootDirectory */
76                         NULL);  /* SecurityDescriptor; ignored */
77
78         return GNOME_VFS_OK;
79 }