Implemented '--syslog' and '--syslog-facility'.
[captive.git] / src / libcaptive / client / vfs.c
1 /* $Id$
2  * captive vfs 'vfs' 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 "captive/client-vfs.h" /* self */
23 #include "vfs.h"        /* self-priv */
24 #include <glib/gmessages.h>
25 #include "captive/macros.h"
26 #include "init.h"
27 #include "captive/parent-Vfs.h"
28 #include "../sandbox/server-Vfs.h"      /* for captive_corba_child_options */
29 #include "giochannel-blind.h"   /* for captive_giochannel_setup() */
30 #include "directory.h"
31 #include "reactos/ddk/iotypes.h"
32 #include "captive/client-directory.h"
33 #include "reactos/ntos/zw.h"    /* for NtQueryVolumeInformationFile() */
34 #include "result.h"
35 #include "init.h"
36
37
38 static gpointer captive_vfs_object_parent_class=NULL;
39
40
41 static GnomeVFSResult captive_vfs_close(CaptiveVfsObject *captive_vfs_object);
42
43 static void captive_vfs_object_finalize(CaptiveVfsObject *captive_vfs_object)
44 {
45         g_return_if_fail(captive_vfs_object!=NULL);
46
47         captive_vfs_close(captive_vfs_object);  /* errors ignored */
48
49         G_OBJECT_CLASS(captive_vfs_object_parent_class)->finalize((GObject *)captive_vfs_object);
50 }
51
52
53 static void captive_vfs_object_class_init(CaptiveVfsObjectClass *class)
54 {
55 GObjectClass *gobject_class=G_OBJECT_CLASS(class);
56
57         captive_vfs_object_parent_class=g_type_class_ref(G_TYPE_OBJECT);
58         gobject_class->finalize=(void (*)(GObject *object))captive_vfs_object_finalize;
59 }
60
61
62 static void captive_vfs_object_init(CaptiveVfsObject *captive_vfs_object)
63 {
64         CAPTIVE_MEMZERO(&captive_vfs_object->options);
65
66         captive_vfs_object->corba_parent_giochanel_blind=NULL;
67 }
68
69
70 GType captive_vfs_object_get_type(void)
71 {
72 static GType captive_vfs_object_type=0;
73
74         if (!captive_vfs_object_type) {
75 static const GTypeInfo captive_vfs_object_info={
76                                 sizeof(CaptiveVfsObjectClass),
77                                 NULL,   /* base_init */
78                                 NULL,   /* base_finalize */
79                                 (GClassInitFunc)captive_vfs_object_class_init,
80                                 NULL,   /* class_finalize */
81                                 NULL,   /* class_data */
82                                 sizeof(CaptiveVfsObject),
83                                 5,      /* n_preallocs */
84                                 (GInstanceInitFunc)captive_vfs_object_init,
85                                 };
86
87                 captive_vfs_object_type=g_type_register_static(G_TYPE_OBJECT,
88                                 "CaptiveVfsObject",&captive_vfs_object_info,0);
89                 }
90
91         return captive_vfs_object_type;
92 }
93
94
95 GnomeVFSResult captive_vfs_new
96                 (CaptiveVfsObject **captive_vfs_object_return,const struct captive_options *options)
97 {
98 CaptiveVfsObject *captive_vfs_object;
99 gboolean errbool;
100
101         g_return_val_if_fail(captive_vfs_object_return!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
102         g_return_val_if_fail(options!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
103
104         /* Here is the first initializaton point of parent in sandboxed mode. */
105         captive_log_init(options);
106
107         *captive_vfs_object_return=NULL;
108
109         captive_vfs_object=g_object_new(
110                         CAPTIVE_VFS_TYPE_OBJECT,        /* object_type */
111                         NULL);  /* first_property_name; FIXME: support properties */
112
113         captive_options_copy(&captive_vfs_object->options,options);
114
115         if (captive_vfs_object->options.image_iochannel)
116                 captive_giochannel_setup(captive_vfs_object->options.image_iochannel);
117
118         *captive_vfs_object_return=captive_vfs_object;
119
120         /* We are sandboxing && we are the master */
121         if (options->sandbox && (options->sandbox_server_argv || options->sandbox_server_ior))
122                 return captive_sandbox_parent_vfs_new(captive_vfs_object);
123
124         g_assert(captive_options==NULL);
125         captive_options=&captive_vfs_object->options;
126
127         errbool=captive_init();
128         g_assert(errbool==TRUE);
129
130         return GNOME_VFS_OK;
131 }
132
133
134 static GnomeVFSResult captive_vfs_close(CaptiveVfsObject *captive_vfs_object)
135 {
136 gboolean errbool;
137
138         g_return_val_if_fail(captive_vfs_object!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
139
140         if (captive_vfs_object->is_sandbox_parent)
141                 return captive_sandbox_parent_vfs_close(captive_vfs_object);
142
143         g_assert(captive_options==&captive_vfs_object->options);
144         errbool=captive_shutdown();
145         g_assert(errbool==TRUE);
146
147         captive_options=NULL;
148         captive_options_free(&captive_vfs_object->options);
149
150         return GNOME_VFS_OK;
151 }
152
153
154 GnomeVFSResult captive_vfs_commit(CaptiveVfsObject *captive_vfs_object)
155 {
156         g_return_val_if_fail(captive_vfs_object!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
157
158         if (captive_vfs_object->is_sandbox_parent)
159                 return captive_sandbox_parent_vfs_commit(captive_vfs_object);
160
161         /* We do not buffer any data if not in sandboxed mode. */
162         return GNOME_VFS_OK;
163 }
164
165
166 GnomeVFSResult captive_vfs_volume_info_get(CaptiveVfsObject *captive_vfs_object,CaptiveVfsVolumeInfo *volume_info)
167 {
168 FILE_FS_FULL_SIZE_INFORMATION FileFsFullSizeInformation_local;
169 NTSTATUS err;
170 IO_STATUS_BLOCK volume_IoStatusBlock;
171 GnomeVFSResult errvfsresult;
172 CaptiveDirectoryObject *captive_directory_object;
173
174         g_return_val_if_fail(captive_vfs_object!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
175         g_return_val_if_fail(volume_info!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
176
177         if (captive_vfs_object->is_sandbox_parent)
178                 return captive_sandbox_parent_vfs_volume_info_get(captive_vfs_object,volume_info);
179
180         if (GNOME_VFS_OK!=(errvfsresult=captive_directory_new_open(&captive_directory_object,captive_vfs_object,"/")))
181                 return errvfsresult;
182
183         err=NtQueryVolumeInformationFile(
184                         captive_directory_object->dir_Handle,   /* FileHandle */
185                         &volume_IoStatusBlock,  /* IoStatusBlock */
186                         &FileFsFullSizeInformation_local,       /* FsInformation */
187                         sizeof(FileFsFullSizeInformation_local),        /* Length */
188                         FileFsFullSizeInformation);     /* FsInformationClass */
189         if (NT_SUCCESS(err)!=NT_SUCCESS(volume_IoStatusBlock.Status)) {
190                 g_assert_not_reached();
191                 goto err_unref_captive_directory_object;
192                 }
193         if (GNOME_VFS_OK!=(errvfsresult=captive_NTSTATUS_to_GnomeVFSResult(err)))
194                 goto err_unref_captive_directory_object;
195
196         g_object_unref(captive_directory_object);
197
198         volume_info->block_size=FileFsFullSizeInformation_local.BytesPerSector
199                         *FileFsFullSizeInformation_local.SectorsPerAllocationUnit;
200         volume_info->bytes          =FileFsFullSizeInformation_local.TotalAllocationUnits.QuadPart          *volume_info->block_size;
201         volume_info->bytes_free     =FileFsFullSizeInformation_local.ActualAvailableAllocationUnits.QuadPart*volume_info->block_size;
202         volume_info->bytes_available=FileFsFullSizeInformation_local.CallerAvailableAllocationUnits.QuadPart*volume_info->block_size;
203
204         return GNOME_VFS_OK;
205
206 err_unref_captive_directory_object:
207         g_object_unref(captive_directory_object);
208         return errvfsresult;
209 }