Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "com/sun/star/io/IOException.hpp"
21 : #include "com/sun/star/uno/RuntimeException.hpp"
22 : #include "osl/diagnose.h"
23 : #include "filstr.hxx"
24 : #include "shell.hxx"
25 : #include "prov.hxx"
26 : #include <boost/scoped_array.hpp>
27 :
28 : using namespace fileaccess;
29 : using namespace com::sun::star;
30 : using namespace com::sun::star::ucb;
31 :
32 : #if OSL_DEBUG_LEVEL > 0
33 : #define THROW_WHERE SAL_WHERE
34 : #else
35 : #define THROW_WHERE ""
36 : #endif
37 :
38 : /******************************************************************************/
39 : /* */
40 : /* XStream_impl implementation */
41 : /* */
42 : /******************************************************************************/
43 :
44 23980 : XStream_impl::XStream_impl( shell* pMyShell,const OUString& aUncPath, bool bLock )
45 : : m_bInputStreamCalled( false ),
46 : m_bOutputStreamCalled( false ),
47 : m_pMyShell( pMyShell ),
48 : m_xProvider( m_pMyShell->m_pProvider ),
49 : m_aFile( aUncPath ),
50 : m_nErrorCode( TASKHANDLER_NO_ERROR ),
51 23980 : m_nMinorErrorCode( TASKHANDLER_NO_ERROR )
52 : {
53 23980 : sal_uInt32 nFlags = ( osl_File_OpenFlag_Read | osl_File_OpenFlag_Write );
54 23980 : if ( !bLock )
55 0 : nFlags |= osl_File_OpenFlag_NoLock;
56 :
57 23980 : osl::FileBase::RC err = m_aFile.open( nFlags );
58 23980 : if( err != osl::FileBase::E_None )
59 : {
60 133 : m_nIsOpen = false;
61 133 : m_aFile.close();
62 :
63 133 : m_nErrorCode = TASKHANDLING_OPEN_FOR_STREAM;
64 133 : m_nMinorErrorCode = err;
65 : }
66 : else
67 23847 : m_nIsOpen = true;
68 23980 : }
69 :
70 :
71 71820 : XStream_impl::~XStream_impl()
72 : {
73 : try
74 : {
75 23940 : closeStream();
76 : }
77 0 : catch (const io::IOException&)
78 : {
79 : OSL_FAIL("unexpected situation");
80 : }
81 0 : catch (const uno::RuntimeException&)
82 : {
83 : OSL_FAIL("unexpected situation");
84 : }
85 47880 : }
86 :
87 :
88 :
89 :
90 :
91 :
92 :
93 :
94 : uno::Reference< io::XInputStream > SAL_CALL
95 35008 : XStream_impl::getInputStream( )
96 : throw( uno::RuntimeException, std::exception)
97 : {
98 : {
99 35008 : osl::MutexGuard aGuard( m_aMutex );
100 35008 : m_bInputStreamCalled = true;
101 : }
102 35008 : return uno::Reference< io::XInputStream >( this );
103 : }
104 :
105 :
106 : uno::Reference< io::XOutputStream > SAL_CALL
107 34868 : XStream_impl::getOutputStream( )
108 : throw( uno::RuntimeException, std::exception )
109 : {
110 : {
111 34868 : osl::MutexGuard aGuard( m_aMutex );
112 34868 : m_bOutputStreamCalled = true;
113 : }
114 34868 : return uno::Reference< io::XOutputStream >( this );
115 : }
116 :
117 :
118 127 : void SAL_CALL XStream_impl::truncate()
119 : throw( io::IOException, uno::RuntimeException, std::exception )
120 : {
121 127 : if (osl::FileBase::E_None != m_aFile.setSize(0))
122 0 : throw io::IOException( THROW_WHERE );
123 :
124 127 : if (osl::FileBase::E_None != m_aFile.setPos(osl_Pos_Absolut,sal_uInt64(0)))
125 0 : throw io::IOException( THROW_WHERE );
126 127 : }
127 :
128 :
129 :
130 :
131 : // XStream_impl private non interface methods
132 :
133 :
134 : sal_Int32 SAL_CALL
135 97216 : XStream_impl::readBytes(
136 : uno::Sequence< sal_Int8 >& aData,
137 : sal_Int32 nBytesToRead )
138 : throw( io::NotConnectedException,
139 : io::BufferSizeExceededException,
140 : io::IOException,
141 : uno::RuntimeException, std::exception)
142 : {
143 97216 : if( ! m_nIsOpen )
144 0 : throw io::IOException( THROW_WHERE );
145 :
146 97216 : boost::scoped_array<sal_Int8> buffer;
147 : try
148 : {
149 97216 : buffer.reset(new sal_Int8[nBytesToRead]);
150 : }
151 0 : catch (const std::bad_alloc&)
152 : {
153 0 : if( m_nIsOpen ) m_aFile.close();
154 0 : throw io::BufferSizeExceededException( THROW_WHERE );
155 : }
156 :
157 97216 : sal_uInt64 nrc(0);
158 97216 : if(m_aFile.read( buffer.get(),sal_uInt64(nBytesToRead),nrc )
159 : != osl::FileBase::E_None)
160 : {
161 0 : throw io::IOException( THROW_WHERE );
162 : }
163 97216 : aData = uno::Sequence< sal_Int8 > ( buffer.get(), (sal_uInt32)nrc );
164 97216 : return ( sal_Int32 ) nrc;
165 : }
166 :
167 :
168 : sal_Int32 SAL_CALL
169 159 : XStream_impl::readSomeBytes(
170 : uno::Sequence< sal_Int8 >& aData,
171 : sal_Int32 nMaxBytesToRead )
172 : throw( io::NotConnectedException,
173 : io::BufferSizeExceededException,
174 : io::IOException,
175 : uno::RuntimeException, std::exception)
176 : {
177 159 : return readBytes( aData,nMaxBytesToRead );
178 : }
179 :
180 :
181 : void SAL_CALL
182 53 : XStream_impl::skipBytes(
183 : sal_Int32 nBytesToSkip )
184 : throw( io::NotConnectedException,
185 : io::BufferSizeExceededException,
186 : io::IOException,
187 : uno::RuntimeException, std::exception )
188 : {
189 53 : m_aFile.setPos( osl_Pos_Current, sal_uInt64( nBytesToSkip ) );
190 53 : }
191 :
192 :
193 : sal_Int32 SAL_CALL
194 0 : XStream_impl::available(
195 : void )
196 : throw( io::NotConnectedException,
197 : io::IOException,
198 : uno::RuntimeException, std::exception)
199 : {
200 0 : return 0;
201 : }
202 :
203 :
204 : void SAL_CALL
205 113139 : XStream_impl::writeBytes( const uno::Sequence< sal_Int8 >& aData )
206 : throw( io::NotConnectedException,
207 : io::BufferSizeExceededException,
208 : io::IOException,
209 : uno::RuntimeException, std::exception)
210 : {
211 113139 : sal_uInt32 length = aData.getLength();
212 113139 : if(length)
213 : {
214 113041 : sal_uInt64 nWrittenBytes(0);
215 113041 : const sal_Int8* p = aData.getConstArray();
216 226082 : if(osl::FileBase::E_None != m_aFile.write((static_cast<void const *>(p)),sal_uInt64(length),nWrittenBytes) ||
217 113041 : nWrittenBytes != length )
218 0 : throw io::IOException( THROW_WHERE );
219 : }
220 113139 : }
221 :
222 :
223 : void SAL_CALL
224 27831 : XStream_impl::closeStream(
225 : void )
226 : throw( io::NotConnectedException,
227 : io::IOException,
228 : uno::RuntimeException )
229 : {
230 27831 : if( m_nIsOpen )
231 : {
232 23807 : osl::FileBase::RC err = m_aFile.close();
233 :
234 23807 : if( err != osl::FileBase::E_None ) {
235 0 : io::IOException ex;
236 0 : ex.Message = "could not close file";
237 0 : throw ex;
238 : }
239 :
240 23807 : m_nIsOpen = false;
241 : }
242 27831 : }
243 :
244 : void SAL_CALL
245 2906 : XStream_impl::closeInput(
246 : void )
247 : throw( io::NotConnectedException,
248 : io::IOException,
249 : uno::RuntimeException, std::exception )
250 : {
251 2906 : osl::MutexGuard aGuard( m_aMutex );
252 2906 : m_bInputStreamCalled = false;
253 :
254 2906 : if( ! m_bOutputStreamCalled )
255 251 : closeStream();
256 2906 : }
257 :
258 :
259 : void SAL_CALL
260 3891 : XStream_impl::closeOutput(
261 : void )
262 : throw( io::NotConnectedException,
263 : io::IOException,
264 : uno::RuntimeException, std::exception )
265 : {
266 3891 : osl::MutexGuard aGuard( m_aMutex );
267 3891 : m_bOutputStreamCalled = false;
268 :
269 3891 : if( ! m_bInputStreamCalled )
270 3640 : closeStream();
271 3891 : }
272 :
273 :
274 : void SAL_CALL
275 94537 : XStream_impl::seek(
276 : sal_Int64 location )
277 : throw( lang::IllegalArgumentException,
278 : io::IOException,
279 : uno::RuntimeException, std::exception )
280 : {
281 94537 : if( location < 0 )
282 0 : throw lang::IllegalArgumentException( THROW_WHERE, uno::Reference< uno::XInterface >(), 0 );
283 94537 : if( osl::FileBase::E_None != m_aFile.setPos( osl_Pos_Absolut, sal_uInt64( location ) ) )
284 0 : throw io::IOException( THROW_WHERE );
285 94537 : }
286 :
287 :
288 : sal_Int64 SAL_CALL
289 14043 : XStream_impl::getPosition(
290 : void )
291 : throw( io::IOException,
292 : uno::RuntimeException, std::exception )
293 : {
294 : sal_uInt64 uPos;
295 14043 : if( osl::FileBase::E_None != m_aFile.getPos( uPos ) )
296 0 : throw io::IOException( THROW_WHERE );
297 14043 : return sal_Int64( uPos );
298 : }
299 :
300 : sal_Int64 SAL_CALL
301 67938 : XStream_impl::getLength(
302 : void )
303 : throw( io::IOException,
304 : uno::RuntimeException, std::exception )
305 : {
306 : sal_uInt64 uEndPos;
307 67938 : if ( m_aFile.getSize(uEndPos) != osl::FileBase::E_None )
308 0 : throw io::IOException( THROW_WHERE );
309 : else
310 67938 : return sal_Int64( uEndPos );
311 : }
312 :
313 : void SAL_CALL
314 25855 : XStream_impl::flush()
315 : throw( io::NotConnectedException,
316 : io::BufferSizeExceededException,
317 : io::IOException,
318 : uno::RuntimeException, std::exception )
319 25855 : {}
320 :
321 234 : void XStream_impl::waitForCompletion()
322 : throw (io::IOException, uno::RuntimeException, std::exception)
323 : {
324 : // At least on UNIX, to reliably learn about any errors encountered by
325 : // asynchronous NFS write operations, without closing the file directly
326 : // afterwards, there appears to be no cheaper way than to call fsync:
327 234 : if (m_nIsOpen && m_aFile.sync() != osl::FileBase::E_None) {
328 : throw io::IOException(
329 : OUString( "could not synchronize file to disc"),
330 0 : static_cast< OWeakObject * >(this));
331 : }
332 234 : }
333 :
334 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|