tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / store / source / storlckb.cxx
blob9c8605f9760c6101bb44995541717abfd4760d27
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "storlckb.hxx"
22 #include <sal/types.h>
23 #include <rtl/string.h>
24 #include <rtl/ref.hxx>
25 #include <osl/mutex.hxx>
27 #include <store/types.h>
29 #include "storbase.hxx"
30 #include "stordata.hxx"
31 #include "storpage.hxx"
33 using namespace store;
35 const sal_uInt32 OStoreLockBytes::m_nTypeId(0x94190310);
37 OStoreLockBytes::OStoreLockBytes()
38 : m_bWriteable (false)
42 OStoreLockBytes::~OStoreLockBytes()
44 if (m_xManager.is() && m_xNode.is())
46 m_xManager->releasePage(m_xNode->m_aDescr);
50 bool OStoreLockBytes::isKindOf (sal_uInt32 nTypeId)
52 return (nTypeId == m_nTypeId);
55 storeError OStoreLockBytes::create (
56 OStorePageManager *pManager,
57 rtl_String const *pPath,
58 rtl_String const *pName,
59 storeAccessMode eMode)
61 rtl::Reference<OStorePageManager> xManager (pManager);
62 if (!xManager.is())
63 return store_E_InvalidAccess;
65 if (!(pPath && pName))
66 return store_E_InvalidParameter;
68 OStoreDirectoryPageObject aPage;
69 storeError eErrCode = xManager->iget (
70 aPage, STORE_ATTRIB_ISFILE,
71 pPath, pName, eMode);
72 if (eErrCode != store_E_None)
73 return eErrCode;
75 if (!(aPage.attrib() & STORE_ATTRIB_ISFILE))
77 // No ISFILE in older versions (backward compatibility).
78 if (aPage.attrib() & STORE_ATTRIB_ISLINK)
79 return store_E_NotFile;
82 inode_holder_type xNode (aPage.get());
83 if (eMode != storeAccessMode::ReadOnly)
84 eErrCode = xManager->acquirePage (xNode->m_aDescr, storeAccessMode::ReadWrite);
85 else
86 eErrCode = xManager->acquirePage (xNode->m_aDescr, storeAccessMode::ReadOnly);
87 if (eErrCode != store_E_None)
88 return eErrCode;
90 m_xManager = std::move(xManager);
91 m_xNode = xNode;
92 m_bWriteable = (eMode != storeAccessMode::ReadOnly);
94 // Check for truncation.
95 if (eMode == storeAccessMode::Create)
97 // Truncate to zero length.
98 eErrCode = setSize(0);
100 return eErrCode;
103 storeError OStoreLockBytes::readAt (
104 sal_uInt32 nOffset,
105 void *pBuffer,
106 sal_uInt32 nBytes,
107 sal_uInt32 &rnDone)
109 rnDone = 0;
111 if (!m_xManager.is())
112 return store_E_InvalidAccess;
114 if (!pBuffer)
115 return store_E_InvalidParameter;
116 if (!nBytes)
117 return store_E_None;
119 // Acquire exclusive access.
120 osl::MutexGuard aGuard (*m_xManager);
122 // Determine data length.
123 OStoreDirectoryPageObject aPage (m_xNode.get());
125 sal_uInt32 nDataLen = aPage.dataLength();
126 if ((nOffset + nBytes) > nDataLen)
127 nBytes = nDataLen - nOffset;
129 // Read data.
130 OStoreDataPageObject aData;
131 sal_uInt8 *pData = static_cast<sal_uInt8*>(pBuffer);
132 while ((0 < nBytes) && (nOffset < nDataLen))
134 // Determine 'Offset' scope.
135 inode::ChunkScope eScope = m_xNode->scope (nOffset);
136 if (eScope == inode::SCOPE_INTERNAL)
138 // Read from inode page (internal scope).
139 inode::ChunkDescriptor aDescr (
140 nOffset, m_xNode->capacity());
142 sal_uInt32 nLength = sal_uInt32(aDescr.m_nLength);
143 if(nLength > nBytes)
145 nLength = nBytes;
147 memcpy (
148 &pData[rnDone],
149 &m_xNode->m_pData[aDescr.m_nOffset],
150 nLength);
152 // Adjust counters.
153 rnDone += nLength;
154 nOffset += nLength;
155 nBytes -= nLength;
157 else
159 // Read from data page (external scope).
160 inode::ChunkDescriptor aDescr (
161 nOffset - m_xNode->capacity(), OStoreDataPageData::capacity(m_xNode->m_aDescr)); // @@@
163 sal_uInt32 nLength = sal_uInt32(aDescr.m_nLength);
164 if(nLength > nBytes)
166 nLength = nBytes;
169 storeError eErrCode = aPage.read (aDescr.m_nPage, aData, *m_xManager);
170 if (eErrCode != store_E_None)
172 if (eErrCode != store_E_NotExists)
173 return eErrCode;
175 memset (
176 &pData[rnDone],
178 nLength);
180 else
182 PageHolderObject< data > xData (aData.makeHolder<data>());
183 memcpy (
184 &pData[rnDone],
185 &xData->m_pData[aDescr.m_nOffset],
186 nLength);
189 // Adjust counters.
190 rnDone += nLength;
191 nOffset += nLength;
192 nBytes -= nLength;
196 // Done.
197 return store_E_None;
200 storeError OStoreLockBytes::writeAt (
201 sal_uInt32 nOffset,
202 const void *pBuffer,
203 sal_uInt32 nBytes,
204 sal_uInt32 &rnDone)
206 rnDone = 0;
208 if (!m_xManager.is())
209 return store_E_InvalidAccess;
210 if (!m_bWriteable)
211 return store_E_AccessViolation;
213 if (!pBuffer)
214 return store_E_InvalidParameter;
215 if (!nBytes)
216 return store_E_None;
218 // Acquire exclusive access.
219 osl::MutexGuard aGuard (*m_xManager);
221 // Write data.
222 OStoreDirectoryPageObject aPage (m_xNode.get());
223 const sal_uInt8 *pData = static_cast<const sal_uInt8*>(pBuffer);
225 storeError eErrCode = store_E_None;
226 while (nBytes > 0)
228 // Determine 'Offset' scope.
229 inode::ChunkScope eScope = m_xNode->scope (nOffset);
230 if (eScope == inode::SCOPE_INTERNAL)
232 // Write to inode page (internal scope).
233 inode::ChunkDescriptor aDescr (
234 nOffset, m_xNode->capacity());
236 sal_uInt32 nLength = sal_uInt32(aDescr.m_nLength);
237 if(nLength > nBytes)
239 nLength = nBytes;
242 memcpy (
243 &m_xNode->m_pData[aDescr.m_nOffset],
244 &pData[rnDone], nLength);
246 // Mark inode dirty.
247 aPage.touch();
249 // Adjust counters.
250 rnDone += nLength;
251 nOffset += nLength;
252 nBytes -= nLength;
254 // Adjust data length.
255 if (aPage.dataLength() < nOffset)
256 aPage.dataLength (nOffset);
258 else
260 // Write to data page (external scope).
261 OStoreDataPageObject aData;
263 inode::ChunkDescriptor aDescr (
264 nOffset - m_xNode->capacity(), OStoreDataPageData::capacity(m_xNode->m_aDescr)); // @@@
266 sal_uInt32 nLength = sal_uInt32(aDescr.m_nLength);
267 if ((aDescr.m_nOffset > 0) || (nBytes < nLength))
269 // Unaligned. Need to load/create data page.
270 eErrCode = aPage.read (aDescr.m_nPage, aData, *m_xManager);
271 if (eErrCode != store_E_None)
273 if (eErrCode != store_E_NotExists)
274 return eErrCode;
276 eErrCode = aData.construct<data>(m_xManager->allocator());
277 if (eErrCode != store_E_None)
278 return eErrCode;
282 PageHolderObject< data > xData (aData.makeHolder<data>());
283 if (!xData.is())
285 eErrCode = aData.construct<data>(m_xManager->allocator());
286 if (eErrCode != store_E_None)
287 return eErrCode;
288 xData = aData.makeHolder<data>();
291 // Modify data page.
292 if(nLength > nBytes)
294 nLength = nBytes;
296 memcpy (
297 &xData->m_pData[aDescr.m_nOffset],
298 &pData[rnDone], nLength);
300 // Save data page.
301 eErrCode = aPage.write (aDescr.m_nPage, aData, *m_xManager);
302 if (eErrCode != store_E_None)
303 return eErrCode;
305 // Adjust counters.
306 rnDone += nLength;
307 nOffset += nLength;
308 nBytes -= nLength;
310 // Adjust data length.
311 if (aPage.dataLength() < nOffset)
312 aPage.dataLength (nOffset);
316 // Check for modified inode.
317 if (aPage.dirty())
318 return m_xManager->saveObjectAt (aPage, aPage.location());
319 else
320 return store_E_None;
323 storeError OStoreLockBytes::setSize (sal_uInt32 nSize)
325 if (!m_xManager.is())
326 return store_E_InvalidAccess;
327 if (!m_bWriteable)
328 return store_E_AccessViolation;
330 // Acquire exclusive access.
331 osl::MutexGuard aGuard (*m_xManager);
333 // Determine current length.
334 OStoreDirectoryPageObject aPage (m_xNode.get());
335 sal_uInt32 nDataLen = aPage.dataLength();
337 if (nSize == nDataLen)
338 return store_E_None;
340 if (nSize < nDataLen)
342 // Truncate.
343 storeError eErrCode = store_E_None;
345 // Determine 'Size' scope.
346 inode::ChunkScope eSizeScope = m_xNode->scope (nSize);
347 if (eSizeScope == inode::SCOPE_INTERNAL)
349 // Internal 'Size' scope. Determine 'Data' scope.
350 inode::ChunkScope eDataScope = m_xNode->scope (nDataLen);
351 if (eDataScope == inode::SCOPE_EXTERNAL)
353 // External 'Data' scope. Truncate all external data pages.
354 eErrCode = aPage.truncate (0, *m_xManager);
355 if (eErrCode != store_E_None)
356 return eErrCode;
359 // Truncate internal data page.
360 inode::ChunkDescriptor aDescr (nSize, m_xNode->capacity());
361 memset (
362 &(m_xNode->m_pData[aDescr.m_nOffset]),
363 0, aDescr.m_nLength);
365 else
367 // External 'Size' scope. Truncate external data pages.
368 inode::ChunkDescriptor aDescr (
369 nSize - m_xNode->capacity(), OStoreDataPageData::capacity(m_xNode->m_aDescr)); // @@@
371 sal_uInt32 nPage = aDescr.m_nPage;
372 if (aDescr.m_nOffset) nPage += 1;
374 eErrCode = aPage.truncate (nPage, *m_xManager);
375 if (eErrCode != store_E_None)
376 return eErrCode;
380 // Set (extended or truncated) size.
381 aPage.dataLength (nSize);
383 // Save modified inode.
384 return m_xManager->saveObjectAt (aPage, aPage.location());
387 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */