1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: store.cxx,v $
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>
42 #include "lockbyte.hxx"
44 #include "storbase.hxx"
45 #include "storpage.hxx"
46 #include "stordir.hxx"
47 #include "storlckb.hxx"
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
>
60 OStoreHandle (store_handle_type
* pHandle
)
61 : rtl::Reference
<store_handle_type
> (pHandle
)
64 static store_handle_type
* SAL_CALL
query (void * pHandle
)
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 (
86 ) SAL_THROW_EXTERN_C()
88 IStoreHandle
*pHandle
= static_cast<IStoreHandle
*>(Handle
);
90 return store_E_InvalidHandle
;
97 * store_releaseHandle.
99 storeError SAL_CALL
store_releaseHandle (
101 ) SAL_THROW_EXTERN_C()
103 IStoreHandle
*pHandle
= static_cast<IStoreHandle
*>(Handle
);
105 return store_E_InvalidHandle
;
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()
125 return store_E_InvalidParameter
;
128 Reference
<ILockBytes
> xLockBytes
;
130 storeError eErrCode
= MemoryLockBytes_createInstance(xLockBytes
);
131 if (eErrCode
!= store_E_None
)
133 OSL_ASSERT(xLockBytes
.is());
135 Reference
<OStorePageManager
> xManager (new OStorePageManager());
137 return store_E_OutOfMemory
;
139 eErrCode
= xManager
->initialize (
140 &*xLockBytes
, store_AccessCreate
, nPageSize
);
141 if (eErrCode
!= store_E_None
)
146 *phFile
= (storeFileHandle
)&(*xManager
);
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()
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
)
171 OSL_ASSERT(xLockBytes
.is());
173 Reference
<OStorePageManager
> xManager (new OStorePageManager());
175 return store_E_OutOfMemory
;
177 eErrCode
= xManager
->initialize (
178 &*xLockBytes
, eAccessMode
, nPageSize
);
179 if (eErrCode
!= store_E_None
)
184 *phFile
= (storeFileHandle
)&(*xManager
);
191 storeError SAL_CALL
store_closeFile (
192 storeFileHandle Handle
193 ) SAL_THROW_EXTERN_C()
195 OStorePageManager
*pManager
=
196 OStoreHandle
<OStorePageManager
>::query (Handle
);
198 return store_E_InvalidHandle
;
200 storeError eErrCode
= pManager
->close();
208 storeError SAL_CALL
store_flushFile (
209 storeFileHandle Handle
210 ) SAL_THROW_EXTERN_C()
212 OStoreHandle
<OStorePageManager
> xManager (
213 OStoreHandle
<OStorePageManager
>::query (Handle
));
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
));
231 return store_E_InvalidHandle
;
234 return store_E_InvalidParameter
;
236 *pnRefCount
= xManager
->getRefererCount();
243 storeError SAL_CALL
store_getFileSize (
244 storeFileHandle Handle
,
246 ) SAL_THROW_EXTERN_C()
248 OStoreHandle
<OStorePageManager
> xManager (
249 OStoreHandle
<OStorePageManager
>::query (Handle
));
251 return store_E_InvalidHandle
;
254 return store_E_InvalidParameter
;
256 return xManager
->size (*pnSize
);
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());
274 return store_E_OutOfMemory
;
276 Reference
<ILockBytes
> xSrcLB
;
277 eErrCode
= FileLockBytes_createInstance (xSrcLB
, pSrcFilename
, store_AccessReadOnly
);
278 if (eErrCode
!= store_E_None
)
280 OSL_ASSERT(xSrcLB
.is());
282 Reference
<ILockBytes
> xDstLB
;
283 eErrCode
= FileLockBytes_createInstance (xDstLB
, pDstFilename
, store_AccessCreate
);
284 if (eErrCode
!= store_E_None
)
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
,
303 storeAccessMode eAccessMode
,
304 storeDirectoryHandle
*phDirectory
305 ) SAL_THROW_EXTERN_C()
307 storeError eErrCode
= store_E_None
;
311 OStoreHandle
<OStorePageManager
> xManager (
312 OStoreHandle
<OStorePageManager
>::query (hFile
));
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
)
330 xDirectory
->acquire();
332 *phDirectory
= (storeDirectoryHandle
)&(*xDirectory
);
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
);
346 return store_E_InvalidHandle
;
348 pDirectory
->release();
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
;
366 return store_E_InvalidParameter
;
368 // Initialize FindData.
369 rtl_zeroMemory (pFindData
, sizeof (storeFindData
));
372 pFindData
->m_nReserved
= (sal_uInt32
)(~0);
373 return xDirectory
->iterate (*pFindData
);
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
;
390 return store_E_InvalidParameter
;
393 if (!pFindData
->m_nReserved
)
394 return store_E_NoMoreFiles
;
397 pFindData
->m_nReserved
-= 1;
398 return xDirectory
->iterate (*pFindData
);
401 /*========================================================================
403 * storeStreamHandle implementation.
405 *======================================================================*/
409 storeError SAL_CALL
store_openStream (
410 storeFileHandle hFile
,
413 storeAccessMode eAccessMode
,
414 storeStreamHandle
*phStream
415 ) SAL_THROW_EXTERN_C()
417 storeError eErrCode
= store_E_None
;
421 OStoreHandle
<OStorePageManager
> xManager (
422 OStoreHandle
<OStorePageManager
>::query (hFile
));
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
)
440 xLockBytes
->acquire();
442 *phStream
= (storeStreamHandle
)&(*xLockBytes
);
449 storeError SAL_CALL
store_closeStream (
450 storeStreamHandle Handle
451 ) SAL_THROW_EXTERN_C()
453 OStoreLockBytes
*pLockBytes
=
454 OStoreHandle
<OStoreLockBytes
>::query (Handle
);
456 return store_E_InvalidHandle
;
458 pLockBytes
->release();
465 storeError SAL_CALL
store_readStream (
466 storeStreamHandle Handle
,
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
);
487 storeError SAL_CALL
store_writeStream (
488 storeStreamHandle Handle
,
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
);
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
,
527 ) SAL_THROW_EXTERN_C()
529 OStoreHandle
<OStoreLockBytes
> xLockBytes (
530 OStoreHandle
<OStoreLockBytes
>::query (Handle
));
531 if (!xLockBytes
.is())
532 return store_E_InvalidHandle
;
535 return store_E_InvalidParameter
;
537 return xLockBytes
->stat (*pnSize
);
541 * store_setStreamSize.
543 storeError SAL_CALL
store_setStreamSize (
544 storeStreamHandle Handle
,
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 *======================================================================*/
564 storeError SAL_CALL
store_attrib (
565 storeFileHandle Handle
,
571 ) SAL_THROW_EXTERN_C()
573 storeError eErrCode
= store_E_None
;
577 OStoreHandle
<OStorePageManager
> xManager (
578 OStoreHandle
<OStorePageManager
>::query (Handle
));
580 return store_E_InvalidHandle
;
582 if (!(pPath
&& pName
))
583 return store_E_InvalidParameter
;
586 OString
aPath (pPath
->buffer
, pPath
->length
, RTL_TEXTENCODING_UTF8
);
587 OString
aName (pName
->buffer
, pName
->length
, RTL_TEXTENCODING_UTF8
);
590 eErrCode
= OStorePageManager::namei (aPath
.pData
, aName
.pData
, aKey
);
591 if (eErrCode
!= store_E_None
)
594 // Obtain or modify page attributes.
595 sal_uInt32 nAttrib
= 0;
596 eErrCode
= xManager
->attrib (aKey
, nMask1
, nMask2
, nAttrib
);
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
));
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.
626 pSrcPath
->buffer
, pSrcPath
->length
, RTL_TEXTENCODING_UTF8
);
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
)
636 // Setup 'Destination' page key.
638 pDstPath
->buffer
, pDstPath
->length
, RTL_TEXTENCODING_UTF8
);
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
)
648 // Link 'Source' hard to 'Destination'.
649 return xManager
->link (aSrcKey
, aDstKey
);
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
));
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.
676 pDstPath
->buffer
, pDstPath
->length
, RTL_TEXTENCODING_UTF8
);
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
)
686 // Insert 'Source' as symlink to 'Destination'.
688 pSrcPath
->buffer
, pSrcPath
->length
, RTL_TEXTENCODING_UTF8
);
690 pSrcName
->buffer
, pSrcName
->length
, RTL_TEXTENCODING_UTF8
);
692 return xManager
->symlink (aSrcPath
.pData
, aSrcName
.pData
, aDstKey
);
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
));
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.
719 pSrcPath
->buffer
, pSrcPath
->length
, RTL_TEXTENCODING_UTF8
);
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
)
729 // Rename 'Source' into 'Destination'.
731 pDstPath
->buffer
, pDstPath
->length
, RTL_TEXTENCODING_UTF8
);
733 pDstName
->buffer
, pDstName
->length
, RTL_TEXTENCODING_UTF8
);
735 return xManager
->rename (aSrcKey
, aDstPath
.pData
, aDstName
.pData
);
741 storeError SAL_CALL
store_remove (
742 storeFileHandle Handle
,
745 ) SAL_THROW_EXTERN_C()
747 storeError eErrCode
= store_E_None
;
749 OStoreHandle
<OStorePageManager
> xManager (
750 OStoreHandle
<OStorePageManager
>::query (Handle
));
752 return store_E_InvalidHandle
;
754 if (!(pPath
&& pName
))
755 return store_E_InvalidParameter
;
758 OString
aPath (pPath
->buffer
, pPath
->length
, RTL_TEXTENCODING_UTF8
);
759 OString
aName (pName
->buffer
, pName
->length
, RTL_TEXTENCODING_UTF8
);
762 eErrCode
= OStorePageManager::namei (aPath
.pData
, aName
.pData
, aKey
);
763 if (eErrCode
!= store_E_None
)
767 return xManager
->remove (aKey
);