83aab0b010ce7ce969a5f8dd6c104b013adedfae
[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 #include <glib/gstrfuncs.h>
31
32
33 /* Returns: g_malloc() allocated absolute pathname string */
34 gchar *captive_path_normalize(const gchar *raw_pathname)
35 {
36 gchar *r,*r_end,*s,*d;
37
38         g_return_val_if_fail(raw_pathname!=NULL,NULL);
39         g_return_val_if_fail(g_path_is_absolute(raw_pathname),NULL);
40
41         r=g_strdup(raw_pathname);
42
43         /* coalesce '/'es */
44         for (d=s=r;*s;s++) {
45                 if (*s==G_DIR_SEPARATOR && d>r && d[-1]==G_DIR_SEPARATOR)
46                         continue;
47                 *d++=*s;
48                 }
49         g_assert(d>r);
50         if (d>(r+1) && d[-1]==G_DIR_SEPARATOR)
51                 d--;
52         *d++=G_DIR_SEPARATOR;
53         r_end=d;
54
55         /* 'r' is NOT NULL-terminated here! */
56
57         for (d=s=r+1;s<r_end;) {
58                 if (!strncmp(s-1,"/./",3)) {
59                         s+=2;
60                         continue;
61                         }
62                 if (!strncmp(s-1,"/../",4)) {
63                         s+=3;
64                         g_assert(d[-1]==G_DIR_SEPARATOR);
65                         if (d>r+1) {
66                                 do {
67                                         d--;
68                                         } while (d[-1]!=G_DIR_SEPARATOR);
69                                 }
70                         continue;
71                         }
72                 *d++=*s++;
73                 }
74         g_assert(d[-1]==G_DIR_SEPARATOR);       /* trailing '/' */
75         if (d>r+1)      /* leave at least "/" */
76                 d--;
77         *d='\0';
78
79         g_assert(g_path_is_absolute(r));
80
81         return r;
82 }
83
84 /* function will leave g_malloc()ed 'ObjectAttributes->ObjectName'!
85  */
86 GnomeVFSResult captive_ObjectAttributes_init(const gchar *pathname,OBJECT_ATTRIBUTES *ObjectAttributes)
87 {
88 gchar *w32_path,*s,*d;
89 const gchar *media_root;
90 gchar *pathname_normalized;
91
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);
94
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
107          */
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 */
111                 default:
112                         g_assert_not_reached();
113                         media_root="";
114                 }
115
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++)
122                 *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]=='\\'))
126                         *d++=*s;
127         *d=0;
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 */
134
135         return GNOME_VFS_OK;
136 }