Implemented vfs_volume_info_get() for proper df(1) entry.
[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
36
37 static gpointer captive_vfs_object_parent_class=NULL;
38
39
40 static GnomeVFSResult captive_vfs_close(CaptiveVfsObject *captive_vfs_object);
41
42 static void captive_vfs_object_finalize(CaptiveVfsObject *captive_vfs_object)
43 {
44         g_return_if_fail(captive_vfs_object!=NULL);
45
46         captive_vfs_close(captive_vfs_object);  /* errors ignored */
47
48         G_OBJECT_CLASS(captive_vfs_object_parent_class)->finalize((GObject *)captive_vfs_object);
49 }
50
51
52 static void captive_vfs_object_class_init(CaptiveVfsObjectClass *class)
53 {
54 GObjectClass *gobject_class=G_OBJECT_CLASS(class);
55
56         captive_vfs_object_parent_class=g_type_class_ref(G_TYPE_OBJECT);
57         gobject_class->finalize=(void (*)(GObject *object))captive_vfs_object_finalize;
58 }
59
60
61 static void captive_vfs_object_init(CaptiveVfsObject *captive_vfs_object)
62 {
63         CAPTIVE_MEMZERO(&captive_vfs_object->options);
64
65         captive_vfs_object->corba_parent_giochanel_blind=NULL;
66 }
67
68
69 GType captive_vfs_object_get_type(void)
70 {
71 static GType captive_vfs_object_type=0;
72
73         if (!captive_vfs_object_type) {
74 static const GTypeInfo captive_vfs_object_info={
75                                 sizeof(CaptiveVfsObjectClass),
76                                 NULL,   /* base_init */
77                                 NULL,   /* base_finalize */
78                                 (GClassInitFunc)captive_vfs_object_class_init,
79                                 NULL,   /* class_finalize */
80                                 NULL,   /* class_data */
81                                 sizeof(CaptiveVfsObject),
82                                 5,      /* n_preallocs */
83                                 (GInstanceInitFunc)captive_vfs_object_init,
84                                 };
85
86                 captive_vfs_object_type=g_type_register_static(G_TYPE_OBJECT,
87                                 "CaptiveVfsObject",&captive_vfs_object_info,0);
88                 }
89
90         return captive_vfs_object_type;
91 }
92
93
94 static void     log_discard_func(const gchar *log_domain,GLogLevelFlags log_level,const gchar *message,gpointer user_data)
95 {
96         /* NOP */
97 }
98
99 GnomeVFSResult captive_vfs_new
100                 (CaptiveVfsObject **captive_vfs_object_return,const struct captive_options *options)
101 {
102 CaptiveVfsObject *captive_vfs_object;
103 gboolean errbool;
104
105         g_return_val_if_fail(captive_vfs_object_return!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
106         g_return_val_if_fail(options!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
107
108         /* Here is the first initializaton point of parent in sandboxed mode. */
109         if (!options->debug_messages) {
110                 g_log_set_handler(
111                                 G_LOG_DOMAIN,   /* log_domain; "Captive" */
112                                 0       /* log_levels */
113                                                 | G_LOG_FLAG_RECURSION
114                                                 | G_LOG_FLAG_FATAL
115                                                 /* The same mask is in:
116                                                  * libcaptive/sandbox/server-GLogFunc.c
117                                                  * libcaptive/client/init.c
118                                                  * libcaptive/client/vfs.c
119                                                  */
120                                                 | G_LOG_LEVEL_MESSAGE
121                                                 | G_LOG_LEVEL_INFO
122                                                 | G_LOG_LEVEL_DEBUG,
123                                 log_discard_func,       /* log_func */
124                                 NULL);  /* user_data */
125                 }
126
127         *captive_vfs_object_return=NULL;
128
129         captive_vfs_object=g_object_new(
130                         CAPTIVE_VFS_TYPE_OBJECT,        /* object_type */
131                         NULL);  /* first_property_name; FIXME: support properties */
132
133         captive_options_copy(&captive_vfs_object->options,options);
134
135         if (captive_vfs_object->options.image_iochannel)
136                 captive_giochannel_setup(captive_vfs_object->options.image_iochannel);
137
138         *captive_vfs_object_return=captive_vfs_object;
139
140         /* We are sandboxing && we are the master */
141         if (options->sandbox && (options->sandbox_server_argv || options->sandbox_server_ior))
142                 return captive_sandbox_parent_vfs_new(captive_vfs_object);
143
144         g_assert(captive_options==NULL);
145         captive_options=&captive_vfs_object->options;
146
147         errbool=captive_init();
148         g_assert(errbool==TRUE);
149
150         return GNOME_VFS_OK;
151 }
152
153
154 static GnomeVFSResult captive_vfs_close(CaptiveVfsObject *captive_vfs_object)
155 {
156 gboolean errbool;
157
158         g_return_val_if_fail(captive_vfs_object!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
159
160         if (captive_vfs_object->is_sandbox_parent)
161                 return captive_sandbox_parent_vfs_close(captive_vfs_object);
162
163         g_assert(captive_options==&captive_vfs_object->options);
164         errbool=captive_shutdown();
165         g_assert(errbool==TRUE);
166
167         captive_options=NULL;
168         captive_options_free(&captive_vfs_object->options);
169
170         return GNOME_VFS_OK;
171 }
172
173
174 GnomeVFSResult captive_vfs_commit(CaptiveVfsObject *captive_vfs_object)
175 {
176         g_return_val_if_fail(captive_vfs_object!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
177
178         if (captive_vfs_object->is_sandbox_parent)
179                 return captive_sandbox_parent_vfs_commit(captive_vfs_object);
180
181         /* We do not buffer any data if not in sandboxed mode. */
182         return GNOME_VFS_OK;
183 }
184
185
186 GnomeVFSResult captive_vfs_volume_info_get(CaptiveVfsObject *captive_vfs_object,CaptiveVfsVolumeInfo *volume_info)
187 {
188 FILE_FS_FULL_SIZE_INFORMATION FileFsFullSizeInformation_local;
189 NTSTATUS err;
190 IO_STATUS_BLOCK volume_IoStatusBlock;
191 GnomeVFSResult errvfsresult;
192 CaptiveDirectoryObject *captive_directory_object;
193
194         g_return_val_if_fail(captive_vfs_object!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
195         g_return_val_if_fail(volume_info!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
196
197         if (captive_vfs_object->is_sandbox_parent)
198                 return captive_sandbox_parent_vfs_volume_info_get(captive_vfs_object,volume_info);
199
200         if (GNOME_VFS_OK!=(errvfsresult=captive_directory_new_open(&captive_directory_object,captive_vfs_object,"/")))
201                 return errvfsresult;
202
203         err=NtQueryVolumeInformationFile(
204                         captive_directory_object->dir_Handle,   /* FileHandle */
205                         &volume_IoStatusBlock,  /* IoStatusBlock */
206                         &FileFsFullSizeInformation_local,       /* FsInformation */
207                         sizeof(FileFsFullSizeInformation_local),        /* Length */
208                         FileFsFullSizeInformation);     /* FsInformationClass */
209         if (NT_SUCCESS(err)!=NT_SUCCESS(volume_IoStatusBlock.Status)) {
210                 g_assert_not_reached();
211                 goto err_unref_captive_directory_object;
212                 }
213         if (GNOME_VFS_OK!=(errvfsresult=captive_NTSTATUS_to_GnomeVFSResult(err)))
214                 goto err_unref_captive_directory_object;
215
216         g_object_unref(captive_directory_object);
217
218         volume_info->block_size=FileFsFullSizeInformation_local.BytesPerSector
219                         *FileFsFullSizeInformation_local.SectorsPerAllocationUnit;
220         volume_info->bytes          =FileFsFullSizeInformation_local.TotalAllocationUnits.QuadPart          *volume_info->block_size;
221         volume_info->bytes_free     =FileFsFullSizeInformation_local.ActualAvailableAllocationUnits.QuadPart*volume_info->block_size;
222         volume_info->bytes_available=FileFsFullSizeInformation_local.CallerAvailableAllocationUnits.QuadPart*volume_info->block_size;
223
224         return GNOME_VFS_OK;
225
226 err_unref_captive_directory_object:
227         g_object_unref(captive_directory_object);
228         return errvfsresult;
229 }