xref: /aoo42x/main/ucb/source/ucp/file/filrec.cxx (revision 2f86921c)
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
10*2f86921cSAndrew Rist  *
11*2f86921cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*2f86921cSAndrew Rist  *
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.
19*2f86921cSAndrew Rist  *
20*2f86921cSAndrew Rist  *************************************************************/
21*2f86921cSAndrew Rist 
22*2f86921cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_ucb.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "filrec.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir namespace fileaccess {
30cdf0e10cSrcweir 
31cdf0e10cSrcweir void ReconnectingFile::disconnect()
32cdf0e10cSrcweir {
33cdf0e10cSrcweir     m_aFile.close();
34cdf0e10cSrcweir     m_bDisconnect = sal_True;
35cdf0e10cSrcweir }
36cdf0e10cSrcweir 
37cdf0e10cSrcweir sal_Bool ReconnectingFile::reconnect()
38cdf0e10cSrcweir {
39cdf0e10cSrcweir     sal_Bool bResult = sal_False;
40cdf0e10cSrcweir     if ( m_bFlagsSet )
41cdf0e10cSrcweir     {
42cdf0e10cSrcweir         disconnect();
43cdf0e10cSrcweir         if ( m_aFile.open( m_nFlags ) == ::osl::FileBase::E_None
44cdf0e10cSrcweir           || m_aFile.open( OpenFlag_Read ) == ::osl::FileBase::E_None )
45cdf0e10cSrcweir         {
46cdf0e10cSrcweir             m_bDisconnect = sal_False;
47cdf0e10cSrcweir             bResult = sal_True;
48cdf0e10cSrcweir         }
49cdf0e10cSrcweir     }
50cdf0e10cSrcweir 
51cdf0e10cSrcweir     return bResult;
52cdf0e10cSrcweir }
53cdf0e10cSrcweir 
54cdf0e10cSrcweir ::osl::FileBase::RC ReconnectingFile::open( sal_uInt32 uFlags )
55cdf0e10cSrcweir {
56cdf0e10cSrcweir     ::osl::FileBase::RC nResult = m_aFile.open( uFlags );
57cdf0e10cSrcweir     if ( nResult == ::osl::FileBase::E_None )
58cdf0e10cSrcweir     {
59cdf0e10cSrcweir         if ( uFlags & OpenFlag_Create )
60cdf0e10cSrcweir             m_nFlags = (uFlags & ( ~OpenFlag_Create )) | OpenFlag_Write;
61cdf0e10cSrcweir         else
62cdf0e10cSrcweir             m_nFlags = uFlags;
63cdf0e10cSrcweir 
64cdf0e10cSrcweir         m_bFlagsSet = sal_True;
65cdf0e10cSrcweir     }
66cdf0e10cSrcweir 
67cdf0e10cSrcweir     return nResult;
68cdf0e10cSrcweir }
69cdf0e10cSrcweir 
70cdf0e10cSrcweir ::osl::FileBase::RC ReconnectingFile::close()
71cdf0e10cSrcweir {
72cdf0e10cSrcweir     m_nFlags = 0;
73cdf0e10cSrcweir     m_bFlagsSet = sal_False;
74cdf0e10cSrcweir     m_bDisconnect = sal_False;
75cdf0e10cSrcweir 
76cdf0e10cSrcweir     return m_aFile.close();
77cdf0e10cSrcweir }
78cdf0e10cSrcweir 
79cdf0e10cSrcweir ::osl::FileBase::RC ReconnectingFile::setPos( sal_uInt32 uHow, sal_Int64 uPos )
80cdf0e10cSrcweir {
81cdf0e10cSrcweir     ::osl::FileBase::RC nRes = ::osl::FileBase::E_NETWORK;
82cdf0e10cSrcweir 
83cdf0e10cSrcweir     if ( uHow == Pos_Absolut && uPos > 0 )
84cdf0e10cSrcweir     {
85cdf0e10cSrcweir         if ( m_bDisconnect )
86cdf0e10cSrcweir         {
87cdf0e10cSrcweir             if ( reconnect() )
88cdf0e10cSrcweir                 nRes = m_aFile.setPos( uHow, uPos );
89cdf0e10cSrcweir         }
90cdf0e10cSrcweir         else
91cdf0e10cSrcweir         {
92cdf0e10cSrcweir             // E_INVAL error code means in this case that
93cdf0e10cSrcweir             // the file handler is invalid
94cdf0e10cSrcweir             nRes = m_aFile.setPos( uHow, uPos );
95cdf0e10cSrcweir             if ( ( nRes == ::osl::FileBase::E_NETWORK
96cdf0e10cSrcweir                 || nRes == ::osl::FileBase::E_INVAL )
97cdf0e10cSrcweir               && reconnect() )
98cdf0e10cSrcweir                 nRes = m_aFile.setPos( uHow, uPos );
99cdf0e10cSrcweir         }
100cdf0e10cSrcweir     }
101cdf0e10cSrcweir     else
102cdf0e10cSrcweir     {
103cdf0e10cSrcweir         if ( !m_bDisconnect )
104cdf0e10cSrcweir             nRes = m_aFile.setPos( uHow, uPos );
105cdf0e10cSrcweir     }
106cdf0e10cSrcweir 
107cdf0e10cSrcweir     return nRes;
108cdf0e10cSrcweir }
109cdf0e10cSrcweir 
110cdf0e10cSrcweir ::osl::FileBase::RC ReconnectingFile::getPos( sal_uInt64& uPos )
111cdf0e10cSrcweir {
112cdf0e10cSrcweir     if ( m_bDisconnect )
113cdf0e10cSrcweir         return ::osl::FileBase::E_NETWORK;
114cdf0e10cSrcweir 
115cdf0e10cSrcweir     return m_aFile.getPos( uPos );
116cdf0e10cSrcweir }
117cdf0e10cSrcweir 
118cdf0e10cSrcweir ::osl::FileBase::RC ReconnectingFile::setSize( sal_uInt64 uSize )
119cdf0e10cSrcweir {
120cdf0e10cSrcweir     ::osl::FileBase::RC nRes = ::osl::FileBase::E_NETWORK;
121cdf0e10cSrcweir 
122cdf0e10cSrcweir     if ( uSize == 0 )
123cdf0e10cSrcweir     {
124cdf0e10cSrcweir         if ( m_bDisconnect )
125cdf0e10cSrcweir         {
126cdf0e10cSrcweir             if ( reconnect() )
127cdf0e10cSrcweir                 nRes = m_aFile.setSize( uSize );
128cdf0e10cSrcweir         }
129cdf0e10cSrcweir         else
130cdf0e10cSrcweir         {
131cdf0e10cSrcweir             // E_INVAL error code means in this case that
132cdf0e10cSrcweir             // the file handler is invalid
133cdf0e10cSrcweir             nRes = m_aFile.setSize( uSize );
134cdf0e10cSrcweir             if ( ( nRes == ::osl::FileBase::E_NETWORK
135cdf0e10cSrcweir                 || nRes == ::osl::FileBase::E_INVAL )
136cdf0e10cSrcweir               && reconnect() )
137cdf0e10cSrcweir                 nRes = m_aFile.setSize( uSize );
138cdf0e10cSrcweir         }
139cdf0e10cSrcweir     }
140cdf0e10cSrcweir     else
141cdf0e10cSrcweir     {
142cdf0e10cSrcweir         if ( !m_bDisconnect )
143cdf0e10cSrcweir             nRes = m_aFile.setSize( uSize );
144cdf0e10cSrcweir     }
145cdf0e10cSrcweir 
146cdf0e10cSrcweir     return nRes;
147cdf0e10cSrcweir }
148cdf0e10cSrcweir 
149cdf0e10cSrcweir ::osl::FileBase::RC ReconnectingFile::getSize( sal_uInt64 &rSize )
150cdf0e10cSrcweir {
151cdf0e10cSrcweir     ::osl::FileBase::RC nRes = ::osl::FileBase::E_NETWORK;
152cdf0e10cSrcweir 
153cdf0e10cSrcweir     if ( !m_bDisconnect )
154cdf0e10cSrcweir         nRes = m_aFile.getSize( rSize );
155cdf0e10cSrcweir 
156cdf0e10cSrcweir     // E_INVAL error code means in this case that
157cdf0e10cSrcweir     // the file handler is invalid
158cdf0e10cSrcweir     if ( ( nRes == ::osl::FileBase::E_NETWORK
159cdf0e10cSrcweir         || nRes == ::osl::FileBase::E_INVAL )
160cdf0e10cSrcweir       && reconnect() )
161cdf0e10cSrcweir     {
162cdf0e10cSrcweir         nRes = m_aFile.getSize( rSize );
163cdf0e10cSrcweir 
164cdf0e10cSrcweir         // the repairing should be disconnected, since the position might be wrong
165cdf0e10cSrcweir         // but the result should be retrieved
166cdf0e10cSrcweir         disconnect();
167cdf0e10cSrcweir     }
168cdf0e10cSrcweir 
169cdf0e10cSrcweir     return nRes;
170cdf0e10cSrcweir }
171cdf0e10cSrcweir 
172cdf0e10cSrcweir ::osl::FileBase::RC ReconnectingFile::read( void *pBuffer, sal_uInt64 uBytesRequested, sal_uInt64& rBytesRead )
173cdf0e10cSrcweir {
174cdf0e10cSrcweir     if ( m_bDisconnect )
175cdf0e10cSrcweir         return ::osl::FileBase::E_NETWORK;
176cdf0e10cSrcweir 
177cdf0e10cSrcweir     return m_aFile.read( pBuffer, uBytesRequested, rBytesRead );
178cdf0e10cSrcweir }
179cdf0e10cSrcweir 
180cdf0e10cSrcweir ::osl::FileBase::RC ReconnectingFile::write(const void *pBuffer, sal_uInt64 uBytesToWrite, sal_uInt64& rBytesWritten)
181cdf0e10cSrcweir {
182cdf0e10cSrcweir     if ( m_bDisconnect )
183cdf0e10cSrcweir         return ::osl::FileBase::E_NETWORK;
184cdf0e10cSrcweir 
185cdf0e10cSrcweir     return m_aFile.write( pBuffer, uBytesToWrite, rBytesWritten );
186cdf0e10cSrcweir }
187cdf0e10cSrcweir 
188cdf0e10cSrcweir ::osl::FileBase::RC ReconnectingFile::sync() const
189cdf0e10cSrcweir {
190cdf0e10cSrcweir     if ( m_bDisconnect )
191cdf0e10cSrcweir         return ::osl::FileBase::E_NETWORK;
192cdf0e10cSrcweir 
193cdf0e10cSrcweir     return m_aFile.sync();
194cdf0e10cSrcweir }
195cdf0e10cSrcweir 
196cdf0e10cSrcweir } // namespace fileaccess
197cdf0e10cSrcweir 
198