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"
41 /** Template helper class as type safe Reference to store_handle_type.
43 template<class store_handle_type
>
44 class OStoreHandle
: public rtl::Reference
<store_handle_type
>
47 explicit OStoreHandle (store_handle_type
* pHandle
)
48 : rtl::Reference
<store_handle_type
> (pHandle
)
51 static store_handle_type
* SAL_CALL
query (void * pHandle
)
54 static_cast<OStoreObject
*>(pHandle
),
55 static_cast<store_handle_type
*>(nullptr));
63 using namespace store
;
65 storeError
store_acquireHandle (
69 OStoreObject
*pHandle
= static_cast<OStoreObject
*>(Handle
);
71 return store_E_InvalidHandle
;
77 storeError
store_releaseHandle (
81 OStoreObject
*pHandle
= static_cast<OStoreObject
*>(Handle
);
83 return store_E_InvalidHandle
;
89 storeError
store_createMemoryFile (
91 storeFileHandle
*phFile
95 return store_E_InvalidParameter
;
98 Reference
<ILockBytes
> xLockBytes
;
100 storeError eErrCode
= MemoryLockBytes_createInstance(xLockBytes
);
101 if (eErrCode
!= store_E_None
)
103 OSL_ASSERT(xLockBytes
.is());
105 Reference
<OStorePageManager
> xManager (new OStorePageManager());
107 eErrCode
= xManager
->initialize (
108 &*xLockBytes
, storeAccessMode::Create
, nPageSize
);
109 if (eErrCode
!= store_E_None
)
114 *phFile
= xManager
.get();
118 storeError
store_openFile (
119 rtl_uString
*pFilename
,
120 storeAccessMode eAccessMode
,
121 sal_uInt16 nPageSize
,
122 storeFileHandle
*phFile
128 if (!(pFilename
&& phFile
))
129 return store_E_InvalidParameter
;
131 Reference
<ILockBytes
> xLockBytes
;
133 storeError eErrCode
= FileLockBytes_createInstance (xLockBytes
, pFilename
, eAccessMode
);
134 if (eErrCode
!= store_E_None
)
136 OSL_ASSERT(xLockBytes
.is());
138 Reference
<OStorePageManager
> xManager (new OStorePageManager());
139 eErrCode
= xManager
->initialize (
140 &*xLockBytes
, eAccessMode
, nPageSize
);
141 if (eErrCode
!= store_E_None
)
146 *phFile
= xManager
.get();
153 storeError
store_closeFile (
154 storeFileHandle Handle
157 OStorePageManager
*pManager
=
158 OStoreHandle
<OStorePageManager
>::query (Handle
);
160 return store_E_InvalidHandle
;
162 storeError eErrCode
= pManager
->close();
167 storeError
store_flushFile (
168 storeFileHandle Handle
171 OStoreHandle
<OStorePageManager
> xManager (
172 OStoreHandle
<OStorePageManager
>::query (Handle
));
174 return store_E_InvalidHandle
;
176 return xManager
->flush();
179 storeError
store_openDirectory (
180 storeFileHandle hFile
,
181 rtl_uString
const *pPath
,
182 rtl_uString
const *pName
,
183 storeAccessMode eAccessMode
,
184 storeDirectoryHandle
*phDirectory
187 storeError eErrCode
= store_E_None
;
189 *phDirectory
= nullptr;
191 OStoreHandle
<OStorePageManager
> xManager (
192 OStoreHandle
<OStorePageManager
>::query (hFile
));
194 return store_E_InvalidHandle
;
196 if (!(pPath
&& pName
&& phDirectory
))
197 return store_E_InvalidParameter
;
199 Reference
<OStoreDirectory_Impl
> xDirectory (new OStoreDirectory_Impl());
201 OString
aPath (pPath
->buffer
, pPath
->length
, RTL_TEXTENCODING_UTF8
);
202 OString
aName (pName
->buffer
, pName
->length
, RTL_TEXTENCODING_UTF8
);
204 eErrCode
= xDirectory
->create (&*xManager
, aPath
.pData
, aName
.pData
, eAccessMode
);
205 if (eErrCode
!= store_E_None
)
208 xDirectory
->acquire();
210 *phDirectory
= xDirectory
.get();
214 storeError
store_findFirst (
215 storeDirectoryHandle Handle
,
216 storeFindData
*pFindData
219 OStoreHandle
<OStoreDirectory_Impl
> xDirectory (
220 OStoreHandle
<OStoreDirectory_Impl
>::query (Handle
));
221 if (!xDirectory
.is())
222 return store_E_InvalidHandle
;
225 return store_E_InvalidParameter
;
227 // Initialize FindData.
228 memset (pFindData
, 0, sizeof (storeFindData
));
231 pFindData
->m_nReserved
= sal_uInt32(~0);
232 return xDirectory
->iterate (*pFindData
);
235 storeError
store_findNext (
236 storeDirectoryHandle Handle
,
237 storeFindData
*pFindData
240 OStoreHandle
<OStoreDirectory_Impl
> xDirectory (
241 OStoreHandle
<OStoreDirectory_Impl
>::query (Handle
));
242 if (!xDirectory
.is())
243 return store_E_InvalidHandle
;
246 return store_E_InvalidParameter
;
249 if (!pFindData
->m_nReserved
)
250 return store_E_NoMoreFiles
;
253 pFindData
->m_nReserved
-= 1;
254 return xDirectory
->iterate (*pFindData
);
257 storeError
store_openStream (
258 storeFileHandle hFile
,
259 rtl_uString
const *pPath
,
260 rtl_uString
const *pName
,
261 storeAccessMode eAccessMode
,
262 storeStreamHandle
*phStream
265 storeError eErrCode
= store_E_None
;
269 OStoreHandle
<OStorePageManager
> xManager (
270 OStoreHandle
<OStorePageManager
>::query (hFile
));
272 return store_E_InvalidHandle
;
274 if (!(pPath
&& pName
&& phStream
))
275 return store_E_InvalidParameter
;
277 Reference
<OStoreLockBytes
> xLockBytes (new OStoreLockBytes());
279 OString
aPath (pPath
->buffer
, pPath
->length
, RTL_TEXTENCODING_UTF8
);
280 OString
aName (pName
->buffer
, pName
->length
, RTL_TEXTENCODING_UTF8
);
282 eErrCode
= xLockBytes
->create (&*xManager
, aPath
.pData
, aName
.pData
, eAccessMode
);
283 if (eErrCode
!= store_E_None
)
286 xLockBytes
->acquire();
288 *phStream
= xLockBytes
.get();
295 storeError
store_readStream (
296 storeStreamHandle Handle
,
303 OStoreHandle
<OStoreLockBytes
> xLockBytes (
304 OStoreHandle
<OStoreLockBytes
>::query (Handle
));
305 if (!xLockBytes
.is())
306 return store_E_InvalidHandle
;
308 if (!(pBuffer
&& pnDone
))
309 return store_E_InvalidParameter
;
311 return xLockBytes
->readAt (nOffset
, pBuffer
, nBytes
, *pnDone
);
314 storeError
store_writeStream (
315 storeStreamHandle Handle
,
322 OStoreHandle
<OStoreLockBytes
> xLockBytes (
323 OStoreHandle
<OStoreLockBytes
>::query (Handle
));
324 if (!xLockBytes
.is())
325 return store_E_InvalidHandle
;
327 if (!(pBuffer
&& pnDone
))
328 return store_E_InvalidParameter
;
330 return xLockBytes
->writeAt (nOffset
, pBuffer
, nBytes
, *pnDone
);
333 storeError
store_remove (
334 storeFileHandle Handle
,
335 rtl_uString
const *pPath
,
336 rtl_uString
const *pName
339 storeError eErrCode
= store_E_None
;
341 OStoreHandle
<OStorePageManager
> xManager (
342 OStoreHandle
<OStorePageManager
>::query (Handle
));
344 return store_E_InvalidHandle
;
346 if (!(pPath
&& pName
))
347 return store_E_InvalidParameter
;
350 OString
aPath (pPath
->buffer
, pPath
->length
, RTL_TEXTENCODING_UTF8
);
351 OString
aName (pName
->buffer
, pName
->length
, RTL_TEXTENCODING_UTF8
);
354 eErrCode
= OStorePageManager::namei (aPath
.pData
, aName
.pData
, aKey
);
355 if (eErrCode
!= store_E_None
)
359 return xManager
->remove (aKey
);
362 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */