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