xref: /aoo42x/main/ucb/source/ucp/file/filrec.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_ucb.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "filrec.hxx"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir namespace fileaccess {
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir void ReconnectingFile::disconnect()
36*cdf0e10cSrcweir {
37*cdf0e10cSrcweir     m_aFile.close();
38*cdf0e10cSrcweir     m_bDisconnect = sal_True;
39*cdf0e10cSrcweir }
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir sal_Bool ReconnectingFile::reconnect()
42*cdf0e10cSrcweir {
43*cdf0e10cSrcweir     sal_Bool bResult = sal_False;
44*cdf0e10cSrcweir     if ( m_bFlagsSet )
45*cdf0e10cSrcweir     {
46*cdf0e10cSrcweir         disconnect();
47*cdf0e10cSrcweir         if ( m_aFile.open( m_nFlags ) == ::osl::FileBase::E_None
48*cdf0e10cSrcweir           || m_aFile.open( OpenFlag_Read ) == ::osl::FileBase::E_None )
49*cdf0e10cSrcweir         {
50*cdf0e10cSrcweir             m_bDisconnect = sal_False;
51*cdf0e10cSrcweir             bResult = sal_True;
52*cdf0e10cSrcweir         }
53*cdf0e10cSrcweir     }
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir     return bResult;
56*cdf0e10cSrcweir }
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir ::osl::FileBase::RC ReconnectingFile::open( sal_uInt32 uFlags )
59*cdf0e10cSrcweir {
60*cdf0e10cSrcweir     ::osl::FileBase::RC nResult = m_aFile.open( uFlags );
61*cdf0e10cSrcweir     if ( nResult == ::osl::FileBase::E_None )
62*cdf0e10cSrcweir     {
63*cdf0e10cSrcweir         if ( uFlags & OpenFlag_Create )
64*cdf0e10cSrcweir             m_nFlags = (uFlags & ( ~OpenFlag_Create )) | OpenFlag_Write;
65*cdf0e10cSrcweir         else
66*cdf0e10cSrcweir             m_nFlags = uFlags;
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir         m_bFlagsSet = sal_True;
69*cdf0e10cSrcweir     }
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir     return nResult;
72*cdf0e10cSrcweir }
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir ::osl::FileBase::RC ReconnectingFile::close()
75*cdf0e10cSrcweir {
76*cdf0e10cSrcweir     m_nFlags = 0;
77*cdf0e10cSrcweir     m_bFlagsSet = sal_False;
78*cdf0e10cSrcweir     m_bDisconnect = sal_False;
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir     return m_aFile.close();
81*cdf0e10cSrcweir }
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir ::osl::FileBase::RC ReconnectingFile::setPos( sal_uInt32 uHow, sal_Int64 uPos )
84*cdf0e10cSrcweir {
85*cdf0e10cSrcweir     ::osl::FileBase::RC nRes = ::osl::FileBase::E_NETWORK;
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir     if ( uHow == Pos_Absolut && uPos > 0 )
88*cdf0e10cSrcweir     {
89*cdf0e10cSrcweir         if ( m_bDisconnect )
90*cdf0e10cSrcweir         {
91*cdf0e10cSrcweir             if ( reconnect() )
92*cdf0e10cSrcweir                 nRes = m_aFile.setPos( uHow, uPos );
93*cdf0e10cSrcweir         }
94*cdf0e10cSrcweir         else
95*cdf0e10cSrcweir         {
96*cdf0e10cSrcweir             // E_INVAL error code means in this case that
97*cdf0e10cSrcweir             // the file handler is invalid
98*cdf0e10cSrcweir             nRes = m_aFile.setPos( uHow, uPos );
99*cdf0e10cSrcweir             if ( ( nRes == ::osl::FileBase::E_NETWORK
100*cdf0e10cSrcweir                 || nRes == ::osl::FileBase::E_INVAL )
101*cdf0e10cSrcweir               && reconnect() )
102*cdf0e10cSrcweir                 nRes = m_aFile.setPos( uHow, uPos );
103*cdf0e10cSrcweir         }
104*cdf0e10cSrcweir     }
105*cdf0e10cSrcweir     else
106*cdf0e10cSrcweir     {
107*cdf0e10cSrcweir         if ( !m_bDisconnect )
108*cdf0e10cSrcweir             nRes = m_aFile.setPos( uHow, uPos );
109*cdf0e10cSrcweir     }
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir     return nRes;
112*cdf0e10cSrcweir }
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir ::osl::FileBase::RC ReconnectingFile::getPos( sal_uInt64& uPos )
115*cdf0e10cSrcweir {
116*cdf0e10cSrcweir     if ( m_bDisconnect )
117*cdf0e10cSrcweir         return ::osl::FileBase::E_NETWORK;
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir     return m_aFile.getPos( uPos );
120*cdf0e10cSrcweir }
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir ::osl::FileBase::RC ReconnectingFile::setSize( sal_uInt64 uSize )
123*cdf0e10cSrcweir {
124*cdf0e10cSrcweir     ::osl::FileBase::RC nRes = ::osl::FileBase::E_NETWORK;
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir     if ( uSize == 0 )
127*cdf0e10cSrcweir     {
128*cdf0e10cSrcweir         if ( m_bDisconnect )
129*cdf0e10cSrcweir         {
130*cdf0e10cSrcweir             if ( reconnect() )
131*cdf0e10cSrcweir                 nRes = m_aFile.setSize( uSize );
132*cdf0e10cSrcweir         }
133*cdf0e10cSrcweir         else
134*cdf0e10cSrcweir         {
135*cdf0e10cSrcweir             // E_INVAL error code means in this case that
136*cdf0e10cSrcweir             // the file handler is invalid
137*cdf0e10cSrcweir             nRes = m_aFile.setSize( uSize );
138*cdf0e10cSrcweir             if ( ( nRes == ::osl::FileBase::E_NETWORK
139*cdf0e10cSrcweir                 || nRes == ::osl::FileBase::E_INVAL )
140*cdf0e10cSrcweir               && reconnect() )
141*cdf0e10cSrcweir                 nRes = m_aFile.setSize( uSize );
142*cdf0e10cSrcweir         }
143*cdf0e10cSrcweir     }
144*cdf0e10cSrcweir     else
145*cdf0e10cSrcweir     {
146*cdf0e10cSrcweir         if ( !m_bDisconnect )
147*cdf0e10cSrcweir             nRes = m_aFile.setSize( uSize );
148*cdf0e10cSrcweir     }
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir     return nRes;
151*cdf0e10cSrcweir }
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir ::osl::FileBase::RC ReconnectingFile::getSize( sal_uInt64 &rSize )
154*cdf0e10cSrcweir {
155*cdf0e10cSrcweir     ::osl::FileBase::RC nRes = ::osl::FileBase::E_NETWORK;
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir     if ( !m_bDisconnect )
158*cdf0e10cSrcweir         nRes = m_aFile.getSize( rSize );
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir     // E_INVAL error code means in this case that
161*cdf0e10cSrcweir     // the file handler is invalid
162*cdf0e10cSrcweir     if ( ( nRes == ::osl::FileBase::E_NETWORK
163*cdf0e10cSrcweir         || nRes == ::osl::FileBase::E_INVAL )
164*cdf0e10cSrcweir       && reconnect() )
165*cdf0e10cSrcweir     {
166*cdf0e10cSrcweir         nRes = m_aFile.getSize( rSize );
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir         // the repairing should be disconnected, since the position might be wrong
169*cdf0e10cSrcweir         // but the result should be retrieved
170*cdf0e10cSrcweir         disconnect();
171*cdf0e10cSrcweir     }
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir     return nRes;
174*cdf0e10cSrcweir }
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir ::osl::FileBase::RC ReconnectingFile::read( void *pBuffer, sal_uInt64 uBytesRequested, sal_uInt64& rBytesRead )
177*cdf0e10cSrcweir {
178*cdf0e10cSrcweir     if ( m_bDisconnect )
179*cdf0e10cSrcweir         return ::osl::FileBase::E_NETWORK;
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir     return m_aFile.read( pBuffer, uBytesRequested, rBytesRead );
182*cdf0e10cSrcweir }
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir ::osl::FileBase::RC ReconnectingFile::write(const void *pBuffer, sal_uInt64 uBytesToWrite, sal_uInt64& rBytesWritten)
185*cdf0e10cSrcweir {
186*cdf0e10cSrcweir     if ( m_bDisconnect )
187*cdf0e10cSrcweir         return ::osl::FileBase::E_NETWORK;
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir     return m_aFile.write( pBuffer, uBytesToWrite, rBytesWritten );
190*cdf0e10cSrcweir }
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir ::osl::FileBase::RC ReconnectingFile::sync() const
193*cdf0e10cSrcweir {
194*cdf0e10cSrcweir     if ( m_bDisconnect )
195*cdf0e10cSrcweir         return ::osl::FileBase::E_NETWORK;
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir     return m_aFile.sync();
198*cdf0e10cSrcweir }
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir } // namespace fileaccess
201*cdf0e10cSrcweir 
202