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