Split (vfs|directory|file) to &, &_parent and &_slave by inheritance.
[captive.git] / src / libcaptive / client / vfs-slave.c
1 /* $Id$
2  * captive vfs 'vfs' interface to reactos of sandbox slave
3  * Copyright (C) 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 "vfs-slave.h"  /* self */
23 #include <glib/gmessages.h>
24 #include "captive/client-vfs.h" /* for CaptiveVfsObject */
25 #include "vfs.h"        /* for CaptiveVfsObject priv */
26 #include "captive/macros.h"
27 #include "directory.h"
28 #include "reactos/ddk/iotypes.h"
29 #include "captive/client-directory.h"
30 #include "reactos/ntos/zw.h"    /* for NtQueryVolumeInformationFile() */
31 #include "result.h"
32 #include "init.h"
33 #include "directory-slave.h"
34 #include "file-slave.h"
35
36
37 struct _CaptiveVfsSlaveObject {
38         CaptiveVfsObject parent_instance;
39         };
40 struct _CaptiveVfsSlaveObjectClass {
41         CaptiveVfsObjectClass parent_class;
42         };
43
44
45 static gpointer captive_vfs_slave_object_parent_class=NULL;
46
47
48 static GnomeVFSResult captive_vfs_slave_init(CaptiveVfsObject *captive_vfs_object);
49 static GnomeVFSResult captive_vfs_slave_close(CaptiveVfsObject *captive_vfs_object);
50 static GnomeVFSResult captive_vfs_slave_commit(CaptiveVfsObject *captive_vfs_object);
51 static GnomeVFSResult captive_vfs_slave_volume_info_get
52                 (CaptiveVfsObject *captive_vfs_object,CaptiveVfsVolumeInfo *volume_info);
53
54
55 static void captive_vfs_slave_object_finalize(CaptiveVfsSlaveObject *captive_vfs_slave_object)
56 {
57         g_return_if_fail(captive_vfs_slave_object!=NULL);
58
59         captive_vfs_slave_close(CAPTIVE_VFS_OBJECT(captive_vfs_slave_object));  /* errors ignored */
60
61         G_OBJECT_CLASS(captive_vfs_slave_object_parent_class)->finalize((GObject *)captive_vfs_slave_object);
62 }
63
64
65 static void captive_vfs_slave_object_class_init(CaptiveVfsSlaveObjectClass *class)
66 {
67 GObjectClass *gobject_class=G_OBJECT_CLASS(class);
68 CaptiveVfsObjectClass *captive_vfs_object_class=CAPTIVE_VFS_OBJECT_CLASS(class);
69
70         captive_vfs_slave_object_parent_class=g_type_class_ref(g_type_parent(G_TYPE_FROM_CLASS(class)));
71         gobject_class->finalize=(void (*)(GObject *object))captive_vfs_slave_object_finalize;
72
73         captive_vfs_object_class->init=captive_vfs_slave_init;
74         captive_vfs_object_class->commit=captive_vfs_slave_commit;
75         captive_vfs_object_class->volume_info_get=captive_vfs_slave_volume_info_get;
76
77         captive_vfs_object_class->directory_new_open=captive_directory_slave_new_open;
78         captive_vfs_object_class->directory_new_make=captive_directory_slave_new_make;
79
80         captive_vfs_object_class->file_new_open=captive_file_slave_new_open;
81         captive_vfs_object_class->file_new_create=captive_file_slave_new_create;
82 }
83
84
85 static void captive_vfs_slave_object_init(CaptiveVfsSlaveObject *captive_vfs_slave_object)
86 {
87 }
88
89
90 GType captive_vfs_slave_object_get_type(void)
91 {
92 static GType captive_vfs_slave_object_type=0;
93
94         if (!captive_vfs_slave_object_type) {
95 static const GTypeInfo captive_vfs_slave_object_info={
96                                 sizeof(CaptiveVfsSlaveObjectClass),
97                                 NULL,   /* base_init */
98                                 NULL,   /* base_finalize */
99                                 (GClassInitFunc)captive_vfs_slave_object_class_init,
100                                 NULL,   /* class_finalize */
101                                 NULL,   /* class_data */
102                                 sizeof(CaptiveVfsSlaveObject),
103                                 5,      /* n_preallocs */
104                                 (GInstanceInitFunc)captive_vfs_slave_object_init,
105                                 };
106
107                 captive_vfs_slave_object_type=g_type_register_static(CAPTIVE_VFS_TYPE_OBJECT,
108                                 "CaptiveVfsSlaveObject",&captive_vfs_slave_object_info,0);
109                 }
110
111         return captive_vfs_slave_object_type;
112 }
113
114
115 static GnomeVFSResult captive_vfs_slave_init(CaptiveVfsObject *captive_vfs_object)
116 {
117 CaptiveVfsSlaveObject *captive_vfs_slave_object;
118 gboolean errbool;
119
120         g_return_val_if_fail(CAPTIVE_VFS_SLAVE_IS_OBJECT(captive_vfs_object),GNOME_VFS_ERROR_BAD_PARAMETERS);
121
122         captive_vfs_slave_object=CAPTIVE_VFS_SLAVE_OBJECT(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_slave_close(CaptiveVfsObject *captive_vfs_object)
135 {
136 CaptiveVfsSlaveObject *captive_vfs_slave_object;
137 gboolean errbool;
138
139         g_return_val_if_fail(CAPTIVE_VFS_SLAVE_IS_OBJECT(captive_vfs_object),GNOME_VFS_ERROR_BAD_PARAMETERS);
140
141         captive_vfs_slave_object=CAPTIVE_VFS_SLAVE_OBJECT(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
149         return GNOME_VFS_OK;
150 }
151
152
153 static GnomeVFSResult captive_vfs_slave_commit(CaptiveVfsObject *captive_vfs_object)
154 {
155 CaptiveVfsSlaveObject *captive_vfs_slave_object;
156
157         g_return_val_if_fail(CAPTIVE_VFS_SLAVE_IS_OBJECT(captive_vfs_object),GNOME_VFS_ERROR_BAD_PARAMETERS);
158
159         captive_vfs_slave_object=CAPTIVE_VFS_SLAVE_OBJECT(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 static GnomeVFSResult captive_vfs_slave_volume_info_get
167                 (CaptiveVfsObject *captive_vfs_object,CaptiveVfsVolumeInfo *volume_info)
168 {
169 CaptiveVfsSlaveObject *captive_vfs_slave_object;
170 FILE_FS_FULL_SIZE_INFORMATION FileFsFullSizeInformation_local;
171 NTSTATUS err;
172 IO_STATUS_BLOCK volume_IoStatusBlock;
173 GnomeVFSResult errvfsresult;
174 CaptiveDirectoryObject *captive_directory_object;
175
176         g_return_val_if_fail(CAPTIVE_VFS_SLAVE_IS_OBJECT(captive_vfs_object),GNOME_VFS_ERROR_BAD_PARAMETERS);
177         g_return_val_if_fail(volume_info!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
178
179         captive_vfs_slave_object=CAPTIVE_VFS_SLAVE_OBJECT(captive_vfs_object);
180
181         if (GNOME_VFS_OK!=(errvfsresult=captive_directory_new_open(&captive_directory_object,captive_vfs_object,"/")))
182                 return errvfsresult;
183
184         err=NtQueryVolumeInformationFile(
185                         CAPTIVE_DIRECTORY_SLAVE_OBJECT(captive_directory_object)->dir_Handle,   /* FileHandle */
186                         &volume_IoStatusBlock,  /* IoStatusBlock */
187                         &FileFsFullSizeInformation_local,       /* FsInformation */
188                         sizeof(FileFsFullSizeInformation_local),        /* Length */
189                         FileFsFullSizeInformation);     /* FsInformationClass */
190         if (NT_SUCCESS(err)!=NT_SUCCESS(volume_IoStatusBlock.Status)) {
191                 g_assert_not_reached();
192                 goto err_unref_captive_directory_object;
193                 }
194         if (GNOME_VFS_OK!=(errvfsresult=captive_NTSTATUS_to_GnomeVFSResult(err)))
195                 goto err_unref_captive_directory_object;
196
197         g_object_unref(captive_directory_object);
198
199         volume_info->block_size=FileFsFullSizeInformation_local.BytesPerSector
200                         *FileFsFullSizeInformation_local.SectorsPerAllocationUnit;
201         volume_info->bytes          =FileFsFullSizeInformation_local.TotalAllocationUnits.QuadPart          *volume_info->block_size;
202         volume_info->bytes_free     =FileFsFullSizeInformation_local.ActualAvailableAllocationUnits.QuadPart*volume_info->block_size;
203         volume_info->bytes_available=FileFsFullSizeInformation_local.CallerAvailableAllocationUnits.QuadPart*volume_info->block_size;
204
205         return GNOME_VFS_OK;
206
207 err_unref_captive_directory_object:
208         g_object_unref(captive_directory_object);
209         return errvfsresult;
210 }