bump product version to 4.1.6.2
[LibreOffice.git] / store / source / store.cxx
blob11780b6e57d62ae9bab54d970f1617cad6c8d51d
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 .
21 #include "store/store.h"
23 #include <sal/types.h>
24 #include <rtl/string.hxx>
25 #include <rtl/ref.hxx>
27 #include "object.hxx"
28 #include "lockbyte.hxx"
30 #include "storbase.hxx"
31 #include "storpage.hxx"
32 #include "stordir.hxx"
33 #include "storlckb.hxx"
35 using rtl::Reference;
37 namespace store
39 /** Template helper class as type safe Reference to store_handle_type.
41 template<class store_handle_type>
42 class OStoreHandle : public rtl::Reference<store_handle_type>
44 public:
45 OStoreHandle (store_handle_type * pHandle)
46 : rtl::Reference<store_handle_type> (pHandle)
49 static store_handle_type * SAL_CALL query (void * pHandle)
51 return store::query (
52 static_cast<IStoreHandle*>(pHandle),
53 static_cast<store_handle_type*>(0));
59 using namespace store;
61 /*========================================================================
63 * storeHandle implementation.
65 *======================================================================*/
67 * store_acquireHandle.
69 storeError SAL_CALL store_acquireHandle (
70 storeHandle Handle
71 ) SAL_THROW_EXTERN_C()
73 IStoreHandle *pHandle = static_cast<IStoreHandle*>(Handle);
74 if (!pHandle)
75 return store_E_InvalidHandle;
77 pHandle->acquire();
78 return store_E_None;
82 * store_releaseHandle.
84 storeError SAL_CALL store_releaseHandle (
85 storeHandle Handle
86 ) SAL_THROW_EXTERN_C()
88 IStoreHandle *pHandle = static_cast<IStoreHandle*>(Handle);
89 if (!pHandle)
90 return store_E_InvalidHandle;
92 pHandle->release();
93 return store_E_None;
96 /*========================================================================
98 * storeFileHandle implementation.
100 *======================================================================*/
102 * store_createMemoryFile.
104 storeError SAL_CALL store_createMemoryFile (
105 sal_uInt16 nPageSize,
106 storeFileHandle *phFile
107 ) SAL_THROW_EXTERN_C()
109 if (!phFile)
110 return store_E_InvalidParameter;
111 *phFile = NULL;
113 Reference<ILockBytes> xLockBytes;
115 storeError eErrCode = MemoryLockBytes_createInstance(xLockBytes);
116 if (eErrCode != store_E_None)
117 return eErrCode;
118 OSL_ASSERT(xLockBytes.is());
120 Reference<OStorePageManager> xManager (new OStorePageManager());
121 if (!xManager.is())
122 return store_E_OutOfMemory;
124 eErrCode = xManager->initialize (
125 &*xLockBytes, store_AccessCreate, nPageSize);
126 if (eErrCode != store_E_None)
127 return eErrCode;
129 xManager->acquire();
131 *phFile = (storeFileHandle)&(*xManager);
132 return store_E_None;
136 * store_openFile.
138 storeError SAL_CALL store_openFile (
139 rtl_uString *pFilename,
140 storeAccessMode eAccessMode,
141 sal_uInt16 nPageSize,
142 storeFileHandle *phFile
143 ) SAL_THROW_EXTERN_C()
145 if (phFile)
146 *phFile = NULL;
148 if (!(pFilename && phFile))
149 return store_E_InvalidParameter;
151 Reference<ILockBytes> xLockBytes;
153 storeError eErrCode = FileLockBytes_createInstance (xLockBytes, pFilename, eAccessMode);
154 if (eErrCode != store_E_None)
155 return eErrCode;
156 OSL_ASSERT(xLockBytes.is());
158 Reference<OStorePageManager> xManager (new OStorePageManager());
159 if (!xManager.is())
160 return store_E_OutOfMemory;
162 eErrCode = xManager->initialize (
163 &*xLockBytes, eAccessMode, nPageSize);
164 if (eErrCode != store_E_None)
165 return eErrCode;
167 xManager->acquire();
169 *phFile = (storeFileHandle)&(*xManager);
170 return store_E_None;
174 * store_closeFile.
176 storeError SAL_CALL store_closeFile (
177 storeFileHandle Handle
178 ) SAL_THROW_EXTERN_C()
180 OStorePageManager *pManager =
181 OStoreHandle<OStorePageManager>::query (Handle);
182 if (!pManager)
183 return store_E_InvalidHandle;
185 storeError eErrCode = pManager->close();
186 pManager->release();
187 return eErrCode;
191 * store_flushFile.
193 storeError SAL_CALL store_flushFile (
194 storeFileHandle Handle
195 ) SAL_THROW_EXTERN_C()
197 OStoreHandle<OStorePageManager> xManager (
198 OStoreHandle<OStorePageManager>::query (Handle));
199 if (!xManager.is())
200 return store_E_InvalidHandle;
202 return xManager->flush();
206 * store_getFileRefererCount.
208 storeError SAL_CALL store_getFileRefererCount (
209 storeFileHandle Handle,
210 sal_uInt32 *pnRefCount
211 ) SAL_THROW_EXTERN_C()
213 OStoreHandle<OStorePageManager> xManager (
214 OStoreHandle<OStorePageManager>::query (Handle));
215 if (!xManager.is())
216 return store_E_InvalidHandle;
218 if (!pnRefCount)
219 return store_E_InvalidParameter;
221 *pnRefCount = xManager->getRefererCount();
222 return store_E_None;
226 * store_getFileSize.
228 storeError SAL_CALL store_getFileSize (
229 storeFileHandle Handle,
230 sal_uInt32 *pnSize
231 ) SAL_THROW_EXTERN_C()
233 OStoreHandle<OStorePageManager> xManager (
234 OStoreHandle<OStorePageManager>::query (Handle));
235 if (!xManager.is())
236 return store_E_InvalidHandle;
238 if (!pnSize)
239 return store_E_InvalidParameter;
241 return xManager->size (*pnSize);
245 * store_rebuildFile.
247 storeError SAL_CALL store_rebuildFile (
248 rtl_uString *pSrcFilename,
249 rtl_uString *pDstFilename
250 ) SAL_THROW_EXTERN_C()
252 storeError eErrCode = store_E_None;
254 if (!(pSrcFilename && pDstFilename))
255 return store_E_InvalidParameter;
257 Reference<OStorePageManager> xManager (new OStorePageManager());
258 if (!xManager.is())
259 return store_E_OutOfMemory;
261 Reference<ILockBytes> xSrcLB;
262 eErrCode = FileLockBytes_createInstance (xSrcLB, pSrcFilename, store_AccessReadOnly);
263 if (eErrCode != store_E_None)
264 return eErrCode;
265 OSL_ASSERT(xSrcLB.is());
267 Reference<ILockBytes> xDstLB;
268 eErrCode = FileLockBytes_createInstance (xDstLB, pDstFilename, store_AccessCreate);
269 if (eErrCode != store_E_None)
270 return eErrCode;
271 OSL_ASSERT(xDstLB.is());
273 return xManager->rebuild (&*xSrcLB, &*xDstLB);
276 /*========================================================================
278 * storeDirectoryHandle implementation.
280 *======================================================================*/
282 * store_openDirectory.
284 storeError SAL_CALL store_openDirectory (
285 storeFileHandle hFile,
286 rtl_uString *pPath,
287 rtl_uString *pName,
288 storeAccessMode eAccessMode,
289 storeDirectoryHandle *phDirectory
290 ) SAL_THROW_EXTERN_C()
292 storeError eErrCode = store_E_None;
293 if (phDirectory)
294 *phDirectory = NULL;
296 OStoreHandle<OStorePageManager> xManager (
297 OStoreHandle<OStorePageManager>::query (hFile));
298 if (!xManager.is())
299 return store_E_InvalidHandle;
301 if (!(pPath && pName && phDirectory))
302 return store_E_InvalidParameter;
304 Reference<OStoreDirectory_Impl> xDirectory (new OStoreDirectory_Impl());
305 if (!xDirectory.is())
306 return store_E_OutOfMemory;
308 OString aPath (pPath->buffer, pPath->length, RTL_TEXTENCODING_UTF8);
309 OString aName (pName->buffer, pName->length, RTL_TEXTENCODING_UTF8);
311 eErrCode = xDirectory->create (&*xManager, aPath.pData, aName.pData, eAccessMode);
312 if (eErrCode != store_E_None)
313 return eErrCode;
315 xDirectory->acquire();
317 *phDirectory = (storeDirectoryHandle)&(*xDirectory);
318 return store_E_None;
322 * store_closeDirectory.
324 storeError SAL_CALL store_closeDirectory (
325 storeDirectoryHandle Handle
326 ) SAL_THROW_EXTERN_C()
328 OStoreDirectory_Impl *pDirectory =
329 OStoreHandle<OStoreDirectory_Impl>::query (Handle);
330 if (!pDirectory)
331 return store_E_InvalidHandle;
333 pDirectory->release();
334 return store_E_None;
338 * store_findFirst.
340 storeError SAL_CALL store_findFirst (
341 storeDirectoryHandle Handle,
342 storeFindData *pFindData
343 ) SAL_THROW_EXTERN_C()
345 OStoreHandle<OStoreDirectory_Impl> xDirectory (
346 OStoreHandle<OStoreDirectory_Impl>::query (Handle));
347 if (!xDirectory.is())
348 return store_E_InvalidHandle;
350 if (!pFindData)
351 return store_E_InvalidParameter;
353 // Initialize FindData.
354 memset (pFindData, 0, sizeof (storeFindData));
356 // Find first.
357 pFindData->m_nReserved = (sal_uInt32)(~0);
358 return xDirectory->iterate (*pFindData);
362 * store_findNext.
364 storeError SAL_CALL store_findNext (
365 storeDirectoryHandle Handle,
366 storeFindData *pFindData
367 ) SAL_THROW_EXTERN_C()
369 OStoreHandle<OStoreDirectory_Impl> xDirectory (
370 OStoreHandle<OStoreDirectory_Impl>::query (Handle));
371 if (!xDirectory.is())
372 return store_E_InvalidHandle;
374 if (!pFindData)
375 return store_E_InvalidParameter;
377 // Check FindData.
378 if (!pFindData->m_nReserved)
379 return store_E_NoMoreFiles;
381 // Find next.
382 pFindData->m_nReserved -= 1;
383 return xDirectory->iterate (*pFindData);
386 /*========================================================================
388 * storeStreamHandle implementation.
390 *======================================================================*/
392 * store_openStream
394 storeError SAL_CALL store_openStream (
395 storeFileHandle hFile,
396 rtl_uString *pPath,
397 rtl_uString *pName,
398 storeAccessMode eAccessMode,
399 storeStreamHandle *phStream
400 ) SAL_THROW_EXTERN_C()
402 storeError eErrCode = store_E_None;
403 if (phStream)
404 *phStream = NULL;
406 OStoreHandle<OStorePageManager> xManager (
407 OStoreHandle<OStorePageManager>::query (hFile));
408 if (!xManager.is())
409 return store_E_InvalidHandle;
411 if (!(pPath && pName && phStream))
412 return store_E_InvalidParameter;
414 Reference<OStoreLockBytes> xLockBytes (new OStoreLockBytes());
415 if (!xLockBytes.is())
416 return store_E_OutOfMemory;
418 OString aPath (pPath->buffer, pPath->length, RTL_TEXTENCODING_UTF8);
419 OString aName (pName->buffer, pName->length, RTL_TEXTENCODING_UTF8);
421 eErrCode = xLockBytes->create (&*xManager, aPath.pData, aName.pData, eAccessMode);
422 if (eErrCode != store_E_None)
423 return eErrCode;
425 xLockBytes->acquire();
427 *phStream = (storeStreamHandle)&(*xLockBytes);
428 return store_E_None;
432 * store_closeStream.
434 storeError SAL_CALL store_closeStream (
435 storeStreamHandle Handle
436 ) SAL_THROW_EXTERN_C()
438 OStoreLockBytes *pLockBytes =
439 OStoreHandle<OStoreLockBytes>::query (Handle);
440 if (!pLockBytes)
441 return store_E_InvalidHandle;
443 pLockBytes->release();
444 return store_E_None;
448 * store_readStream.
450 storeError SAL_CALL store_readStream (
451 storeStreamHandle Handle,
452 sal_uInt32 nOffset,
453 void *pBuffer,
454 sal_uInt32 nBytes,
455 sal_uInt32 *pnDone
456 ) SAL_THROW_EXTERN_C()
458 OStoreHandle<OStoreLockBytes> xLockBytes (
459 OStoreHandle<OStoreLockBytes>::query (Handle));
460 if (!xLockBytes.is())
461 return store_E_InvalidHandle;
463 if (!(pBuffer && pnDone))
464 return store_E_InvalidParameter;
466 return xLockBytes->readAt (nOffset, pBuffer, nBytes, *pnDone);
470 * store_writeStream.
472 storeError SAL_CALL store_writeStream (
473 storeStreamHandle Handle,
474 sal_uInt32 nOffset,
475 const void *pBuffer,
476 sal_uInt32 nBytes,
477 sal_uInt32 *pnDone
478 ) SAL_THROW_EXTERN_C()
480 OStoreHandle<OStoreLockBytes> xLockBytes (
481 OStoreHandle<OStoreLockBytes>::query (Handle));
482 if (!xLockBytes.is())
483 return store_E_InvalidHandle;
485 if (!(pBuffer && pnDone))
486 return store_E_InvalidParameter;
488 return xLockBytes->writeAt (nOffset, pBuffer, nBytes, *pnDone);
492 * store_flushStream.
494 storeError SAL_CALL store_flushStream (
495 storeStreamHandle Handle
496 ) SAL_THROW_EXTERN_C()
498 OStoreHandle<OStoreLockBytes> xLockBytes (
499 OStoreHandle<OStoreLockBytes>::query (Handle));
500 if (!xLockBytes.is())
501 return store_E_InvalidHandle;
503 return xLockBytes->flush();
507 * store_getStreamSize.
509 storeError SAL_CALL store_getStreamSize (
510 storeStreamHandle Handle,
511 sal_uInt32 *pnSize
512 ) SAL_THROW_EXTERN_C()
514 OStoreHandle<OStoreLockBytes> xLockBytes (
515 OStoreHandle<OStoreLockBytes>::query (Handle));
516 if (!xLockBytes.is())
517 return store_E_InvalidHandle;
519 if (!pnSize)
520 return store_E_InvalidParameter;
522 return xLockBytes->stat (*pnSize);
526 * store_setStreamSize.
528 storeError SAL_CALL store_setStreamSize (
529 storeStreamHandle Handle,
530 sal_uInt32 nSize
531 ) SAL_THROW_EXTERN_C()
533 OStoreHandle<OStoreLockBytes> xLockBytes (
534 OStoreHandle<OStoreLockBytes>::query (Handle));
535 if (!xLockBytes.is())
536 return store_E_InvalidHandle;
538 return xLockBytes->setSize (nSize);
541 /*========================================================================
543 * Common storeDirectoryHandle and storeStreamHandle operations.
545 *======================================================================*/
547 * store_attrib.
549 storeError SAL_CALL store_attrib (
550 storeFileHandle Handle,
551 rtl_uString *pPath,
552 rtl_uString *pName,
553 sal_uInt32 nMask1,
554 sal_uInt32 nMask2,
555 sal_uInt32 *pnAttrib
556 ) SAL_THROW_EXTERN_C()
558 storeError eErrCode = store_E_None;
559 if (pnAttrib)
560 *pnAttrib = 0;
562 OStoreHandle<OStorePageManager> xManager (
563 OStoreHandle<OStorePageManager>::query (Handle));
564 if (!xManager.is())
565 return store_E_InvalidHandle;
567 if (!(pPath && pName))
568 return store_E_InvalidParameter;
570 // Setup page key.
571 OString aPath (pPath->buffer, pPath->length, RTL_TEXTENCODING_UTF8);
572 OString aName (pName->buffer, pName->length, RTL_TEXTENCODING_UTF8);
573 OStorePageKey aKey;
575 eErrCode = OStorePageManager::namei (aPath.pData, aName.pData, aKey);
576 if (eErrCode != store_E_None)
577 return eErrCode;
579 // Obtain or modify page attributes.
580 sal_uInt32 nAttrib = 0;
581 eErrCode = xManager->attrib (aKey, nMask1, nMask2, nAttrib);
582 if (pnAttrib)
583 *pnAttrib = nAttrib;
584 return eErrCode;
588 * store_link.
590 storeError SAL_CALL store_link (
591 storeFileHandle Handle,
592 rtl_uString *pSrcPath, rtl_uString *pSrcName,
593 rtl_uString *pDstPath, rtl_uString *pDstName
594 ) SAL_THROW_EXTERN_C()
596 storeError eErrCode = store_E_None;
598 OStoreHandle<OStorePageManager> xManager (
599 OStoreHandle<OStorePageManager>::query (Handle));
600 if (!xManager.is())
601 return store_E_InvalidHandle;
603 if (!(pSrcPath && pSrcName))
604 return store_E_InvalidParameter;
606 if (!(pDstPath && pDstName))
607 return store_E_InvalidParameter;
609 // Setup 'Source' page key.
610 OString aSrcPath (
611 pSrcPath->buffer, pSrcPath->length, RTL_TEXTENCODING_UTF8);
612 OString aSrcName (
613 pSrcName->buffer, pSrcName->length, RTL_TEXTENCODING_UTF8);
614 OStorePageKey aSrcKey;
616 eErrCode = OStorePageManager::namei (
617 aSrcPath.pData, aSrcName.pData, aSrcKey);
618 if (eErrCode != store_E_None)
619 return eErrCode;
621 // Setup 'Destination' page key.
622 OString aDstPath (
623 pDstPath->buffer, pDstPath->length, RTL_TEXTENCODING_UTF8);
624 OString aDstName (
625 pDstName->buffer, pDstName->length, RTL_TEXTENCODING_UTF8);
626 OStorePageKey aDstKey;
628 eErrCode = OStorePageManager::namei (
629 aDstPath.pData, aDstName.pData, aDstKey);
630 if (eErrCode != store_E_None)
631 return eErrCode;
633 // Link 'Source' hard to 'Destination'.
634 return xManager->link (aSrcKey, aDstKey);
638 * store_symlink.
640 storeError SAL_CALL store_symlink (
641 storeFileHandle Handle,
642 rtl_uString *pSrcPath, rtl_uString *pSrcName,
643 rtl_uString *pDstPath, rtl_uString *pDstName
644 ) SAL_THROW_EXTERN_C()
646 storeError eErrCode = store_E_None;
648 OStoreHandle<OStorePageManager> xManager (
649 OStoreHandle<OStorePageManager>::query (Handle));
650 if (!xManager.is())
651 return store_E_InvalidHandle;
653 if (!(pSrcPath && pSrcName))
654 return store_E_InvalidParameter;
656 if (!(pDstPath && pDstName))
657 return store_E_InvalidParameter;
659 // Setup 'Destination' page key.
660 OString aDstPath (
661 pDstPath->buffer, pDstPath->length, RTL_TEXTENCODING_UTF8);
662 OString aDstName (
663 pDstName->buffer, pDstName->length, RTL_TEXTENCODING_UTF8);
664 OStorePageKey aDstKey;
666 eErrCode = OStorePageManager::namei (
667 aDstPath.pData, aDstName.pData, aDstKey);
668 if (eErrCode != store_E_None)
669 return eErrCode;
671 // Insert 'Source' as symlink to 'Destination'.
672 OString aSrcPath (
673 pSrcPath->buffer, pSrcPath->length, RTL_TEXTENCODING_UTF8);
674 OString aSrcName (
675 pSrcName->buffer, pSrcName->length, RTL_TEXTENCODING_UTF8);
677 return xManager->symlink (aSrcPath.pData, aSrcName.pData, aDstKey);
681 * store_rename.
683 storeError SAL_CALL store_rename (
684 storeFileHandle Handle,
685 rtl_uString *pSrcPath, rtl_uString *pSrcName,
686 rtl_uString *pDstPath, rtl_uString *pDstName
687 ) SAL_THROW_EXTERN_C()
689 storeError eErrCode = store_E_None;
691 OStoreHandle<OStorePageManager> xManager (
692 OStoreHandle<OStorePageManager>::query (Handle));
693 if (!xManager.is())
694 return store_E_InvalidHandle;
696 if (!(pSrcPath && pSrcName))
697 return store_E_InvalidParameter;
699 if (!(pDstPath && pDstName))
700 return store_E_InvalidParameter;
702 // Setup 'Source' page key.
703 OString aSrcPath (
704 pSrcPath->buffer, pSrcPath->length, RTL_TEXTENCODING_UTF8);
705 OString aSrcName (
706 pSrcName->buffer, pSrcName->length, RTL_TEXTENCODING_UTF8);
707 OStorePageKey aSrcKey;
709 eErrCode = OStorePageManager::namei (
710 aSrcPath.pData, aSrcName.pData, aSrcKey);
711 if (eErrCode != store_E_None)
712 return eErrCode;
714 // Rename 'Source' into 'Destination'.
715 OString aDstPath (
716 pDstPath->buffer, pDstPath->length, RTL_TEXTENCODING_UTF8);
717 OString aDstName (
718 pDstName->buffer, pDstName->length, RTL_TEXTENCODING_UTF8);
720 return xManager->rename (aSrcKey, aDstPath.pData, aDstName.pData);
724 * store_remove.
726 storeError SAL_CALL store_remove (
727 storeFileHandle Handle,
728 rtl_uString *pPath,
729 rtl_uString *pName
730 ) SAL_THROW_EXTERN_C()
732 storeError eErrCode = store_E_None;
734 OStoreHandle<OStorePageManager> xManager (
735 OStoreHandle<OStorePageManager>::query (Handle));
736 if (!xManager.is())
737 return store_E_InvalidHandle;
739 if (!(pPath && pName))
740 return store_E_InvalidParameter;
742 // Setup page key.
743 OString aPath (pPath->buffer, pPath->length, RTL_TEXTENCODING_UTF8);
744 OString aName (pName->buffer, pName->length, RTL_TEXTENCODING_UTF8);
745 OStorePageKey aKey;
747 eErrCode = OStorePageManager::namei (aPath.pData, aName.pData, aKey);
748 if (eErrCode != store_E_None)
749 return eErrCode;
751 // Remove.
752 return xManager->remove (aKey);
755 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */