xref: /aoo41x/main/store/source/stordir.cxx (revision 73d9b18a)
1*73d9b18aSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*73d9b18aSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*73d9b18aSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*73d9b18aSAndrew Rist  * distributed with this work for additional information
6*73d9b18aSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*73d9b18aSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*73d9b18aSAndrew Rist  * "License"); you may not use this file except in compliance
9*73d9b18aSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*73d9b18aSAndrew Rist  *
11*73d9b18aSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*73d9b18aSAndrew Rist  *
13*73d9b18aSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*73d9b18aSAndrew Rist  * software distributed under the License is distributed on an
15*73d9b18aSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*73d9b18aSAndrew Rist  * KIND, either express or implied.  See the License for the
17*73d9b18aSAndrew Rist  * specific language governing permissions and limitations
18*73d9b18aSAndrew Rist  * under the License.
19*73d9b18aSAndrew Rist  *
20*73d9b18aSAndrew Rist  *************************************************************/
21*73d9b18aSAndrew Rist 
22*73d9b18aSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_store.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "stordir.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #ifndef _SAL_TYPES_H_
30cdf0e10cSrcweir #include <sal/types.h>
31cdf0e10cSrcweir #endif
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #ifndef _RTL_TEXTCVT_H_
34cdf0e10cSrcweir #include <rtl/textcvt.h>
35cdf0e10cSrcweir #endif
36cdf0e10cSrcweir #ifndef _RTL_REF_HXX_
37cdf0e10cSrcweir #include <rtl/ref.hxx>
38cdf0e10cSrcweir #endif
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #ifndef _OSL_MUTEX_HXX_
41cdf0e10cSrcweir #include <osl/mutex.hxx>
42cdf0e10cSrcweir #endif
43cdf0e10cSrcweir 
44cdf0e10cSrcweir #ifndef _STORE_TYPES_H_
45cdf0e10cSrcweir #include "store/types.h"
46cdf0e10cSrcweir #endif
47cdf0e10cSrcweir #ifndef _STORE_OBJECT_HXX_
48cdf0e10cSrcweir #include "object.hxx"
49cdf0e10cSrcweir #endif
50cdf0e10cSrcweir 
51cdf0e10cSrcweir #ifndef _STORE_STORBASE_HXX_
52cdf0e10cSrcweir #include "storbase.hxx"
53cdf0e10cSrcweir #endif
54cdf0e10cSrcweir #ifndef _STORE_STORDATA_HXX_
55cdf0e10cSrcweir #include "stordata.hxx"
56cdf0e10cSrcweir #endif
57cdf0e10cSrcweir #ifndef _STORE_STORPAGE_HXX_
58cdf0e10cSrcweir #include "storpage.hxx"
59cdf0e10cSrcweir #endif
60cdf0e10cSrcweir 
61cdf0e10cSrcweir using namespace store;
62cdf0e10cSrcweir 
63cdf0e10cSrcweir /*========================================================================
64cdf0e10cSrcweir  *
65cdf0e10cSrcweir  * OStore... internals.
66cdf0e10cSrcweir  *
67cdf0e10cSrcweir  *======================================================================*/
68cdf0e10cSrcweir /*
69cdf0e10cSrcweir  * __store_convertTextToUnicode.
70cdf0e10cSrcweir  */
__store_convertTextToUnicode(rtl_TextToUnicodeConverter hConverter,const sal_Char * pSrcBuffer,sal_Size nSrcLength,sal_Unicode * pDstBuffer,sal_Size nDstLength)71cdf0e10cSrcweir inline sal_Size __store_convertTextToUnicode (
72cdf0e10cSrcweir 	rtl_TextToUnicodeConverter  hConverter,
73cdf0e10cSrcweir 	const sal_Char *pSrcBuffer, sal_Size nSrcLength,
74cdf0e10cSrcweir 	sal_Unicode    *pDstBuffer, sal_Size nDstLength)
75cdf0e10cSrcweir {
76cdf0e10cSrcweir 	sal_uInt32 nCvtInfo = 0;
77cdf0e10cSrcweir 	sal_Size nCvtBytes = 0;
78cdf0e10cSrcweir 	return rtl_convertTextToUnicode (
79cdf0e10cSrcweir 		hConverter, 0,
80cdf0e10cSrcweir 		pSrcBuffer, nSrcLength,
81cdf0e10cSrcweir 		pDstBuffer, nDstLength,
82cdf0e10cSrcweir 		OSTRING_TO_OUSTRING_CVTFLAGS,
83cdf0e10cSrcweir 		&nCvtInfo, &nCvtBytes);
84cdf0e10cSrcweir }
85cdf0e10cSrcweir 
86cdf0e10cSrcweir /*========================================================================
87cdf0e10cSrcweir  *
88cdf0e10cSrcweir  * OStoreDirectory_Impl implementation.
89cdf0e10cSrcweir  *
90cdf0e10cSrcweir  *======================================================================*/
91cdf0e10cSrcweir const sal_uInt32 OStoreDirectory_Impl::m_nTypeId = sal_uInt32(0x89191107);
92cdf0e10cSrcweir 
93cdf0e10cSrcweir /*
94cdf0e10cSrcweir  * OStoreDirectory_Impl.
95cdf0e10cSrcweir  */
OStoreDirectory_Impl(void)96cdf0e10cSrcweir OStoreDirectory_Impl::OStoreDirectory_Impl (void)
97cdf0e10cSrcweir 	: m_xManager (),
98cdf0e10cSrcweir 	  m_aDescr   (0, 0, 0),
99cdf0e10cSrcweir 	  m_nPath    (0),
100cdf0e10cSrcweir 	  m_hTextCvt (NULL)
101cdf0e10cSrcweir {}
102cdf0e10cSrcweir 
103cdf0e10cSrcweir /*
104cdf0e10cSrcweir  * ~OStoreDirectory_Impl.
105cdf0e10cSrcweir  */
~OStoreDirectory_Impl(void)106cdf0e10cSrcweir OStoreDirectory_Impl::~OStoreDirectory_Impl (void)
107cdf0e10cSrcweir {
108cdf0e10cSrcweir 	if (m_xManager.is())
109cdf0e10cSrcweir 	{
110cdf0e10cSrcweir 		if (m_aDescr.m_nAddr != STORE_PAGE_NULL)
111cdf0e10cSrcweir 			m_xManager->releasePage (m_aDescr, store_AccessReadOnly);
112cdf0e10cSrcweir 	}
113cdf0e10cSrcweir 	rtl_destroyTextToUnicodeConverter (m_hTextCvt);
114cdf0e10cSrcweir }
115cdf0e10cSrcweir 
116cdf0e10cSrcweir /*
117cdf0e10cSrcweir  * isKindOf.
118cdf0e10cSrcweir  */
isKindOf(sal_uInt32 nTypeId)119cdf0e10cSrcweir sal_Bool SAL_CALL OStoreDirectory_Impl::isKindOf (sal_uInt32 nTypeId)
120cdf0e10cSrcweir {
121cdf0e10cSrcweir 	return (nTypeId == m_nTypeId);
122cdf0e10cSrcweir }
123cdf0e10cSrcweir 
124cdf0e10cSrcweir /*
125cdf0e10cSrcweir  * create.
126cdf0e10cSrcweir  */
create(OStorePageManager * pManager,rtl_String * pPath,rtl_String * pName,storeAccessMode eMode)127cdf0e10cSrcweir storeError OStoreDirectory_Impl::create (
128cdf0e10cSrcweir 	OStorePageManager *pManager,
129cdf0e10cSrcweir 	rtl_String        *pPath,
130cdf0e10cSrcweir 	rtl_String        *pName,
131cdf0e10cSrcweir 	storeAccessMode    eMode)
132cdf0e10cSrcweir {
133cdf0e10cSrcweir 	rtl::Reference<OStorePageManager> xManager (pManager);
134cdf0e10cSrcweir 	if (!xManager.is())
135cdf0e10cSrcweir 		return store_E_InvalidAccess;
136cdf0e10cSrcweir 
137cdf0e10cSrcweir 	if (!(pPath && pName))
138cdf0e10cSrcweir 		return store_E_InvalidParameter;
139cdf0e10cSrcweir 
140cdf0e10cSrcweir 	OStoreDirectoryPageObject aPage;
141cdf0e10cSrcweir 	storeError eErrCode = xManager->iget (
142cdf0e10cSrcweir 		aPage, STORE_ATTRIB_ISDIR,
143cdf0e10cSrcweir 		pPath, pName, eMode);
144cdf0e10cSrcweir 	if (eErrCode != store_E_None)
145cdf0e10cSrcweir 		return eErrCode;
146cdf0e10cSrcweir 
147cdf0e10cSrcweir 	if (!(aPage.attrib() & STORE_ATTRIB_ISDIR))
148cdf0e10cSrcweir 		return store_E_NotDirectory;
149cdf0e10cSrcweir 
150cdf0e10cSrcweir     inode_holder_type xNode (aPage.get());
151cdf0e10cSrcweir 	eErrCode = xManager->acquirePage (xNode->m_aDescr, store_AccessReadOnly);
152cdf0e10cSrcweir 	if (eErrCode != store_E_None)
153cdf0e10cSrcweir 		return eErrCode;
154cdf0e10cSrcweir 
155cdf0e10cSrcweir 	// Evaluate iteration path.
156cdf0e10cSrcweir 	m_nPath = aPage.path();
157cdf0e10cSrcweir 	m_nPath = rtl_crc32 (m_nPath, "/", 1);
158cdf0e10cSrcweir 
159cdf0e10cSrcweir 	// Save page manager, and descriptor.
160cdf0e10cSrcweir 	m_xManager = xManager;
161cdf0e10cSrcweir 	m_aDescr   = xNode->m_aDescr;
162cdf0e10cSrcweir 
163cdf0e10cSrcweir 	return store_E_None;
164cdf0e10cSrcweir }
165cdf0e10cSrcweir 
166cdf0e10cSrcweir /*
167cdf0e10cSrcweir  * iterate.
168cdf0e10cSrcweir  */
iterate(storeFindData & rFindData)169cdf0e10cSrcweir storeError OStoreDirectory_Impl::iterate (storeFindData &rFindData)
170cdf0e10cSrcweir {
171cdf0e10cSrcweir 	if (!m_xManager.is())
172cdf0e10cSrcweir 		return store_E_InvalidAccess;
173cdf0e10cSrcweir 
174cdf0e10cSrcweir 	storeError eErrCode = store_E_NoMoreFiles;
175cdf0e10cSrcweir 	if (!rFindData.m_nReserved)
176cdf0e10cSrcweir 		return eErrCode;
177cdf0e10cSrcweir 
178cdf0e10cSrcweir 	// Acquire exclusive access.
179cdf0e10cSrcweir 	osl::MutexGuard aGuard (*m_xManager);
180cdf0e10cSrcweir 
181cdf0e10cSrcweir 	// Check TextConverter.
182cdf0e10cSrcweir 	if (m_hTextCvt == NULL)
183cdf0e10cSrcweir 		m_hTextCvt = rtl_createTextToUnicodeConverter(RTL_TEXTENCODING_UTF8);
184cdf0e10cSrcweir 
185cdf0e10cSrcweir 	// Setup iteration key.
186cdf0e10cSrcweir 	OStorePageKey aKey (rFindData.m_nReserved, m_nPath);
187cdf0e10cSrcweir 
188cdf0e10cSrcweir 	// Iterate.
189cdf0e10cSrcweir 	for (;;)
190cdf0e10cSrcweir 	{
191cdf0e10cSrcweir         OStorePageLink aLink;
192cdf0e10cSrcweir 		eErrCode = m_xManager->iterate (aKey, aLink, rFindData.m_nAttrib);
193cdf0e10cSrcweir 		if (!((eErrCode == store_E_None) && (aKey.m_nHigh == store::htonl(m_nPath))))
194cdf0e10cSrcweir 			break;
195cdf0e10cSrcweir 
196cdf0e10cSrcweir 		if (!(rFindData.m_nAttrib & STORE_ATTRIB_ISLINK))
197cdf0e10cSrcweir 		{
198cdf0e10cSrcweir 			// Load page.
199cdf0e10cSrcweir             OStoreDirectoryPageObject aPage;
200cdf0e10cSrcweir 			eErrCode = m_xManager->loadObjectAt (aPage, aLink.location());
201cdf0e10cSrcweir 			if (eErrCode == store_E_None)
202cdf0e10cSrcweir 			{
203cdf0e10cSrcweir                 inode_holder_type xNode (aPage.get());
204cdf0e10cSrcweir 
205cdf0e10cSrcweir 				// Setup FindData.
206cdf0e10cSrcweir 				sal_Char *p = xNode->m_aNameBlock.m_pData;
207cdf0e10cSrcweir 				sal_Size  n = rtl_str_getLength (p);
208cdf0e10cSrcweir 				sal_Size  k = rFindData.m_nLength;
209cdf0e10cSrcweir 
210cdf0e10cSrcweir 				n = __store_convertTextToUnicode (
211cdf0e10cSrcweir 					m_hTextCvt, p, n,
212cdf0e10cSrcweir 					rFindData.m_pszName, STORE_MAXIMUM_NAMESIZE - 1);
213cdf0e10cSrcweir 				if (k > n)
214cdf0e10cSrcweir 				{
215cdf0e10cSrcweir 					k = (k - n) * sizeof(sal_Unicode);
216cdf0e10cSrcweir 					memset (&rFindData.m_pszName[n], 0, k);
217cdf0e10cSrcweir 				}
218cdf0e10cSrcweir 
219cdf0e10cSrcweir 				rFindData.m_nLength  = n;
220cdf0e10cSrcweir 				rFindData.m_nAttrib |= aPage.attrib();
221cdf0e10cSrcweir 				rFindData.m_nSize    = aPage.dataLength();
222cdf0e10cSrcweir 
223cdf0e10cSrcweir 				// Leave.
224cdf0e10cSrcweir 				rFindData.m_nReserved = store::ntohl(aKey.m_nLow);
225cdf0e10cSrcweir 				return store_E_None;
226cdf0e10cSrcweir 			}
227cdf0e10cSrcweir 		}
228cdf0e10cSrcweir 
229cdf0e10cSrcweir 		if (aKey.m_nLow == 0)
230cdf0e10cSrcweir 			break;
231cdf0e10cSrcweir 		aKey.m_nLow = store::htonl(store::ntohl(aKey.m_nLow) - 1);
232cdf0e10cSrcweir 	}
233cdf0e10cSrcweir 
234cdf0e10cSrcweir 	// Finished.
235cdf0e10cSrcweir 	memset (&rFindData, 0, sizeof (storeFindData));
236cdf0e10cSrcweir 	return store_E_NoMoreFiles;
237cdf0e10cSrcweir }
238