xref: /aoo42x/main/store/source/stordir.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_store.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "stordir.hxx"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #ifndef _SAL_TYPES_H_
34*cdf0e10cSrcweir #include <sal/types.h>
35*cdf0e10cSrcweir #endif
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #ifndef _RTL_TEXTCVT_H_
38*cdf0e10cSrcweir #include <rtl/textcvt.h>
39*cdf0e10cSrcweir #endif
40*cdf0e10cSrcweir #ifndef _RTL_REF_HXX_
41*cdf0e10cSrcweir #include <rtl/ref.hxx>
42*cdf0e10cSrcweir #endif
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir #ifndef _OSL_MUTEX_HXX_
45*cdf0e10cSrcweir #include <osl/mutex.hxx>
46*cdf0e10cSrcweir #endif
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir #ifndef _STORE_TYPES_H_
49*cdf0e10cSrcweir #include "store/types.h"
50*cdf0e10cSrcweir #endif
51*cdf0e10cSrcweir #ifndef _STORE_OBJECT_HXX_
52*cdf0e10cSrcweir #include "object.hxx"
53*cdf0e10cSrcweir #endif
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir #ifndef _STORE_STORBASE_HXX_
56*cdf0e10cSrcweir #include "storbase.hxx"
57*cdf0e10cSrcweir #endif
58*cdf0e10cSrcweir #ifndef _STORE_STORDATA_HXX_
59*cdf0e10cSrcweir #include "stordata.hxx"
60*cdf0e10cSrcweir #endif
61*cdf0e10cSrcweir #ifndef _STORE_STORPAGE_HXX_
62*cdf0e10cSrcweir #include "storpage.hxx"
63*cdf0e10cSrcweir #endif
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir using namespace store;
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir /*========================================================================
68*cdf0e10cSrcweir  *
69*cdf0e10cSrcweir  * OStore... internals.
70*cdf0e10cSrcweir  *
71*cdf0e10cSrcweir  *======================================================================*/
72*cdf0e10cSrcweir /*
73*cdf0e10cSrcweir  * __store_convertTextToUnicode.
74*cdf0e10cSrcweir  */
75*cdf0e10cSrcweir inline sal_Size __store_convertTextToUnicode (
76*cdf0e10cSrcweir 	rtl_TextToUnicodeConverter  hConverter,
77*cdf0e10cSrcweir 	const sal_Char *pSrcBuffer, sal_Size nSrcLength,
78*cdf0e10cSrcweir 	sal_Unicode    *pDstBuffer, sal_Size nDstLength)
79*cdf0e10cSrcweir {
80*cdf0e10cSrcweir 	sal_uInt32 nCvtInfo = 0;
81*cdf0e10cSrcweir 	sal_Size nCvtBytes = 0;
82*cdf0e10cSrcweir 	return rtl_convertTextToUnicode (
83*cdf0e10cSrcweir 		hConverter, 0,
84*cdf0e10cSrcweir 		pSrcBuffer, nSrcLength,
85*cdf0e10cSrcweir 		pDstBuffer, nDstLength,
86*cdf0e10cSrcweir 		OSTRING_TO_OUSTRING_CVTFLAGS,
87*cdf0e10cSrcweir 		&nCvtInfo, &nCvtBytes);
88*cdf0e10cSrcweir }
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir /*========================================================================
91*cdf0e10cSrcweir  *
92*cdf0e10cSrcweir  * OStoreDirectory_Impl implementation.
93*cdf0e10cSrcweir  *
94*cdf0e10cSrcweir  *======================================================================*/
95*cdf0e10cSrcweir const sal_uInt32 OStoreDirectory_Impl::m_nTypeId = sal_uInt32(0x89191107);
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir /*
98*cdf0e10cSrcweir  * OStoreDirectory_Impl.
99*cdf0e10cSrcweir  */
100*cdf0e10cSrcweir OStoreDirectory_Impl::OStoreDirectory_Impl (void)
101*cdf0e10cSrcweir 	: m_xManager (),
102*cdf0e10cSrcweir 	  m_aDescr   (0, 0, 0),
103*cdf0e10cSrcweir 	  m_nPath    (0),
104*cdf0e10cSrcweir 	  m_hTextCvt (NULL)
105*cdf0e10cSrcweir {}
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir /*
108*cdf0e10cSrcweir  * ~OStoreDirectory_Impl.
109*cdf0e10cSrcweir  */
110*cdf0e10cSrcweir OStoreDirectory_Impl::~OStoreDirectory_Impl (void)
111*cdf0e10cSrcweir {
112*cdf0e10cSrcweir 	if (m_xManager.is())
113*cdf0e10cSrcweir 	{
114*cdf0e10cSrcweir 		if (m_aDescr.m_nAddr != STORE_PAGE_NULL)
115*cdf0e10cSrcweir 			m_xManager->releasePage (m_aDescr, store_AccessReadOnly);
116*cdf0e10cSrcweir 	}
117*cdf0e10cSrcweir 	rtl_destroyTextToUnicodeConverter (m_hTextCvt);
118*cdf0e10cSrcweir }
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir /*
121*cdf0e10cSrcweir  * isKindOf.
122*cdf0e10cSrcweir  */
123*cdf0e10cSrcweir sal_Bool SAL_CALL OStoreDirectory_Impl::isKindOf (sal_uInt32 nTypeId)
124*cdf0e10cSrcweir {
125*cdf0e10cSrcweir 	return (nTypeId == m_nTypeId);
126*cdf0e10cSrcweir }
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir /*
129*cdf0e10cSrcweir  * create.
130*cdf0e10cSrcweir  */
131*cdf0e10cSrcweir storeError OStoreDirectory_Impl::create (
132*cdf0e10cSrcweir 	OStorePageManager *pManager,
133*cdf0e10cSrcweir 	rtl_String        *pPath,
134*cdf0e10cSrcweir 	rtl_String        *pName,
135*cdf0e10cSrcweir 	storeAccessMode    eMode)
136*cdf0e10cSrcweir {
137*cdf0e10cSrcweir 	rtl::Reference<OStorePageManager> xManager (pManager);
138*cdf0e10cSrcweir 	if (!xManager.is())
139*cdf0e10cSrcweir 		return store_E_InvalidAccess;
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir 	if (!(pPath && pName))
142*cdf0e10cSrcweir 		return store_E_InvalidParameter;
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir 	OStoreDirectoryPageObject aPage;
145*cdf0e10cSrcweir 	storeError eErrCode = xManager->iget (
146*cdf0e10cSrcweir 		aPage, STORE_ATTRIB_ISDIR,
147*cdf0e10cSrcweir 		pPath, pName, eMode);
148*cdf0e10cSrcweir 	if (eErrCode != store_E_None)
149*cdf0e10cSrcweir 		return eErrCode;
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir 	if (!(aPage.attrib() & STORE_ATTRIB_ISDIR))
152*cdf0e10cSrcweir 		return store_E_NotDirectory;
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir     inode_holder_type xNode (aPage.get());
155*cdf0e10cSrcweir 	eErrCode = xManager->acquirePage (xNode->m_aDescr, store_AccessReadOnly);
156*cdf0e10cSrcweir 	if (eErrCode != store_E_None)
157*cdf0e10cSrcweir 		return eErrCode;
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir 	// Evaluate iteration path.
160*cdf0e10cSrcweir 	m_nPath = aPage.path();
161*cdf0e10cSrcweir 	m_nPath = rtl_crc32 (m_nPath, "/", 1);
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir 	// Save page manager, and descriptor.
164*cdf0e10cSrcweir 	m_xManager = xManager;
165*cdf0e10cSrcweir 	m_aDescr   = xNode->m_aDescr;
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir 	return store_E_None;
168*cdf0e10cSrcweir }
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir /*
171*cdf0e10cSrcweir  * iterate.
172*cdf0e10cSrcweir  */
173*cdf0e10cSrcweir storeError OStoreDirectory_Impl::iterate (storeFindData &rFindData)
174*cdf0e10cSrcweir {
175*cdf0e10cSrcweir 	if (!m_xManager.is())
176*cdf0e10cSrcweir 		return store_E_InvalidAccess;
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir 	storeError eErrCode = store_E_NoMoreFiles;
179*cdf0e10cSrcweir 	if (!rFindData.m_nReserved)
180*cdf0e10cSrcweir 		return eErrCode;
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir 	// Acquire exclusive access.
183*cdf0e10cSrcweir 	osl::MutexGuard aGuard (*m_xManager);
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir 	// Check TextConverter.
186*cdf0e10cSrcweir 	if (m_hTextCvt == NULL)
187*cdf0e10cSrcweir 		m_hTextCvt = rtl_createTextToUnicodeConverter(RTL_TEXTENCODING_UTF8);
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir 	// Setup iteration key.
190*cdf0e10cSrcweir 	OStorePageKey aKey (rFindData.m_nReserved, m_nPath);
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir 	// Iterate.
193*cdf0e10cSrcweir 	for (;;)
194*cdf0e10cSrcweir 	{
195*cdf0e10cSrcweir         OStorePageLink aLink;
196*cdf0e10cSrcweir 		eErrCode = m_xManager->iterate (aKey, aLink, rFindData.m_nAttrib);
197*cdf0e10cSrcweir 		if (!((eErrCode == store_E_None) && (aKey.m_nHigh == store::htonl(m_nPath))))
198*cdf0e10cSrcweir 			break;
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir 		if (!(rFindData.m_nAttrib & STORE_ATTRIB_ISLINK))
201*cdf0e10cSrcweir 		{
202*cdf0e10cSrcweir 			// Load page.
203*cdf0e10cSrcweir             OStoreDirectoryPageObject aPage;
204*cdf0e10cSrcweir 			eErrCode = m_xManager->loadObjectAt (aPage, aLink.location());
205*cdf0e10cSrcweir 			if (eErrCode == store_E_None)
206*cdf0e10cSrcweir 			{
207*cdf0e10cSrcweir                 inode_holder_type xNode (aPage.get());
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir 				// Setup FindData.
210*cdf0e10cSrcweir 				sal_Char *p = xNode->m_aNameBlock.m_pData;
211*cdf0e10cSrcweir 				sal_Size  n = rtl_str_getLength (p);
212*cdf0e10cSrcweir 				sal_Size  k = rFindData.m_nLength;
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir 				n = __store_convertTextToUnicode (
215*cdf0e10cSrcweir 					m_hTextCvt, p, n,
216*cdf0e10cSrcweir 					rFindData.m_pszName, STORE_MAXIMUM_NAMESIZE - 1);
217*cdf0e10cSrcweir 				if (k > n)
218*cdf0e10cSrcweir 				{
219*cdf0e10cSrcweir 					k = (k - n) * sizeof(sal_Unicode);
220*cdf0e10cSrcweir 					memset (&rFindData.m_pszName[n], 0, k);
221*cdf0e10cSrcweir 				}
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir 				rFindData.m_nLength  = n;
224*cdf0e10cSrcweir 				rFindData.m_nAttrib |= aPage.attrib();
225*cdf0e10cSrcweir 				rFindData.m_nSize    = aPage.dataLength();
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir 				// Leave.
228*cdf0e10cSrcweir 				rFindData.m_nReserved = store::ntohl(aKey.m_nLow);
229*cdf0e10cSrcweir 				return store_E_None;
230*cdf0e10cSrcweir 			}
231*cdf0e10cSrcweir 		}
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir 		if (aKey.m_nLow == 0)
234*cdf0e10cSrcweir 			break;
235*cdf0e10cSrcweir 		aKey.m_nLow = store::htonl(store::ntohl(aKey.m_nLow) - 1);
236*cdf0e10cSrcweir 	}
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir 	// Finished.
239*cdf0e10cSrcweir 	memset (&rFindData, 0, sizeof (storeFindData));
240*cdf0e10cSrcweir 	return store_E_NoMoreFiles;
241*cdf0e10cSrcweir }
242