Fixed 'GIOChanel' buffers signedness gcc(1) warnings.
[captive.git] / src / libcaptive / sandbox / server-CaptiveIOChannel.c
1 /* $Id$
2  * CORBA/ORBit client side of image GIOChannel object of 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-CaptiveIOChannel.h"    /* self */
23 #include "sandbox.h"
24 #include <glib/gmessages.h>
25 #include "captive/macros.h"
26 #include "split.h"
27 #include <libgnomevfs/gnome-vfs-file-size.h>
28 #include "captive/storage.h"
29
30
31 static void impl_Captive_CaptiveIOChannel_fini(impl_POA_Captive_CaptiveIOChannel *servant,CORBA_Environment *ev);
32 static void impl_Captive_CaptiveIOChannel_read(impl_POA_Captive_CaptiveIOChannel *servant,
33                 Captive_Bytes **buffer_corba_return,const Captive_GSize count,CORBA_Environment *ev);
34 static void impl_Captive_CaptiveIOChannel_write(impl_POA_Captive_CaptiveIOChannel *servant,
35                 const Captive_Bytes *buffer,Captive_GSize *bytes_written_return,CORBA_Environment *ev);
36 static void impl_Captive_CaptiveIOChannel_seek
37                 (impl_POA_Captive_CaptiveIOChannel *servant,const Captive_GInt64 offset,const Captive_GSeekType type,CORBA_Environment *ev);
38 static Captive_GUInt64 impl_Captive_CaptiveIOChannel_size(impl_POA_Captive_CaptiveIOChannel *servant,CORBA_Environment *ev);
39
40
41 static PortableServer_ServantBase__epv impl_Captive_CaptiveIOChannel_base_epv={
42         NULL,   /* _private data */
43         (gpointer)&impl_Captive_CaptiveIOChannel_fini,  /* finalize routine */
44         NULL,   /* default_POA routine */
45         };
46 static POA_Captive_CaptiveIOChannel__epv impl_Captive_CaptiveIOChannel_epv={
47         NULL,   /* _private */
48         (gpointer)&impl_Captive_CaptiveIOChannel_read,
49         (gpointer)&impl_Captive_CaptiveIOChannel_write,
50         (gpointer)&impl_Captive_CaptiveIOChannel_seek,
51         (gpointer)&impl_Captive_CaptiveIOChannel_size,
52         };
53 static POA_Captive_CaptiveIOChannel__vepv impl_Captive_CaptiveIOChannel_vepv={
54         &impl_Captive_CaptiveIOChannel_base_epv,
55         &impl_Captive_CaptiveIOChannel_epv,
56         };
57
58
59 Captive_CaptiveIOChannel impl_Captive_CaptiveIOChannel__create
60                 (PortableServer_POA poa,GIOChannel *image_iochannel,CORBA_Environment *ev)
61 {
62 Captive_CaptiveIOChannel retval;
63 impl_POA_Captive_CaptiveIOChannel *newservant;
64 PortableServer_ObjectId *objid;
65
66         captive_new0(newservant);       /* FIXME: leak */
67         newservant->servant.vepv=&impl_Captive_CaptiveIOChannel_vepv;
68         newservant->poa=poa;
69
70         newservant->image_iochannel=image_iochannel;
71
72         /* captive_giochannel_size() only _after_ g_io_channel_set_encoding() ! */
73         newservant->image_size=captive_giochannel_size(newservant->image_iochannel);
74         g_return_val_if_fail(newservant->image_size>0,NULL);
75
76         POA_Captive_CaptiveIOChannel__init((PortableServer_Servant)newservant,ev);
77         objid=PortableServer_POA_activate_object(poa,newservant,ev);
78         CORBA_free(objid);
79         retval=PortableServer_POA_servant_to_reference(poa,newservant,ev);
80
81         return retval;
82 }
83
84
85 static void impl_Captive_CaptiveIOChannel_fini(impl_POA_Captive_CaptiveIOChannel *servant,CORBA_Environment *ev)
86 {
87         /* FIXME: Any flushes? */
88 }
89
90
91 void impl_Captive_CaptiveIOChannel__destroy(impl_POA_Captive_CaptiveIOChannel *servant,CORBA_Environment *ev)
92 {
93 PortableServer_ObjectId *objid;
94
95         objid=PortableServer_POA_servant_to_id(servant->poa,servant,ev);
96         PortableServer_POA_deactivate_object(servant->poa,objid,ev);
97         CORBA_free(objid);
98         impl_Captive_CaptiveIOChannel_fini(servant,ev);
99         g_free(servant);
100 }
101
102
103 static void impl_Captive_CaptiveIOChannel_read(impl_POA_Captive_CaptiveIOChannel *servant,
104                 Captive_Bytes **buffer_corba_return,const Captive_GSize count,CORBA_Environment *ev)
105 {
106 Captive_Bytes *buffer_corba;
107 gsize bytes_read;
108 GIOStatus erriostatus;
109
110         buffer_corba=Captive_Bytes__alloc();
111         buffer_corba->_maximum=count;
112         buffer_corba->_length=0;
113         buffer_corba->_buffer=Captive_Bytes_allocbuf(buffer_corba->_maximum);
114         buffer_corba->_release=TRUE;
115
116         erriostatus=g_io_channel_read_chars(servant->image_iochannel,
117                         (gchar *)buffer_corba->_buffer, /* buf */
118                         count,  /* count */
119                         &bytes_read,    /* bytes_read */
120                         NULL);  /* error */
121         if (erriostatus==G_IO_STATUS_EOF) {
122                 g_assert(bytes_read==0);
123                 bytes_read=0;
124                 erriostatus=G_IO_STATUS_NORMAL;
125                 }
126         if (erriostatus!=G_IO_STATUS_NORMAL) {
127                 captive_sandbox_child_GnomeVFSResultException_throw(ev,GNOME_VFS_ERROR_IO);
128                 Captive_Bytes__freekids(buffer_corba,NULL/* 'd'; meaning? */);
129                 CORBA_free(buffer_corba);
130                 return;
131                 }
132
133         buffer_corba->_length=bytes_read;
134         *buffer_corba_return=buffer_corba;
135 }
136
137
138 static void impl_Captive_CaptiveIOChannel_write(impl_POA_Captive_CaptiveIOChannel *servant,
139                 const Captive_Bytes *buffer_corba,Captive_GSize *bytes_written_return,CORBA_Environment *ev)
140 {
141 gsize bytes_written;
142
143         if (G_IO_STATUS_NORMAL!=g_io_channel_write_chars(servant->image_iochannel,
144                         (const gchar *)buffer_corba->_buffer,   /* buf */
145                         buffer_corba->_length,  /* count */
146                         &bytes_written, /* bytes_written */
147                         NULL)   /* error */
148                                         || bytes_written!=buffer_corba->_length) {
149                 captive_sandbox_child_GnomeVFSResultException_throw(ev,GNOME_VFS_ERROR_IO);
150                 return;
151                 }
152
153         *bytes_written_return=bytes_written;
154 }
155
156
157 static void impl_Captive_CaptiveIOChannel_seek
158                 (impl_POA_Captive_CaptiveIOChannel *servant,const Captive_GInt64 offset,const Captive_GSeekType type,CORBA_Environment *ev)
159 {
160         if (G_IO_STATUS_NORMAL!=g_io_channel_seek_position(servant->image_iochannel,
161                         offset, /* offset */
162                         type,   /* type */
163                         NULL)) {        /* error */
164                 captive_sandbox_child_GnomeVFSResultException_throw(ev,GNOME_VFS_ERROR_IO);
165                 return;
166                 }
167 }
168
169
170 static Captive_GUInt64 impl_Captive_CaptiveIOChannel_size(impl_POA_Captive_CaptiveIOChannel *servant,CORBA_Environment *ev)
171 {
172         return servant->image_size;
173 }