Fixed EOF reading broken by recent --bug-pathname implementation.
[captive.git] / src / libcaptive / sandbox / parent-File.c
1 /* $Id$
2  * CORBA/ORBit client side of File object of sandbox_parent()
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 "captive/parent-File.h"        /* self */
23 #include <glib/gmessages.h>
24 #include "split.h"
25 #include "sandbox.h"
26 #include "../client/file.h"
27 #include "../client/vfs.h"
28 #include "FileInfo.h"
29 #include "captive/macros.h"
30
31
32 GnomeVFSResult captive_sandbox_parent_file_new_open(CaptiveFileObject *captive_file_object,
33                 const gchar *pathname,GnomeVFSOpenMode mode)
34 {
35 xmlNode *xml_action=NULL;
36
37         g_return_val_if_fail(captive_file_object!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
38         g_return_val_if_fail(pathname!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
39
40         if (captive_file_object->vfs->corba_bug_action) {
41                 xml_action=xmlNewTextChild(captive_file_object->vfs->corba_bug_action,NULL,"file_new_open",NULL);
42                 xmlNewProp(xml_action,"object",captive_printf_alloca("%p",captive_file_object));
43                 xmlNewProp(xml_action,"pathname",pathname);
44                 xmlNewProp(xml_action,"mode",captive_printf_alloca("%u",(unsigned)mode));
45                 }
46
47         captive_file_object->corba_File_object=Captive_Vfs_file_new_open(
48                         captive_file_object->vfs->corba_Vfs_object,pathname,mode,&captive_corba_ev);
49         if (xml_action)
50                 xmlNewProp(xml_action,"result",(captive_corba_ev._major==CORBA_NO_EXCEPTION ? "1" : "0"));
51
52         return captive_sandbox_parent_return_from_CORBA_Environment(&captive_corba_ev);
53 }
54
55
56 GnomeVFSResult captive_sandbox_parent_file_new_create(CaptiveFileObject *captive_file_object,
57                 const gchar *pathname,GnomeVFSOpenMode mode,gboolean exclusive,guint perm)
58 {
59 xmlNode *xml_action=NULL;
60
61         g_return_val_if_fail(captive_file_object!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
62         g_return_val_if_fail(pathname!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
63
64         if (captive_file_object->vfs->corba_bug_action) {
65                 xml_action=xmlNewTextChild(captive_file_object->vfs->corba_bug_action,NULL,"file_new_create",NULL);
66                 xmlNewProp(xml_action,"object",captive_printf_alloca("%p",captive_file_object));
67                 xmlNewProp(xml_action,"pathname",pathname);
68                 xmlNewProp(xml_action,"mode",captive_printf_alloca("%u",(unsigned)mode));
69                 xmlNewProp(xml_action,"exclusive",captive_printf_alloca("%u",(unsigned)exclusive));
70                 xmlNewProp(xml_action,"perm",captive_printf_alloca("%u",(unsigned)perm));
71                 }
72
73         captive_file_object->corba_File_object=Captive_Vfs_file_new_create(
74                         captive_file_object->vfs->corba_Vfs_object,pathname,mode,exclusive,perm,&captive_corba_ev);
75         if (xml_action)
76                 xmlNewProp(xml_action,"result",(captive_corba_ev._major==CORBA_NO_EXCEPTION ? "1" : "0"));
77
78         return captive_sandbox_parent_return_from_CORBA_Environment(&captive_corba_ev);
79 }
80
81
82 GnomeVFSResult captive_sandbox_parent_file_close(CaptiveFileObject *captive_file_object)
83 {
84 GnomeVFSResult r;
85 xmlNode *xml_action=NULL;
86
87         g_return_val_if_fail(captive_file_object!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
88         g_return_val_if_fail(captive_file_object->file_Handle==NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
89
90         if (captive_file_object->vfs->corba_bug_action) {
91                 xml_action=xmlNewTextChild(captive_file_object->vfs->corba_bug_action,NULL,"file_close",NULL);
92                 xmlNewProp(xml_action,"object",captive_printf_alloca("%p",captive_file_object));
93                 }
94
95         Captive_File_shutdown(captive_file_object->corba_File_object,&captive_corba_ev);
96         r=captive_sandbox_parent_return_from_CORBA_Environment(&captive_corba_ev);
97         if (xml_action)
98                 xmlNewProp(xml_action,"result",gnome_vfs_result_to_string(r));
99         if (r!=GNOME_VFS_OK)
100                 return r;
101
102         CORBA_Object_release((CORBA_Object)captive_file_object->corba_File_object,&captive_corba_ev);
103         captive_file_object->corba_File_object=CORBA_OBJECT_NIL;
104
105         return captive_sandbox_parent_return_from_CORBA_Environment(&captive_corba_ev);
106 }
107
108
109 GnomeVFSResult captive_sandbox_parent_file_read(CaptiveFileObject *captive_file_object,
110                 gpointer buffer_captive,GnomeVFSFileSize num_bytes,GnomeVFSFileSize *bytes_read_return)
111 {
112 GnomeVFSResult r;
113 Captive_Bytes *buffer_corba;
114 xmlNode *xml_action=NULL;
115
116         g_return_val_if_fail(captive_file_object!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
117         g_return_val_if_fail(buffer_captive!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
118         g_return_val_if_fail(bytes_read_return!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
119         g_return_val_if_fail(num_bytes==(ULONG)num_bytes,GNOME_VFS_ERROR_BAD_PARAMETERS);
120         g_return_val_if_fail(captive_file_object->file_Handle==NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
121
122         *bytes_read_return=0;
123
124         if (captive_file_object->vfs->corba_bug_action) {
125                 xml_action=xmlNewTextChild(captive_file_object->vfs->corba_bug_action,NULL,"file_read",NULL);
126                 xmlNewProp(xml_action,"object",captive_printf_alloca("%p",captive_file_object));
127                 xmlNewProp(xml_action,"num_bytes",captive_printf_alloca("%lu",(unsigned long)num_bytes));
128                 }
129
130         Captive_File_read(captive_file_object->corba_File_object,&buffer_corba,num_bytes,&captive_corba_ev);
131         r=captive_sandbox_parent_return_from_CORBA_Environment(&captive_corba_ev);
132         if (xml_action)
133                 xmlNewProp(xml_action,"result",gnome_vfs_result_to_string(r));
134         if (r!=GNOME_VFS_OK)
135                 return r;
136
137         g_return_val_if_fail(buffer_corba->_length<=num_bytes,GNOME_VFS_ERROR_GENERIC);
138         memcpy(buffer_captive,buffer_corba->_buffer,buffer_corba->_length);
139         *bytes_read_return=buffer_corba->_length;
140
141         Captive_Bytes__freekids(buffer_corba,NULL/* 'd'; meaning? */);
142         CORBA_free(buffer_corba);
143
144         if (xml_action)
145                 xmlNewProp(xml_action,"bytes_read_return",captive_printf_alloca("%lu",(unsigned long)*bytes_read_return));
146
147         return GNOME_VFS_OK;
148 }
149
150
151 GnomeVFSResult captive_sandbox_parent_file_write(CaptiveFileObject *captive_file_object,
152                 gconstpointer buffer_captive,GnomeVFSFileSize num_bytes,GnomeVFSFileSize *bytes_written_return)
153 {
154 GnomeVFSResult r;
155 Captive_Bytes buffer_corba_local;
156 Captive_GnomeVFSFileSize bytes_written_corba;
157 xmlNode *xml_action=NULL;
158
159         g_return_val_if_fail(captive_file_object!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
160         g_return_val_if_fail(buffer_captive!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
161         g_return_val_if_fail(bytes_written_return!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
162         g_return_val_if_fail(num_bytes==(ULONG)num_bytes,GNOME_VFS_ERROR_BAD_PARAMETERS);
163         g_return_val_if_fail(captive_file_object->file_Handle==NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
164
165         *bytes_written_return=0;
166
167         if (captive_file_object->vfs->corba_bug_action) {
168                 xml_action=xmlNewTextChild(captive_file_object->vfs->corba_bug_action,NULL,"file_write",NULL);
169                 xmlNewProp(xml_action,"object",captive_printf_alloca("%p",captive_file_object));
170                 xmlNewProp(xml_action,"num_bytes",captive_printf_alloca("%lu",(unsigned long)num_bytes));
171                 }
172
173         buffer_corba_local._maximum=num_bytes;
174         buffer_corba_local._length=num_bytes;
175         buffer_corba_local._buffer=(/* de-const */gpointer)buffer_captive;
176         buffer_corba_local._release=FALSE;
177
178         Captive_File_write(captive_file_object->corba_File_object,&buffer_corba_local,&bytes_written_corba,&captive_corba_ev);
179         r=captive_sandbox_parent_return_from_CORBA_Environment(&captive_corba_ev);
180         if (xml_action)
181                 xmlNewProp(xml_action,"result",gnome_vfs_result_to_string(r));
182         if (r!=GNOME_VFS_OK)
183                 return r;
184
185         *bytes_written_return=bytes_written_corba;
186
187         if (xml_action)
188                 xmlNewProp(xml_action,"bytes_written_return",captive_printf_alloca("%lu",(unsigned long)*bytes_written_return));
189
190         return GNOME_VFS_OK;
191 }
192
193
194 GnomeVFSResult captive_sandbox_parent_file_seek
195                 (CaptiveFileObject *captive_file_object,GnomeVFSSeekPosition whence,GnomeVFSFileOffset offset)
196 {
197 GnomeVFSResult r;
198 xmlNode *xml_action=NULL;
199
200         g_return_val_if_fail(captive_file_object!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
201         g_return_val_if_fail(captive_file_object->file_Handle==NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
202
203         if (captive_file_object->vfs->corba_bug_action) {
204                 xml_action=xmlNewTextChild(captive_file_object->vfs->corba_bug_action,NULL,"file_seek",NULL);
205                 xmlNewProp(xml_action,"object",captive_printf_alloca("%p",captive_file_object));
206                 xmlNewProp(xml_action,"whence",
207                                 (whence==GNOME_VFS_SEEK_START ? "start" :
208                                         (whence==GNOME_VFS_SEEK_CURRENT ? "current" :
209                                                 (whence==GNOME_VFS_SEEK_END ? "end" : captive_printf_alloca("%lu",(unsigned long)whence)))));
210                 xmlNewProp(xml_action,"offset",captive_printf_alloca("%" G_GINT64_FORMAT,(gint64)offset));
211                 }
212
213         Captive_File_seek(captive_file_object->corba_File_object,whence,offset,&captive_corba_ev);
214         r=captive_sandbox_parent_return_from_CORBA_Environment(&captive_corba_ev);
215         if (xml_action)
216                 xmlNewProp(xml_action,"result",gnome_vfs_result_to_string(r));
217         if (r!=GNOME_VFS_OK)
218                 return r;
219
220         return GNOME_VFS_OK;
221 }
222
223
224 GnomeVFSResult captive_sandbox_parent_file_tell(CaptiveFileObject *captive_file_object,GnomeVFSFileOffset *offset_return)
225 {
226 Captive_GnomeVFSFileOffset offset_corba;
227 GnomeVFSResult r;
228 xmlNode *xml_action=NULL;
229
230         g_return_val_if_fail(captive_file_object!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
231         g_return_val_if_fail(offset_return!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
232         g_return_val_if_fail(captive_file_object->file_Handle==NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
233
234         if (captive_file_object->vfs->corba_bug_action) {
235                 xml_action=xmlNewTextChild(captive_file_object->vfs->corba_bug_action,NULL,"file_tell",NULL);
236                 xmlNewProp(xml_action,"object",captive_printf_alloca("%p",captive_file_object));
237                 }
238
239         Captive_File_tell(captive_file_object->corba_File_object,&offset_corba,&captive_corba_ev);
240         r=captive_sandbox_parent_return_from_CORBA_Environment(&captive_corba_ev);
241         if (xml_action)
242                 xmlNewProp(xml_action,"result",gnome_vfs_result_to_string(r));
243         if (r!=GNOME_VFS_OK)
244                 return r;
245
246         *offset_return=offset_corba;
247
248         if (xml_action)
249                 xmlNewProp(xml_action,"offset_return",captive_printf_alloca("%" G_GINT64_FORMAT,(gint64)*offset_return));
250
251         return GNOME_VFS_OK;
252 }
253
254
255 GnomeVFSResult captive_sandbox_parent_file_remove(CaptiveFileObject *captive_file_object)
256 {
257 GnomeVFSResult r;
258 xmlNode *xml_action=NULL;
259
260         g_return_val_if_fail(captive_file_object!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
261         g_return_val_if_fail(captive_file_object->file_Handle==NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
262
263         if (captive_file_object->vfs->corba_bug_action) {
264                 xml_action=xmlNewTextChild(captive_file_object->vfs->corba_bug_action,NULL,"file_remove",NULL);
265                 xmlNewProp(xml_action,"object",captive_printf_alloca("%p",captive_file_object));
266                 }
267
268         Captive_File_remove(captive_file_object->corba_File_object,&captive_corba_ev);
269         r=captive_sandbox_parent_return_from_CORBA_Environment(&captive_corba_ev);
270         if (xml_action)
271                 xmlNewProp(xml_action,"result",gnome_vfs_result_to_string(r));
272         if (r!=GNOME_VFS_OK)
273                 return r;
274
275         return GNOME_VFS_OK;
276 }
277
278
279 GnomeVFSResult captive_sandbox_parent_file_file_info_get(CaptiveFileObject *captive_file_object,
280                 GnomeVFSFileInfo *file_info_captive)
281 {
282 Captive_GnomeVFSFileInfo *file_info_corba;
283 GnomeVFSResult r;
284 xmlNode *xml_action=NULL;
285
286         g_return_val_if_fail(captive_file_object!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
287         g_return_val_if_fail(file_info_captive!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
288         g_return_val_if_fail(captive_file_object->file_Handle==NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
289
290         if (captive_file_object->vfs->corba_bug_action) {
291                 xml_action=xmlNewTextChild(captive_file_object->vfs->corba_bug_action,NULL,"file_file_info_get",NULL);
292                 xmlNewProp(xml_action,"object",captive_printf_alloca("%p",captive_file_object));
293                 }
294
295         Captive_File_file_info_get(captive_file_object->corba_File_object,&file_info_corba,&captive_corba_ev);
296         r=captive_sandbox_parent_return_from_CORBA_Environment(&captive_corba_ev);
297         if (xml_action)
298                 xmlNewProp(xml_action,"result",gnome_vfs_result_to_string(r));
299         if (r!=GNOME_VFS_OK)
300                 return r;
301
302         r=captive_sandbox_file_info_corba_to_captive(file_info_captive,file_info_corba);
303         Captive_GnomeVFSFileInfo__freekids(file_info_corba,NULL/* 'd'; meaning? */);
304         CORBA_free(file_info_corba);
305         if (xml_action)
306                 xmlSetProp(xml_action,"result",gnome_vfs_result_to_string(r));
307         if (r!=GNOME_VFS_OK)
308                 return r;
309
310         return GNOME_VFS_OK;
311 }
312
313
314 GnomeVFSResult captive_sandbox_parent_file_file_info_set(CaptiveFileObject *captive_file_object,
315     const GnomeVFSFileInfo *file_info_captive,GnomeVFSSetFileInfoMask mask)
316 {
317 Captive_GnomeVFSFileInfo file_info_corba;
318 GnomeVFSResult r;
319 xmlNode *xml_action=NULL;
320
321         g_return_val_if_fail(captive_file_object!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
322         g_return_val_if_fail(file_info_captive!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
323         g_return_val_if_fail(captive_file_object->file_Handle==NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
324
325         if (captive_file_object->vfs->corba_bug_action) {
326                 xml_action=xmlNewTextChild(captive_file_object->vfs->corba_bug_action,NULL,"file_file_info_set",NULL);
327                 xmlNewProp(xml_action,"object",captive_printf_alloca("%p",captive_file_object));
328                 xmlNewProp(xml_action,"mask",captive_printf_alloca("%u",(unsigned)mask));
329                 }
330
331         if (GNOME_VFS_OK!=(r=captive_sandbox_file_info_captive_to_corba(&file_info_corba,file_info_captive)))
332                 return r;
333
334         Captive_File_file_info_set(captive_file_object->corba_File_object,&file_info_corba,mask,&captive_corba_ev);
335         r=captive_sandbox_parent_return_from_CORBA_Environment(&captive_corba_ev);
336         if (xml_action)
337                 xmlNewProp(xml_action,"result",gnome_vfs_result_to_string(r));
338         if (r!=GNOME_VFS_OK)
339                 return r;
340
341         Captive_GnomeVFSFileInfo__freekids(&file_info_corba,NULL/* 'd'; meaning? */);
342
343         return GNOME_VFS_OK;
344 }
345
346
347 GnomeVFSResult captive_sandbox_parent_file_truncate(CaptiveFileObject *captive_file_object,GnomeVFSFileSize file_size)
348 {
349 GnomeVFSResult r;
350 xmlNode *xml_action=NULL;
351
352         g_return_val_if_fail(captive_file_object!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
353         g_return_val_if_fail(captive_file_object->file_Handle==NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
354
355         if (captive_file_object->vfs->corba_bug_action) {
356                 xml_action=xmlNewTextChild(captive_file_object->vfs->corba_bug_action,NULL,"file_truncate",NULL);
357                 xmlNewProp(xml_action,"object",captive_printf_alloca("%p",captive_file_object));
358                 xmlNewProp(xml_action,"file_size",captive_printf_alloca("%" G_GUINT64_FORMAT,(guint64)file_size));
359                 }
360
361         Captive_File_truncate(captive_file_object->corba_File_object,file_size,&captive_corba_ev);
362         r=captive_sandbox_parent_return_from_CORBA_Environment(&captive_corba_ev);
363         if (xml_action)
364                 xmlNewProp(xml_action,"result",gnome_vfs_result_to_string(r));
365         if (r!=GNOME_VFS_OK)
366                 return r;
367
368         return GNOME_VFS_OK;
369 }
370
371
372 GnomeVFSResult captive_sandbox_parent_file_move
373                 (CaptiveFileObject *captive_file_object_old,const gchar *pathname_new,gboolean force_replace)
374 {
375 GnomeVFSResult r;
376 xmlNode *xml_action=NULL;
377
378         g_return_val_if_fail(captive_file_object_old!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
379         g_return_val_if_fail(pathname_new!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
380         g_return_val_if_fail(captive_file_object_old->file_Handle==NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
381
382         if (captive_file_object_old->vfs->corba_bug_action) {
383                 xml_action=xmlNewTextChild(captive_file_object_old->vfs->corba_bug_action,NULL,"file_truncate",NULL);
384                 xmlNewProp(xml_action,"object",captive_printf_alloca("%p",captive_file_object_old));
385                 xmlNewProp(xml_action,"pathname_new",pathname_new);
386                 xmlNewProp(xml_action,"force_replace",captive_printf_alloca("%u",(unsigned)force_replace));
387                 }
388
389         Captive_File_move(captive_file_object_old->corba_File_object,pathname_new,force_replace,&captive_corba_ev);
390         r=captive_sandbox_parent_return_from_CORBA_Environment(&captive_corba_ev);
391         if (xml_action)
392                 xmlNewProp(xml_action,"result",gnome_vfs_result_to_string(r));
393         if (r!=GNOME_VFS_OK)
394                 return r;
395
396         return GNOME_VFS_OK;
397 }