+Generates --bug-pathname resources file for sandbox crash bugreport
[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                 return r;
133         if (xml_action)
134                 xmlNewProp(xml_action,"result",gnome_vfs_result_to_string(r));
135         if (r!=GNOME_VFS_OK)
136                 return r;
137
138         g_return_val_if_fail(buffer_corba->_length<=num_bytes,GNOME_VFS_ERROR_GENERIC);
139         memcpy(buffer_captive,buffer_corba->_buffer,buffer_corba->_length);
140         *bytes_read_return=buffer_corba->_length;
141
142         Captive_Bytes__freekids(buffer_corba,NULL/* 'd'; meaning? */);
143         CORBA_free(buffer_corba);
144
145         if (xml_action)
146                 xmlNewProp(xml_action,"bytes_read_return",captive_printf_alloca("%lu",(unsigned long)*bytes_read_return));
147
148         return GNOME_VFS_OK;
149 }
150
151
152 GnomeVFSResult captive_sandbox_parent_file_write(CaptiveFileObject *captive_file_object,
153                 gconstpointer buffer_captive,GnomeVFSFileSize num_bytes,GnomeVFSFileSize *bytes_written_return)
154 {
155 GnomeVFSResult r;
156 Captive_Bytes buffer_corba_local;
157 Captive_GnomeVFSFileSize bytes_written_corba;
158 xmlNode *xml_action=NULL;
159
160         g_return_val_if_fail(captive_file_object!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
161         g_return_val_if_fail(buffer_captive!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
162         g_return_val_if_fail(bytes_written_return!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
163         g_return_val_if_fail(num_bytes==(ULONG)num_bytes,GNOME_VFS_ERROR_BAD_PARAMETERS);
164         g_return_val_if_fail(captive_file_object->file_Handle==NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
165
166         *bytes_written_return=0;
167
168         if (captive_file_object->vfs->corba_bug_action) {
169                 xml_action=xmlNewTextChild(captive_file_object->vfs->corba_bug_action,NULL,"file_write",NULL);
170                 xmlNewProp(xml_action,"object",captive_printf_alloca("%p",captive_file_object));
171                 xmlNewProp(xml_action,"num_bytes",captive_printf_alloca("%lu",(unsigned long)num_bytes));
172                 }
173
174         buffer_corba_local._maximum=num_bytes;
175         buffer_corba_local._length=num_bytes;
176         buffer_corba_local._buffer=(/* de-const */gpointer)buffer_captive;
177         buffer_corba_local._release=FALSE;
178
179         Captive_File_write(captive_file_object->corba_File_object,&buffer_corba_local,&bytes_written_corba,&captive_corba_ev);
180         r=captive_sandbox_parent_return_from_CORBA_Environment(&captive_corba_ev);
181         if (xml_action)
182                 xmlNewProp(xml_action,"result",gnome_vfs_result_to_string(r));
183         if (r!=GNOME_VFS_OK)
184                 return r;
185
186         *bytes_written_return=bytes_written_corba;
187
188         if (xml_action)
189                 xmlNewProp(xml_action,"bytes_written_return",captive_printf_alloca("%lu",(unsigned long)*bytes_written_return));
190
191         return GNOME_VFS_OK;
192 }
193
194
195 GnomeVFSResult captive_sandbox_parent_file_seek
196                 (CaptiveFileObject *captive_file_object,GnomeVFSSeekPosition whence,GnomeVFSFileOffset offset)
197 {
198 GnomeVFSResult r;
199 xmlNode *xml_action=NULL;
200
201         g_return_val_if_fail(captive_file_object!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
202         g_return_val_if_fail(captive_file_object->file_Handle==NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
203
204         if (captive_file_object->vfs->corba_bug_action) {
205                 xml_action=xmlNewTextChild(captive_file_object->vfs->corba_bug_action,NULL,"file_seek",NULL);
206                 xmlNewProp(xml_action,"object",captive_printf_alloca("%p",captive_file_object));
207                 xmlNewProp(xml_action,"whence",
208                                 (whence==GNOME_VFS_SEEK_START ? "start" :
209                                         (whence==GNOME_VFS_SEEK_CURRENT ? "current" :
210                                                 (whence==GNOME_VFS_SEEK_END ? "end" : captive_printf_alloca("%lu",(unsigned long)whence)))));
211                 xmlNewProp(xml_action,"offset",captive_printf_alloca("%" G_GINT64_FORMAT,(gint64)offset));
212                 }
213
214         Captive_File_seek(captive_file_object->corba_File_object,whence,offset,&captive_corba_ev);
215         r=captive_sandbox_parent_return_from_CORBA_Environment(&captive_corba_ev);
216         if (xml_action)
217                 xmlNewProp(xml_action,"result",gnome_vfs_result_to_string(r));
218         if (r!=GNOME_VFS_OK)
219                 return r;
220
221         return GNOME_VFS_OK;
222 }
223
224
225 GnomeVFSResult captive_sandbox_parent_file_tell(CaptiveFileObject *captive_file_object,GnomeVFSFileOffset *offset_return)
226 {
227 Captive_GnomeVFSFileOffset offset_corba;
228 GnomeVFSResult r;
229 xmlNode *xml_action=NULL;
230
231         g_return_val_if_fail(captive_file_object!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
232         g_return_val_if_fail(offset_return!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
233         g_return_val_if_fail(captive_file_object->file_Handle==NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
234
235         if (captive_file_object->vfs->corba_bug_action) {
236                 xml_action=xmlNewTextChild(captive_file_object->vfs->corba_bug_action,NULL,"file_tell",NULL);
237                 xmlNewProp(xml_action,"object",captive_printf_alloca("%p",captive_file_object));
238                 }
239
240         Captive_File_tell(captive_file_object->corba_File_object,&offset_corba,&captive_corba_ev);
241         r=captive_sandbox_parent_return_from_CORBA_Environment(&captive_corba_ev);
242         if (xml_action)
243                 xmlNewProp(xml_action,"result",gnome_vfs_result_to_string(r));
244         if (r!=GNOME_VFS_OK)
245                 return r;
246
247         *offset_return=offset_corba;
248
249         if (xml_action)
250                 xmlNewProp(xml_action,"offset_return",captive_printf_alloca("%" G_GINT64_FORMAT,(gint64)*offset_return));
251
252         return GNOME_VFS_OK;
253 }
254
255
256 GnomeVFSResult captive_sandbox_parent_file_remove(CaptiveFileObject *captive_file_object)
257 {
258 GnomeVFSResult r;
259 xmlNode *xml_action=NULL;
260
261         g_return_val_if_fail(captive_file_object!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
262         g_return_val_if_fail(captive_file_object->file_Handle==NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
263
264         if (captive_file_object->vfs->corba_bug_action) {
265                 xml_action=xmlNewTextChild(captive_file_object->vfs->corba_bug_action,NULL,"file_remove",NULL);
266                 xmlNewProp(xml_action,"object",captive_printf_alloca("%p",captive_file_object));
267                 }
268
269         Captive_File_remove(captive_file_object->corba_File_object,&captive_corba_ev);
270         r=captive_sandbox_parent_return_from_CORBA_Environment(&captive_corba_ev);
271         if (xml_action)
272                 xmlNewProp(xml_action,"result",gnome_vfs_result_to_string(r));
273         if (r!=GNOME_VFS_OK)
274                 return r;
275
276         return GNOME_VFS_OK;
277 }
278
279
280 GnomeVFSResult captive_sandbox_parent_file_file_info_get(CaptiveFileObject *captive_file_object,
281                 GnomeVFSFileInfo *file_info_captive)
282 {
283 Captive_GnomeVFSFileInfo *file_info_corba;
284 GnomeVFSResult r;
285 xmlNode *xml_action=NULL;
286
287         g_return_val_if_fail(captive_file_object!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
288         g_return_val_if_fail(file_info_captive!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
289         g_return_val_if_fail(captive_file_object->file_Handle==NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
290
291         if (captive_file_object->vfs->corba_bug_action) {
292                 xml_action=xmlNewTextChild(captive_file_object->vfs->corba_bug_action,NULL,"file_file_info_get",NULL);
293                 xmlNewProp(xml_action,"object",captive_printf_alloca("%p",captive_file_object));
294                 }
295
296         Captive_File_file_info_get(captive_file_object->corba_File_object,&file_info_corba,&captive_corba_ev);
297         r=captive_sandbox_parent_return_from_CORBA_Environment(&captive_corba_ev);
298         if (xml_action)
299                 xmlNewProp(xml_action,"result",gnome_vfs_result_to_string(r));
300         if (r!=GNOME_VFS_OK)
301                 return r;
302
303         r=captive_sandbox_file_info_corba_to_captive(file_info_captive,file_info_corba);
304         Captive_GnomeVFSFileInfo__freekids(file_info_corba,NULL/* 'd'; meaning? */);
305         CORBA_free(file_info_corba);
306         if (xml_action)
307                 xmlSetProp(xml_action,"result",gnome_vfs_result_to_string(r));
308         if (r!=GNOME_VFS_OK)
309                 return r;
310
311         return GNOME_VFS_OK;
312 }
313
314
315 GnomeVFSResult captive_sandbox_parent_file_file_info_set(CaptiveFileObject *captive_file_object,
316     const GnomeVFSFileInfo *file_info_captive,GnomeVFSSetFileInfoMask mask)
317 {
318 Captive_GnomeVFSFileInfo file_info_corba;
319 GnomeVFSResult r;
320 xmlNode *xml_action=NULL;
321
322         g_return_val_if_fail(captive_file_object!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
323         g_return_val_if_fail(file_info_captive!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
324         g_return_val_if_fail(captive_file_object->file_Handle==NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
325
326         if (captive_file_object->vfs->corba_bug_action) {
327                 xml_action=xmlNewTextChild(captive_file_object->vfs->corba_bug_action,NULL,"file_file_info_set",NULL);
328                 xmlNewProp(xml_action,"object",captive_printf_alloca("%p",captive_file_object));
329                 xmlNewProp(xml_action,"mask",captive_printf_alloca("%u",(unsigned)mask));
330                 }
331
332         if (GNOME_VFS_OK!=(r=captive_sandbox_file_info_captive_to_corba(&file_info_corba,file_info_captive)))
333                 return r;
334
335         Captive_File_file_info_set(captive_file_object->corba_File_object,&file_info_corba,mask,&captive_corba_ev);
336         r=captive_sandbox_parent_return_from_CORBA_Environment(&captive_corba_ev);
337         if (xml_action)
338                 xmlNewProp(xml_action,"result",gnome_vfs_result_to_string(r));
339         if (r!=GNOME_VFS_OK)
340                 return r;
341
342         Captive_GnomeVFSFileInfo__freekids(&file_info_corba,NULL/* 'd'; meaning? */);
343
344         return GNOME_VFS_OK;
345 }
346
347
348 GnomeVFSResult captive_sandbox_parent_file_truncate(CaptiveFileObject *captive_file_object,GnomeVFSFileSize file_size)
349 {
350 GnomeVFSResult r;
351 xmlNode *xml_action=NULL;
352
353         g_return_val_if_fail(captive_file_object!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
354         g_return_val_if_fail(captive_file_object->file_Handle==NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
355
356         if (captive_file_object->vfs->corba_bug_action) {
357                 xml_action=xmlNewTextChild(captive_file_object->vfs->corba_bug_action,NULL,"file_truncate",NULL);
358                 xmlNewProp(xml_action,"object",captive_printf_alloca("%p",captive_file_object));
359                 xmlNewProp(xml_action,"file_size",captive_printf_alloca("%" G_GUINT64_FORMAT,(guint64)file_size));
360                 }
361
362         Captive_File_truncate(captive_file_object->corba_File_object,file_size,&captive_corba_ev);
363         r=captive_sandbox_parent_return_from_CORBA_Environment(&captive_corba_ev);
364         if (xml_action)
365                 xmlNewProp(xml_action,"result",gnome_vfs_result_to_string(r));
366         if (r!=GNOME_VFS_OK)
367                 return r;
368
369         return GNOME_VFS_OK;
370 }
371
372
373 GnomeVFSResult captive_sandbox_parent_file_move
374                 (CaptiveFileObject *captive_file_object_old,const gchar *pathname_new,gboolean force_replace)
375 {
376 GnomeVFSResult r;
377 xmlNode *xml_action=NULL;
378
379         g_return_val_if_fail(captive_file_object_old!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
380         g_return_val_if_fail(pathname_new!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
381         g_return_val_if_fail(captive_file_object_old->file_Handle==NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
382
383         if (captive_file_object_old->vfs->corba_bug_action) {
384                 xml_action=xmlNewTextChild(captive_file_object_old->vfs->corba_bug_action,NULL,"file_truncate",NULL);
385                 xmlNewProp(xml_action,"object",captive_printf_alloca("%p",captive_file_object_old));
386                 xmlNewProp(xml_action,"pathname_new",pathname_new);
387                 xmlNewProp(xml_action,"force_replace",captive_printf_alloca("%u",(unsigned)force_replace));
388                 }
389
390         Captive_File_move(captive_file_object_old->corba_File_object,pathname_new,force_replace,&captive_corba_ev);
391         r=captive_sandbox_parent_return_from_CORBA_Environment(&captive_corba_ev);
392         if (xml_action)
393                 xmlNewProp(xml_action,"result",gnome_vfs_result_to_string(r));
394         if (r!=GNOME_VFS_OK)
395                 return r;
396
397         return GNOME_VFS_OK;
398 }