Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / store / source / store.cxx
blob5c61e6cbb37ccebd10c07856ca8388be8d8edfc4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include "store/store.h"
32 #include <sal/types.h>
33 #include <rtl/memory.h>
34 #include <rtl/string.hxx>
35 #include <rtl/ref.hxx>
37 #include "object.hxx"
38 #include "lockbyte.hxx"
40 #include "storbase.hxx"
41 #include "storpage.hxx"
42 #include "stordir.hxx"
43 #include "storlckb.hxx"
45 using rtl::Reference;
46 using rtl::OString;
48 namespace store
50 /** Template helper class as type safe Reference to store_handle_type.
52 template<class store_handle_type>
53 class OStoreHandle : public rtl::Reference<store_handle_type>
55 public:
56 OStoreHandle (store_handle_type * pHandle)
57 : rtl::Reference<store_handle_type> (pHandle)
60 static store_handle_type * SAL_CALL query (void * pHandle)
62 return store::query (
63 static_cast<IStoreHandle*>(pHandle),
64 static_cast<store_handle_type*>(0));
70 using namespace store;
72 /*========================================================================
74 * storeHandle implementation.
76 *======================================================================*/
78 * store_acquireHandle.
80 storeError SAL_CALL store_acquireHandle (
81 storeHandle Handle
82 ) SAL_THROW_EXTERN_C()
84 IStoreHandle *pHandle = static_cast<IStoreHandle*>(Handle);
85 if (!pHandle)
86 return store_E_InvalidHandle;
88 pHandle->acquire();
89 return store_E_None;
93 * store_releaseHandle.
95 storeError SAL_CALL store_releaseHandle (
96 storeHandle Handle
97 ) SAL_THROW_EXTERN_C()
99 IStoreHandle *pHandle = static_cast<IStoreHandle*>(Handle);
100 if (!pHandle)
101 return store_E_InvalidHandle;
103 pHandle->release();
104 return store_E_None;
107 /*========================================================================
109 * storeFileHandle implementation.
111 *======================================================================*/
113 * store_createMemoryFile.
115 storeError SAL_CALL store_createMemoryFile (
116 sal_uInt16 nPageSize,
117 storeFileHandle *phFile
118 ) SAL_THROW_EXTERN_C()
120 if (!phFile)
121 return store_E_InvalidParameter;
122 *phFile = NULL;
124 Reference<ILockBytes> xLockBytes;
126 storeError eErrCode = MemoryLockBytes_createInstance(xLockBytes);
127 if (eErrCode != store_E_None)
128 return eErrCode;
129 OSL_ASSERT(xLockBytes.is());
131 Reference<OStorePageManager> xManager (new OStorePageManager());
132 if (!xManager.is())
133 return store_E_OutOfMemory;
135 eErrCode = xManager->initialize (
136 &*xLockBytes, store_AccessCreate, nPageSize);
137 if (eErrCode != store_E_None)
138 return eErrCode;
140 xManager->acquire();
142 *phFile = (storeFileHandle)&(*xManager);
143 return store_E_None;
147 * store_openFile.
149 storeError SAL_CALL store_openFile (
150 rtl_uString *pFilename,
151 storeAccessMode eAccessMode,
152 sal_uInt16 nPageSize,
153 storeFileHandle *phFile
154 ) SAL_THROW_EXTERN_C()
156 if (phFile)
157 *phFile = NULL;
159 if (!(pFilename && phFile))
160 return store_E_InvalidParameter;
162 Reference<ILockBytes> xLockBytes;
164 storeError eErrCode = FileLockBytes_createInstance (xLockBytes, pFilename, eAccessMode);
165 if (eErrCode != store_E_None)
166 return eErrCode;
167 OSL_ASSERT(xLockBytes.is());
169 Reference<OStorePageManager> xManager (new OStorePageManager());
170 if (!xManager.is())
171 return store_E_OutOfMemory;
173 eErrCode = xManager->initialize (
174 &*xLockBytes, eAccessMode, nPageSize);
175 if (eErrCode != store_E_None)
176 return eErrCode;
178 xManager->acquire();
180 *phFile = (storeFileHandle)&(*xManager);
181 return store_E_None;
185 * store_closeFile.
187 storeError SAL_CALL store_closeFile (
188 storeFileHandle Handle
189 ) SAL_THROW_EXTERN_C()
191 OStorePageManager *pManager =
192 OStoreHandle<OStorePageManager>::query (Handle);
193 if (!pManager)
194 return store_E_InvalidHandle;
196 storeError eErrCode = pManager->close();
197 pManager->release();
198 return eErrCode;
202 * store_flushFile.
204 storeError SAL_CALL store_flushFile (
205 storeFileHandle Handle
206 ) SAL_THROW_EXTERN_C()
208 OStoreHandle<OStorePageManager> xManager (
209 OStoreHandle<OStorePageManager>::query (Handle));
210 if (!xManager.is())
211 return store_E_InvalidHandle;
213 return xManager->flush();
217 * store_getFileRefererCount.
219 storeError SAL_CALL store_getFileRefererCount (
220 storeFileHandle Handle,
221 sal_uInt32 *pnRefCount
222 ) SAL_THROW_EXTERN_C()
224 OStoreHandle<OStorePageManager> xManager (
225 OStoreHandle<OStorePageManager>::query (Handle));
226 if (!xManager.is())
227 return store_E_InvalidHandle;
229 if (!pnRefCount)
230 return store_E_InvalidParameter;
232 *pnRefCount = xManager->getRefererCount();
233 return store_E_None;
237 * store_getFileSize.
239 storeError SAL_CALL store_getFileSize (
240 storeFileHandle Handle,
241 sal_uInt32 *pnSize
242 ) SAL_THROW_EXTERN_C()
244 OStoreHandle<OStorePageManager> xManager (
245 OStoreHandle<OStorePageManager>::query (Handle));
246 if (!xManager.is())
247 return store_E_InvalidHandle;
249 if (!pnSize)
250 return store_E_InvalidParameter;
252 return xManager->size (*pnSize);
256 * store_rebuildFile.
258 storeError SAL_CALL store_rebuildFile (
259 rtl_uString *pSrcFilename,
260 rtl_uString *pDstFilename
261 ) SAL_THROW_EXTERN_C()
263 storeError eErrCode = store_E_None;
265 if (!(pSrcFilename && pDstFilename))
266 return store_E_InvalidParameter;
268 Reference<OStorePageManager> xManager (new OStorePageManager());
269 if (!xManager.is())
270 return store_E_OutOfMemory;
272 Reference<ILockBytes> xSrcLB;
273 eErrCode = FileLockBytes_createInstance (xSrcLB, pSrcFilename, store_AccessReadOnly);
274 if (eErrCode != store_E_None)
275 return eErrCode;
276 OSL_ASSERT(xSrcLB.is());
278 Reference<ILockBytes> xDstLB;
279 eErrCode = FileLockBytes_createInstance (xDstLB, pDstFilename, store_AccessCreate);
280 if (eErrCode != store_E_None)
281 return eErrCode;
282 OSL_ASSERT(xDstLB.is());
284 return xManager->rebuild (&*xSrcLB, &*xDstLB);
287 /*========================================================================
289 * storeDirectoryHandle implementation.
291 *======================================================================*/
293 * store_openDirectory.
295 storeError SAL_CALL store_openDirectory (
296 storeFileHandle hFile,
297 rtl_uString *pPath,
298 rtl_uString *pName,
299 storeAccessMode eAccessMode,
300 storeDirectoryHandle *phDirectory
301 ) SAL_THROW_EXTERN_C()
303 storeError eErrCode = store_E_None;
304 if (phDirectory)
305 *phDirectory = NULL;
307 OStoreHandle<OStorePageManager> xManager (
308 OStoreHandle<OStorePageManager>::query (hFile));
309 if (!xManager.is())
310 return store_E_InvalidHandle;
312 if (!(pPath && pName && phDirectory))
313 return store_E_InvalidParameter;
315 Reference<OStoreDirectory_Impl> xDirectory (new OStoreDirectory_Impl());
316 if (!xDirectory.is())
317 return store_E_OutOfMemory;
319 OString aPath (pPath->buffer, pPath->length, RTL_TEXTENCODING_UTF8);
320 OString aName (pName->buffer, pName->length, RTL_TEXTENCODING_UTF8);
322 eErrCode = xDirectory->create (&*xManager, aPath.pData, aName.pData, eAccessMode);
323 if (eErrCode != store_E_None)
324 return eErrCode;
326 xDirectory->acquire();
328 *phDirectory = (storeDirectoryHandle)&(*xDirectory);
329 return store_E_None;
333 * store_closeDirectory.
335 storeError SAL_CALL store_closeDirectory (
336 storeDirectoryHandle Handle
337 ) SAL_THROW_EXTERN_C()
339 OStoreDirectory_Impl *pDirectory =
340 OStoreHandle<OStoreDirectory_Impl>::query (Handle);
341 if (!pDirectory)
342 return store_E_InvalidHandle;
344 pDirectory->release();
345 return store_E_None;
349 * store_findFirst.
351 storeError SAL_CALL store_findFirst (
352 storeDirectoryHandle Handle,
353 storeFindData *pFindData
354 ) SAL_THROW_EXTERN_C()
356 OStoreHandle<OStoreDirectory_Impl> xDirectory (
357 OStoreHandle<OStoreDirectory_Impl>::query (Handle));
358 if (!xDirectory.is())
359 return store_E_InvalidHandle;
361 if (!pFindData)
362 return store_E_InvalidParameter;
364 // Initialize FindData.
365 rtl_zeroMemory (pFindData, sizeof (storeFindData));
367 // Find first.
368 pFindData->m_nReserved = (sal_uInt32)(~0);
369 return xDirectory->iterate (*pFindData);
373 * store_findNext.
375 storeError SAL_CALL store_findNext (
376 storeDirectoryHandle Handle,
377 storeFindData *pFindData
378 ) SAL_THROW_EXTERN_C()
380 OStoreHandle<OStoreDirectory_Impl> xDirectory (
381 OStoreHandle<OStoreDirectory_Impl>::query (Handle));
382 if (!xDirectory.is())
383 return store_E_InvalidHandle;
385 if (!pFindData)
386 return store_E_InvalidParameter;
388 // Check FindData.
389 if (!pFindData->m_nReserved)
390 return store_E_NoMoreFiles;
392 // Find next.
393 pFindData->m_nReserved -= 1;
394 return xDirectory->iterate (*pFindData);
397 /*========================================================================
399 * storeStreamHandle implementation.
401 *======================================================================*/
403 * store_openStream
405 storeError SAL_CALL store_openStream (
406 storeFileHandle hFile,
407 rtl_uString *pPath,
408 rtl_uString *pName,
409 storeAccessMode eAccessMode,
410 storeStreamHandle *phStream
411 ) SAL_THROW_EXTERN_C()
413 storeError eErrCode = store_E_None;
414 if (phStream)
415 *phStream = NULL;
417 OStoreHandle<OStorePageManager> xManager (
418 OStoreHandle<OStorePageManager>::query (hFile));
419 if (!xManager.is())
420 return store_E_InvalidHandle;
422 if (!(pPath && pName && phStream))
423 return store_E_InvalidParameter;
425 Reference<OStoreLockBytes> xLockBytes (new OStoreLockBytes());
426 if (!xLockBytes.is())
427 return store_E_OutOfMemory;
429 OString aPath (pPath->buffer, pPath->length, RTL_TEXTENCODING_UTF8);
430 OString aName (pName->buffer, pName->length, RTL_TEXTENCODING_UTF8);
432 eErrCode = xLockBytes->create (&*xManager, aPath.pData, aName.pData, eAccessMode);
433 if (eErrCode != store_E_None)
434 return eErrCode;
436 xLockBytes->acquire();
438 *phStream = (storeStreamHandle)&(*xLockBytes);
439 return store_E_None;
443 * store_closeStream.
445 storeError SAL_CALL store_closeStream (
446 storeStreamHandle Handle
447 ) SAL_THROW_EXTERN_C()
449 OStoreLockBytes *pLockBytes =
450 OStoreHandle<OStoreLockBytes>::query (Handle);
451 if (!pLockBytes)
452 return store_E_InvalidHandle;
454 pLockBytes->release();
455 return store_E_None;
459 * store_readStream.
461 storeError SAL_CALL store_readStream (
462 storeStreamHandle Handle,
463 sal_uInt32 nOffset,
464 void *pBuffer,
465 sal_uInt32 nBytes,
466 sal_uInt32 *pnDone
467 ) SAL_THROW_EXTERN_C()
469 OStoreHandle<OStoreLockBytes> xLockBytes (
470 OStoreHandle<OStoreLockBytes>::query (Handle));
471 if (!xLockBytes.is())
472 return store_E_InvalidHandle;
474 if (!(pBuffer && pnDone))
475 return store_E_InvalidParameter;
477 return xLockBytes->readAt (nOffset, pBuffer, nBytes, *pnDone);
481 * store_writeStream.
483 storeError SAL_CALL store_writeStream (
484 storeStreamHandle Handle,
485 sal_uInt32 nOffset,
486 const void *pBuffer,
487 sal_uInt32 nBytes,
488 sal_uInt32 *pnDone
489 ) SAL_THROW_EXTERN_C()
491 OStoreHandle<OStoreLockBytes> xLockBytes (
492 OStoreHandle<OStoreLockBytes>::query (Handle));
493 if (!xLockBytes.is())
494 return store_E_InvalidHandle;
496 if (!(pBuffer && pnDone))
497 return store_E_InvalidParameter;
499 return xLockBytes->writeAt (nOffset, pBuffer, nBytes, *pnDone);
503 * store_flushStream.
505 storeError SAL_CALL store_flushStream (
506 storeStreamHandle Handle
507 ) SAL_THROW_EXTERN_C()
509 OStoreHandle<OStoreLockBytes> xLockBytes (
510 OStoreHandle<OStoreLockBytes>::query (Handle));
511 if (!xLockBytes.is())
512 return store_E_InvalidHandle;
514 return xLockBytes->flush();
518 * store_getStreamSize.
520 storeError SAL_CALL store_getStreamSize (
521 storeStreamHandle Handle,
522 sal_uInt32 *pnSize
523 ) SAL_THROW_EXTERN_C()
525 OStoreHandle<OStoreLockBytes> xLockBytes (
526 OStoreHandle<OStoreLockBytes>::query (Handle));
527 if (!xLockBytes.is())
528 return store_E_InvalidHandle;
530 if (!pnSize)
531 return store_E_InvalidParameter;
533 return xLockBytes->stat (*pnSize);
537 * store_setStreamSize.
539 storeError SAL_CALL store_setStreamSize (
540 storeStreamHandle Handle,
541 sal_uInt32 nSize
542 ) SAL_THROW_EXTERN_C()
544 OStoreHandle<OStoreLockBytes> xLockBytes (
545 OStoreHandle<OStoreLockBytes>::query (Handle));
546 if (!xLockBytes.is())
547 return store_E_InvalidHandle;
549 return xLockBytes->setSize (nSize);
552 /*========================================================================
554 * Common storeDirectoryHandle and storeStreamHandle operations.
556 *======================================================================*/
558 * store_attrib.
560 storeError SAL_CALL store_attrib (
561 storeFileHandle Handle,
562 rtl_uString *pPath,
563 rtl_uString *pName,
564 sal_uInt32 nMask1,
565 sal_uInt32 nMask2,
566 sal_uInt32 *pnAttrib
567 ) SAL_THROW_EXTERN_C()
569 storeError eErrCode = store_E_None;
570 if (pnAttrib)
571 *pnAttrib = 0;
573 OStoreHandle<OStorePageManager> xManager (
574 OStoreHandle<OStorePageManager>::query (Handle));
575 if (!xManager.is())
576 return store_E_InvalidHandle;
578 if (!(pPath && pName))
579 return store_E_InvalidParameter;
581 // Setup page key.
582 OString aPath (pPath->buffer, pPath->length, RTL_TEXTENCODING_UTF8);
583 OString aName (pName->buffer, pName->length, RTL_TEXTENCODING_UTF8);
584 OStorePageKey aKey;
586 eErrCode = OStorePageManager::namei (aPath.pData, aName.pData, aKey);
587 if (eErrCode != store_E_None)
588 return eErrCode;
590 // Obtain or modify page attributes.
591 sal_uInt32 nAttrib = 0;
592 eErrCode = xManager->attrib (aKey, nMask1, nMask2, nAttrib);
593 if (pnAttrib)
594 *pnAttrib = nAttrib;
595 return eErrCode;
599 * store_link.
601 storeError SAL_CALL store_link (
602 storeFileHandle Handle,
603 rtl_uString *pSrcPath, rtl_uString *pSrcName,
604 rtl_uString *pDstPath, rtl_uString *pDstName
605 ) SAL_THROW_EXTERN_C()
607 storeError eErrCode = store_E_None;
609 OStoreHandle<OStorePageManager> xManager (
610 OStoreHandle<OStorePageManager>::query (Handle));
611 if (!xManager.is())
612 return store_E_InvalidHandle;
614 if (!(pSrcPath && pSrcName))
615 return store_E_InvalidParameter;
617 if (!(pDstPath && pDstName))
618 return store_E_InvalidParameter;
620 // Setup 'Source' page key.
621 OString aSrcPath (
622 pSrcPath->buffer, pSrcPath->length, RTL_TEXTENCODING_UTF8);
623 OString aSrcName (
624 pSrcName->buffer, pSrcName->length, RTL_TEXTENCODING_UTF8);
625 OStorePageKey aSrcKey;
627 eErrCode = OStorePageManager::namei (
628 aSrcPath.pData, aSrcName.pData, aSrcKey);
629 if (eErrCode != store_E_None)
630 return eErrCode;
632 // Setup 'Destination' page key.
633 OString aDstPath (
634 pDstPath->buffer, pDstPath->length, RTL_TEXTENCODING_UTF8);
635 OString aDstName (
636 pDstName->buffer, pDstName->length, RTL_TEXTENCODING_UTF8);
637 OStorePageKey aDstKey;
639 eErrCode = OStorePageManager::namei (
640 aDstPath.pData, aDstName.pData, aDstKey);
641 if (eErrCode != store_E_None)
642 return eErrCode;
644 // Link 'Source' hard to 'Destination'.
645 return xManager->link (aSrcKey, aDstKey);
649 * store_symlink.
651 storeError SAL_CALL store_symlink (
652 storeFileHandle Handle,
653 rtl_uString *pSrcPath, rtl_uString *pSrcName,
654 rtl_uString *pDstPath, rtl_uString *pDstName
655 ) SAL_THROW_EXTERN_C()
657 storeError eErrCode = store_E_None;
659 OStoreHandle<OStorePageManager> xManager (
660 OStoreHandle<OStorePageManager>::query (Handle));
661 if (!xManager.is())
662 return store_E_InvalidHandle;
664 if (!(pSrcPath && pSrcName))
665 return store_E_InvalidParameter;
667 if (!(pDstPath && pDstName))
668 return store_E_InvalidParameter;
670 // Setup 'Destination' page key.
671 OString aDstPath (
672 pDstPath->buffer, pDstPath->length, RTL_TEXTENCODING_UTF8);
673 OString aDstName (
674 pDstName->buffer, pDstName->length, RTL_TEXTENCODING_UTF8);
675 OStorePageKey aDstKey;
677 eErrCode = OStorePageManager::namei (
678 aDstPath.pData, aDstName.pData, aDstKey);
679 if (eErrCode != store_E_None)
680 return eErrCode;
682 // Insert 'Source' as symlink to 'Destination'.
683 OString aSrcPath (
684 pSrcPath->buffer, pSrcPath->length, RTL_TEXTENCODING_UTF8);
685 OString aSrcName (
686 pSrcName->buffer, pSrcName->length, RTL_TEXTENCODING_UTF8);
688 return xManager->symlink (aSrcPath.pData, aSrcName.pData, aDstKey);
692 * store_rename.
694 storeError SAL_CALL store_rename (
695 storeFileHandle Handle,
696 rtl_uString *pSrcPath, rtl_uString *pSrcName,
697 rtl_uString *pDstPath, rtl_uString *pDstName
698 ) SAL_THROW_EXTERN_C()
700 storeError eErrCode = store_E_None;
702 OStoreHandle<OStorePageManager> xManager (
703 OStoreHandle<OStorePageManager>::query (Handle));
704 if (!xManager.is())
705 return store_E_InvalidHandle;
707 if (!(pSrcPath && pSrcName))
708 return store_E_InvalidParameter;
710 if (!(pDstPath && pDstName))
711 return store_E_InvalidParameter;
713 // Setup 'Source' page key.
714 OString aSrcPath (
715 pSrcPath->buffer, pSrcPath->length, RTL_TEXTENCODING_UTF8);
716 OString aSrcName (
717 pSrcName->buffer, pSrcName->length, RTL_TEXTENCODING_UTF8);
718 OStorePageKey aSrcKey;
720 eErrCode = OStorePageManager::namei (
721 aSrcPath.pData, aSrcName.pData, aSrcKey);
722 if (eErrCode != store_E_None)
723 return eErrCode;
725 // Rename 'Source' into 'Destination'.
726 OString aDstPath (
727 pDstPath->buffer, pDstPath->length, RTL_TEXTENCODING_UTF8);
728 OString aDstName (
729 pDstName->buffer, pDstName->length, RTL_TEXTENCODING_UTF8);
731 return xManager->rename (aSrcKey, aDstPath.pData, aDstName.pData);
735 * store_remove.
737 storeError SAL_CALL store_remove (
738 storeFileHandle Handle,
739 rtl_uString *pPath,
740 rtl_uString *pName
741 ) SAL_THROW_EXTERN_C()
743 storeError eErrCode = store_E_None;
745 OStoreHandle<OStorePageManager> xManager (
746 OStoreHandle<OStorePageManager>::query (Handle));
747 if (!xManager.is())
748 return store_E_InvalidHandle;
750 if (!(pPath && pName))
751 return store_E_InvalidParameter;
753 // Setup page key.
754 OString aPath (pPath->buffer, pPath->length, RTL_TEXTENCODING_UTF8);
755 OString aName (pName->buffer, pName->length, RTL_TEXTENCODING_UTF8);
756 OStorePageKey aKey;
758 eErrCode = OStorePageManager::namei (aPath.pData, aName.pData, aKey);
759 if (eErrCode != store_E_None)
760 return eErrCode;
762 // Remove.
763 return xManager->remove (aKey);
766 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */