Finished and deployed CORBA sandbox separation
[captive.git] / src / libcaptive / sandbox / server-File.c
1 /* $Id$
2  * CORBA/ORBit server side of File object, ran by sandbox_child()
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 "server-File.h"        /* self */
23 #include "sandbox.h"
24 #include <glib/gmessages.h>
25 #include "captive/macros.h"
26 #include "split.h"
27 #include "FileInfo.h"
28
29
30 static void impl_Captive_File_fini(impl_POA_Captive_File *servant,CORBA_Environment *ev);
31 static void impl_Captive_File_read(impl_POA_Captive_File *servant,Captive_Bytes **buffer,
32                 const Captive_GnomeVFSFileSize num_bytes,CORBA_Environment *ev);
33 static void impl_Captive_File_write(impl_POA_Captive_File *servant,
34                 const Captive_Bytes *buffer,Captive_GnomeVFSFileSize *bytes_written_return,CORBA_Environment *ev);
35 static void impl_Captive_File_seek(impl_POA_Captive_File *servant,
36                 const Captive_GnomeVFSSeekPosition whence,const Captive_GnomeVFSFileOffset offset,CORBA_Environment *ev);
37 static void impl_Captive_File_tell
38                 (impl_POA_Captive_File *servant,Captive_GnomeVFSFileOffset *offset_return,CORBA_Environment *ev);
39 static void impl_Captive_File_remove(impl_POA_Captive_File *servant,CORBA_Environment *ev);
40 static void impl_Captive_File_file_info_get
41                 (impl_POA_Captive_File *servant,Captive_GnomeVFSFileInfo **file_info,CORBA_Environment *ev);
42 static void impl_Captive_File_file_info_set(impl_POA_Captive_File *servant,
43                 const Captive_GnomeVFSFileInfo *file_info,const Captive_GnomeVFSSetFileInfoMask mask,CORBA_Environment *ev);
44 static void impl_Captive_File_truncate
45                 (impl_POA_Captive_File *servant,const Captive_GnomeVFSFileSize file_size,CORBA_Environment *ev);
46 static void impl_Captive_File_move
47                 (impl_POA_Captive_File *servant,const CORBA_char *pathname_new,const CORBA_boolean force_replace,CORBA_Environment *ev);
48
49
50 static PortableServer_ServantBase__epv impl_Captive_File_base_epv={
51         NULL,   /* _private data */
52         (gpointer)&impl_Captive_File_fini,      /* finalize routine */
53         NULL,   /* default_POA routine */
54         };
55 static POA_Captive_File__epv impl_Captive_File_epv={
56         NULL,   /* _private */
57         (gpointer)&impl_Captive_File_read,
58         (gpointer)&impl_Captive_File_write,
59         (gpointer)&impl_Captive_File_seek,
60         (gpointer)&impl_Captive_File_tell,
61         (gpointer)&impl_Captive_File_remove,
62         (gpointer)&impl_Captive_File_file_info_get,
63         (gpointer)&impl_Captive_File_file_info_set,
64         (gpointer)&impl_Captive_File_truncate,
65         (gpointer)&impl_Captive_File_move,
66         };
67 static POA_Captive_File__vepv impl_Captive_File_vepv={
68         &impl_Captive_File_base_epv,
69         &impl_Captive_File_epv,
70         };
71
72
73 Captive_File impl_Captive_File__create(PortableServer_POA poa,CORBA_Environment *ev)
74 {
75 Captive_File retval;
76 impl_POA_Captive_File *newservant;
77 PortableServer_ObjectId *objid;
78
79         captive_new0(newservant);       /* FIXME: leak */
80         newservant->servant.vepv=&impl_Captive_File_vepv;
81         newservant->poa=poa;
82         newservant->captive_file_object=NULL;
83         POA_Captive_File__init((PortableServer_Servant)newservant,ev);
84         objid=PortableServer_POA_activate_object(poa,newservant,ev);
85         CORBA_free(objid);
86         retval=PortableServer_POA_servant_to_reference(poa,newservant,ev);
87
88         return retval;
89 }
90
91
92 static void impl_Captive_File_fini(impl_POA_Captive_File *servant,CORBA_Environment *ev)
93 {
94         if (servant->captive_file_object) {
95                 g_object_unref(servant->captive_file_object);
96                 servant->captive_file_object=NULL;
97                 }
98 }
99
100
101 static void impl_Captive_File__destroy(impl_POA_Captive_File *servant,CORBA_Environment *ev)
102 {
103 PortableServer_ObjectId *objid;
104
105         objid=PortableServer_POA_servant_to_id(servant->poa,servant,ev);
106         PortableServer_POA_deactivate_object(servant->poa,objid,ev);
107         CORBA_free(objid);
108         impl_Captive_File_fini(servant,ev);
109         g_free(servant);
110 }
111
112
113 Captive_File impl_Captive_Vfs_file_new_open
114                 (impl_POA_Captive_Vfs *servant,const CORBA_char *pathname,const Captive_GnomeVFSOpenMode mode,CORBA_Environment *ev)
115 {
116 Captive_File retval;
117 impl_POA_Captive_File *retval_servant;
118 GnomeVFSResult errvfsresult;
119
120         retval=impl_Captive_File__create(servant->poa,ev);
121         if (ev->_major!=CORBA_NO_EXCEPTION)
122                 return NULL;
123
124         retval_servant=PortableServer_POA_reference_to_servant(servant->poa,retval,ev);
125         if (ev->_major!=CORBA_NO_EXCEPTION)
126                 return NULL;    /* 'retval' leak */
127
128         if (GNOME_VFS_OK!=(errvfsresult=captive_file_new_open(&retval_servant->captive_file_object,
129                         servant->captive_vfs_object,pathname,mode))) {
130                 impl_Captive_File__destroy(retval_servant,ev);
131                 captive_sandbox_child_GnomeVFSResultException_throw(ev,errvfsresult);
132                 return NULL;
133                 }
134
135         return retval;
136 }
137
138
139 Captive_File impl_Captive_Vfs_file_new_create(impl_POA_Captive_Vfs *servant,const CORBA_char *pathname,
140                 const Captive_GnomeVFSOpenMode mode,const CORBA_boolean exclusive,const CORBA_unsigned_long perm,CORBA_Environment *ev)
141 {
142 Captive_File retval;
143 impl_POA_Captive_File *retval_servant;
144 GnomeVFSResult errvfsresult;
145
146         retval=impl_Captive_File__create(servant->poa,ev);
147         if (ev->_major!=CORBA_NO_EXCEPTION)
148                 return NULL;
149
150         retval_servant=PortableServer_POA_reference_to_servant(servant->poa,retval,ev);
151         if (ev->_major!=CORBA_NO_EXCEPTION)
152                 return NULL;    /* 'retval' leak */
153
154         if (GNOME_VFS_OK!=(errvfsresult=captive_file_new_create(&retval_servant->captive_file_object,
155                         servant->captive_vfs_object,pathname,mode,exclusive,perm))) {
156                 impl_Captive_File__destroy(retval_servant,ev);
157                 captive_sandbox_child_GnomeVFSResultException_throw(ev,errvfsresult);
158                 return NULL;
159                 }
160
161         return retval;
162 }
163
164
165 static void impl_Captive_File_read(impl_POA_Captive_File *servant,Captive_Bytes **buffer_corba_return,
166                 const Captive_GnomeVFSFileSize num_bytes,CORBA_Environment *ev)
167 {
168 Captive_Bytes *buffer_corba;
169 GnomeVFSFileSize bytes_read;
170 GnomeVFSResult errvfsresult;
171
172         buffer_corba=Captive_Bytes__alloc();
173         buffer_corba->_maximum=num_bytes;
174         buffer_corba->_length=0;
175         buffer_corba->_buffer=Captive_Bytes_allocbuf(buffer_corba->_maximum);
176         buffer_corba->_release=TRUE;
177
178         if (GNOME_VFS_OK!=(errvfsresult=captive_file_read(servant->captive_file_object,
179                         buffer_corba->_buffer,num_bytes,&bytes_read))) {
180                 captive_sandbox_child_GnomeVFSResultException_throw(ev,errvfsresult);
181                 Captive_Bytes__freekids(buffer_corba,NULL/* 'd'; meaning? */);
182                 CORBA_free(buffer_corba);
183                 return;
184                 }
185
186         buffer_corba->_length=bytes_read;
187         *buffer_corba_return=buffer_corba;
188 }
189
190
191 static void impl_Captive_File_write(impl_POA_Captive_File *servant,
192                 const Captive_Bytes *buffer_corba,Captive_GnomeVFSFileSize *bytes_written_return,CORBA_Environment *ev)
193 {
194 GnomeVFSFileSize bytes_written;
195 GnomeVFSResult errvfsresult;
196
197         if (GNOME_VFS_OK!=(errvfsresult=captive_file_write(servant->captive_file_object,
198                         buffer_corba->_buffer,buffer_corba->_length,&bytes_written))) {
199                 captive_sandbox_child_GnomeVFSResultException_throw(ev,errvfsresult);
200                 return;
201                 }
202
203         *bytes_written_return=bytes_written;
204 }
205
206
207 static void impl_Captive_File_seek(impl_POA_Captive_File *servant,
208                 const Captive_GnomeVFSSeekPosition whence,const Captive_GnomeVFSFileOffset offset,CORBA_Environment *ev)
209 {
210 GnomeVFSResult errvfsresult;
211
212         if (GNOME_VFS_OK!=(errvfsresult=captive_file_seek(servant->captive_file_object,whence,offset))) {
213                 captive_sandbox_child_GnomeVFSResultException_throw(ev,errvfsresult);
214                 return;
215                 }
216 }
217
218
219 static void impl_Captive_File_tell
220                 (impl_POA_Captive_File *servant,Captive_GnomeVFSFileOffset *offset_return,CORBA_Environment *ev)
221 {
222 GnomeVFSFileOffset offset_captive;
223 GnomeVFSResult errvfsresult;
224
225         if (GNOME_VFS_OK!=(errvfsresult=captive_file_tell(servant->captive_file_object,&offset_captive))) {
226                 captive_sandbox_child_GnomeVFSResultException_throw(ev,errvfsresult);
227                 return;
228                 }
229         
230         *offset_return=offset_captive;
231 }
232
233
234 static void impl_Captive_File_remove(impl_POA_Captive_File *servant,CORBA_Environment *ev)
235 {
236 GnomeVFSResult errvfsresult;
237
238         if (GNOME_VFS_OK!=(errvfsresult=captive_file_remove(servant->captive_file_object))) {
239                 captive_sandbox_child_GnomeVFSResultException_throw(ev,errvfsresult);
240                 return;
241                 }
242 }
243
244
245 static void impl_Captive_File_file_info_get
246                 (impl_POA_Captive_File *servant,Captive_GnomeVFSFileInfo **file_info_corba_return,CORBA_Environment *ev)
247 {
248 GnomeVFSFileInfo file_info_captive;
249 GnomeVFSResult errvfsresult;
250 Captive_GnomeVFSFileInfo *file_info_corba;
251
252         if (GNOME_VFS_OK!=(errvfsresult=captive_file_file_info_get(servant->captive_file_object,&file_info_captive))) {
253                 captive_sandbox_child_GnomeVFSResultException_throw(ev,errvfsresult);
254                 return;
255                 }
256
257         file_info_corba=Captive_GnomeVFSFileInfo__alloc();
258
259         if (GNOME_VFS_OK!=(errvfsresult=captive_sandbox_file_info_captive_to_corba(
260                         file_info_corba,&file_info_captive))) {
261                 captive_sandbox_child_GnomeVFSResultException_throw(ev,errvfsresult);
262                 Captive_GnomeVFSFileInfo__freekids(file_info_corba,NULL/* 'd'; meaning? */);
263                 CORBA_free(file_info_corba);
264                 return;
265                 }
266
267         *file_info_corba_return=file_info_corba;
268 }
269
270
271 static void impl_Captive_File_file_info_set(impl_POA_Captive_File *servant,
272                 const Captive_GnomeVFSFileInfo *file_info_corba,const Captive_GnomeVFSSetFileInfoMask mask,CORBA_Environment *ev)
273 {
274 GnomeVFSFileInfo file_info_captive;
275 GnomeVFSResult errvfsresult;
276
277         if (GNOME_VFS_OK!=(errvfsresult=captive_sandbox_file_info_corba_to_captive(
278                         &file_info_captive,file_info_corba))) {
279                 captive_sandbox_child_GnomeVFSResultException_throw(ev,errvfsresult);
280                 return;
281                 }
282
283         if (GNOME_VFS_OK!=(errvfsresult=captive_file_file_info_set(servant->captive_file_object,&file_info_captive,mask))) {
284                 captive_sandbox_child_GnomeVFSResultException_throw(ev,errvfsresult);
285                 return;
286                 }
287 }
288
289
290 static void impl_Captive_File_truncate
291                 (impl_POA_Captive_File *servant,const Captive_GnomeVFSFileSize file_size,CORBA_Environment *ev)
292 {
293 GnomeVFSResult errvfsresult;
294
295         if (GNOME_VFS_OK!=(errvfsresult=captive_file_truncate(servant->captive_file_object,file_size))) {
296                 captive_sandbox_child_GnomeVFSResultException_throw(ev,errvfsresult);
297                 return;
298                 }
299 }
300
301
302 static void impl_Captive_File_move
303                 (impl_POA_Captive_File *servant,const CORBA_char *pathname_new,const CORBA_boolean force_replace,CORBA_Environment *ev)
304 {
305 GnomeVFSResult errvfsresult;
306
307         if (GNOME_VFS_OK!=(errvfsresult=captive_file_move(servant->captive_file_object,pathname_new,force_replace))) {
308                 captive_sandbox_child_GnomeVFSResultException_throw(ev,errvfsresult);
309                 return;
310                 }
311 }