Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include "filrec.hxx"
22 :
23 : namespace fileaccess {
24 :
25 0 : void ReconnectingFile::disconnect()
26 : {
27 0 : m_aFile.close();
28 0 : m_bDisconnect = true;
29 0 : }
30 :
31 0 : bool ReconnectingFile::reconnect()
32 : {
33 0 : bool bResult = false;
34 0 : if ( m_bFlagsSet )
35 : {
36 0 : disconnect();
37 0 : if ( m_aFile.open( m_nFlags ) == ::osl::FileBase::E_None
38 0 : || m_aFile.open( osl_File_OpenFlag_Read ) == ::osl::FileBase::E_None )
39 : {
40 0 : m_bDisconnect = false;
41 0 : bResult = true;
42 : }
43 : }
44 :
45 0 : return bResult;
46 : }
47 :
48 33669 : ::osl::FileBase::RC ReconnectingFile::open( sal_uInt32 uFlags )
49 : {
50 33669 : ::osl::FileBase::RC nResult = m_aFile.open( uFlags );
51 33669 : if ( nResult == ::osl::FileBase::E_None )
52 : {
53 33510 : if ( uFlags & osl_File_OpenFlag_Create )
54 0 : m_nFlags = (uFlags & ( ~osl_File_OpenFlag_Create )) | osl_File_OpenFlag_Write;
55 : else
56 33510 : m_nFlags = uFlags;
57 :
58 33510 : m_bFlagsSet = true;
59 : }
60 :
61 33669 : return nResult;
62 : }
63 :
64 67258 : ::osl::FileBase::RC ReconnectingFile::close()
65 : {
66 67258 : m_nFlags = 0;
67 67258 : m_bFlagsSet = false;
68 67258 : m_bDisconnect = false;
69 :
70 67258 : return m_aFile.close();
71 : }
72 :
73 564832 : ::osl::FileBase::RC ReconnectingFile::setPos( sal_uInt32 uHow, sal_Int64 uPos )
74 : {
75 564832 : ::osl::FileBase::RC nRes = ::osl::FileBase::E_NETWORK;
76 :
77 564832 : if ( uHow == osl_Pos_Absolut && uPos > 0 )
78 : {
79 653184 : if ( m_bDisconnect )
80 : {
81 0 : if ( reconnect() )
82 0 : nRes = m_aFile.setPos( uHow, uPos );
83 : }
84 : else
85 : {
86 : // E_INVAL error code means in this case that
87 : // the file handler is invalid
88 326592 : nRes = m_aFile.setPos( uHow, uPos );
89 326592 : if ( ( nRes == ::osl::FileBase::E_NETWORK
90 326592 : || nRes == ::osl::FileBase::E_INVAL )
91 326592 : && reconnect() )
92 0 : nRes = m_aFile.setPos( uHow, uPos );
93 : }
94 : }
95 : else
96 : {
97 238240 : if ( !m_bDisconnect )
98 238240 : nRes = m_aFile.setPos( uHow, uPos );
99 : }
100 :
101 564832 : return nRes;
102 : }
103 :
104 30914091 : ::osl::FileBase::RC ReconnectingFile::getPos( sal_uInt64& uPos )
105 : {
106 30914091 : if ( m_bDisconnect )
107 0 : return ::osl::FileBase::E_NETWORK;
108 :
109 30914091 : return m_aFile.getPos( uPos );
110 : }
111 :
112 127 : ::osl::FileBase::RC ReconnectingFile::setSize( sal_uInt64 uSize )
113 : {
114 127 : ::osl::FileBase::RC nRes = ::osl::FileBase::E_NETWORK;
115 :
116 127 : if ( uSize == 0 )
117 : {
118 127 : if ( m_bDisconnect )
119 : {
120 0 : if ( reconnect() )
121 0 : nRes = m_aFile.setSize( uSize );
122 : }
123 : else
124 : {
125 : // E_INVAL error code means in this case that
126 : // the file handler is invalid
127 127 : nRes = m_aFile.setSize( uSize );
128 127 : if ( ( nRes == ::osl::FileBase::E_NETWORK
129 127 : || nRes == ::osl::FileBase::E_INVAL )
130 127 : && reconnect() )
131 0 : nRes = m_aFile.setSize( uSize );
132 : }
133 : }
134 : else
135 : {
136 0 : if ( !m_bDisconnect )
137 0 : nRes = m_aFile.setSize( uSize );
138 : }
139 :
140 127 : return nRes;
141 : }
142 :
143 259521 : ::osl::FileBase::RC ReconnectingFile::getSize( sal_uInt64 &rSize )
144 : {
145 259521 : ::osl::FileBase::RC nRes = ::osl::FileBase::E_NETWORK;
146 :
147 259521 : if ( !m_bDisconnect )
148 259521 : nRes = m_aFile.getSize( rSize );
149 :
150 : // E_INVAL error code means in this case that
151 : // the file handler is invalid
152 259521 : if ( ( nRes == ::osl::FileBase::E_NETWORK
153 259521 : || nRes == ::osl::FileBase::E_INVAL )
154 259521 : && reconnect() )
155 : {
156 0 : nRes = m_aFile.getSize( rSize );
157 :
158 : // the repairing should be disconnected, since the position might be wrong
159 : // but the result should be retrieved
160 0 : disconnect();
161 : }
162 :
163 259521 : return nRes;
164 : }
165 :
166 943734 : ::osl::FileBase::RC ReconnectingFile::read( void *pBuffer, sal_uInt64 uBytesRequested, sal_uInt64& rBytesRead )
167 : {
168 943734 : if ( m_bDisconnect )
169 0 : return ::osl::FileBase::E_NETWORK;
170 :
171 943734 : return m_aFile.read( pBuffer, uBytesRequested, rBytesRead );
172 : }
173 :
174 113041 : ::osl::FileBase::RC ReconnectingFile::write(const void *pBuffer, sal_uInt64 uBytesToWrite, sal_uInt64& rBytesWritten)
175 : {
176 113041 : if ( m_bDisconnect )
177 0 : return ::osl::FileBase::E_NETWORK;
178 :
179 113041 : return m_aFile.write( pBuffer, uBytesToWrite, rBytesWritten );
180 : }
181 :
182 234 : ::osl::FileBase::RC ReconnectingFile::sync() const
183 : {
184 234 : if ( m_bDisconnect )
185 0 : return ::osl::FileBase::E_NETWORK;
186 :
187 234 : return m_aFile.sync();
188 : }
189 :
190 : } // namespace fileaccess
191 :
192 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|