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 :
27 : using namespace fileaccess;
28 : using namespace com::sun::star;
29 : using namespace com::sun::star::ucb;
30 :
31 : #if OSL_DEBUG_LEVEL > 0
32 : #define THROW_WHERE SAL_WHERE
33 : #else
34 : #define THROW_WHERE ""
35 : #endif
36 :
37 : /******************************************************************************/
38 : /* */
39 : /* XStream_impl implementation */
40 : /* */
41 : /******************************************************************************/
42 :
43 0 : XStream_impl::XStream_impl( shell* pMyShell,const OUString& aUncPath, sal_Bool bLock )
44 : : m_bInputStreamCalled( false ),
45 : m_bOutputStreamCalled( false ),
46 : m_pMyShell( pMyShell ),
47 : m_xProvider( m_pMyShell->m_pProvider ),
48 : m_aFile( aUncPath ),
49 : m_nErrorCode( TASKHANDLER_NO_ERROR ),
50 0 : m_nMinorErrorCode( TASKHANDLER_NO_ERROR )
51 : {
52 0 : sal_uInt32 nFlags = ( osl_File_OpenFlag_Read | osl_File_OpenFlag_Write );
53 0 : if ( !bLock )
54 0 : nFlags |= osl_File_OpenFlag_NoLock;
55 :
56 0 : osl::FileBase::RC err = m_aFile.open( nFlags );
57 0 : if( err != osl::FileBase::E_None )
58 : {
59 0 : m_nIsOpen = false;
60 0 : m_aFile.close();
61 :
62 0 : m_nErrorCode = TASKHANDLING_OPEN_FOR_STREAM;
63 0 : m_nMinorErrorCode = err;
64 : }
65 : else
66 0 : m_nIsOpen = true;
67 0 : }
68 :
69 :
70 0 : XStream_impl::~XStream_impl()
71 : {
72 : try
73 : {
74 0 : closeStream();
75 : }
76 0 : catch (const io::IOException&)
77 : {
78 : OSL_FAIL("unexpected situation");
79 : }
80 0 : catch (const uno::RuntimeException&)
81 : {
82 : OSL_FAIL("unexpected situation");
83 : }
84 0 : }
85 :
86 :
87 0 : sal_Int32 SAL_CALL XStream_impl::CtorSuccess()
88 : {
89 0 : return m_nErrorCode;
90 : }
91 :
92 :
93 :
94 0 : sal_Int32 SAL_CALL XStream_impl::getMinorError()
95 : {
96 0 : return m_nMinorErrorCode;
97 : }
98 :
99 :
100 :
101 : uno::Reference< io::XInputStream > SAL_CALL
102 0 : XStream_impl::getInputStream( )
103 : throw( uno::RuntimeException, std::exception)
104 : {
105 : {
106 0 : osl::MutexGuard aGuard( m_aMutex );
107 0 : m_bInputStreamCalled = true;
108 : }
109 0 : return uno::Reference< io::XInputStream >( this );
110 : }
111 :
112 :
113 : uno::Reference< io::XOutputStream > SAL_CALL
114 0 : XStream_impl::getOutputStream( )
115 : throw( uno::RuntimeException, std::exception )
116 : {
117 : {
118 0 : osl::MutexGuard aGuard( m_aMutex );
119 0 : m_bOutputStreamCalled = true;
120 : }
121 0 : return uno::Reference< io::XOutputStream >( this );
122 : }
123 :
124 :
125 0 : void SAL_CALL XStream_impl::truncate(void)
126 : throw( io::IOException, uno::RuntimeException, std::exception )
127 : {
128 0 : if (osl::FileBase::E_None != m_aFile.setSize(0))
129 0 : throw io::IOException( OUString(THROW_WHERE), uno::Reference< uno::XInterface >() );
130 :
131 0 : if (osl::FileBase::E_None != m_aFile.setPos(osl_Pos_Absolut,sal_uInt64(0)))
132 0 : throw io::IOException( OUString(THROW_WHERE), uno::Reference< uno::XInterface >() );
133 0 : }
134 :
135 :
136 :
137 :
138 : // XStream_impl private non interface methods
139 :
140 :
141 : sal_Int32 SAL_CALL
142 0 : XStream_impl::readBytes(
143 : uno::Sequence< sal_Int8 >& aData,
144 : sal_Int32 nBytesToRead )
145 : throw( io::NotConnectedException,
146 : io::BufferSizeExceededException,
147 : io::IOException,
148 : uno::RuntimeException, std::exception)
149 : {
150 0 : if( ! m_nIsOpen )
151 0 : throw io::IOException( OUString(THROW_WHERE), uno::Reference< uno::XInterface >() );
152 :
153 : sal_Int8 * buffer;
154 : try
155 : {
156 0 : buffer = new sal_Int8[nBytesToRead];
157 : }
158 0 : catch (const std::bad_alloc&)
159 : {
160 0 : if( m_nIsOpen ) m_aFile.close();
161 0 : throw io::BufferSizeExceededException( OUString(THROW_WHERE), uno::Reference< uno::XInterface >() );
162 : }
163 :
164 0 : sal_uInt64 nrc(0);
165 0 : if(m_aFile.read( (void* )buffer,sal_uInt64(nBytesToRead),nrc )
166 : != osl::FileBase::E_None)
167 : {
168 0 : delete[] buffer;
169 0 : throw io::IOException( OUString(THROW_WHERE), uno::Reference< uno::XInterface >() );
170 : }
171 0 : aData = uno::Sequence< sal_Int8 > ( buffer, (sal_uInt32)nrc );
172 0 : delete[] buffer;
173 0 : return ( sal_Int32 ) nrc;
174 : }
175 :
176 :
177 : sal_Int32 SAL_CALL
178 0 : XStream_impl::readSomeBytes(
179 : uno::Sequence< sal_Int8 >& aData,
180 : sal_Int32 nMaxBytesToRead )
181 : throw( io::NotConnectedException,
182 : io::BufferSizeExceededException,
183 : io::IOException,
184 : uno::RuntimeException, std::exception)
185 : {
186 0 : return readBytes( aData,nMaxBytesToRead );
187 : }
188 :
189 :
190 : void SAL_CALL
191 0 : XStream_impl::skipBytes(
192 : sal_Int32 nBytesToSkip )
193 : throw( io::NotConnectedException,
194 : io::BufferSizeExceededException,
195 : io::IOException,
196 : uno::RuntimeException, std::exception )
197 : {
198 0 : m_aFile.setPos( osl_Pos_Current, sal_uInt64( nBytesToSkip ) );
199 0 : }
200 :
201 :
202 : sal_Int32 SAL_CALL
203 0 : XStream_impl::available(
204 : void )
205 : throw( io::NotConnectedException,
206 : io::IOException,
207 : uno::RuntimeException, std::exception)
208 : {
209 0 : return 0;
210 : }
211 :
212 :
213 : void SAL_CALL
214 0 : XStream_impl::writeBytes( const uno::Sequence< sal_Int8 >& aData )
215 : throw( io::NotConnectedException,
216 : io::BufferSizeExceededException,
217 : io::IOException,
218 : uno::RuntimeException, std::exception)
219 : {
220 0 : sal_uInt32 length = aData.getLength();
221 0 : if(length)
222 : {
223 0 : sal_uInt64 nWrittenBytes(0);
224 0 : const sal_Int8* p = aData.getConstArray();
225 0 : if(osl::FileBase::E_None != m_aFile.write(((void*)(p)),sal_uInt64(length),nWrittenBytes) ||
226 0 : nWrittenBytes != length )
227 0 : throw io::IOException( OUString(THROW_WHERE), uno::Reference< uno::XInterface >() );
228 : }
229 0 : }
230 :
231 :
232 : void SAL_CALL
233 0 : XStream_impl::closeStream(
234 : void )
235 : throw( io::NotConnectedException,
236 : io::IOException,
237 : uno::RuntimeException )
238 : {
239 0 : if( m_nIsOpen )
240 : {
241 0 : osl::FileBase::RC err = m_aFile.close();
242 :
243 0 : if( err != osl::FileBase::E_None ) {
244 0 : io::IOException ex;
245 0 : ex.Message = "could not close file";
246 0 : throw ex;
247 : }
248 :
249 0 : m_nIsOpen = false;
250 : }
251 0 : }
252 :
253 : void SAL_CALL
254 0 : XStream_impl::closeInput(
255 : void )
256 : throw( io::NotConnectedException,
257 : io::IOException,
258 : uno::RuntimeException, std::exception )
259 : {
260 0 : osl::MutexGuard aGuard( m_aMutex );
261 0 : m_bInputStreamCalled = false;
262 :
263 0 : if( ! m_bOutputStreamCalled )
264 0 : closeStream();
265 0 : }
266 :
267 :
268 : void SAL_CALL
269 0 : XStream_impl::closeOutput(
270 : void )
271 : throw( io::NotConnectedException,
272 : io::IOException,
273 : uno::RuntimeException, std::exception )
274 : {
275 0 : osl::MutexGuard aGuard( m_aMutex );
276 0 : m_bOutputStreamCalled = false;
277 :
278 0 : if( ! m_bInputStreamCalled )
279 0 : closeStream();
280 0 : }
281 :
282 :
283 : void SAL_CALL
284 0 : XStream_impl::seek(
285 : sal_Int64 location )
286 : throw( lang::IllegalArgumentException,
287 : io::IOException,
288 : uno::RuntimeException, std::exception )
289 : {
290 0 : if( location < 0 )
291 0 : throw lang::IllegalArgumentException( OUString(THROW_WHERE), uno::Reference< uno::XInterface >(), 0 );
292 0 : if( osl::FileBase::E_None != m_aFile.setPos( osl_Pos_Absolut, sal_uInt64( location ) ) )
293 0 : throw io::IOException( OUString(THROW_WHERE), uno::Reference< uno::XInterface >() );
294 0 : }
295 :
296 :
297 : sal_Int64 SAL_CALL
298 0 : XStream_impl::getPosition(
299 : void )
300 : throw( io::IOException,
301 : uno::RuntimeException, std::exception )
302 : {
303 : sal_uInt64 uPos;
304 0 : if( osl::FileBase::E_None != m_aFile.getPos( uPos ) )
305 0 : throw io::IOException( OUString(THROW_WHERE), uno::Reference< uno::XInterface >() );
306 0 : return sal_Int64( uPos );
307 : }
308 :
309 : sal_Int64 SAL_CALL
310 0 : XStream_impl::getLength(
311 : void )
312 : throw( io::IOException,
313 : uno::RuntimeException, std::exception )
314 : {
315 : sal_uInt64 uEndPos;
316 0 : if ( m_aFile.getSize(uEndPos) != osl::FileBase::E_None )
317 0 : throw io::IOException( OUString(THROW_WHERE), uno::Reference< uno::XInterface >() );
318 : else
319 0 : return sal_Int64( uEndPos );
320 : }
321 :
322 : void SAL_CALL
323 0 : XStream_impl::flush()
324 : throw( io::NotConnectedException,
325 : io::BufferSizeExceededException,
326 : io::IOException,
327 : uno::RuntimeException, std::exception )
328 0 : {}
329 :
330 0 : void XStream_impl::waitForCompletion()
331 : throw (io::IOException, uno::RuntimeException, std::exception)
332 : {
333 : // At least on UNIX, to reliably learn about any errors encountered by
334 : // asynchronous NFS write operations, without closing the file directly
335 : // afterwards, there appears to be no cheaper way than to call fsync:
336 0 : if (m_nIsOpen && m_aFile.sync() != osl::FileBase::E_None) {
337 : throw io::IOException(
338 : OUString( "could not synchronize file to disc"),
339 0 : static_cast< OWeakObject * >(this));
340 : }
341 0 : }
342 :
343 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|