xref: /AOO41X/main/ucb/source/ucp/gvfs/gvfs_stream.cxx (revision 2f86921c33504fdff5a030df6c0b258927045abb)
1*2f86921cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2f86921cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2f86921cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2f86921cSAndrew Rist  * distributed with this work for additional information
6*2f86921cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2f86921cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2f86921cSAndrew Rist  * "License"); you may not use this file except in compliance
9*2f86921cSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*2f86921cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*2f86921cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2f86921cSAndrew Rist  * software distributed under the License is distributed on an
15*2f86921cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2f86921cSAndrew Rist  * KIND, either express or implied.  See the License for the
17*2f86921cSAndrew Rist  * specific language governing permissions and limitations
18*2f86921cSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*2f86921cSAndrew Rist  *************************************************************/
21*2f86921cSAndrew Rist 
22*2f86921cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_ucb.hxx"
26cdf0e10cSrcweir #include "gvfs_stream.hxx"
27cdf0e10cSrcweir #include <rtl/memory.h>
28cdf0e10cSrcweir #include <com/sun/star/ucb/InteractiveAugmentedIOException.hpp>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <libgnomevfs/gnome-vfs-ops.h>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir using namespace cppu;
33cdf0e10cSrcweir using namespace rtl;
34cdf0e10cSrcweir using namespace com::sun::star::io;
35cdf0e10cSrcweir using namespace com::sun::star::uno;
36cdf0e10cSrcweir using namespace com::sun::star::ucb;
37cdf0e10cSrcweir using namespace gvfs;
38cdf0e10cSrcweir 
Stream(GnomeVFSHandle * handle,const GnomeVFSFileInfo * aInfo)39cdf0e10cSrcweir Stream::Stream( GnomeVFSHandle         *handle,
40cdf0e10cSrcweir         const GnomeVFSFileInfo *aInfo ) :
41cdf0e10cSrcweir     m_eof (sal_False),
42cdf0e10cSrcweir     m_bInputStreamCalled( sal_False ),
43cdf0e10cSrcweir     m_bOutputStreamCalled( sal_False )
44cdf0e10cSrcweir {
45cdf0e10cSrcweir     m_handle = handle;
46cdf0e10cSrcweir     gnome_vfs_file_info_copy (&m_info, aInfo);
47cdf0e10cSrcweir }
48cdf0e10cSrcweir 
~Stream(void)49cdf0e10cSrcweir Stream::~Stream( void )
50cdf0e10cSrcweir {
51cdf0e10cSrcweir     if (m_handle) {
52cdf0e10cSrcweir         gnome_vfs_close (m_handle);
53cdf0e10cSrcweir         m_handle = NULL;
54cdf0e10cSrcweir     }
55cdf0e10cSrcweir }
56cdf0e10cSrcweir 
queryInterface(const Type & type)57cdf0e10cSrcweir Any Stream::queryInterface( const Type &type )
58cdf0e10cSrcweir     throw( RuntimeException )
59cdf0e10cSrcweir {
60cdf0e10cSrcweir     Any aRet = ::cppu::queryInterface
61cdf0e10cSrcweir         ( type,
62cdf0e10cSrcweir           static_cast< XStream * >( this ),
63cdf0e10cSrcweir           static_cast< XInputStream * >( this ),
64cdf0e10cSrcweir           static_cast< XOutputStream * >( this ),
65cdf0e10cSrcweir           static_cast< XSeekable * >( this ),
66cdf0e10cSrcweir           static_cast< XTruncate * >( this ) );
67cdf0e10cSrcweir 
68cdf0e10cSrcweir     return aRet.hasValue() ? aRet : OWeakObject::queryInterface( type );
69cdf0e10cSrcweir }
70cdf0e10cSrcweir 
71cdf0e10cSrcweir // -------------------------------------------------------------------
72cdf0e10cSrcweir //                            XStream
73cdf0e10cSrcweir // -------------------------------------------------------------------
74cdf0e10cSrcweir 
75cdf0e10cSrcweir com::sun::star::uno::Reference< com::sun::star::io::XInputStream > SAL_CALL
getInputStream()76cdf0e10cSrcweir Stream::getInputStream(  )
77cdf0e10cSrcweir     throw( com::sun::star::uno::RuntimeException )
78cdf0e10cSrcweir {
79cdf0e10cSrcweir     {
80cdf0e10cSrcweir         osl::MutexGuard aGuard( m_aMutex );
81cdf0e10cSrcweir         m_bInputStreamCalled = true;
82cdf0e10cSrcweir     }
83cdf0e10cSrcweir     return Reference< XInputStream >( this );
84cdf0e10cSrcweir }
85cdf0e10cSrcweir 
86cdf0e10cSrcweir com::sun::star::uno::Reference< com::sun::star::io::XOutputStream > SAL_CALL
getOutputStream()87cdf0e10cSrcweir Stream::getOutputStream(  )
88cdf0e10cSrcweir     throw( com::sun::star::uno::RuntimeException )
89cdf0e10cSrcweir {
90cdf0e10cSrcweir     {
91cdf0e10cSrcweir         osl::MutexGuard aGuard( m_aMutex );
92cdf0e10cSrcweir         m_bOutputStreamCalled = true;
93cdf0e10cSrcweir     }
94cdf0e10cSrcweir     return Reference< XOutputStream >( this );
95cdf0e10cSrcweir }
96cdf0e10cSrcweir 
97cdf0e10cSrcweir // -------------------------------------------------------------------
98cdf0e10cSrcweir //                            XInputStream
99cdf0e10cSrcweir // -------------------------------------------------------------------
100cdf0e10cSrcweir 
readBytes(Sequence<sal_Int8> & aData,sal_Int32 nBytesToRead)101cdf0e10cSrcweir sal_Int32 SAL_CALL Stream::readBytes(
102cdf0e10cSrcweir     Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
103cdf0e10cSrcweir         throw( NotConnectedException,
104cdf0e10cSrcweir                BufferSizeExceededException,
105cdf0e10cSrcweir                IOException,
106cdf0e10cSrcweir                RuntimeException )
107cdf0e10cSrcweir {
108cdf0e10cSrcweir     GnomeVFSResult   result;
109cdf0e10cSrcweir     GnomeVFSFileSize nBytesRead = 0;
110cdf0e10cSrcweir 
111cdf0e10cSrcweir     if( ! m_handle )
112cdf0e10cSrcweir         throw IOException();
113cdf0e10cSrcweir 
114cdf0e10cSrcweir     if( m_eof ) {
115cdf0e10cSrcweir         aData.realloc( 0 );
116cdf0e10cSrcweir         return 0;
117cdf0e10cSrcweir     }
118cdf0e10cSrcweir 
119cdf0e10cSrcweir     try {
120cdf0e10cSrcweir         aData.realloc( nBytesToRead );
121cdf0e10cSrcweir     } catch ( const Exception &e ) {
122cdf0e10cSrcweir         throw BufferSizeExceededException();
123cdf0e10cSrcweir     }
124cdf0e10cSrcweir 
125cdf0e10cSrcweir     do {
126cdf0e10cSrcweir         result = gnome_vfs_read( m_handle, aData.getArray(),
127cdf0e10cSrcweir                      nBytesToRead, &nBytesRead );
128cdf0e10cSrcweir     } while( result == GNOME_VFS_ERROR_INTERRUPTED );
129cdf0e10cSrcweir 
130cdf0e10cSrcweir     if (result != GNOME_VFS_OK &&
131cdf0e10cSrcweir         result != GNOME_VFS_ERROR_EOF)
132cdf0e10cSrcweir         throwOnError( result );
133cdf0e10cSrcweir 
134cdf0e10cSrcweir     if (result == GNOME_VFS_ERROR_EOF)
135cdf0e10cSrcweir         m_eof = sal_True;
136cdf0e10cSrcweir 
137cdf0e10cSrcweir     aData.realloc( sal::static_int_cast<sal_uInt32>(nBytesRead) );
138cdf0e10cSrcweir 
139cdf0e10cSrcweir     return sal::static_int_cast<sal_Int32>(nBytesRead);
140cdf0e10cSrcweir }
141cdf0e10cSrcweir 
readSomeBytes(Sequence<sal_Int8> & aData,sal_Int32 nMaxBytesToRead)142cdf0e10cSrcweir sal_Int32 SAL_CALL Stream::readSomeBytes(
143cdf0e10cSrcweir     Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
144cdf0e10cSrcweir             throw( NotConnectedException,
145cdf0e10cSrcweir                BufferSizeExceededException,
146cdf0e10cSrcweir                IOException,
147cdf0e10cSrcweir                RuntimeException )
148cdf0e10cSrcweir {
149cdf0e10cSrcweir     // Again - having 2 methods here just sucks; cf. filinpstr.cxx
150cdf0e10cSrcweir     // This can never be an effective non-blocking API - so why bother ?
151cdf0e10cSrcweir     return readBytes( aData, nMaxBytesToRead );
152cdf0e10cSrcweir }
153cdf0e10cSrcweir 
skipBytes(sal_Int32 nBytesToSkip)154cdf0e10cSrcweir void SAL_CALL Stream::skipBytes( sal_Int32 nBytesToSkip )
155cdf0e10cSrcweir         throw( NotConnectedException,
156cdf0e10cSrcweir                BufferSizeExceededException,
157cdf0e10cSrcweir                IOException,
158cdf0e10cSrcweir                RuntimeException )
159cdf0e10cSrcweir {
160cdf0e10cSrcweir     GnomeVFSResult result;
161cdf0e10cSrcweir 
162cdf0e10cSrcweir     if( ! m_handle )
163cdf0e10cSrcweir         throw IOException();
164cdf0e10cSrcweir 
165cdf0e10cSrcweir     result = gnome_vfs_seek( m_handle, GNOME_VFS_SEEK_CURRENT, nBytesToSkip );
166cdf0e10cSrcweir 
167cdf0e10cSrcweir     if ( result == GNOME_VFS_ERROR_BAD_PARAMETERS ||
168cdf0e10cSrcweir          result == GNOME_VFS_ERROR_NOT_SUPPORTED )
169cdf0e10cSrcweir         g_warning ("FIXME: just read them in ...");
170cdf0e10cSrcweir 
171cdf0e10cSrcweir     throwOnError( result );
172cdf0e10cSrcweir }
173cdf0e10cSrcweir 
available()174cdf0e10cSrcweir sal_Int32 SAL_CALL Stream::available(  )
175cdf0e10cSrcweir         throw( NotConnectedException,
176cdf0e10cSrcweir                IOException,
177cdf0e10cSrcweir                RuntimeException )
178cdf0e10cSrcweir {
179cdf0e10cSrcweir     return 0; // cf. filinpstr.cxx
180cdf0e10cSrcweir }
181cdf0e10cSrcweir 
closeInput(void)182cdf0e10cSrcweir void SAL_CALL Stream::closeInput( void )
183cdf0e10cSrcweir         throw( NotConnectedException,
184cdf0e10cSrcweir                    IOException,
185cdf0e10cSrcweir                    RuntimeException )
186cdf0e10cSrcweir {
187cdf0e10cSrcweir     osl::MutexGuard aGuard( m_aMutex );
188cdf0e10cSrcweir     m_bInputStreamCalled = false;
189cdf0e10cSrcweir 
190cdf0e10cSrcweir     if( ! m_bOutputStreamCalled )
191cdf0e10cSrcweir         closeStream();
192cdf0e10cSrcweir }
193cdf0e10cSrcweir 
194cdf0e10cSrcweir // -------------------------------------------------------------------
195cdf0e10cSrcweir //                            XSeekable
196cdf0e10cSrcweir // -------------------------------------------------------------------
197cdf0e10cSrcweir 
seek(sal_Int64 location)198cdf0e10cSrcweir void SAL_CALL Stream::seek( sal_Int64 location )
199cdf0e10cSrcweir         throw( ::com::sun::star::lang::IllegalArgumentException,
200cdf0e10cSrcweir                IOException,
201cdf0e10cSrcweir                RuntimeException )
202cdf0e10cSrcweir {
203cdf0e10cSrcweir     GnomeVFSResult result;
204cdf0e10cSrcweir 
205cdf0e10cSrcweir     if( ! m_handle )
206cdf0e10cSrcweir         throw IOException();
207cdf0e10cSrcweir 
208cdf0e10cSrcweir     if ( location < 0 )
209cdf0e10cSrcweir         throw ::com::sun::star::lang::IllegalArgumentException();
210cdf0e10cSrcweir 
211cdf0e10cSrcweir     m_eof = sal_False;
212cdf0e10cSrcweir     result = gnome_vfs_seek( m_handle, GNOME_VFS_SEEK_START, location );
213cdf0e10cSrcweir 
214cdf0e10cSrcweir     if (result == GNOME_VFS_ERROR_EOF)
215cdf0e10cSrcweir         throw ::com::sun::star::lang::IllegalArgumentException();
216cdf0e10cSrcweir 
217cdf0e10cSrcweir     throwOnError( result );
218cdf0e10cSrcweir }
219cdf0e10cSrcweir 
getPosition()220cdf0e10cSrcweir sal_Int64 SAL_CALL Stream::getPosition()
221cdf0e10cSrcweir         throw( IOException,
222cdf0e10cSrcweir                RuntimeException )
223cdf0e10cSrcweir {
224cdf0e10cSrcweir     GnomeVFSFileSize nBytesIn = 0;
225cdf0e10cSrcweir 
226cdf0e10cSrcweir     if( ! m_handle )
227cdf0e10cSrcweir         throw IOException();
228cdf0e10cSrcweir 
229cdf0e10cSrcweir     throwOnError( gnome_vfs_tell( m_handle, &nBytesIn ) );
230cdf0e10cSrcweir 
231cdf0e10cSrcweir     return nBytesIn;
232cdf0e10cSrcweir }
233cdf0e10cSrcweir 
getLength()234cdf0e10cSrcweir sal_Int64 SAL_CALL Stream::getLength()
235cdf0e10cSrcweir     throw( IOException, RuntimeException )
236cdf0e10cSrcweir {
237cdf0e10cSrcweir     // FIXME: so this sucks; it may be stale but ...
238cdf0e10cSrcweir     if (m_info.valid_fields & GNOME_VFS_FILE_INFO_FIELDS_SIZE)
239cdf0e10cSrcweir         return m_info.size;
240cdf0e10cSrcweir     else {
241cdf0e10cSrcweir         g_warning ("FIXME: No valid length");
242cdf0e10cSrcweir         return 0;
243cdf0e10cSrcweir     }
244cdf0e10cSrcweir }
245cdf0e10cSrcweir 
246cdf0e10cSrcweir // -------------------------------------------------------------------
247cdf0e10cSrcweir //                            XTruncate
248cdf0e10cSrcweir // -------------------------------------------------------------------
249cdf0e10cSrcweir 
truncate(void)250cdf0e10cSrcweir void SAL_CALL Stream::truncate( void )
251cdf0e10cSrcweir     throw( com::sun::star::io::IOException,
252cdf0e10cSrcweir            com::sun::star::uno::RuntimeException )
253cdf0e10cSrcweir {
254cdf0e10cSrcweir     if( ! m_handle )
255cdf0e10cSrcweir         throw IOException();
256cdf0e10cSrcweir 
257cdf0e10cSrcweir     throwOnError( gnome_vfs_truncate_handle( m_handle, 0 ) );
258cdf0e10cSrcweir }
259cdf0e10cSrcweir 
260cdf0e10cSrcweir // -------------------------------------------------------------------
261cdf0e10cSrcweir //                            XOutputStream
262cdf0e10cSrcweir // -------------------------------------------------------------------
263cdf0e10cSrcweir 
writeBytes(const com::sun::star::uno::Sequence<sal_Int8> & aData)264cdf0e10cSrcweir void SAL_CALL Stream::writeBytes( const com::sun::star::uno::Sequence< sal_Int8 >& aData )
265cdf0e10cSrcweir     throw( com::sun::star::io::NotConnectedException,
266cdf0e10cSrcweir            com::sun::star::io::BufferSizeExceededException,
267cdf0e10cSrcweir            com::sun::star::io::IOException,
268cdf0e10cSrcweir            com::sun::star::uno::RuntimeException)
269cdf0e10cSrcweir {
270cdf0e10cSrcweir     GnomeVFSResult   result = GNOME_VFS_OK;
271cdf0e10cSrcweir     GnomeVFSFileSize toWrite = aData.getLength();
272cdf0e10cSrcweir     const sal_Int8 *p = aData.getConstArray();
273cdf0e10cSrcweir 
274cdf0e10cSrcweir     if( ! m_handle )
275cdf0e10cSrcweir         throw IOException();
276cdf0e10cSrcweir 
277cdf0e10cSrcweir     while( toWrite > 0) {
278cdf0e10cSrcweir         GnomeVFSFileSize bytesWritten = 0;
279cdf0e10cSrcweir 
280cdf0e10cSrcweir         result = gnome_vfs_write( m_handle, p, toWrite, &bytesWritten );
281cdf0e10cSrcweir         if( result == GNOME_VFS_ERROR_INTERRUPTED )
282cdf0e10cSrcweir             continue;
283cdf0e10cSrcweir         throwOnError( result );
284cdf0e10cSrcweir         g_assert( bytesWritten <= toWrite );
285cdf0e10cSrcweir         toWrite -= bytesWritten;
286cdf0e10cSrcweir         p += bytesWritten;
287cdf0e10cSrcweir     }
288cdf0e10cSrcweir }
289cdf0e10cSrcweir 
flush(void)290cdf0e10cSrcweir void SAL_CALL Stream::flush( void )
291cdf0e10cSrcweir     throw( NotConnectedException, BufferSizeExceededException,
292cdf0e10cSrcweir            IOException, RuntimeException )
293cdf0e10cSrcweir {
294cdf0e10cSrcweir }
295cdf0e10cSrcweir 
closeOutput(void)296cdf0e10cSrcweir void SAL_CALL Stream::closeOutput( void )
297cdf0e10cSrcweir     throw( com::sun::star::io::NotConnectedException,
298cdf0e10cSrcweir            com::sun::star::io::IOException,
299cdf0e10cSrcweir            com::sun::star::uno::RuntimeException )
300cdf0e10cSrcweir {
301cdf0e10cSrcweir     osl::MutexGuard aGuard( m_aMutex );
302cdf0e10cSrcweir     m_bOutputStreamCalled = false;
303cdf0e10cSrcweir 
304cdf0e10cSrcweir     if( ! m_bInputStreamCalled )
305cdf0e10cSrcweir         closeStream();
306cdf0e10cSrcweir }
307cdf0e10cSrcweir 
308cdf0e10cSrcweir // -------------------------------------------------------------------
309cdf0e10cSrcweir //                            Misc.
310cdf0e10cSrcweir // -------------------------------------------------------------------
311cdf0e10cSrcweir 
closeStream(void)312cdf0e10cSrcweir void Stream::closeStream( void )
313cdf0e10cSrcweir     throw( ::com::sun::star::io::NotConnectedException,
314cdf0e10cSrcweir            ::com::sun::star::io::IOException,
315cdf0e10cSrcweir            ::com::sun::star::uno::RuntimeException )
316cdf0e10cSrcweir {
317cdf0e10cSrcweir     if (m_handle) {
318cdf0e10cSrcweir         gnome_vfs_close (m_handle);
319cdf0e10cSrcweir         m_handle = NULL;
320cdf0e10cSrcweir     } else
321cdf0e10cSrcweir         throw IOException();
322cdf0e10cSrcweir }
323cdf0e10cSrcweir 
throwOnError(GnomeVFSResult result)324cdf0e10cSrcweir void Stream::throwOnError( GnomeVFSResult result )
325cdf0e10cSrcweir     throw( NotConnectedException,
326cdf0e10cSrcweir            BufferSizeExceededException,
327cdf0e10cSrcweir            IOException,
328cdf0e10cSrcweir            RuntimeException )
329cdf0e10cSrcweir {
330cdf0e10cSrcweir     if( result != GNOME_VFS_OK ) {
331cdf0e10cSrcweir         ::rtl::OUString aMsg = ::rtl::OUString::createFromAscii
332cdf0e10cSrcweir               ( gnome_vfs_result_to_string( result ) );
333cdf0e10cSrcweir 
334cdf0e10cSrcweir         g_warning( "Input Stream exceptional result '%s' (%d)",
335cdf0e10cSrcweir                gnome_vfs_result_to_string( result ), result );
336cdf0e10cSrcweir 
337cdf0e10cSrcweir         throw IOException( aMsg, static_cast< cppu::OWeakObject * >( this ) );
338cdf0e10cSrcweir     }
339cdf0e10cSrcweir }
340