LibreOffice Module store (master) 1
store.cxx
Go to the documentation of this file.
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#include <store/store.h>
21
22#include <sal/types.h>
23#include <rtl/string.hxx>
24#include <rtl/ref.hxx>
25
26#include "object.hxx"
27#include "lockbyte.hxx"
28
29#include "storbase.hxx"
30#include "storpage.hxx"
31#include "stordir.hxx"
32#include "storlckb.hxx"
33
34using rtl::Reference;
35
36namespace store
37{
38
39namespace {
40
43template<class store_handle_type>
44class OStoreHandle : public rtl::Reference<store_handle_type>
45{
46public:
47 explicit OStoreHandle (store_handle_type * pHandle)
48 : rtl::Reference<store_handle_type> (pHandle)
49 {}
50
51 static store_handle_type * SAL_CALL query (void * pHandle)
52 {
53 return store::query (
54 static_cast<OStoreObject*>(pHandle),
55 static_cast<store_handle_type*>(nullptr));
56 }
57};
58
59}
60
61}
62
63using namespace store;
64
66 storeHandle Handle
68{
69 OStoreObject *pHandle = static_cast<OStoreObject*>(Handle);
70 if (!pHandle)
72
73 pHandle->acquire();
74 return store_E_None;
75}
76
78 storeHandle Handle
80{
81 OStoreObject *pHandle = static_cast<OStoreObject*>(Handle);
82 if (!pHandle)
84
85 pHandle->release();
86 return store_E_None;
87}
88
90 sal_uInt16 nPageSize,
91 storeFileHandle *phFile
93{
94 if (!phFile)
96 *phFile = nullptr;
97
98 Reference<ILockBytes> xLockBytes;
99
100 storeError eErrCode = MemoryLockBytes_createInstance(xLockBytes);
101 if (eErrCode != store_E_None)
102 return eErrCode;
103 OSL_ASSERT(xLockBytes.is());
104
106 if (!xManager.is())
107 return store_E_OutOfMemory;
108
109 eErrCode = xManager->initialize (
110 &*xLockBytes, storeAccessMode::Create, nPageSize);
111 if (eErrCode != store_E_None)
112 return eErrCode;
113
114 xManager->acquire();
115
116 *phFile = xManager.get();
117 return store_E_None;
118}
119
121 rtl_uString *pFilename,
122 storeAccessMode eAccessMode,
123 sal_uInt16 nPageSize,
124 storeFileHandle *phFile
126{
127 if (phFile)
128 *phFile = nullptr;
129
130 if (!(pFilename && phFile))
132
133 Reference<ILockBytes> xLockBytes;
134
135 storeError eErrCode = FileLockBytes_createInstance (xLockBytes, pFilename, eAccessMode);
136 if (eErrCode != store_E_None)
137 return eErrCode;
138 OSL_ASSERT(xLockBytes.is());
139
141 if (!xManager.is())
142 return store_E_OutOfMemory;
143
144 eErrCode = xManager->initialize (
145 &*xLockBytes, eAccessMode, nPageSize);
146 if (eErrCode != store_E_None)
147 return eErrCode;
148
149 xManager->acquire();
150
151 *phFile = xManager.get();
152 return store_E_None;
153}
154
155/*
156 * store_closeFile.
157 */
159 storeFileHandle Handle
161{
162 OStorePageManager *pManager =
164 if (!pManager)
166
167 storeError eErrCode = pManager->close();
168 pManager->release();
169 return eErrCode;
170}
171
173 storeFileHandle Handle
175{
176 OStoreHandle<OStorePageManager> xManager (
178 if (!xManager.is())
180
181 return xManager->flush();
182}
183
185 storeFileHandle hFile,
186 rtl_uString const *pPath,
187 rtl_uString const *pName,
188 storeAccessMode eAccessMode,
189 storeDirectoryHandle *phDirectory
191{
192 storeError eErrCode = store_E_None;
193 if (phDirectory)
194 *phDirectory = nullptr;
195
196 OStoreHandle<OStorePageManager> xManager (
198 if (!xManager.is())
200
201 if (!(pPath && pName && phDirectory))
203
205 if (!xDirectory.is())
206 return store_E_OutOfMemory;
207
208 OString aPath (pPath->buffer, pPath->length, RTL_TEXTENCODING_UTF8);
209 OString aName (pName->buffer, pName->length, RTL_TEXTENCODING_UTF8);
210
211 eErrCode = xDirectory->create (&*xManager, aPath.pData, aName.pData, eAccessMode);
212 if (eErrCode != store_E_None)
213 return eErrCode;
214
215 xDirectory->acquire();
216
217 *phDirectory = xDirectory.get();
218 return store_E_None;
219}
220
223 storeFindData *pFindData
225{
226 OStoreHandle<OStoreDirectory_Impl> xDirectory (
228 if (!xDirectory.is())
230
231 if (!pFindData)
233
234 // Initialize FindData.
235 memset (pFindData, 0, sizeof (storeFindData));
236
237 // Find first.
238 pFindData->m_nReserved = sal_uInt32(~0);
239 return xDirectory->iterate (*pFindData);
240}
241
244 storeFindData *pFindData
246{
247 OStoreHandle<OStoreDirectory_Impl> xDirectory (
249 if (!xDirectory.is())
251
252 if (!pFindData)
254
255 // Check FindData.
256 if (!pFindData->m_nReserved)
257 return store_E_NoMoreFiles;
258
259 // Find next.
260 pFindData->m_nReserved -= 1;
261 return xDirectory->iterate (*pFindData);
262}
263
265 storeFileHandle hFile,
266 rtl_uString const *pPath,
267 rtl_uString const *pName,
268 storeAccessMode eAccessMode,
269 storeStreamHandle *phStream
271{
272 storeError eErrCode = store_E_None;
273 if (phStream)
274 *phStream = nullptr;
275
276 OStoreHandle<OStorePageManager> xManager (
278 if (!xManager.is())
280
281 if (!(pPath && pName && phStream))
283
285 if (!xLockBytes.is())
286 return store_E_OutOfMemory;
287
288 OString aPath (pPath->buffer, pPath->length, RTL_TEXTENCODING_UTF8);
289 OString aName (pName->buffer, pName->length, RTL_TEXTENCODING_UTF8);
290
291 eErrCode = xLockBytes->create (&*xManager, aPath.pData, aName.pData, eAccessMode);
292 if (eErrCode != store_E_None)
293 return eErrCode;
294
295 xLockBytes->acquire();
296
297 *phStream = xLockBytes.get();
298 return store_E_None;
299}
300
301/*
302 * store_readStream.
303 */
305 storeStreamHandle Handle,
306 sal_uInt32 nOffset,
307 void *pBuffer,
308 sal_uInt32 nBytes,
309 sal_uInt32 *pnDone
311{
312 OStoreHandle<OStoreLockBytes> xLockBytes (
314 if (!xLockBytes.is())
316
317 if (!(pBuffer && pnDone))
319
320 return xLockBytes->readAt (nOffset, pBuffer, nBytes, *pnDone);
321}
322
324 storeStreamHandle Handle,
325 sal_uInt32 nOffset,
326 const void *pBuffer,
327 sal_uInt32 nBytes,
328 sal_uInt32 *pnDone
330{
331 OStoreHandle<OStoreLockBytes> xLockBytes (
333 if (!xLockBytes.is())
335
336 if (!(pBuffer && pnDone))
338
339 return xLockBytes->writeAt (nOffset, pBuffer, nBytes, *pnDone);
340}
341
343 storeFileHandle Handle,
344 rtl_uString const *pPath,
345 rtl_uString const *pName
347{
348 storeError eErrCode = store_E_None;
349
350 OStoreHandle<OStorePageManager> xManager (
352 if (!xManager.is())
354
355 if (!(pPath && pName))
357
358 // Setup page key.
359 OString aPath (pPath->buffer, pPath->length, RTL_TEXTENCODING_UTF8);
360 OString aName (pName->buffer, pName->length, RTL_TEXTENCODING_UTF8);
361 OStorePageKey aKey;
362
363 eErrCode = OStorePageManager::namei (aPath.pData, aName.pData, aKey);
364 if (eErrCode != store_E_None)
365 return eErrCode;
366
367 // Remove.
368 return xManager->remove (aKey);
369}
370
371/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const char * pName
storeError close()
close.
Definition: storbios.cxx:862
OUString aName
Old OStorePageCache implementation.
Definition: lockbyte.cxx:133
storeError FileLockBytes_createInstance(rtl::Reference< ILockBytes > &rxLockBytes, rtl_uString *pFilename, storeAccessMode eAccessMode)
Definition: lockbyte.cxx:837
storeError MemoryLockBytes_createInstance(rtl::Reference< ILockBytes > &rxLockBytes)
Definition: lockbyte.cxx:873
store_handle_type *SAL_CALL query(OStoreObject *pHandle, store_handle_type *)
Template helper function as dynamic_cast replacement.
storeError store_remove(storeFileHandle Handle, rtl_uString const *pPath, rtl_uString const *pName) SAL_THROW_EXTERN_C()
Remove a file entry.
Definition: store.cxx:342
storeError store_acquireHandle(storeHandle Handle) SAL_THROW_EXTERN_C()
Acquire a Handle.
Definition: store.cxx:65
storeError store_findFirst(storeDirectoryHandle Handle, storeFindData *pFindData) SAL_THROW_EXTERN_C()
Find first directory entry.
Definition: store.cxx:221
storeError store_releaseHandle(storeHandle Handle) SAL_THROW_EXTERN_C()
Release a Handle.
Definition: store.cxx:77
storeError store_readStream(storeStreamHandle Handle, sal_uInt32 nOffset, void *pBuffer, sal_uInt32 nBytes, sal_uInt32 *pnDone) SAL_THROW_EXTERN_C()
Read from a stream.
Definition: store.cxx:304
storeError store_openStream(storeFileHandle hFile, rtl_uString const *pPath, rtl_uString const *pName, storeAccessMode eAccessMode, storeStreamHandle *phStream) SAL_THROW_EXTERN_C()
Open a stream.
Definition: store.cxx:264
storeError store_findNext(storeDirectoryHandle Handle, storeFindData *pFindData) SAL_THROW_EXTERN_C()
Find next directory entry.
Definition: store.cxx:242
storeError store_openDirectory(storeFileHandle hFile, rtl_uString const *pPath, rtl_uString const *pName, storeAccessMode eAccessMode, storeDirectoryHandle *phDirectory) SAL_THROW_EXTERN_C()
Open a directory.
Definition: store.cxx:184
storeError store_flushFile(storeFileHandle Handle) SAL_THROW_EXTERN_C()
Flush a file.
Definition: store.cxx:172
storeError store_createMemoryFile(sal_uInt16 nPageSize, storeFileHandle *phFile) SAL_THROW_EXTERN_C()
Open a temporary file in memory.
Definition: store.cxx:89
storeError store_closeFile(storeFileHandle Handle) SAL_THROW_EXTERN_C()
Close a file.
Definition: store.cxx:158
storeError store_writeStream(storeStreamHandle Handle, sal_uInt32 nOffset, const void *pBuffer, sal_uInt32 nBytes, sal_uInt32 *pnDone) SAL_THROW_EXTERN_C()
Write to a stream.
Definition: store.cxx:323
storeError store_openFile(rtl_uString *pFilename, storeAccessMode eAccessMode, sal_uInt16 nPageSize, storeFileHandle *phFile) SAL_THROW_EXTERN_C()
Open a file.
Definition: store.cxx:120
void * storeHandle
Handle opaque type.
Definition: store.h:33
void * storeStreamHandle
Stream Handle opaque type.
Definition: store.h:149
void * storeDirectoryHandle
Directory Handle opaque type.
Definition: store.h:105
void * storeFileHandle
File Handle opaque type.
Definition: store.h:54
Find Data structure.
Definition: types.h:105
storeAccessMode
Access Mode enumeration.
Definition: types.h:62
storeError
Error Code enumeration.
Definition: types.h:73
@ store_E_InvalidParameter
Definition: types.h:82
@ store_E_InvalidHandle
Definition: types.h:81
@ store_E_OutOfMemory
Definition: types.h:90
@ store_E_None
Definition: types.h:74
@ store_E_NoMoreFiles
Definition: types.h:88
#define SAL_THROW_EXTERN_C()