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>
38 #include "lockbyte.hxx"
40 #include "storbase.hxx"
41 #include "storpage.hxx"
42 #include "stordir.hxx"
43 #include "storlckb.hxx"
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
>
56 OStoreHandle (store_handle_type
* pHandle
)
57 : rtl::Reference
<store_handle_type
> (pHandle
)
60 static store_handle_type
* SAL_CALL
query (void * pHandle
)
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 (
82 ) SAL_THROW_EXTERN_C()
84 IStoreHandle
*pHandle
= static_cast<IStoreHandle
*>(Handle
);
86 return store_E_InvalidHandle
;
93 * store_releaseHandle.
95 storeError SAL_CALL
store_releaseHandle (
97 ) SAL_THROW_EXTERN_C()
99 IStoreHandle
*pHandle
= static_cast<IStoreHandle
*>(Handle
);
101 return store_E_InvalidHandle
;
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()
121 return store_E_InvalidParameter
;
124 Reference
<ILockBytes
> xLockBytes
;
126 storeError eErrCode
= MemoryLockBytes_createInstance(xLockBytes
);
127 if (eErrCode
!= store_E_None
)
129 OSL_ASSERT(xLockBytes
.is());
131 Reference
<OStorePageManager
> xManager (new OStorePageManager());
133 return store_E_OutOfMemory
;
135 eErrCode
= xManager
->initialize (
136 &*xLockBytes
, store_AccessCreate
, nPageSize
);
137 if (eErrCode
!= store_E_None
)
142 *phFile
= (storeFileHandle
)&(*xManager
);
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()
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
)
167 OSL_ASSERT(xLockBytes
.is());
169 Reference
<OStorePageManager
> xManager (new OStorePageManager());
171 return store_E_OutOfMemory
;
173 eErrCode
= xManager
->initialize (
174 &*xLockBytes
, eAccessMode
, nPageSize
);
175 if (eErrCode
!= store_E_None
)
180 *phFile
= (storeFileHandle
)&(*xManager
);
187 storeError SAL_CALL
store_closeFile (
188 storeFileHandle Handle
189 ) SAL_THROW_EXTERN_C()
191 OStorePageManager
*pManager
=
192 OStoreHandle
<OStorePageManager
>::query (Handle
);
194 return store_E_InvalidHandle
;
196 storeError eErrCode
= pManager
->close();
204 storeError SAL_CALL
store_flushFile (
205 storeFileHandle Handle
206 ) SAL_THROW_EXTERN_C()
208 OStoreHandle
<OStorePageManager
> xManager (
209 OStoreHandle
<OStorePageManager
>::query (Handle
));
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
));
227 return store_E_InvalidHandle
;
230 return store_E_InvalidParameter
;
232 *pnRefCount
= xManager
->getRefererCount();
239 storeError SAL_CALL
store_getFileSize (
240 storeFileHandle Handle
,
242 ) SAL_THROW_EXTERN_C()
244 OStoreHandle
<OStorePageManager
> xManager (
245 OStoreHandle
<OStorePageManager
>::query (Handle
));
247 return store_E_InvalidHandle
;
250 return store_E_InvalidParameter
;
252 return xManager
->size (*pnSize
);
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());
270 return store_E_OutOfMemory
;
272 Reference
<ILockBytes
> xSrcLB
;
273 eErrCode
= FileLockBytes_createInstance (xSrcLB
, pSrcFilename
, store_AccessReadOnly
);
274 if (eErrCode
!= store_E_None
)
276 OSL_ASSERT(xSrcLB
.is());
278 Reference
<ILockBytes
> xDstLB
;
279 eErrCode
= FileLockBytes_createInstance (xDstLB
, pDstFilename
, store_AccessCreate
);
280 if (eErrCode
!= store_E_None
)
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
,
299 storeAccessMode eAccessMode
,
300 storeDirectoryHandle
*phDirectory
301 ) SAL_THROW_EXTERN_C()
303 storeError eErrCode
= store_E_None
;
307 OStoreHandle
<OStorePageManager
> xManager (
308 OStoreHandle
<OStorePageManager
>::query (hFile
));
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
)
326 xDirectory
->acquire();
328 *phDirectory
= (storeDirectoryHandle
)&(*xDirectory
);
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
);
342 return store_E_InvalidHandle
;
344 pDirectory
->release();
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
;
362 return store_E_InvalidParameter
;
364 // Initialize FindData.
365 rtl_zeroMemory (pFindData
, sizeof (storeFindData
));
368 pFindData
->m_nReserved
= (sal_uInt32
)(~0);
369 return xDirectory
->iterate (*pFindData
);
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
;
386 return store_E_InvalidParameter
;
389 if (!pFindData
->m_nReserved
)
390 return store_E_NoMoreFiles
;
393 pFindData
->m_nReserved
-= 1;
394 return xDirectory
->iterate (*pFindData
);
397 /*========================================================================
399 * storeStreamHandle implementation.
401 *======================================================================*/
405 storeError SAL_CALL
store_openStream (
406 storeFileHandle hFile
,
409 storeAccessMode eAccessMode
,
410 storeStreamHandle
*phStream
411 ) SAL_THROW_EXTERN_C()
413 storeError eErrCode
= store_E_None
;
417 OStoreHandle
<OStorePageManager
> xManager (
418 OStoreHandle
<OStorePageManager
>::query (hFile
));
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
)
436 xLockBytes
->acquire();
438 *phStream
= (storeStreamHandle
)&(*xLockBytes
);
445 storeError SAL_CALL
store_closeStream (
446 storeStreamHandle Handle
447 ) SAL_THROW_EXTERN_C()
449 OStoreLockBytes
*pLockBytes
=
450 OStoreHandle
<OStoreLockBytes
>::query (Handle
);
452 return store_E_InvalidHandle
;
454 pLockBytes
->release();
461 storeError SAL_CALL
store_readStream (
462 storeStreamHandle Handle
,
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
);
483 storeError SAL_CALL
store_writeStream (
484 storeStreamHandle Handle
,
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
);
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
,
523 ) SAL_THROW_EXTERN_C()
525 OStoreHandle
<OStoreLockBytes
> xLockBytes (
526 OStoreHandle
<OStoreLockBytes
>::query (Handle
));
527 if (!xLockBytes
.is())
528 return store_E_InvalidHandle
;
531 return store_E_InvalidParameter
;
533 return xLockBytes
->stat (*pnSize
);
537 * store_setStreamSize.
539 storeError SAL_CALL
store_setStreamSize (
540 storeStreamHandle Handle
,
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 *======================================================================*/
560 storeError SAL_CALL
store_attrib (
561 storeFileHandle Handle
,
567 ) SAL_THROW_EXTERN_C()
569 storeError eErrCode
= store_E_None
;
573 OStoreHandle
<OStorePageManager
> xManager (
574 OStoreHandle
<OStorePageManager
>::query (Handle
));
576 return store_E_InvalidHandle
;
578 if (!(pPath
&& pName
))
579 return store_E_InvalidParameter
;
582 OString
aPath (pPath
->buffer
, pPath
->length
, RTL_TEXTENCODING_UTF8
);
583 OString
aName (pName
->buffer
, pName
->length
, RTL_TEXTENCODING_UTF8
);
586 eErrCode
= OStorePageManager::namei (aPath
.pData
, aName
.pData
, aKey
);
587 if (eErrCode
!= store_E_None
)
590 // Obtain or modify page attributes.
591 sal_uInt32 nAttrib
= 0;
592 eErrCode
= xManager
->attrib (aKey
, nMask1
, nMask2
, nAttrib
);
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
));
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.
622 pSrcPath
->buffer
, pSrcPath
->length
, RTL_TEXTENCODING_UTF8
);
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
)
632 // Setup 'Destination' page key.
634 pDstPath
->buffer
, pDstPath
->length
, RTL_TEXTENCODING_UTF8
);
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
)
644 // Link 'Source' hard to 'Destination'.
645 return xManager
->link (aSrcKey
, aDstKey
);
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
));
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.
672 pDstPath
->buffer
, pDstPath
->length
, RTL_TEXTENCODING_UTF8
);
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
)
682 // Insert 'Source' as symlink to 'Destination'.
684 pSrcPath
->buffer
, pSrcPath
->length
, RTL_TEXTENCODING_UTF8
);
686 pSrcName
->buffer
, pSrcName
->length
, RTL_TEXTENCODING_UTF8
);
688 return xManager
->symlink (aSrcPath
.pData
, aSrcName
.pData
, aDstKey
);
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
));
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.
715 pSrcPath
->buffer
, pSrcPath
->length
, RTL_TEXTENCODING_UTF8
);
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
)
725 // Rename 'Source' into 'Destination'.
727 pDstPath
->buffer
, pDstPath
->length
, RTL_TEXTENCODING_UTF8
);
729 pDstName
->buffer
, pDstName
->length
, RTL_TEXTENCODING_UTF8
);
731 return xManager
->rename (aSrcKey
, aDstPath
.pData
, aDstName
.pData
);
737 storeError SAL_CALL
store_remove (
738 storeFileHandle Handle
,
741 ) SAL_THROW_EXTERN_C()
743 storeError eErrCode
= store_E_None
;
745 OStoreHandle
<OStorePageManager
> xManager (
746 OStoreHandle
<OStorePageManager
>::query (Handle
));
748 return store_E_InvalidHandle
;
750 if (!(pPath
&& pName
))
751 return store_E_InvalidParameter
;
754 OString
aPath (pPath
->buffer
, pPath
->length
, RTL_TEXTENCODING_UTF8
);
755 OString
aName (pName
->buffer
, pName
->length
, RTL_TEXTENCODING_UTF8
);
758 eErrCode
= OStorePageManager::namei (aPath
.pData
, aName
.pData
, aKey
);
759 if (eErrCode
!= store_E_None
)
763 return xManager
->remove (aKey
);
766 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */