1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
);
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
,
72 if (eErrCode
!= store_E_None
)
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
);
86 eErrCode
= xManager
->acquirePage (xNode
->m_aDescr
, storeAccessMode::ReadOnly
);
87 if (eErrCode
!= store_E_None
)
90 m_xManager
= std::move(xManager
);
92 m_bWriteable
= (eMode
!= storeAccessMode::ReadOnly
);
94 // Check for truncation.
95 if (eMode
== storeAccessMode::Create
)
97 // Truncate to zero length.
98 eErrCode
= setSize(0);
103 storeError
OStoreLockBytes::readAt (
111 if (!m_xManager
.is())
112 return store_E_InvalidAccess
;
115 return store_E_InvalidParameter
;
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
;
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
);
149 &m_xNode
->m_pData
[aDescr
.m_nOffset
],
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
);
169 storeError eErrCode
= aPage
.read (aDescr
.m_nPage
, aData
, *m_xManager
);
170 if (eErrCode
!= store_E_None
)
172 if (eErrCode
!= store_E_NotExists
)
182 PageHolderObject
< data
> xData (aData
.makeHolder
<data
>());
185 &xData
->m_pData
[aDescr
.m_nOffset
],
200 storeError
OStoreLockBytes::writeAt (
208 if (!m_xManager
.is())
209 return store_E_InvalidAccess
;
211 return store_E_AccessViolation
;
214 return store_E_InvalidParameter
;
218 // Acquire exclusive access.
219 osl::MutexGuard
aGuard (*m_xManager
);
222 OStoreDirectoryPageObject
aPage (m_xNode
.get());
223 const sal_uInt8
*pData
= static_cast<const sal_uInt8
*>(pBuffer
);
225 storeError eErrCode
= store_E_None
;
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
);
243 &m_xNode
->m_pData
[aDescr
.m_nOffset
],
244 &pData
[rnDone
], nLength
);
254 // Adjust data length.
255 if (aPage
.dataLength() < nOffset
)
256 aPage
.dataLength (nOffset
);
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
)
276 eErrCode
= aData
.construct
<data
>(m_xManager
->allocator());
277 if (eErrCode
!= store_E_None
)
282 PageHolderObject
< data
> xData (aData
.makeHolder
<data
>());
285 eErrCode
= aData
.construct
<data
>(m_xManager
->allocator());
286 if (eErrCode
!= store_E_None
)
288 xData
= aData
.makeHolder
<data
>();
297 &xData
->m_pData
[aDescr
.m_nOffset
],
298 &pData
[rnDone
], nLength
);
301 eErrCode
= aPage
.write (aDescr
.m_nPage
, aData
, *m_xManager
);
302 if (eErrCode
!= store_E_None
)
310 // Adjust data length.
311 if (aPage
.dataLength() < nOffset
)
312 aPage
.dataLength (nOffset
);
316 // Check for modified inode.
318 return m_xManager
->saveObjectAt (aPage
, aPage
.location());
323 storeError
OStoreLockBytes::setSize (sal_uInt32 nSize
)
325 if (!m_xManager
.is())
326 return store_E_InvalidAccess
;
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
)
340 if (nSize
< nDataLen
)
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
)
359 // Truncate internal data page.
360 inode::ChunkDescriptor
aDescr (nSize
, m_xNode
->capacity());
362 &(m_xNode
->m_pData
[aDescr
.m_nOffset
]),
363 0, aDescr
.m_nLength
);
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
)
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: */