xref: /trunk/main/ucb/source/ucp/file/filrec.cxx (revision 421ed02e)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_file.hxx"
26 
27 #include "filrec.hxx"
28 
29 namespace fileaccess {
30 
disconnect()31 void ReconnectingFile::disconnect()
32 {
33     m_aFile.close();
34     m_bDisconnect = sal_True;
35 }
36 
reconnect()37 sal_Bool ReconnectingFile::reconnect()
38 {
39     sal_Bool bResult = sal_False;
40     if ( m_bFlagsSet )
41     {
42         disconnect();
43         if ( m_aFile.open( m_nFlags ) == ::osl::FileBase::E_None
44           || m_aFile.open( OpenFlag_Read ) == ::osl::FileBase::E_None )
45         {
46             m_bDisconnect = sal_False;
47             bResult = sal_True;
48         }
49     }
50 
51     return bResult;
52 }
53 
open(sal_uInt32 uFlags)54 ::osl::FileBase::RC ReconnectingFile::open( sal_uInt32 uFlags )
55 {
56     ::osl::FileBase::RC nResult = m_aFile.open( uFlags );
57     if ( nResult == ::osl::FileBase::E_None )
58     {
59         if ( uFlags & OpenFlag_Create )
60             m_nFlags = (uFlags & ( ~OpenFlag_Create )) | OpenFlag_Write;
61         else
62             m_nFlags = uFlags;
63 
64         m_bFlagsSet = sal_True;
65     }
66 
67     return nResult;
68 }
69 
close()70 ::osl::FileBase::RC ReconnectingFile::close()
71 {
72     m_nFlags = 0;
73     m_bFlagsSet = sal_False;
74     m_bDisconnect = sal_False;
75 
76     return m_aFile.close();
77 }
78 
setPos(sal_uInt32 uHow,sal_Int64 uPos)79 ::osl::FileBase::RC ReconnectingFile::setPos( sal_uInt32 uHow, sal_Int64 uPos )
80 {
81     ::osl::FileBase::RC nRes = ::osl::FileBase::E_NETWORK;
82 
83     if ( uHow == Pos_Absolut && uPos > 0 )
84     {
85         if ( m_bDisconnect )
86         {
87             if ( reconnect() )
88                 nRes = m_aFile.setPos( uHow, uPos );
89         }
90         else
91         {
92             // E_INVAL error code means in this case that
93             // the file handler is invalid
94             nRes = m_aFile.setPos( uHow, uPos );
95             if ( ( nRes == ::osl::FileBase::E_NETWORK
96                 || nRes == ::osl::FileBase::E_INVAL )
97               && reconnect() )
98                 nRes = m_aFile.setPos( uHow, uPos );
99         }
100     }
101     else
102     {
103         if ( !m_bDisconnect )
104             nRes = m_aFile.setPos( uHow, uPos );
105     }
106 
107     return nRes;
108 }
109 
getPos(sal_uInt64 & uPos)110 ::osl::FileBase::RC ReconnectingFile::getPos( sal_uInt64& uPos )
111 {
112     if ( m_bDisconnect )
113         return ::osl::FileBase::E_NETWORK;
114 
115     return m_aFile.getPos( uPos );
116 }
117 
setSize(sal_uInt64 uSize)118 ::osl::FileBase::RC ReconnectingFile::setSize( sal_uInt64 uSize )
119 {
120     ::osl::FileBase::RC nRes = ::osl::FileBase::E_NETWORK;
121 
122     if ( uSize == 0 )
123     {
124         if ( m_bDisconnect )
125         {
126             if ( reconnect() )
127                 nRes = m_aFile.setSize( uSize );
128         }
129         else
130         {
131             // E_INVAL error code means in this case that
132             // the file handler is invalid
133             nRes = m_aFile.setSize( uSize );
134             if ( ( nRes == ::osl::FileBase::E_NETWORK
135                 || nRes == ::osl::FileBase::E_INVAL )
136               && reconnect() )
137                 nRes = m_aFile.setSize( uSize );
138         }
139     }
140     else
141     {
142         if ( !m_bDisconnect )
143             nRes = m_aFile.setSize( uSize );
144     }
145 
146     return nRes;
147 }
148 
getSize(sal_uInt64 & rSize)149 ::osl::FileBase::RC ReconnectingFile::getSize( sal_uInt64 &rSize )
150 {
151     ::osl::FileBase::RC nRes = ::osl::FileBase::E_NETWORK;
152 
153     if ( !m_bDisconnect )
154         nRes = m_aFile.getSize( rSize );
155 
156     // E_INVAL error code means in this case that
157     // the file handler is invalid
158     if ( ( nRes == ::osl::FileBase::E_NETWORK
159         || nRes == ::osl::FileBase::E_INVAL )
160       && reconnect() )
161     {
162         nRes = m_aFile.getSize( rSize );
163 
164         // the repairing should be disconnected, since the position might be wrong
165         // but the result should be retrieved
166         disconnect();
167     }
168 
169     return nRes;
170 }
171 
read(void * pBuffer,sal_uInt64 uBytesRequested,sal_uInt64 & rBytesRead)172 ::osl::FileBase::RC ReconnectingFile::read( void *pBuffer, sal_uInt64 uBytesRequested, sal_uInt64& rBytesRead )
173 {
174     if ( m_bDisconnect )
175         return ::osl::FileBase::E_NETWORK;
176 
177     return m_aFile.read( pBuffer, uBytesRequested, rBytesRead );
178 }
179 
write(const void * pBuffer,sal_uInt64 uBytesToWrite,sal_uInt64 & rBytesWritten)180 ::osl::FileBase::RC ReconnectingFile::write(const void *pBuffer, sal_uInt64 uBytesToWrite, sal_uInt64& rBytesWritten)
181 {
182     if ( m_bDisconnect )
183         return ::osl::FileBase::E_NETWORK;
184 
185     return m_aFile.write( pBuffer, uBytesToWrite, rBytesWritten );
186 }
187 
sync() const188 ::osl::FileBase::RC ReconnectingFile::sync() const
189 {
190     if ( m_bDisconnect )
191         return ::osl::FileBase::E_NETWORK;
192 
193     return m_aFile.sync();
194 }
195 
196 } // namespace fileaccess
197 
198