Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / store / source / store.cxx
blob254e064915ad016e6976a15c1f0c7fcb5f23a37b
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 <store/store.h>
22 #include <sal/types.h>
23 #include <rtl/string.hxx>
24 #include <rtl/ref.hxx>
26 #include "object.hxx"
27 #include "lockbyte.hxx"
29 #include "storbase.hxx"
30 #include "storpage.hxx"
31 #include "stordir.hxx"
32 #include "storlckb.hxx"
34 using rtl::Reference;
36 namespace store
39 namespace {
41 /** Template helper class as type safe Reference to store_handle_type.
43 template<class store_handle_type>
44 class OStoreHandle : public rtl::Reference<store_handle_type>
46 public:
47 explicit OStoreHandle (store_handle_type * pHandle)
48 : rtl::Reference<store_handle_type> (pHandle)
51 static store_handle_type * SAL_CALL query (void * pHandle)
53 return store::query (
54 static_cast<OStoreObject*>(pHandle),
55 static_cast<store_handle_type*>(nullptr));
63 using namespace store;
65 storeError store_acquireHandle (
66 storeHandle Handle
67 ) SAL_THROW_EXTERN_C()
69 OStoreObject *pHandle = static_cast<OStoreObject*>(Handle);
70 if (!pHandle)
71 return store_E_InvalidHandle;
73 pHandle->acquire();
74 return store_E_None;
77 storeError store_releaseHandle (
78 storeHandle Handle
79 ) SAL_THROW_EXTERN_C()
81 OStoreObject *pHandle = static_cast<OStoreObject*>(Handle);
82 if (!pHandle)
83 return store_E_InvalidHandle;
85 pHandle->release();
86 return store_E_None;
89 storeError store_createMemoryFile (
90 sal_uInt16 nPageSize,
91 storeFileHandle *phFile
92 ) SAL_THROW_EXTERN_C()
94 if (!phFile)
95 return store_E_InvalidParameter;
96 *phFile = nullptr;
98 Reference<ILockBytes> xLockBytes;
100 storeError eErrCode = MemoryLockBytes_createInstance(xLockBytes);
101 if (eErrCode != store_E_None)
102 return eErrCode;
103 OSL_ASSERT(xLockBytes.is());
105 Reference<OStorePageManager> xManager (new OStorePageManager());
106 if (!xManager.is())
107 return store_E_OutOfMemory;
109 eErrCode = xManager->initialize (
110 &*xLockBytes, storeAccessMode::Create, nPageSize);
111 if (eErrCode != store_E_None)
112 return eErrCode;
114 xManager->acquire();
116 *phFile = xManager.get();
117 return store_E_None;
120 storeError store_openFile (
121 rtl_uString *pFilename,
122 storeAccessMode eAccessMode,
123 sal_uInt16 nPageSize,
124 storeFileHandle *phFile
125 ) SAL_THROW_EXTERN_C()
127 if (phFile)
128 *phFile = nullptr;
130 if (!(pFilename && phFile))
131 return store_E_InvalidParameter;
133 Reference<ILockBytes> xLockBytes;
135 storeError eErrCode = FileLockBytes_createInstance (xLockBytes, pFilename, eAccessMode);
136 if (eErrCode != store_E_None)
137 return eErrCode;
138 OSL_ASSERT(xLockBytes.is());
140 Reference<OStorePageManager> xManager (new OStorePageManager());
141 if (!xManager.is())
142 return store_E_OutOfMemory;
144 eErrCode = xManager->initialize (
145 &*xLockBytes, eAccessMode, nPageSize);
146 if (eErrCode != store_E_None)
147 return eErrCode;
149 xManager->acquire();
151 *phFile = xManager.get();
152 return store_E_None;
156 * store_closeFile.
158 storeError store_closeFile (
159 storeFileHandle Handle
160 ) SAL_THROW_EXTERN_C()
162 OStorePageManager *pManager =
163 OStoreHandle<OStorePageManager>::query (Handle);
164 if (!pManager)
165 return store_E_InvalidHandle;
167 storeError eErrCode = pManager->close();
168 pManager->release();
169 return eErrCode;
172 storeError store_flushFile (
173 storeFileHandle Handle
174 ) SAL_THROW_EXTERN_C()
176 OStoreHandle<OStorePageManager> xManager (
177 OStoreHandle<OStorePageManager>::query (Handle));
178 if (!xManager.is())
179 return store_E_InvalidHandle;
181 return xManager->flush();
184 storeError store_openDirectory (
185 storeFileHandle hFile,
186 rtl_uString const *pPath,
187 rtl_uString const *pName,
188 storeAccessMode eAccessMode,
189 storeDirectoryHandle *phDirectory
190 ) SAL_THROW_EXTERN_C()
192 storeError eErrCode = store_E_None;
193 if (phDirectory)
194 *phDirectory = nullptr;
196 OStoreHandle<OStorePageManager> xManager (
197 OStoreHandle<OStorePageManager>::query (hFile));
198 if (!xManager.is())
199 return store_E_InvalidHandle;
201 if (!(pPath && pName && phDirectory))
202 return store_E_InvalidParameter;
204 Reference<OStoreDirectory_Impl> xDirectory (new OStoreDirectory_Impl());
205 if (!xDirectory.is())
206 return store_E_OutOfMemory;
208 OString aPath (pPath->buffer, pPath->length, RTL_TEXTENCODING_UTF8);
209 OString aName (pName->buffer, pName->length, RTL_TEXTENCODING_UTF8);
211 eErrCode = xDirectory->create (&*xManager, aPath.pData, aName.pData, eAccessMode);
212 if (eErrCode != store_E_None)
213 return eErrCode;
215 xDirectory->acquire();
217 *phDirectory = xDirectory.get();
218 return store_E_None;
221 storeError store_findFirst (
222 storeDirectoryHandle Handle,
223 storeFindData *pFindData
224 ) SAL_THROW_EXTERN_C()
226 OStoreHandle<OStoreDirectory_Impl> xDirectory (
227 OStoreHandle<OStoreDirectory_Impl>::query (Handle));
228 if (!xDirectory.is())
229 return store_E_InvalidHandle;
231 if (!pFindData)
232 return store_E_InvalidParameter;
234 // Initialize FindData.
235 memset (pFindData, 0, sizeof (storeFindData));
237 // Find first.
238 pFindData->m_nReserved = sal_uInt32(~0);
239 return xDirectory->iterate (*pFindData);
242 storeError store_findNext (
243 storeDirectoryHandle Handle,
244 storeFindData *pFindData
245 ) SAL_THROW_EXTERN_C()
247 OStoreHandle<OStoreDirectory_Impl> xDirectory (
248 OStoreHandle<OStoreDirectory_Impl>::query (Handle));
249 if (!xDirectory.is())
250 return store_E_InvalidHandle;
252 if (!pFindData)
253 return store_E_InvalidParameter;
255 // Check FindData.
256 if (!pFindData->m_nReserved)
257 return store_E_NoMoreFiles;
259 // Find next.
260 pFindData->m_nReserved -= 1;
261 return xDirectory->iterate (*pFindData);
264 storeError store_openStream (
265 storeFileHandle hFile,
266 rtl_uString const *pPath,
267 rtl_uString const *pName,
268 storeAccessMode eAccessMode,
269 storeStreamHandle *phStream
270 ) SAL_THROW_EXTERN_C()
272 storeError eErrCode = store_E_None;
273 if (phStream)
274 *phStream = nullptr;
276 OStoreHandle<OStorePageManager> xManager (
277 OStoreHandle<OStorePageManager>::query (hFile));
278 if (!xManager.is())
279 return store_E_InvalidHandle;
281 if (!(pPath && pName && phStream))
282 return store_E_InvalidParameter;
284 Reference<OStoreLockBytes> xLockBytes (new OStoreLockBytes());
285 if (!xLockBytes.is())
286 return store_E_OutOfMemory;
288 OString aPath (pPath->buffer, pPath->length, RTL_TEXTENCODING_UTF8);
289 OString aName (pName->buffer, pName->length, RTL_TEXTENCODING_UTF8);
291 eErrCode = xLockBytes->create (&*xManager, aPath.pData, aName.pData, eAccessMode);
292 if (eErrCode != store_E_None)
293 return eErrCode;
295 xLockBytes->acquire();
297 *phStream = xLockBytes.get();
298 return store_E_None;
302 * store_readStream.
304 storeError store_readStream (
305 storeStreamHandle Handle,
306 sal_uInt32 nOffset,
307 void *pBuffer,
308 sal_uInt32 nBytes,
309 sal_uInt32 *pnDone
310 ) SAL_THROW_EXTERN_C()
312 OStoreHandle<OStoreLockBytes> xLockBytes (
313 OStoreHandle<OStoreLockBytes>::query (Handle));
314 if (!xLockBytes.is())
315 return store_E_InvalidHandle;
317 if (!(pBuffer && pnDone))
318 return store_E_InvalidParameter;
320 return xLockBytes->readAt (nOffset, pBuffer, nBytes, *pnDone);
323 storeError store_writeStream (
324 storeStreamHandle Handle,
325 sal_uInt32 nOffset,
326 const void *pBuffer,
327 sal_uInt32 nBytes,
328 sal_uInt32 *pnDone
329 ) SAL_THROW_EXTERN_C()
331 OStoreHandle<OStoreLockBytes> xLockBytes (
332 OStoreHandle<OStoreLockBytes>::query (Handle));
333 if (!xLockBytes.is())
334 return store_E_InvalidHandle;
336 if (!(pBuffer && pnDone))
337 return store_E_InvalidParameter;
339 return xLockBytes->writeAt (nOffset, pBuffer, nBytes, *pnDone);
342 storeError store_remove (
343 storeFileHandle Handle,
344 rtl_uString const *pPath,
345 rtl_uString const *pName
346 ) SAL_THROW_EXTERN_C()
348 storeError eErrCode = store_E_None;
350 OStoreHandle<OStorePageManager> xManager (
351 OStoreHandle<OStorePageManager>::query (Handle));
352 if (!xManager.is())
353 return store_E_InvalidHandle;
355 if (!(pPath && pName))
356 return store_E_InvalidParameter;
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;
363 eErrCode = OStorePageManager::namei (aPath.pData, aName.pData, aKey);
364 if (eErrCode != store_E_None)
365 return eErrCode;
367 // Remove.
368 return xManager->remove (aKey);
371 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */