/src/libreoffice/ucb/source/ucp/file/filrec.cxx
Line | Count | Source |
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 | | void ReconnectingFile::disconnect() |
26 | 0 | { |
27 | 0 | m_aFile.close(); |
28 | 0 | m_bDisconnect = true; |
29 | 0 | } |
30 | | |
31 | | bool ReconnectingFile::reconnect() |
32 | 0 | { |
33 | 0 | bool bResult = false; |
34 | 0 | if ( m_bFlagsSet ) |
35 | 0 | { |
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 | 0 | { |
40 | 0 | m_bDisconnect = false; |
41 | 0 | bResult = true; |
42 | 0 | } |
43 | 0 | } |
44 | |
|
45 | 0 | return bResult; |
46 | 0 | } |
47 | | |
48 | | ::osl::FileBase::RC ReconnectingFile::open( sal_uInt32 uFlags ) |
49 | 50.9k | { |
50 | 50.9k | ::osl::FileBase::RC nResult = m_aFile.open( uFlags ); |
51 | 50.9k | if ( nResult == ::osl::FileBase::E_None ) |
52 | 50.9k | { |
53 | 50.9k | if ( uFlags & osl_File_OpenFlag_Create ) |
54 | 0 | m_nFlags = (uFlags & ( ~osl_File_OpenFlag_Create )) | osl_File_OpenFlag_Write; |
55 | 50.9k | else |
56 | 50.9k | m_nFlags = uFlags; |
57 | | |
58 | 50.9k | m_bFlagsSet = true; |
59 | 50.9k | } |
60 | | |
61 | 50.9k | return nResult; |
62 | 50.9k | } |
63 | | |
64 | | ::osl::FileBase::RC ReconnectingFile::close() |
65 | 101k | { |
66 | 101k | m_nFlags = 0; |
67 | 101k | m_bFlagsSet = false; |
68 | 101k | m_bDisconnect = false; |
69 | | |
70 | 101k | return m_aFile.close(); |
71 | 101k | } |
72 | | |
73 | | ::osl::FileBase::RC ReconnectingFile::setPos( sal_uInt32 uHow, sal_Int64 uPos ) |
74 | 246k | { |
75 | 246k | ::osl::FileBase::RC nRes = ::osl::FileBase::E_NETWORK; |
76 | | |
77 | 246k | if ( uHow == osl_Pos_Absolut && uPos > 0 ) |
78 | 195k | { |
79 | 195k | if ( m_bDisconnect ) |
80 | 0 | { |
81 | 0 | if ( reconnect() ) |
82 | 0 | nRes = m_aFile.setPos( uHow, uPos ); |
83 | 0 | } |
84 | 195k | else |
85 | 195k | { |
86 | | // E_INVAL error code means in this case that |
87 | | // the file handler is invalid |
88 | 195k | nRes = m_aFile.setPos( uHow, uPos ); |
89 | 195k | if ( ( nRes == ::osl::FileBase::E_NETWORK |
90 | 195k | || nRes == ::osl::FileBase::E_INVAL ) |
91 | 0 | && reconnect() ) |
92 | 0 | nRes = m_aFile.setPos( uHow, uPos ); |
93 | 195k | } |
94 | 195k | } |
95 | 51.3k | else |
96 | 51.3k | { |
97 | 51.3k | if ( !m_bDisconnect ) |
98 | 51.3k | nRes = m_aFile.setPos( uHow, uPos ); |
99 | 51.3k | } |
100 | | |
101 | 246k | return nRes; |
102 | 246k | } |
103 | | |
104 | | ::osl::FileBase::RC ReconnectingFile::getPos( sal_uInt64& uPos ) |
105 | 0 | { |
106 | 0 | if ( m_bDisconnect ) |
107 | 0 | return ::osl::FileBase::E_NETWORK; |
108 | | |
109 | 0 | return m_aFile.getPos( uPos ); |
110 | 0 | } |
111 | | |
112 | | ::osl::FileBase::RC ReconnectingFile::setSize( sal_uInt64 uSize ) |
113 | 0 | { |
114 | 0 | ::osl::FileBase::RC nRes = ::osl::FileBase::E_NETWORK; |
115 | |
|
116 | 0 | if ( uSize == 0 ) |
117 | 0 | { |
118 | 0 | if ( m_bDisconnect ) |
119 | 0 | { |
120 | 0 | if ( reconnect() ) |
121 | 0 | nRes = m_aFile.setSize( uSize ); |
122 | 0 | } |
123 | 0 | else |
124 | 0 | { |
125 | | // E_INVAL error code means in this case that |
126 | | // the file handler is invalid |
127 | 0 | nRes = m_aFile.setSize( uSize ); |
128 | 0 | if ( ( nRes == ::osl::FileBase::E_NETWORK |
129 | 0 | || nRes == ::osl::FileBase::E_INVAL ) |
130 | 0 | && reconnect() ) |
131 | 0 | nRes = m_aFile.setSize( uSize ); |
132 | 0 | } |
133 | 0 | } |
134 | 0 | else |
135 | 0 | { |
136 | 0 | if ( !m_bDisconnect ) |
137 | 0 | nRes = m_aFile.setSize( uSize ); |
138 | 0 | } |
139 | |
|
140 | 0 | return nRes; |
141 | 0 | } |
142 | | |
143 | | ::osl::FileBase::RC ReconnectingFile::getSize( sal_uInt64 &rSize ) |
144 | 138k | { |
145 | 138k | ::osl::FileBase::RC nRes = ::osl::FileBase::E_NETWORK; |
146 | | |
147 | 138k | if ( !m_bDisconnect ) |
148 | 138k | nRes = m_aFile.getSize( rSize ); |
149 | | |
150 | | // E_INVAL error code means in this case that |
151 | | // the file handler is invalid |
152 | 138k | if ( ( nRes == ::osl::FileBase::E_NETWORK |
153 | 138k | || nRes == ::osl::FileBase::E_INVAL ) |
154 | 0 | && reconnect() ) |
155 | 0 | { |
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 | 0 | } |
162 | | |
163 | 138k | return nRes; |
164 | 138k | } |
165 | | |
166 | | ::osl::FileBase::RC ReconnectingFile::read( void *pBuffer, sal_uInt64 uBytesRequested, sal_uInt64& rBytesRead ) |
167 | 246k | { |
168 | 246k | if ( m_bDisconnect ) |
169 | 0 | return ::osl::FileBase::E_NETWORK; |
170 | | |
171 | 246k | return m_aFile.read( pBuffer, uBytesRequested, rBytesRead ); |
172 | 246k | } |
173 | | |
174 | | ::osl::FileBase::RC ReconnectingFile::write(const void *pBuffer, sal_uInt64 uBytesToWrite, sal_uInt64& rBytesWritten) |
175 | 18 | { |
176 | 18 | if ( m_bDisconnect ) |
177 | 0 | return ::osl::FileBase::E_NETWORK; |
178 | | |
179 | 18 | return m_aFile.write( pBuffer, uBytesToWrite, rBytesWritten ); |
180 | 18 | } |
181 | | |
182 | | ::osl::FileBase::RC ReconnectingFile::sync() const |
183 | 0 | { |
184 | 0 | if ( m_bDisconnect ) |
185 | 0 | return ::osl::FileBase::E_NETWORK; |
186 | | |
187 | 0 | return m_aFile.sync(); |
188 | 0 | } |
189 | | |
190 | | } // namespace fileaccess |
191 | | |
192 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |