Bump for 3.6-28
[LibreOffice.git] / ucb / source / ucp / file / filrec.cxx
blob0ec0f125eea9b22d337f1bcaed75a602573453b4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include "filrec.hxx"
32 namespace fileaccess {
34 void ReconnectingFile::disconnect()
36 m_aFile.close();
37 m_bDisconnect = sal_True;
40 sal_Bool ReconnectingFile::reconnect()
42 sal_Bool bResult = sal_False;
43 if ( m_bFlagsSet )
45 disconnect();
46 if ( m_aFile.open( m_nFlags ) == ::osl::FileBase::E_None
47 || m_aFile.open( osl_File_OpenFlag_Read ) == ::osl::FileBase::E_None )
49 m_bDisconnect = sal_False;
50 bResult = sal_True;
54 return bResult;
57 ::osl::FileBase::RC ReconnectingFile::open( sal_uInt32 uFlags )
59 ::osl::FileBase::RC nResult = m_aFile.open( uFlags );
60 if ( nResult == ::osl::FileBase::E_None )
62 if ( uFlags & osl_File_OpenFlag_Create )
63 m_nFlags = (uFlags & ( ~osl_File_OpenFlag_Create )) | osl_File_OpenFlag_Write;
64 else
65 m_nFlags = uFlags;
67 m_bFlagsSet = sal_True;
70 return nResult;
73 ::osl::FileBase::RC ReconnectingFile::close()
75 m_nFlags = 0;
76 m_bFlagsSet = sal_False;
77 m_bDisconnect = sal_False;
79 return m_aFile.close();
82 ::osl::FileBase::RC ReconnectingFile::setPos( sal_uInt32 uHow, sal_Int64 uPos )
84 ::osl::FileBase::RC nRes = ::osl::FileBase::E_NETWORK;
86 if ( uHow == osl_Pos_Absolut && uPos > 0 )
88 if ( m_bDisconnect )
90 if ( reconnect() )
91 nRes = m_aFile.setPos( uHow, uPos );
93 else
95 // E_INVAL error code means in this case that
96 // the file handler is invalid
97 nRes = m_aFile.setPos( uHow, uPos );
98 if ( ( nRes == ::osl::FileBase::E_NETWORK
99 || nRes == ::osl::FileBase::E_INVAL )
100 && reconnect() )
101 nRes = m_aFile.setPos( uHow, uPos );
104 else
106 if ( !m_bDisconnect )
107 nRes = m_aFile.setPos( uHow, uPos );
110 return nRes;
113 ::osl::FileBase::RC ReconnectingFile::getPos( sal_uInt64& uPos )
115 if ( m_bDisconnect )
116 return ::osl::FileBase::E_NETWORK;
118 return m_aFile.getPos( uPos );
121 ::osl::FileBase::RC ReconnectingFile::setSize( sal_uInt64 uSize )
123 ::osl::FileBase::RC nRes = ::osl::FileBase::E_NETWORK;
125 if ( uSize == 0 )
127 if ( m_bDisconnect )
129 if ( reconnect() )
130 nRes = m_aFile.setSize( uSize );
132 else
134 // E_INVAL error code means in this case that
135 // the file handler is invalid
136 nRes = m_aFile.setSize( uSize );
137 if ( ( nRes == ::osl::FileBase::E_NETWORK
138 || nRes == ::osl::FileBase::E_INVAL )
139 && reconnect() )
140 nRes = m_aFile.setSize( uSize );
143 else
145 if ( !m_bDisconnect )
146 nRes = m_aFile.setSize( uSize );
149 return nRes;
152 ::osl::FileBase::RC ReconnectingFile::getSize( sal_uInt64 &rSize )
154 ::osl::FileBase::RC nRes = ::osl::FileBase::E_NETWORK;
156 if ( !m_bDisconnect )
157 nRes = m_aFile.getSize( rSize );
159 // E_INVAL error code means in this case that
160 // the file handler is invalid
161 if ( ( nRes == ::osl::FileBase::E_NETWORK
162 || nRes == ::osl::FileBase::E_INVAL )
163 && reconnect() )
165 nRes = m_aFile.getSize( rSize );
167 // the repairing should be disconnected, since the position might be wrong
168 // but the result should be retrieved
169 disconnect();
172 return nRes;
175 ::osl::FileBase::RC ReconnectingFile::read( void *pBuffer, sal_uInt64 uBytesRequested, sal_uInt64& rBytesRead )
177 if ( m_bDisconnect )
178 return ::osl::FileBase::E_NETWORK;
180 return m_aFile.read( pBuffer, uBytesRequested, rBytesRead );
183 ::osl::FileBase::RC ReconnectingFile::write(const void *pBuffer, sal_uInt64 uBytesToWrite, sal_uInt64& rBytesWritten)
185 if ( m_bDisconnect )
186 return ::osl::FileBase::E_NETWORK;
188 return m_aFile.write( pBuffer, uBytesToWrite, rBytesWritten );
191 ::osl::FileBase::RC ReconnectingFile::sync() const
193 if ( m_bDisconnect )
194 return ::osl::FileBase::E_NETWORK;
196 return m_aFile.sync();
199 } // namespace fileaccess
201 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */