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