1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
27 #include "lockbyte.hxx"
29 #include "storbase.hxx"
30 #include "storpage.hxx"
31 #include "stordir.hxx"
32 #include "storlckb.hxx"
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
>
44 OStoreHandle (store_handle_type
* pHandle
)
45 : rtl::Reference
<store_handle_type
> (pHandle
)
48 static store_handle_type
* SAL_CALL
query (void * pHandle
)
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 (
69 ) SAL_THROW_EXTERN_C()
71 IStoreHandle
*pHandle
= static_cast<IStoreHandle
*>(Handle
);
73 return store_E_InvalidHandle
;
80 * store_releaseHandle.
82 storeError SAL_CALL
store_releaseHandle (
84 ) SAL_THROW_EXTERN_C()
86 IStoreHandle
*pHandle
= static_cast<IStoreHandle
*>(Handle
);
88 return store_E_InvalidHandle
;
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()
108 return store_E_InvalidParameter
;
111 Reference
<ILockBytes
> xLockBytes
;
113 storeError eErrCode
= MemoryLockBytes_createInstance(xLockBytes
);
114 if (eErrCode
!= store_E_None
)
116 OSL_ASSERT(xLockBytes
.is());
118 Reference
<OStorePageManager
> xManager (new OStorePageManager());
120 return store_E_OutOfMemory
;
122 eErrCode
= xManager
->initialize (
123 &*xLockBytes
, store_AccessCreate
, nPageSize
);
124 if (eErrCode
!= store_E_None
)
129 *phFile
= (storeFileHandle
)&(*xManager
);
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()
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
)
154 OSL_ASSERT(xLockBytes
.is());
156 Reference
<OStorePageManager
> xManager (new OStorePageManager());
158 return store_E_OutOfMemory
;
160 eErrCode
= xManager
->initialize (
161 &*xLockBytes
, eAccessMode
, nPageSize
);
162 if (eErrCode
!= store_E_None
)
167 *phFile
= (storeFileHandle
)&(*xManager
);
174 storeError SAL_CALL
store_closeFile (
175 storeFileHandle Handle
176 ) SAL_THROW_EXTERN_C()
178 OStorePageManager
*pManager
=
179 OStoreHandle
<OStorePageManager
>::query (Handle
);
181 return store_E_InvalidHandle
;
183 storeError eErrCode
= pManager
->close();
191 storeError SAL_CALL
store_flushFile (
192 storeFileHandle Handle
193 ) SAL_THROW_EXTERN_C()
195 OStoreHandle
<OStorePageManager
> xManager (
196 OStoreHandle
<OStorePageManager
>::query (Handle
));
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
));
214 return store_E_InvalidHandle
;
217 return store_E_InvalidParameter
;
219 *pnRefCount
= xManager
->getRefererCount();
226 storeError SAL_CALL
store_getFileSize (
227 storeFileHandle Handle
,
229 ) SAL_THROW_EXTERN_C()
231 OStoreHandle
<OStorePageManager
> xManager (
232 OStoreHandle
<OStorePageManager
>::query (Handle
));
234 return store_E_InvalidHandle
;
237 return store_E_InvalidParameter
;
239 return xManager
->size (*pnSize
);
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());
257 return store_E_OutOfMemory
;
259 Reference
<ILockBytes
> xSrcLB
;
260 eErrCode
= FileLockBytes_createInstance (xSrcLB
, pSrcFilename
, store_AccessReadOnly
);
261 if (eErrCode
!= store_E_None
)
263 OSL_ASSERT(xSrcLB
.is());
265 Reference
<ILockBytes
> xDstLB
;
266 eErrCode
= FileLockBytes_createInstance (xDstLB
, pDstFilename
, store_AccessCreate
);
267 if (eErrCode
!= store_E_None
)
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
,
286 storeAccessMode eAccessMode
,
287 storeDirectoryHandle
*phDirectory
288 ) SAL_THROW_EXTERN_C()
290 storeError eErrCode
= store_E_None
;
294 OStoreHandle
<OStorePageManager
> xManager (
295 OStoreHandle
<OStorePageManager
>::query (hFile
));
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
)
313 xDirectory
->acquire();
315 *phDirectory
= (storeDirectoryHandle
)&(*xDirectory
);
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
);
329 return store_E_InvalidHandle
;
331 pDirectory
->release();
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
;
349 return store_E_InvalidParameter
;
351 // Initialize FindData.
352 memset (pFindData
, 0, sizeof (storeFindData
));
355 pFindData
->m_nReserved
= (sal_uInt32
)(~0);
356 return xDirectory
->iterate (*pFindData
);
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
;
373 return store_E_InvalidParameter
;
376 if (!pFindData
->m_nReserved
)
377 return store_E_NoMoreFiles
;
380 pFindData
->m_nReserved
-= 1;
381 return xDirectory
->iterate (*pFindData
);
384 /*========================================================================
386 * storeStreamHandle implementation.
388 *======================================================================*/
392 storeError SAL_CALL
store_openStream (
393 storeFileHandle hFile
,
396 storeAccessMode eAccessMode
,
397 storeStreamHandle
*phStream
398 ) SAL_THROW_EXTERN_C()
400 storeError eErrCode
= store_E_None
;
404 OStoreHandle
<OStorePageManager
> xManager (
405 OStoreHandle
<OStorePageManager
>::query (hFile
));
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
)
423 xLockBytes
->acquire();
425 *phStream
= (storeStreamHandle
)&(*xLockBytes
);
432 storeError SAL_CALL
store_closeStream (
433 storeStreamHandle Handle
434 ) SAL_THROW_EXTERN_C()
436 OStoreLockBytes
*pLockBytes
=
437 OStoreHandle
<OStoreLockBytes
>::query (Handle
);
439 return store_E_InvalidHandle
;
441 pLockBytes
->release();
448 storeError SAL_CALL
store_readStream (
449 storeStreamHandle Handle
,
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
);
470 storeError SAL_CALL
store_writeStream (
471 storeStreamHandle Handle
,
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
);
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
,
510 ) SAL_THROW_EXTERN_C()
512 OStoreHandle
<OStoreLockBytes
> xLockBytes (
513 OStoreHandle
<OStoreLockBytes
>::query (Handle
));
514 if (!xLockBytes
.is())
515 return store_E_InvalidHandle
;
518 return store_E_InvalidParameter
;
520 return xLockBytes
->stat (*pnSize
);
524 * store_setStreamSize.
526 storeError SAL_CALL
store_setStreamSize (
527 storeStreamHandle Handle
,
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 *======================================================================*/
547 storeError SAL_CALL
store_attrib (
548 storeFileHandle Handle
,
554 ) SAL_THROW_EXTERN_C()
556 storeError eErrCode
= store_E_None
;
560 OStoreHandle
<OStorePageManager
> xManager (
561 OStoreHandle
<OStorePageManager
>::query (Handle
));
563 return store_E_InvalidHandle
;
565 if (!(pPath
&& pName
))
566 return store_E_InvalidParameter
;
569 OString
aPath (pPath
->buffer
, pPath
->length
, RTL_TEXTENCODING_UTF8
);
570 OString
aName (pName
->buffer
, pName
->length
, RTL_TEXTENCODING_UTF8
);
573 eErrCode
= OStorePageManager::namei (aPath
.pData
, aName
.pData
, aKey
);
574 if (eErrCode
!= store_E_None
)
577 // Obtain or modify page attributes.
578 sal_uInt32 nAttrib
= 0;
579 eErrCode
= xManager
->attrib (aKey
, nMask1
, nMask2
, nAttrib
);
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
));
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.
609 pSrcPath
->buffer
, pSrcPath
->length
, RTL_TEXTENCODING_UTF8
);
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
)
619 // Setup 'Destination' page key.
621 pDstPath
->buffer
, pDstPath
->length
, RTL_TEXTENCODING_UTF8
);
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
)
631 // Link 'Source' hard to 'Destination'.
632 return xManager
->link (aSrcKey
, aDstKey
);
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
));
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.
659 pDstPath
->buffer
, pDstPath
->length
, RTL_TEXTENCODING_UTF8
);
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
)
669 // Insert 'Source' as symlink to 'Destination'.
671 pSrcPath
->buffer
, pSrcPath
->length
, RTL_TEXTENCODING_UTF8
);
673 pSrcName
->buffer
, pSrcName
->length
, RTL_TEXTENCODING_UTF8
);
675 return xManager
->symlink (aSrcPath
.pData
, aSrcName
.pData
, aDstKey
);
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
));
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.
702 pSrcPath
->buffer
, pSrcPath
->length
, RTL_TEXTENCODING_UTF8
);
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
)
712 // Rename 'Source' into 'Destination'.
714 pDstPath
->buffer
, pDstPath
->length
, RTL_TEXTENCODING_UTF8
);
716 pDstName
->buffer
, pDstName
->length
, RTL_TEXTENCODING_UTF8
);
718 return xManager
->rename (aSrcKey
, aDstPath
.pData
, aDstName
.pData
);
724 storeError SAL_CALL
store_remove (
725 storeFileHandle Handle
,
728 ) SAL_THROW_EXTERN_C()
730 storeError eErrCode
= store_E_None
;
732 OStoreHandle
<OStorePageManager
> xManager (
733 OStoreHandle
<OStorePageManager
>::query (Handle
));
735 return store_E_InvalidHandle
;
737 if (!(pPath
&& pName
))
738 return store_E_InvalidParameter
;
741 OString
aPath (pPath
->buffer
, pPath
->length
, RTL_TEXTENCODING_UTF8
);
742 OString
aName (pName
->buffer
, pName
->length
, RTL_TEXTENCODING_UTF8
);
745 eErrCode
= OStorePageManager::namei (aPath
.pData
, aName
.pData
, aKey
);
746 if (eErrCode
!= store_E_None
)
750 return xManager
->remove (aKey
);
753 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */