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 .
21 #include "store/store.h"
23 #include <sal/types.h>
24 #include <rtl/string.hxx>
25 #include <rtl/ref.hxx>
28 #include "lockbyte.hxx"
30 #include "storbase.hxx"
31 #include "storpage.hxx"
32 #include "stordir.hxx"
33 #include "storlckb.hxx"
39 /** Template helper class as type safe Reference to store_handle_type.
41 template<class store_handle_type
>
42 class OStoreHandle
: public rtl::Reference
<store_handle_type
>
45 OStoreHandle (store_handle_type
* pHandle
)
46 : rtl::Reference
<store_handle_type
> (pHandle
)
49 static store_handle_type
* SAL_CALL
query (void * pHandle
)
52 static_cast<IStoreHandle
*>(pHandle
),
53 static_cast<store_handle_type
*>(0));
59 using namespace store
;
61 /*========================================================================
63 * storeHandle implementation.
65 *======================================================================*/
67 * store_acquireHandle.
69 storeError SAL_CALL
store_acquireHandle (
71 ) SAL_THROW_EXTERN_C()
73 IStoreHandle
*pHandle
= static_cast<IStoreHandle
*>(Handle
);
75 return store_E_InvalidHandle
;
82 * store_releaseHandle.
84 storeError SAL_CALL
store_releaseHandle (
86 ) SAL_THROW_EXTERN_C()
88 IStoreHandle
*pHandle
= static_cast<IStoreHandle
*>(Handle
);
90 return store_E_InvalidHandle
;
96 /*========================================================================
98 * storeFileHandle implementation.
100 *======================================================================*/
102 * store_createMemoryFile.
104 storeError SAL_CALL
store_createMemoryFile (
105 sal_uInt16 nPageSize
,
106 storeFileHandle
*phFile
107 ) SAL_THROW_EXTERN_C()
110 return store_E_InvalidParameter
;
113 Reference
<ILockBytes
> xLockBytes
;
115 storeError eErrCode
= MemoryLockBytes_createInstance(xLockBytes
);
116 if (eErrCode
!= store_E_None
)
118 OSL_ASSERT(xLockBytes
.is());
120 Reference
<OStorePageManager
> xManager (new OStorePageManager());
122 return store_E_OutOfMemory
;
124 eErrCode
= xManager
->initialize (
125 &*xLockBytes
, store_AccessCreate
, nPageSize
);
126 if (eErrCode
!= store_E_None
)
131 *phFile
= (storeFileHandle
)&(*xManager
);
138 storeError SAL_CALL
store_openFile (
139 rtl_uString
*pFilename
,
140 storeAccessMode eAccessMode
,
141 sal_uInt16 nPageSize
,
142 storeFileHandle
*phFile
143 ) SAL_THROW_EXTERN_C()
148 if (!(pFilename
&& phFile
))
149 return store_E_InvalidParameter
;
151 Reference
<ILockBytes
> xLockBytes
;
153 storeError eErrCode
= FileLockBytes_createInstance (xLockBytes
, pFilename
, eAccessMode
);
154 if (eErrCode
!= store_E_None
)
156 OSL_ASSERT(xLockBytes
.is());
158 Reference
<OStorePageManager
> xManager (new OStorePageManager());
160 return store_E_OutOfMemory
;
162 eErrCode
= xManager
->initialize (
163 &*xLockBytes
, eAccessMode
, nPageSize
);
164 if (eErrCode
!= store_E_None
)
169 *phFile
= (storeFileHandle
)&(*xManager
);
176 storeError SAL_CALL
store_closeFile (
177 storeFileHandle Handle
178 ) SAL_THROW_EXTERN_C()
180 OStorePageManager
*pManager
=
181 OStoreHandle
<OStorePageManager
>::query (Handle
);
183 return store_E_InvalidHandle
;
185 storeError eErrCode
= pManager
->close();
193 storeError SAL_CALL
store_flushFile (
194 storeFileHandle Handle
195 ) SAL_THROW_EXTERN_C()
197 OStoreHandle
<OStorePageManager
> xManager (
198 OStoreHandle
<OStorePageManager
>::query (Handle
));
200 return store_E_InvalidHandle
;
202 return xManager
->flush();
206 * store_getFileRefererCount.
208 storeError SAL_CALL
store_getFileRefererCount (
209 storeFileHandle Handle
,
210 sal_uInt32
*pnRefCount
211 ) SAL_THROW_EXTERN_C()
213 OStoreHandle
<OStorePageManager
> xManager (
214 OStoreHandle
<OStorePageManager
>::query (Handle
));
216 return store_E_InvalidHandle
;
219 return store_E_InvalidParameter
;
221 *pnRefCount
= xManager
->getRefererCount();
228 storeError SAL_CALL
store_getFileSize (
229 storeFileHandle Handle
,
231 ) SAL_THROW_EXTERN_C()
233 OStoreHandle
<OStorePageManager
> xManager (
234 OStoreHandle
<OStorePageManager
>::query (Handle
));
236 return store_E_InvalidHandle
;
239 return store_E_InvalidParameter
;
241 return xManager
->size (*pnSize
);
247 storeError SAL_CALL
store_rebuildFile (
248 rtl_uString
*pSrcFilename
,
249 rtl_uString
*pDstFilename
250 ) SAL_THROW_EXTERN_C()
252 storeError eErrCode
= store_E_None
;
254 if (!(pSrcFilename
&& pDstFilename
))
255 return store_E_InvalidParameter
;
257 Reference
<OStorePageManager
> xManager (new OStorePageManager());
259 return store_E_OutOfMemory
;
261 Reference
<ILockBytes
> xSrcLB
;
262 eErrCode
= FileLockBytes_createInstance (xSrcLB
, pSrcFilename
, store_AccessReadOnly
);
263 if (eErrCode
!= store_E_None
)
265 OSL_ASSERT(xSrcLB
.is());
267 Reference
<ILockBytes
> xDstLB
;
268 eErrCode
= FileLockBytes_createInstance (xDstLB
, pDstFilename
, store_AccessCreate
);
269 if (eErrCode
!= store_E_None
)
271 OSL_ASSERT(xDstLB
.is());
273 return xManager
->rebuild (&*xSrcLB
, &*xDstLB
);
276 /*========================================================================
278 * storeDirectoryHandle implementation.
280 *======================================================================*/
282 * store_openDirectory.
284 storeError SAL_CALL
store_openDirectory (
285 storeFileHandle hFile
,
288 storeAccessMode eAccessMode
,
289 storeDirectoryHandle
*phDirectory
290 ) SAL_THROW_EXTERN_C()
292 storeError eErrCode
= store_E_None
;
296 OStoreHandle
<OStorePageManager
> xManager (
297 OStoreHandle
<OStorePageManager
>::query (hFile
));
299 return store_E_InvalidHandle
;
301 if (!(pPath
&& pName
&& phDirectory
))
302 return store_E_InvalidParameter
;
304 Reference
<OStoreDirectory_Impl
> xDirectory (new OStoreDirectory_Impl());
305 if (!xDirectory
.is())
306 return store_E_OutOfMemory
;
308 OString
aPath (pPath
->buffer
, pPath
->length
, RTL_TEXTENCODING_UTF8
);
309 OString
aName (pName
->buffer
, pName
->length
, RTL_TEXTENCODING_UTF8
);
311 eErrCode
= xDirectory
->create (&*xManager
, aPath
.pData
, aName
.pData
, eAccessMode
);
312 if (eErrCode
!= store_E_None
)
315 xDirectory
->acquire();
317 *phDirectory
= (storeDirectoryHandle
)&(*xDirectory
);
322 * store_closeDirectory.
324 storeError SAL_CALL
store_closeDirectory (
325 storeDirectoryHandle Handle
326 ) SAL_THROW_EXTERN_C()
328 OStoreDirectory_Impl
*pDirectory
=
329 OStoreHandle
<OStoreDirectory_Impl
>::query (Handle
);
331 return store_E_InvalidHandle
;
333 pDirectory
->release();
340 storeError SAL_CALL
store_findFirst (
341 storeDirectoryHandle Handle
,
342 storeFindData
*pFindData
343 ) SAL_THROW_EXTERN_C()
345 OStoreHandle
<OStoreDirectory_Impl
> xDirectory (
346 OStoreHandle
<OStoreDirectory_Impl
>::query (Handle
));
347 if (!xDirectory
.is())
348 return store_E_InvalidHandle
;
351 return store_E_InvalidParameter
;
353 // Initialize FindData.
354 memset (pFindData
, 0, sizeof (storeFindData
));
357 pFindData
->m_nReserved
= (sal_uInt32
)(~0);
358 return xDirectory
->iterate (*pFindData
);
364 storeError SAL_CALL
store_findNext (
365 storeDirectoryHandle Handle
,
366 storeFindData
*pFindData
367 ) SAL_THROW_EXTERN_C()
369 OStoreHandle
<OStoreDirectory_Impl
> xDirectory (
370 OStoreHandle
<OStoreDirectory_Impl
>::query (Handle
));
371 if (!xDirectory
.is())
372 return store_E_InvalidHandle
;
375 return store_E_InvalidParameter
;
378 if (!pFindData
->m_nReserved
)
379 return store_E_NoMoreFiles
;
382 pFindData
->m_nReserved
-= 1;
383 return xDirectory
->iterate (*pFindData
);
386 /*========================================================================
388 * storeStreamHandle implementation.
390 *======================================================================*/
394 storeError SAL_CALL
store_openStream (
395 storeFileHandle hFile
,
398 storeAccessMode eAccessMode
,
399 storeStreamHandle
*phStream
400 ) SAL_THROW_EXTERN_C()
402 storeError eErrCode
= store_E_None
;
406 OStoreHandle
<OStorePageManager
> xManager (
407 OStoreHandle
<OStorePageManager
>::query (hFile
));
409 return store_E_InvalidHandle
;
411 if (!(pPath
&& pName
&& phStream
))
412 return store_E_InvalidParameter
;
414 Reference
<OStoreLockBytes
> xLockBytes (new OStoreLockBytes());
415 if (!xLockBytes
.is())
416 return store_E_OutOfMemory
;
418 OString
aPath (pPath
->buffer
, pPath
->length
, RTL_TEXTENCODING_UTF8
);
419 OString
aName (pName
->buffer
, pName
->length
, RTL_TEXTENCODING_UTF8
);
421 eErrCode
= xLockBytes
->create (&*xManager
, aPath
.pData
, aName
.pData
, eAccessMode
);
422 if (eErrCode
!= store_E_None
)
425 xLockBytes
->acquire();
427 *phStream
= (storeStreamHandle
)&(*xLockBytes
);
434 storeError SAL_CALL
store_closeStream (
435 storeStreamHandle Handle
436 ) SAL_THROW_EXTERN_C()
438 OStoreLockBytes
*pLockBytes
=
439 OStoreHandle
<OStoreLockBytes
>::query (Handle
);
441 return store_E_InvalidHandle
;
443 pLockBytes
->release();
450 storeError SAL_CALL
store_readStream (
451 storeStreamHandle Handle
,
456 ) SAL_THROW_EXTERN_C()
458 OStoreHandle
<OStoreLockBytes
> xLockBytes (
459 OStoreHandle
<OStoreLockBytes
>::query (Handle
));
460 if (!xLockBytes
.is())
461 return store_E_InvalidHandle
;
463 if (!(pBuffer
&& pnDone
))
464 return store_E_InvalidParameter
;
466 return xLockBytes
->readAt (nOffset
, pBuffer
, nBytes
, *pnDone
);
472 storeError SAL_CALL
store_writeStream (
473 storeStreamHandle Handle
,
478 ) SAL_THROW_EXTERN_C()
480 OStoreHandle
<OStoreLockBytes
> xLockBytes (
481 OStoreHandle
<OStoreLockBytes
>::query (Handle
));
482 if (!xLockBytes
.is())
483 return store_E_InvalidHandle
;
485 if (!(pBuffer
&& pnDone
))
486 return store_E_InvalidParameter
;
488 return xLockBytes
->writeAt (nOffset
, pBuffer
, nBytes
, *pnDone
);
494 storeError SAL_CALL
store_flushStream (
495 storeStreamHandle Handle
496 ) SAL_THROW_EXTERN_C()
498 OStoreHandle
<OStoreLockBytes
> xLockBytes (
499 OStoreHandle
<OStoreLockBytes
>::query (Handle
));
500 if (!xLockBytes
.is())
501 return store_E_InvalidHandle
;
503 return xLockBytes
->flush();
507 * store_getStreamSize.
509 storeError SAL_CALL
store_getStreamSize (
510 storeStreamHandle Handle
,
512 ) SAL_THROW_EXTERN_C()
514 OStoreHandle
<OStoreLockBytes
> xLockBytes (
515 OStoreHandle
<OStoreLockBytes
>::query (Handle
));
516 if (!xLockBytes
.is())
517 return store_E_InvalidHandle
;
520 return store_E_InvalidParameter
;
522 return xLockBytes
->stat (*pnSize
);
526 * store_setStreamSize.
528 storeError SAL_CALL
store_setStreamSize (
529 storeStreamHandle Handle
,
531 ) SAL_THROW_EXTERN_C()
533 OStoreHandle
<OStoreLockBytes
> xLockBytes (
534 OStoreHandle
<OStoreLockBytes
>::query (Handle
));
535 if (!xLockBytes
.is())
536 return store_E_InvalidHandle
;
538 return xLockBytes
->setSize (nSize
);
541 /*========================================================================
543 * Common storeDirectoryHandle and storeStreamHandle operations.
545 *======================================================================*/
549 storeError SAL_CALL
store_attrib (
550 storeFileHandle Handle
,
556 ) SAL_THROW_EXTERN_C()
558 storeError eErrCode
= store_E_None
;
562 OStoreHandle
<OStorePageManager
> xManager (
563 OStoreHandle
<OStorePageManager
>::query (Handle
));
565 return store_E_InvalidHandle
;
567 if (!(pPath
&& pName
))
568 return store_E_InvalidParameter
;
571 OString
aPath (pPath
->buffer
, pPath
->length
, RTL_TEXTENCODING_UTF8
);
572 OString
aName (pName
->buffer
, pName
->length
, RTL_TEXTENCODING_UTF8
);
575 eErrCode
= OStorePageManager::namei (aPath
.pData
, aName
.pData
, aKey
);
576 if (eErrCode
!= store_E_None
)
579 // Obtain or modify page attributes.
580 sal_uInt32 nAttrib
= 0;
581 eErrCode
= xManager
->attrib (aKey
, nMask1
, nMask2
, nAttrib
);
590 storeError SAL_CALL
store_link (
591 storeFileHandle Handle
,
592 rtl_uString
*pSrcPath
, rtl_uString
*pSrcName
,
593 rtl_uString
*pDstPath
, rtl_uString
*pDstName
594 ) SAL_THROW_EXTERN_C()
596 storeError eErrCode
= store_E_None
;
598 OStoreHandle
<OStorePageManager
> xManager (
599 OStoreHandle
<OStorePageManager
>::query (Handle
));
601 return store_E_InvalidHandle
;
603 if (!(pSrcPath
&& pSrcName
))
604 return store_E_InvalidParameter
;
606 if (!(pDstPath
&& pDstName
))
607 return store_E_InvalidParameter
;
609 // Setup 'Source' page key.
611 pSrcPath
->buffer
, pSrcPath
->length
, RTL_TEXTENCODING_UTF8
);
613 pSrcName
->buffer
, pSrcName
->length
, RTL_TEXTENCODING_UTF8
);
614 OStorePageKey aSrcKey
;
616 eErrCode
= OStorePageManager::namei (
617 aSrcPath
.pData
, aSrcName
.pData
, aSrcKey
);
618 if (eErrCode
!= store_E_None
)
621 // Setup 'Destination' page key.
623 pDstPath
->buffer
, pDstPath
->length
, RTL_TEXTENCODING_UTF8
);
625 pDstName
->buffer
, pDstName
->length
, RTL_TEXTENCODING_UTF8
);
626 OStorePageKey aDstKey
;
628 eErrCode
= OStorePageManager::namei (
629 aDstPath
.pData
, aDstName
.pData
, aDstKey
);
630 if (eErrCode
!= store_E_None
)
633 // Link 'Source' hard to 'Destination'.
634 return xManager
->link (aSrcKey
, aDstKey
);
640 storeError SAL_CALL
store_symlink (
641 storeFileHandle Handle
,
642 rtl_uString
*pSrcPath
, rtl_uString
*pSrcName
,
643 rtl_uString
*pDstPath
, rtl_uString
*pDstName
644 ) SAL_THROW_EXTERN_C()
646 storeError eErrCode
= store_E_None
;
648 OStoreHandle
<OStorePageManager
> xManager (
649 OStoreHandle
<OStorePageManager
>::query (Handle
));
651 return store_E_InvalidHandle
;
653 if (!(pSrcPath
&& pSrcName
))
654 return store_E_InvalidParameter
;
656 if (!(pDstPath
&& pDstName
))
657 return store_E_InvalidParameter
;
659 // Setup 'Destination' page key.
661 pDstPath
->buffer
, pDstPath
->length
, RTL_TEXTENCODING_UTF8
);
663 pDstName
->buffer
, pDstName
->length
, RTL_TEXTENCODING_UTF8
);
664 OStorePageKey aDstKey
;
666 eErrCode
= OStorePageManager::namei (
667 aDstPath
.pData
, aDstName
.pData
, aDstKey
);
668 if (eErrCode
!= store_E_None
)
671 // Insert 'Source' as symlink to 'Destination'.
673 pSrcPath
->buffer
, pSrcPath
->length
, RTL_TEXTENCODING_UTF8
);
675 pSrcName
->buffer
, pSrcName
->length
, RTL_TEXTENCODING_UTF8
);
677 return xManager
->symlink (aSrcPath
.pData
, aSrcName
.pData
, aDstKey
);
683 storeError SAL_CALL
store_rename (
684 storeFileHandle Handle
,
685 rtl_uString
*pSrcPath
, rtl_uString
*pSrcName
,
686 rtl_uString
*pDstPath
, rtl_uString
*pDstName
687 ) SAL_THROW_EXTERN_C()
689 storeError eErrCode
= store_E_None
;
691 OStoreHandle
<OStorePageManager
> xManager (
692 OStoreHandle
<OStorePageManager
>::query (Handle
));
694 return store_E_InvalidHandle
;
696 if (!(pSrcPath
&& pSrcName
))
697 return store_E_InvalidParameter
;
699 if (!(pDstPath
&& pDstName
))
700 return store_E_InvalidParameter
;
702 // Setup 'Source' page key.
704 pSrcPath
->buffer
, pSrcPath
->length
, RTL_TEXTENCODING_UTF8
);
706 pSrcName
->buffer
, pSrcName
->length
, RTL_TEXTENCODING_UTF8
);
707 OStorePageKey aSrcKey
;
709 eErrCode
= OStorePageManager::namei (
710 aSrcPath
.pData
, aSrcName
.pData
, aSrcKey
);
711 if (eErrCode
!= store_E_None
)
714 // Rename 'Source' into 'Destination'.
716 pDstPath
->buffer
, pDstPath
->length
, RTL_TEXTENCODING_UTF8
);
718 pDstName
->buffer
, pDstName
->length
, RTL_TEXTENCODING_UTF8
);
720 return xManager
->rename (aSrcKey
, aDstPath
.pData
, aDstName
.pData
);
726 storeError SAL_CALL
store_remove (
727 storeFileHandle Handle
,
730 ) SAL_THROW_EXTERN_C()
732 storeError eErrCode
= store_E_None
;
734 OStoreHandle
<OStorePageManager
> xManager (
735 OStoreHandle
<OStorePageManager
>::query (Handle
));
737 return store_E_InvalidHandle
;
739 if (!(pPath
&& pName
))
740 return store_E_InvalidParameter
;
743 OString
aPath (pPath
->buffer
, pPath
->length
, RTL_TEXTENCODING_UTF8
);
744 OString
aName (pName
->buffer
, pName
->length
, RTL_TEXTENCODING_UTF8
);
747 eErrCode
= OStorePageManager::namei (aPath
.pData
, aName
.pData
, aKey
);
748 if (eErrCode
!= store_E_None
)
752 return xManager
->remove (aKey
);
755 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */