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