merge the formfield patch from ooo-build
[ooovba.git] / store / source / store.cxx
blob87e85fc820fd69bb4129e6841fe679367d4b64ed
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: store.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_store.hxx"
34 #include "store/store.h"
36 #include <sal/types.h>
37 #include <rtl/memory.h>
38 #include <rtl/string.hxx>
39 #include <rtl/ref.hxx>
41 #include "object.hxx"
42 #include "lockbyte.hxx"
44 #include "storbase.hxx"
45 #include "storpage.hxx"
46 #include "stordir.hxx"
47 #include "storlckb.hxx"
49 using rtl::Reference;
50 using rtl::OString;
52 namespace store
54 /** Template helper class as type safe Reference to store_handle_type.
56 template<class store_handle_type>
57 class OStoreHandle : public rtl::Reference<store_handle_type>
59 public:
60 OStoreHandle (store_handle_type * pHandle)
61 : rtl::Reference<store_handle_type> (pHandle)
64 static store_handle_type * SAL_CALL query (void * pHandle)
66 return store::query (
67 static_cast<IStoreHandle*>(pHandle),
68 static_cast<store_handle_type*>(0));
74 using namespace store;
76 /*========================================================================
78 * storeHandle implementation.
80 *======================================================================*/
82 * store_acquireHandle.
84 storeError SAL_CALL store_acquireHandle (
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->acquire();
93 return store_E_None;
97 * store_releaseHandle.
99 storeError SAL_CALL store_releaseHandle (
100 storeHandle Handle
101 ) SAL_THROW_EXTERN_C()
103 IStoreHandle *pHandle = static_cast<IStoreHandle*>(Handle);
104 if (!pHandle)
105 return store_E_InvalidHandle;
107 pHandle->release();
108 return store_E_None;
111 /*========================================================================
113 * storeFileHandle implementation.
115 *======================================================================*/
117 * store_createMemoryFile.
119 storeError SAL_CALL store_createMemoryFile (
120 sal_uInt16 nPageSize,
121 storeFileHandle *phFile
122 ) SAL_THROW_EXTERN_C()
124 if (!phFile)
125 return store_E_InvalidParameter;
126 *phFile = NULL;
128 Reference<ILockBytes> xLockBytes;
130 storeError eErrCode = MemoryLockBytes_createInstance(xLockBytes);
131 if (eErrCode != store_E_None)
132 return eErrCode;
133 OSL_ASSERT(xLockBytes.is());
135 Reference<OStorePageManager> xManager (new OStorePageManager());
136 if (!xManager.is())
137 return store_E_OutOfMemory;
139 eErrCode = xManager->initialize (
140 &*xLockBytes, store_AccessCreate, nPageSize);
141 if (eErrCode != store_E_None)
142 return eErrCode;
144 xManager->acquire();
146 *phFile = (storeFileHandle)&(*xManager);
147 return store_E_None;
151 * store_openFile.
153 storeError SAL_CALL store_openFile (
154 rtl_uString *pFilename,
155 storeAccessMode eAccessMode,
156 sal_uInt16 nPageSize,
157 storeFileHandle *phFile
158 ) SAL_THROW_EXTERN_C()
160 if (phFile)
161 *phFile = NULL;
163 if (!(pFilename && phFile))
164 return store_E_InvalidParameter;
166 Reference<ILockBytes> xLockBytes;
168 storeError eErrCode = FileLockBytes_createInstance (xLockBytes, pFilename, eAccessMode);
169 if (eErrCode != store_E_None)
170 return eErrCode;
171 OSL_ASSERT(xLockBytes.is());
173 Reference<OStorePageManager> xManager (new OStorePageManager());
174 if (!xManager.is())
175 return store_E_OutOfMemory;
177 eErrCode = xManager->initialize (
178 &*xLockBytes, eAccessMode, nPageSize);
179 if (eErrCode != store_E_None)
180 return eErrCode;
182 xManager->acquire();
184 *phFile = (storeFileHandle)&(*xManager);
185 return store_E_None;
189 * store_closeFile.
191 storeError SAL_CALL store_closeFile (
192 storeFileHandle Handle
193 ) SAL_THROW_EXTERN_C()
195 OStorePageManager *pManager =
196 OStoreHandle<OStorePageManager>::query (Handle);
197 if (!pManager)
198 return store_E_InvalidHandle;
200 storeError eErrCode = pManager->close();
201 pManager->release();
202 return eErrCode;
206 * store_flushFile.
208 storeError SAL_CALL store_flushFile (
209 storeFileHandle Handle
210 ) SAL_THROW_EXTERN_C()
212 OStoreHandle<OStorePageManager> xManager (
213 OStoreHandle<OStorePageManager>::query (Handle));
214 if (!xManager.is())
215 return store_E_InvalidHandle;
217 return xManager->flush();
221 * store_getFileRefererCount.
223 storeError SAL_CALL store_getFileRefererCount (
224 storeFileHandle Handle,
225 sal_uInt32 *pnRefCount
226 ) SAL_THROW_EXTERN_C()
228 OStoreHandle<OStorePageManager> xManager (
229 OStoreHandle<OStorePageManager>::query (Handle));
230 if (!xManager.is())
231 return store_E_InvalidHandle;
233 if (!pnRefCount)
234 return store_E_InvalidParameter;
236 *pnRefCount = xManager->getRefererCount();
237 return store_E_None;
241 * store_getFileSize.
243 storeError SAL_CALL store_getFileSize (
244 storeFileHandle Handle,
245 sal_uInt32 *pnSize
246 ) SAL_THROW_EXTERN_C()
248 OStoreHandle<OStorePageManager> xManager (
249 OStoreHandle<OStorePageManager>::query (Handle));
250 if (!xManager.is())
251 return store_E_InvalidHandle;
253 if (!pnSize)
254 return store_E_InvalidParameter;
256 return xManager->size (*pnSize);
260 * store_rebuildFile.
262 storeError SAL_CALL store_rebuildFile (
263 rtl_uString *pSrcFilename,
264 rtl_uString *pDstFilename
265 ) SAL_THROW_EXTERN_C()
267 storeError eErrCode = store_E_None;
269 if (!(pSrcFilename && pDstFilename))
270 return store_E_InvalidParameter;
272 Reference<OStorePageManager> xManager (new OStorePageManager());
273 if (!xManager.is())
274 return store_E_OutOfMemory;
276 Reference<ILockBytes> xSrcLB;
277 eErrCode = FileLockBytes_createInstance (xSrcLB, pSrcFilename, store_AccessReadOnly);
278 if (eErrCode != store_E_None)
279 return eErrCode;
280 OSL_ASSERT(xSrcLB.is());
282 Reference<ILockBytes> xDstLB;
283 eErrCode = FileLockBytes_createInstance (xDstLB, pDstFilename, store_AccessCreate);
284 if (eErrCode != store_E_None)
285 return eErrCode;
286 OSL_ASSERT(xDstLB.is());
288 return xManager->rebuild (&*xSrcLB, &*xDstLB);
291 /*========================================================================
293 * storeDirectoryHandle implementation.
295 *======================================================================*/
297 * store_openDirectory.
299 storeError SAL_CALL store_openDirectory (
300 storeFileHandle hFile,
301 rtl_uString *pPath,
302 rtl_uString *pName,
303 storeAccessMode eAccessMode,
304 storeDirectoryHandle *phDirectory
305 ) SAL_THROW_EXTERN_C()
307 storeError eErrCode = store_E_None;
308 if (phDirectory)
309 *phDirectory = NULL;
311 OStoreHandle<OStorePageManager> xManager (
312 OStoreHandle<OStorePageManager>::query (hFile));
313 if (!xManager.is())
314 return store_E_InvalidHandle;
316 if (!(pPath && pName && phDirectory))
317 return store_E_InvalidParameter;
319 Reference<OStoreDirectory_Impl> xDirectory (new OStoreDirectory_Impl());
320 if (!xDirectory.is())
321 return store_E_OutOfMemory;
323 OString aPath (pPath->buffer, pPath->length, RTL_TEXTENCODING_UTF8);
324 OString aName (pName->buffer, pName->length, RTL_TEXTENCODING_UTF8);
326 eErrCode = xDirectory->create (&*xManager, aPath.pData, aName.pData, eAccessMode);
327 if (eErrCode != store_E_None)
328 return eErrCode;
330 xDirectory->acquire();
332 *phDirectory = (storeDirectoryHandle)&(*xDirectory);
333 return store_E_None;
337 * store_closeDirectory.
339 storeError SAL_CALL store_closeDirectory (
340 storeDirectoryHandle Handle
341 ) SAL_THROW_EXTERN_C()
343 OStoreDirectory_Impl *pDirectory =
344 OStoreHandle<OStoreDirectory_Impl>::query (Handle);
345 if (!pDirectory)
346 return store_E_InvalidHandle;
348 pDirectory->release();
349 return store_E_None;
353 * store_findFirst.
355 storeError SAL_CALL store_findFirst (
356 storeDirectoryHandle Handle,
357 storeFindData *pFindData
358 ) SAL_THROW_EXTERN_C()
360 OStoreHandle<OStoreDirectory_Impl> xDirectory (
361 OStoreHandle<OStoreDirectory_Impl>::query (Handle));
362 if (!xDirectory.is())
363 return store_E_InvalidHandle;
365 if (!pFindData)
366 return store_E_InvalidParameter;
368 // Initialize FindData.
369 rtl_zeroMemory (pFindData, sizeof (storeFindData));
371 // Find first.
372 pFindData->m_nReserved = (sal_uInt32)(~0);
373 return xDirectory->iterate (*pFindData);
377 * store_findNext.
379 storeError SAL_CALL store_findNext (
380 storeDirectoryHandle Handle,
381 storeFindData *pFindData
382 ) SAL_THROW_EXTERN_C()
384 OStoreHandle<OStoreDirectory_Impl> xDirectory (
385 OStoreHandle<OStoreDirectory_Impl>::query (Handle));
386 if (!xDirectory.is())
387 return store_E_InvalidHandle;
389 if (!pFindData)
390 return store_E_InvalidParameter;
392 // Check FindData.
393 if (!pFindData->m_nReserved)
394 return store_E_NoMoreFiles;
396 // Find next.
397 pFindData->m_nReserved -= 1;
398 return xDirectory->iterate (*pFindData);
401 /*========================================================================
403 * storeStreamHandle implementation.
405 *======================================================================*/
407 * store_openStream
409 storeError SAL_CALL store_openStream (
410 storeFileHandle hFile,
411 rtl_uString *pPath,
412 rtl_uString *pName,
413 storeAccessMode eAccessMode,
414 storeStreamHandle *phStream
415 ) SAL_THROW_EXTERN_C()
417 storeError eErrCode = store_E_None;
418 if (phStream)
419 *phStream = NULL;
421 OStoreHandle<OStorePageManager> xManager (
422 OStoreHandle<OStorePageManager>::query (hFile));
423 if (!xManager.is())
424 return store_E_InvalidHandle;
426 if (!(pPath && pName && phStream))
427 return store_E_InvalidParameter;
429 Reference<OStoreLockBytes> xLockBytes (new OStoreLockBytes());
430 if (!xLockBytes.is())
431 return store_E_OutOfMemory;
433 OString aPath (pPath->buffer, pPath->length, RTL_TEXTENCODING_UTF8);
434 OString aName (pName->buffer, pName->length, RTL_TEXTENCODING_UTF8);
436 eErrCode = xLockBytes->create (&*xManager, aPath.pData, aName.pData, eAccessMode);
437 if (eErrCode != store_E_None)
438 return eErrCode;
440 xLockBytes->acquire();
442 *phStream = (storeStreamHandle)&(*xLockBytes);
443 return store_E_None;
447 * store_closeStream.
449 storeError SAL_CALL store_closeStream (
450 storeStreamHandle Handle
451 ) SAL_THROW_EXTERN_C()
453 OStoreLockBytes *pLockBytes =
454 OStoreHandle<OStoreLockBytes>::query (Handle);
455 if (!pLockBytes)
456 return store_E_InvalidHandle;
458 pLockBytes->release();
459 return store_E_None;
463 * store_readStream.
465 storeError SAL_CALL store_readStream (
466 storeStreamHandle Handle,
467 sal_uInt32 nOffset,
468 void *pBuffer,
469 sal_uInt32 nBytes,
470 sal_uInt32 *pnDone
471 ) SAL_THROW_EXTERN_C()
473 OStoreHandle<OStoreLockBytes> xLockBytes (
474 OStoreHandle<OStoreLockBytes>::query (Handle));
475 if (!xLockBytes.is())
476 return store_E_InvalidHandle;
478 if (!(pBuffer && pnDone))
479 return store_E_InvalidParameter;
481 return xLockBytes->readAt (nOffset, pBuffer, nBytes, *pnDone);
485 * store_writeStream.
487 storeError SAL_CALL store_writeStream (
488 storeStreamHandle Handle,
489 sal_uInt32 nOffset,
490 const void *pBuffer,
491 sal_uInt32 nBytes,
492 sal_uInt32 *pnDone
493 ) SAL_THROW_EXTERN_C()
495 OStoreHandle<OStoreLockBytes> xLockBytes (
496 OStoreHandle<OStoreLockBytes>::query (Handle));
497 if (!xLockBytes.is())
498 return store_E_InvalidHandle;
500 if (!(pBuffer && pnDone))
501 return store_E_InvalidParameter;
503 return xLockBytes->writeAt (nOffset, pBuffer, nBytes, *pnDone);
507 * store_flushStream.
509 storeError SAL_CALL store_flushStream (
510 storeStreamHandle Handle
511 ) SAL_THROW_EXTERN_C()
513 OStoreHandle<OStoreLockBytes> xLockBytes (
514 OStoreHandle<OStoreLockBytes>::query (Handle));
515 if (!xLockBytes.is())
516 return store_E_InvalidHandle;
518 return xLockBytes->flush();
522 * store_getStreamSize.
524 storeError SAL_CALL store_getStreamSize (
525 storeStreamHandle Handle,
526 sal_uInt32 *pnSize
527 ) SAL_THROW_EXTERN_C()
529 OStoreHandle<OStoreLockBytes> xLockBytes (
530 OStoreHandle<OStoreLockBytes>::query (Handle));
531 if (!xLockBytes.is())
532 return store_E_InvalidHandle;
534 if (!pnSize)
535 return store_E_InvalidParameter;
537 return xLockBytes->stat (*pnSize);
541 * store_setStreamSize.
543 storeError SAL_CALL store_setStreamSize (
544 storeStreamHandle Handle,
545 sal_uInt32 nSize
546 ) SAL_THROW_EXTERN_C()
548 OStoreHandle<OStoreLockBytes> xLockBytes (
549 OStoreHandle<OStoreLockBytes>::query (Handle));
550 if (!xLockBytes.is())
551 return store_E_InvalidHandle;
553 return xLockBytes->setSize (nSize);
556 /*========================================================================
558 * Common storeDirectoryHandle and storeStreamHandle operations.
560 *======================================================================*/
562 * store_attrib.
564 storeError SAL_CALL store_attrib (
565 storeFileHandle Handle,
566 rtl_uString *pPath,
567 rtl_uString *pName,
568 sal_uInt32 nMask1,
569 sal_uInt32 nMask2,
570 sal_uInt32 *pnAttrib
571 ) SAL_THROW_EXTERN_C()
573 storeError eErrCode = store_E_None;
574 if (pnAttrib)
575 *pnAttrib = 0;
577 OStoreHandle<OStorePageManager> xManager (
578 OStoreHandle<OStorePageManager>::query (Handle));
579 if (!xManager.is())
580 return store_E_InvalidHandle;
582 if (!(pPath && pName))
583 return store_E_InvalidParameter;
585 // Setup page key.
586 OString aPath (pPath->buffer, pPath->length, RTL_TEXTENCODING_UTF8);
587 OString aName (pName->buffer, pName->length, RTL_TEXTENCODING_UTF8);
588 OStorePageKey aKey;
590 eErrCode = OStorePageManager::namei (aPath.pData, aName.pData, aKey);
591 if (eErrCode != store_E_None)
592 return eErrCode;
594 // Obtain or modify page attributes.
595 sal_uInt32 nAttrib = 0;
596 eErrCode = xManager->attrib (aKey, nMask1, nMask2, nAttrib);
597 if (pnAttrib)
598 *pnAttrib = nAttrib;
599 return eErrCode;
603 * store_link.
605 storeError SAL_CALL store_link (
606 storeFileHandle Handle,
607 rtl_uString *pSrcPath, rtl_uString *pSrcName,
608 rtl_uString *pDstPath, rtl_uString *pDstName
609 ) SAL_THROW_EXTERN_C()
611 storeError eErrCode = store_E_None;
613 OStoreHandle<OStorePageManager> xManager (
614 OStoreHandle<OStorePageManager>::query (Handle));
615 if (!xManager.is())
616 return store_E_InvalidHandle;
618 if (!(pSrcPath && pSrcName))
619 return store_E_InvalidParameter;
621 if (!(pDstPath && pDstName))
622 return store_E_InvalidParameter;
624 // Setup 'Source' page key.
625 OString aSrcPath (
626 pSrcPath->buffer, pSrcPath->length, RTL_TEXTENCODING_UTF8);
627 OString aSrcName (
628 pSrcName->buffer, pSrcName->length, RTL_TEXTENCODING_UTF8);
629 OStorePageKey aSrcKey;
631 eErrCode = OStorePageManager::namei (
632 aSrcPath.pData, aSrcName.pData, aSrcKey);
633 if (eErrCode != store_E_None)
634 return eErrCode;
636 // Setup 'Destination' page key.
637 OString aDstPath (
638 pDstPath->buffer, pDstPath->length, RTL_TEXTENCODING_UTF8);
639 OString aDstName (
640 pDstName->buffer, pDstName->length, RTL_TEXTENCODING_UTF8);
641 OStorePageKey aDstKey;
643 eErrCode = OStorePageManager::namei (
644 aDstPath.pData, aDstName.pData, aDstKey);
645 if (eErrCode != store_E_None)
646 return eErrCode;
648 // Link 'Source' hard to 'Destination'.
649 return xManager->link (aSrcKey, aDstKey);
653 * store_symlink.
655 storeError SAL_CALL store_symlink (
656 storeFileHandle Handle,
657 rtl_uString *pSrcPath, rtl_uString *pSrcName,
658 rtl_uString *pDstPath, rtl_uString *pDstName
659 ) SAL_THROW_EXTERN_C()
661 storeError eErrCode = store_E_None;
663 OStoreHandle<OStorePageManager> xManager (
664 OStoreHandle<OStorePageManager>::query (Handle));
665 if (!xManager.is())
666 return store_E_InvalidHandle;
668 if (!(pSrcPath && pSrcName))
669 return store_E_InvalidParameter;
671 if (!(pDstPath && pDstName))
672 return store_E_InvalidParameter;
674 // Setup 'Destination' page key.
675 OString aDstPath (
676 pDstPath->buffer, pDstPath->length, RTL_TEXTENCODING_UTF8);
677 OString aDstName (
678 pDstName->buffer, pDstName->length, RTL_TEXTENCODING_UTF8);
679 OStorePageKey aDstKey;
681 eErrCode = OStorePageManager::namei (
682 aDstPath.pData, aDstName.pData, aDstKey);
683 if (eErrCode != store_E_None)
684 return eErrCode;
686 // Insert 'Source' as symlink to 'Destination'.
687 OString aSrcPath (
688 pSrcPath->buffer, pSrcPath->length, RTL_TEXTENCODING_UTF8);
689 OString aSrcName (
690 pSrcName->buffer, pSrcName->length, RTL_TEXTENCODING_UTF8);
692 return xManager->symlink (aSrcPath.pData, aSrcName.pData, aDstKey);
696 * store_rename.
698 storeError SAL_CALL store_rename (
699 storeFileHandle Handle,
700 rtl_uString *pSrcPath, rtl_uString *pSrcName,
701 rtl_uString *pDstPath, rtl_uString *pDstName
702 ) SAL_THROW_EXTERN_C()
704 storeError eErrCode = store_E_None;
706 OStoreHandle<OStorePageManager> xManager (
707 OStoreHandle<OStorePageManager>::query (Handle));
708 if (!xManager.is())
709 return store_E_InvalidHandle;
711 if (!(pSrcPath && pSrcName))
712 return store_E_InvalidParameter;
714 if (!(pDstPath && pDstName))
715 return store_E_InvalidParameter;
717 // Setup 'Source' page key.
718 OString aSrcPath (
719 pSrcPath->buffer, pSrcPath->length, RTL_TEXTENCODING_UTF8);
720 OString aSrcName (
721 pSrcName->buffer, pSrcName->length, RTL_TEXTENCODING_UTF8);
722 OStorePageKey aSrcKey;
724 eErrCode = OStorePageManager::namei (
725 aSrcPath.pData, aSrcName.pData, aSrcKey);
726 if (eErrCode != store_E_None)
727 return eErrCode;
729 // Rename 'Source' into 'Destination'.
730 OString aDstPath (
731 pDstPath->buffer, pDstPath->length, RTL_TEXTENCODING_UTF8);
732 OString aDstName (
733 pDstName->buffer, pDstName->length, RTL_TEXTENCODING_UTF8);
735 return xManager->rename (aSrcKey, aDstPath.pData, aDstName.pData);
739 * store_remove.
741 storeError SAL_CALL store_remove (
742 storeFileHandle Handle,
743 rtl_uString *pPath,
744 rtl_uString *pName
745 ) SAL_THROW_EXTERN_C()
747 storeError eErrCode = store_E_None;
749 OStoreHandle<OStorePageManager> xManager (
750 OStoreHandle<OStorePageManager>::query (Handle));
751 if (!xManager.is())
752 return store_E_InvalidHandle;
754 if (!(pPath && pName))
755 return store_E_InvalidParameter;
757 // Setup page key.
758 OString aPath (pPath->buffer, pPath->length, RTL_TEXTENCODING_UTF8);
759 OString aName (pName->buffer, pName->length, RTL_TEXTENCODING_UTF8);
760 OStorePageKey aKey;
762 eErrCode = OStorePageManager::namei (aPath.pData, aName.pData, aKey);
763 if (eErrCode != store_E_None)
764 return eErrCode;
766 // Remove.
767 return xManager->remove (aKey);