Branch libreoffice-5-0-4
[LibreOffice.git] / store / source / store.cxx
blob914063a8a28dc06123c3ccf462a3ad1cfb13bb36
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
38 /** Template helper class as type safe Reference to store_handle_type.
40 template<class store_handle_type>
41 class OStoreHandle : public rtl::Reference<store_handle_type>
43 public:
44 OStoreHandle (store_handle_type * pHandle)
45 : rtl::Reference<store_handle_type> (pHandle)
48 static store_handle_type * SAL_CALL query (void * pHandle)
50 return store::query (
51 static_cast<IStoreHandle*>(pHandle),
52 static_cast<store_handle_type*>(0));
57 using namespace store;
59 /*========================================================================
61 * storeHandle implementation.
63 *======================================================================*/
65 * store_acquireHandle.
67 storeError SAL_CALL store_acquireHandle (
68 storeHandle Handle
69 ) SAL_THROW_EXTERN_C()
71 IStoreHandle *pHandle = static_cast<IStoreHandle*>(Handle);
72 if (!pHandle)
73 return store_E_InvalidHandle;
75 pHandle->acquire();
76 return store_E_None;
80 * store_releaseHandle.
82 storeError SAL_CALL store_releaseHandle (
83 storeHandle Handle
84 ) SAL_THROW_EXTERN_C()
86 IStoreHandle *pHandle = static_cast<IStoreHandle*>(Handle);
87 if (!pHandle)
88 return store_E_InvalidHandle;
90 pHandle->release();
91 return store_E_None;
94 /*========================================================================
96 * storeFileHandle implementation.
98 *======================================================================*/
100 * store_createMemoryFile.
102 storeError SAL_CALL store_createMemoryFile (
103 sal_uInt16 nPageSize,
104 storeFileHandle *phFile
105 ) SAL_THROW_EXTERN_C()
107 if (!phFile)
108 return store_E_InvalidParameter;
109 *phFile = NULL;
111 Reference<ILockBytes> xLockBytes;
113 storeError eErrCode = MemoryLockBytes_createInstance(xLockBytes);
114 if (eErrCode != store_E_None)
115 return eErrCode;
116 OSL_ASSERT(xLockBytes.is());
118 Reference<OStorePageManager> xManager (new OStorePageManager());
119 if (!xManager.is())
120 return store_E_OutOfMemory;
122 eErrCode = xManager->initialize (
123 &*xLockBytes, store_AccessCreate, nPageSize);
124 if (eErrCode != store_E_None)
125 return eErrCode;
127 xManager->acquire();
129 *phFile = (storeFileHandle)&(*xManager);
130 return store_E_None;
134 * store_openFile.
136 storeError SAL_CALL store_openFile (
137 rtl_uString *pFilename,
138 storeAccessMode eAccessMode,
139 sal_uInt16 nPageSize,
140 storeFileHandle *phFile
141 ) SAL_THROW_EXTERN_C()
143 if (phFile)
144 *phFile = NULL;
146 if (!(pFilename && phFile))
147 return store_E_InvalidParameter;
149 Reference<ILockBytes> xLockBytes;
151 storeError eErrCode = FileLockBytes_createInstance (xLockBytes, pFilename, eAccessMode);
152 if (eErrCode != store_E_None)
153 return eErrCode;
154 OSL_ASSERT(xLockBytes.is());
156 Reference<OStorePageManager> xManager (new OStorePageManager());
157 if (!xManager.is())
158 return store_E_OutOfMemory;
160 eErrCode = xManager->initialize (
161 &*xLockBytes, eAccessMode, nPageSize);
162 if (eErrCode != store_E_None)
163 return eErrCode;
165 xManager->acquire();
167 *phFile = (storeFileHandle)&(*xManager);
168 return store_E_None;
172 * store_closeFile.
174 storeError SAL_CALL store_closeFile (
175 storeFileHandle Handle
176 ) SAL_THROW_EXTERN_C()
178 OStorePageManager *pManager =
179 OStoreHandle<OStorePageManager>::query (Handle);
180 if (!pManager)
181 return store_E_InvalidHandle;
183 storeError eErrCode = pManager->close();
184 pManager->release();
185 return eErrCode;
189 * store_flushFile.
191 storeError SAL_CALL store_flushFile (
192 storeFileHandle Handle
193 ) SAL_THROW_EXTERN_C()
195 OStoreHandle<OStorePageManager> xManager (
196 OStoreHandle<OStorePageManager>::query (Handle));
197 if (!xManager.is())
198 return store_E_InvalidHandle;
200 return xManager->flush();
204 * store_getFileRefererCount.
206 storeError SAL_CALL store_getFileRefererCount (
207 storeFileHandle Handle,
208 sal_uInt32 *pnRefCount
209 ) SAL_THROW_EXTERN_C()
211 OStoreHandle<OStorePageManager> xManager (
212 OStoreHandle<OStorePageManager>::query (Handle));
213 if (!xManager.is())
214 return store_E_InvalidHandle;
216 if (!pnRefCount)
217 return store_E_InvalidParameter;
219 *pnRefCount = xManager->getRefererCount();
220 return store_E_None;
224 * store_getFileSize.
226 storeError SAL_CALL store_getFileSize (
227 storeFileHandle Handle,
228 sal_uInt32 *pnSize
229 ) SAL_THROW_EXTERN_C()
231 OStoreHandle<OStorePageManager> xManager (
232 OStoreHandle<OStorePageManager>::query (Handle));
233 if (!xManager.is())
234 return store_E_InvalidHandle;
236 if (!pnSize)
237 return store_E_InvalidParameter;
239 return xManager->size (*pnSize);
243 * store_rebuildFile.
245 storeError SAL_CALL store_rebuildFile (
246 rtl_uString *pSrcFilename,
247 rtl_uString *pDstFilename
248 ) SAL_THROW_EXTERN_C()
250 storeError eErrCode = store_E_None;
252 if (!(pSrcFilename && pDstFilename))
253 return store_E_InvalidParameter;
255 Reference<OStorePageManager> xManager (new OStorePageManager());
256 if (!xManager.is())
257 return store_E_OutOfMemory;
259 Reference<ILockBytes> xSrcLB;
260 eErrCode = FileLockBytes_createInstance (xSrcLB, pSrcFilename, store_AccessReadOnly);
261 if (eErrCode != store_E_None)
262 return eErrCode;
263 OSL_ASSERT(xSrcLB.is());
265 Reference<ILockBytes> xDstLB;
266 eErrCode = FileLockBytes_createInstance (xDstLB, pDstFilename, store_AccessCreate);
267 if (eErrCode != store_E_None)
268 return eErrCode;
269 OSL_ASSERT(xDstLB.is());
271 return xManager->rebuild (&*xSrcLB, &*xDstLB);
274 /*========================================================================
276 * storeDirectoryHandle implementation.
278 *======================================================================*/
280 * store_openDirectory.
282 storeError SAL_CALL store_openDirectory (
283 storeFileHandle hFile,
284 rtl_uString *pPath,
285 rtl_uString *pName,
286 storeAccessMode eAccessMode,
287 storeDirectoryHandle *phDirectory
288 ) SAL_THROW_EXTERN_C()
290 storeError eErrCode = store_E_None;
291 if (phDirectory)
292 *phDirectory = NULL;
294 OStoreHandle<OStorePageManager> xManager (
295 OStoreHandle<OStorePageManager>::query (hFile));
296 if (!xManager.is())
297 return store_E_InvalidHandle;
299 if (!(pPath && pName && phDirectory))
300 return store_E_InvalidParameter;
302 Reference<OStoreDirectory_Impl> xDirectory (new OStoreDirectory_Impl());
303 if (!xDirectory.is())
304 return store_E_OutOfMemory;
306 OString aPath (pPath->buffer, pPath->length, RTL_TEXTENCODING_UTF8);
307 OString aName (pName->buffer, pName->length, RTL_TEXTENCODING_UTF8);
309 eErrCode = xDirectory->create (&*xManager, aPath.pData, aName.pData, eAccessMode);
310 if (eErrCode != store_E_None)
311 return eErrCode;
313 xDirectory->acquire();
315 *phDirectory = (storeDirectoryHandle)&(*xDirectory);
316 return store_E_None;
320 * store_closeDirectory.
322 storeError SAL_CALL store_closeDirectory (
323 storeDirectoryHandle Handle
324 ) SAL_THROW_EXTERN_C()
326 OStoreDirectory_Impl *pDirectory =
327 OStoreHandle<OStoreDirectory_Impl>::query (Handle);
328 if (!pDirectory)
329 return store_E_InvalidHandle;
331 pDirectory->release();
332 return store_E_None;
336 * store_findFirst.
338 storeError SAL_CALL store_findFirst (
339 storeDirectoryHandle Handle,
340 storeFindData *pFindData
341 ) SAL_THROW_EXTERN_C()
343 OStoreHandle<OStoreDirectory_Impl> xDirectory (
344 OStoreHandle<OStoreDirectory_Impl>::query (Handle));
345 if (!xDirectory.is())
346 return store_E_InvalidHandle;
348 if (!pFindData)
349 return store_E_InvalidParameter;
351 // Initialize FindData.
352 memset (pFindData, 0, sizeof (storeFindData));
354 // Find first.
355 pFindData->m_nReserved = (sal_uInt32)(~0);
356 return xDirectory->iterate (*pFindData);
360 * store_findNext.
362 storeError SAL_CALL store_findNext (
363 storeDirectoryHandle Handle,
364 storeFindData *pFindData
365 ) SAL_THROW_EXTERN_C()
367 OStoreHandle<OStoreDirectory_Impl> xDirectory (
368 OStoreHandle<OStoreDirectory_Impl>::query (Handle));
369 if (!xDirectory.is())
370 return store_E_InvalidHandle;
372 if (!pFindData)
373 return store_E_InvalidParameter;
375 // Check FindData.
376 if (!pFindData->m_nReserved)
377 return store_E_NoMoreFiles;
379 // Find next.
380 pFindData->m_nReserved -= 1;
381 return xDirectory->iterate (*pFindData);
384 /*========================================================================
386 * storeStreamHandle implementation.
388 *======================================================================*/
390 * store_openStream
392 storeError SAL_CALL store_openStream (
393 storeFileHandle hFile,
394 rtl_uString *pPath,
395 rtl_uString *pName,
396 storeAccessMode eAccessMode,
397 storeStreamHandle *phStream
398 ) SAL_THROW_EXTERN_C()
400 storeError eErrCode = store_E_None;
401 if (phStream)
402 *phStream = NULL;
404 OStoreHandle<OStorePageManager> xManager (
405 OStoreHandle<OStorePageManager>::query (hFile));
406 if (!xManager.is())
407 return store_E_InvalidHandle;
409 if (!(pPath && pName && phStream))
410 return store_E_InvalidParameter;
412 Reference<OStoreLockBytes> xLockBytes (new OStoreLockBytes());
413 if (!xLockBytes.is())
414 return store_E_OutOfMemory;
416 OString aPath (pPath->buffer, pPath->length, RTL_TEXTENCODING_UTF8);
417 OString aName (pName->buffer, pName->length, RTL_TEXTENCODING_UTF8);
419 eErrCode = xLockBytes->create (&*xManager, aPath.pData, aName.pData, eAccessMode);
420 if (eErrCode != store_E_None)
421 return eErrCode;
423 xLockBytes->acquire();
425 *phStream = (storeStreamHandle)&(*xLockBytes);
426 return store_E_None;
430 * store_closeStream.
432 storeError SAL_CALL store_closeStream (
433 storeStreamHandle Handle
434 ) SAL_THROW_EXTERN_C()
436 OStoreLockBytes *pLockBytes =
437 OStoreHandle<OStoreLockBytes>::query (Handle);
438 if (!pLockBytes)
439 return store_E_InvalidHandle;
441 pLockBytes->release();
442 return store_E_None;
446 * store_readStream.
448 storeError SAL_CALL store_readStream (
449 storeStreamHandle Handle,
450 sal_uInt32 nOffset,
451 void *pBuffer,
452 sal_uInt32 nBytes,
453 sal_uInt32 *pnDone
454 ) SAL_THROW_EXTERN_C()
456 OStoreHandle<OStoreLockBytes> xLockBytes (
457 OStoreHandle<OStoreLockBytes>::query (Handle));
458 if (!xLockBytes.is())
459 return store_E_InvalidHandle;
461 if (!(pBuffer && pnDone))
462 return store_E_InvalidParameter;
464 return xLockBytes->readAt (nOffset, pBuffer, nBytes, *pnDone);
468 * store_writeStream.
470 storeError SAL_CALL store_writeStream (
471 storeStreamHandle Handle,
472 sal_uInt32 nOffset,
473 const void *pBuffer,
474 sal_uInt32 nBytes,
475 sal_uInt32 *pnDone
476 ) SAL_THROW_EXTERN_C()
478 OStoreHandle<OStoreLockBytes> xLockBytes (
479 OStoreHandle<OStoreLockBytes>::query (Handle));
480 if (!xLockBytes.is())
481 return store_E_InvalidHandle;
483 if (!(pBuffer && pnDone))
484 return store_E_InvalidParameter;
486 return xLockBytes->writeAt (nOffset, pBuffer, nBytes, *pnDone);
490 * store_flushStream.
492 storeError SAL_CALL store_flushStream (
493 storeStreamHandle Handle
494 ) SAL_THROW_EXTERN_C()
496 OStoreHandle<OStoreLockBytes> xLockBytes (
497 OStoreHandle<OStoreLockBytes>::query (Handle));
498 if (!xLockBytes.is())
499 return store_E_InvalidHandle;
501 return xLockBytes->flush();
505 * store_getStreamSize.
507 storeError SAL_CALL store_getStreamSize (
508 storeStreamHandle Handle,
509 sal_uInt32 *pnSize
510 ) SAL_THROW_EXTERN_C()
512 OStoreHandle<OStoreLockBytes> xLockBytes (
513 OStoreHandle<OStoreLockBytes>::query (Handle));
514 if (!xLockBytes.is())
515 return store_E_InvalidHandle;
517 if (!pnSize)
518 return store_E_InvalidParameter;
520 return xLockBytes->stat (*pnSize);
524 * store_setStreamSize.
526 storeError SAL_CALL store_setStreamSize (
527 storeStreamHandle Handle,
528 sal_uInt32 nSize
529 ) SAL_THROW_EXTERN_C()
531 OStoreHandle<OStoreLockBytes> xLockBytes (
532 OStoreHandle<OStoreLockBytes>::query (Handle));
533 if (!xLockBytes.is())
534 return store_E_InvalidHandle;
536 return xLockBytes->setSize (nSize);
539 /*========================================================================
541 * Common storeDirectoryHandle and storeStreamHandle operations.
543 *======================================================================*/
545 * store_attrib.
547 storeError SAL_CALL store_attrib (
548 storeFileHandle Handle,
549 rtl_uString *pPath,
550 rtl_uString *pName,
551 sal_uInt32 nMask1,
552 sal_uInt32 nMask2,
553 sal_uInt32 *pnAttrib
554 ) SAL_THROW_EXTERN_C()
556 storeError eErrCode = store_E_None;
557 if (pnAttrib)
558 *pnAttrib = 0;
560 OStoreHandle<OStorePageManager> xManager (
561 OStoreHandle<OStorePageManager>::query (Handle));
562 if (!xManager.is())
563 return store_E_InvalidHandle;
565 if (!(pPath && pName))
566 return store_E_InvalidParameter;
568 // Setup page key.
569 OString aPath (pPath->buffer, pPath->length, RTL_TEXTENCODING_UTF8);
570 OString aName (pName->buffer, pName->length, RTL_TEXTENCODING_UTF8);
571 OStorePageKey aKey;
573 eErrCode = OStorePageManager::namei (aPath.pData, aName.pData, aKey);
574 if (eErrCode != store_E_None)
575 return eErrCode;
577 // Obtain or modify page attributes.
578 sal_uInt32 nAttrib = 0;
579 eErrCode = xManager->attrib (aKey, nMask1, nMask2, nAttrib);
580 if (pnAttrib)
581 *pnAttrib = nAttrib;
582 return eErrCode;
586 * store_link.
588 storeError SAL_CALL store_link (
589 storeFileHandle Handle,
590 rtl_uString *pSrcPath, rtl_uString *pSrcName,
591 rtl_uString *pDstPath, rtl_uString *pDstName
592 ) SAL_THROW_EXTERN_C()
594 storeError eErrCode = store_E_None;
596 OStoreHandle<OStorePageManager> xManager (
597 OStoreHandle<OStorePageManager>::query (Handle));
598 if (!xManager.is())
599 return store_E_InvalidHandle;
601 if (!(pSrcPath && pSrcName))
602 return store_E_InvalidParameter;
604 if (!(pDstPath && pDstName))
605 return store_E_InvalidParameter;
607 // Setup 'Source' page key.
608 OString aSrcPath (
609 pSrcPath->buffer, pSrcPath->length, RTL_TEXTENCODING_UTF8);
610 OString aSrcName (
611 pSrcName->buffer, pSrcName->length, RTL_TEXTENCODING_UTF8);
612 OStorePageKey aSrcKey;
614 eErrCode = OStorePageManager::namei (
615 aSrcPath.pData, aSrcName.pData, aSrcKey);
616 if (eErrCode != store_E_None)
617 return eErrCode;
619 // Setup 'Destination' page key.
620 OString aDstPath (
621 pDstPath->buffer, pDstPath->length, RTL_TEXTENCODING_UTF8);
622 OString aDstName (
623 pDstName->buffer, pDstName->length, RTL_TEXTENCODING_UTF8);
624 OStorePageKey aDstKey;
626 eErrCode = OStorePageManager::namei (
627 aDstPath.pData, aDstName.pData, aDstKey);
628 if (eErrCode != store_E_None)
629 return eErrCode;
631 // Link 'Source' hard to 'Destination'.
632 return xManager->link (aSrcKey, aDstKey);
636 * store_symlink.
638 storeError SAL_CALL store_symlink (
639 storeFileHandle Handle,
640 rtl_uString *pSrcPath, rtl_uString *pSrcName,
641 rtl_uString *pDstPath, rtl_uString *pDstName
642 ) SAL_THROW_EXTERN_C()
644 storeError eErrCode = store_E_None;
646 OStoreHandle<OStorePageManager> xManager (
647 OStoreHandle<OStorePageManager>::query (Handle));
648 if (!xManager.is())
649 return store_E_InvalidHandle;
651 if (!(pSrcPath && pSrcName))
652 return store_E_InvalidParameter;
654 if (!(pDstPath && pDstName))
655 return store_E_InvalidParameter;
657 // Setup 'Destination' page key.
658 OString aDstPath (
659 pDstPath->buffer, pDstPath->length, RTL_TEXTENCODING_UTF8);
660 OString aDstName (
661 pDstName->buffer, pDstName->length, RTL_TEXTENCODING_UTF8);
662 OStorePageKey aDstKey;
664 eErrCode = OStorePageManager::namei (
665 aDstPath.pData, aDstName.pData, aDstKey);
666 if (eErrCode != store_E_None)
667 return eErrCode;
669 // Insert 'Source' as symlink to 'Destination'.
670 OString aSrcPath (
671 pSrcPath->buffer, pSrcPath->length, RTL_TEXTENCODING_UTF8);
672 OString aSrcName (
673 pSrcName->buffer, pSrcName->length, RTL_TEXTENCODING_UTF8);
675 return xManager->symlink (aSrcPath.pData, aSrcName.pData, aDstKey);
679 * store_rename.
681 storeError SAL_CALL store_rename (
682 storeFileHandle Handle,
683 rtl_uString *pSrcPath, rtl_uString *pSrcName,
684 rtl_uString *pDstPath, rtl_uString *pDstName
685 ) SAL_THROW_EXTERN_C()
687 storeError eErrCode = store_E_None;
689 OStoreHandle<OStorePageManager> xManager (
690 OStoreHandle<OStorePageManager>::query (Handle));
691 if (!xManager.is())
692 return store_E_InvalidHandle;
694 if (!(pSrcPath && pSrcName))
695 return store_E_InvalidParameter;
697 if (!(pDstPath && pDstName))
698 return store_E_InvalidParameter;
700 // Setup 'Source' page key.
701 OString aSrcPath (
702 pSrcPath->buffer, pSrcPath->length, RTL_TEXTENCODING_UTF8);
703 OString aSrcName (
704 pSrcName->buffer, pSrcName->length, RTL_TEXTENCODING_UTF8);
705 OStorePageKey aSrcKey;
707 eErrCode = OStorePageManager::namei (
708 aSrcPath.pData, aSrcName.pData, aSrcKey);
709 if (eErrCode != store_E_None)
710 return eErrCode;
712 // Rename 'Source' into 'Destination'.
713 OString aDstPath (
714 pDstPath->buffer, pDstPath->length, RTL_TEXTENCODING_UTF8);
715 OString aDstName (
716 pDstName->buffer, pDstName->length, RTL_TEXTENCODING_UTF8);
718 return xManager->rename (aSrcKey, aDstPath.pData, aDstName.pData);
722 * store_remove.
724 storeError SAL_CALL store_remove (
725 storeFileHandle Handle,
726 rtl_uString *pPath,
727 rtl_uString *pName
728 ) SAL_THROW_EXTERN_C()
730 storeError eErrCode = store_E_None;
732 OStoreHandle<OStorePageManager> xManager (
733 OStoreHandle<OStorePageManager>::query (Handle));
734 if (!xManager.is())
735 return store_E_InvalidHandle;
737 if (!(pPath && pName))
738 return store_E_InvalidParameter;
740 // Setup page key.
741 OString aPath (pPath->buffer, pPath->length, RTL_TEXTENCODING_UTF8);
742 OString aName (pName->buffer, pName->length, RTL_TEXTENCODING_UTF8);
743 OStorePageKey aKey;
745 eErrCode = OStorePageManager::namei (aPath.pData, aName.pData, aKey);
746 if (eErrCode != store_E_None)
747 return eErrCode;
749 // Remove.
750 return xManager->remove (aKey);
753 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */