Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "stordir.hxx"
21 :
22 : #include <sal/types.h>
23 :
24 : #include <rtl/textcvt.h>
25 : #include <rtl/ref.hxx>
26 :
27 : #include <osl/mutex.hxx>
28 :
29 : #include "store/types.h"
30 : #include "object.hxx"
31 :
32 : #include "storbase.hxx"
33 : #include "stordata.hxx"
34 : #include "storpage.hxx"
35 :
36 : using namespace store;
37 :
38 : /*========================================================================
39 : *
40 : * OStore... internals.
41 : *
42 : *======================================================================*/
43 : /*
44 : * __store_convertTextToUnicode.
45 : */
46 0 : inline sal_Size __store_convertTextToUnicode (
47 : rtl_TextToUnicodeConverter hConverter,
48 : const sal_Char *pSrcBuffer, sal_Size nSrcLength,
49 : sal_Unicode *pDstBuffer, sal_Size nDstLength)
50 : {
51 0 : sal_uInt32 nCvtInfo = 0;
52 0 : sal_Size nCvtBytes = 0;
53 : return rtl_convertTextToUnicode (
54 : hConverter, 0,
55 : pSrcBuffer, nSrcLength,
56 : pDstBuffer, nDstLength,
57 : OSTRING_TO_OUSTRING_CVTFLAGS,
58 0 : &nCvtInfo, &nCvtBytes);
59 : }
60 :
61 : /*========================================================================
62 : *
63 : * OStoreDirectory_Impl implementation.
64 : *
65 : *======================================================================*/
66 : const sal_uInt32 OStoreDirectory_Impl::m_nTypeId = sal_uInt32(0x89191107);
67 :
68 : /*
69 : * OStoreDirectory_Impl.
70 : */
71 0 : OStoreDirectory_Impl::OStoreDirectory_Impl (void)
72 : : m_xManager (),
73 : m_aDescr (0, 0, 0),
74 : m_nPath (0),
75 0 : m_hTextCvt (NULL)
76 0 : {}
77 :
78 : /*
79 : * ~OStoreDirectory_Impl.
80 : */
81 0 : OStoreDirectory_Impl::~OStoreDirectory_Impl (void)
82 : {
83 0 : if (m_xManager.is())
84 : {
85 0 : if (m_aDescr.m_nAddr != STORE_PAGE_NULL)
86 0 : m_xManager->releasePage (m_aDescr);
87 : }
88 0 : rtl_destroyTextToUnicodeConverter (m_hTextCvt);
89 0 : }
90 :
91 : /*
92 : * isKindOf.
93 : */
94 0 : bool OStoreDirectory_Impl::isKindOf (sal_uInt32 nTypeId)
95 : {
96 0 : return (nTypeId == m_nTypeId);
97 : }
98 :
99 : /*
100 : * create.
101 : */
102 0 : storeError OStoreDirectory_Impl::create (
103 : OStorePageManager *pManager,
104 : rtl_String *pPath,
105 : rtl_String *pName,
106 : storeAccessMode eMode)
107 : {
108 0 : rtl::Reference<OStorePageManager> xManager (pManager);
109 0 : if (!xManager.is())
110 0 : return store_E_InvalidAccess;
111 :
112 0 : if (!(pPath && pName))
113 0 : return store_E_InvalidParameter;
114 :
115 0 : OStoreDirectoryPageObject aPage;
116 : storeError eErrCode = xManager->iget (
117 : aPage, STORE_ATTRIB_ISDIR,
118 0 : pPath, pName, eMode);
119 0 : if (eErrCode != store_E_None)
120 0 : return eErrCode;
121 :
122 0 : if (!(aPage.attrib() & STORE_ATTRIB_ISDIR))
123 0 : return store_E_NotDirectory;
124 :
125 0 : inode_holder_type xNode (aPage.get());
126 0 : eErrCode = xManager->acquirePage (xNode->m_aDescr, store_AccessReadOnly);
127 0 : if (eErrCode != store_E_None)
128 0 : return eErrCode;
129 :
130 : // Evaluate iteration path.
131 0 : m_nPath = aPage.path();
132 0 : m_nPath = rtl_crc32 (m_nPath, "/", 1);
133 :
134 : // Save page manager, and descriptor.
135 0 : m_xManager = xManager;
136 0 : m_aDescr = xNode->m_aDescr;
137 :
138 0 : return store_E_None;
139 : }
140 :
141 : /*
142 : * iterate.
143 : */
144 0 : storeError OStoreDirectory_Impl::iterate (storeFindData &rFindData)
145 : {
146 0 : if (!m_xManager.is())
147 0 : return store_E_InvalidAccess;
148 :
149 0 : storeError eErrCode = store_E_NoMoreFiles;
150 0 : if (!rFindData.m_nReserved)
151 0 : return eErrCode;
152 :
153 : // Acquire exclusive access.
154 0 : osl::MutexGuard aGuard (*m_xManager);
155 :
156 : // Check TextConverter.
157 0 : if (m_hTextCvt == NULL)
158 0 : m_hTextCvt = rtl_createTextToUnicodeConverter(RTL_TEXTENCODING_UTF8);
159 :
160 : // Setup iteration key.
161 0 : OStorePageKey aKey (rFindData.m_nReserved, m_nPath);
162 :
163 : // Iterate.
164 : for (;;)
165 : {
166 0 : OStorePageLink aLink;
167 0 : eErrCode = m_xManager->iterate (aKey, aLink, rFindData.m_nAttrib);
168 0 : if (!((eErrCode == store_E_None) && (aKey.m_nHigh == store::htonl(m_nPath))))
169 0 : break;
170 :
171 0 : if (!(rFindData.m_nAttrib & STORE_ATTRIB_ISLINK))
172 : {
173 : // Load page.
174 0 : OStoreDirectoryPageObject aPage;
175 0 : eErrCode = m_xManager->loadObjectAt (aPage, aLink.location());
176 0 : if (eErrCode == store_E_None)
177 : {
178 0 : inode_holder_type xNode (aPage.get());
179 :
180 : // Setup FindData.
181 0 : sal_Char *p = xNode->m_aNameBlock.m_pData;
182 0 : sal_Size n = rtl_str_getLength (p);
183 0 : sal_Size k = rFindData.m_nLength;
184 :
185 : n = __store_convertTextToUnicode (
186 : m_hTextCvt, p, n,
187 0 : rFindData.m_pszName, STORE_MAXIMUM_NAMESIZE - 1);
188 0 : if (k > n)
189 : {
190 0 : k = (k - n) * sizeof(sal_Unicode);
191 0 : memset (&rFindData.m_pszName[n], 0, k);
192 : }
193 :
194 0 : rFindData.m_nLength = n;
195 0 : rFindData.m_nAttrib |= aPage.attrib();
196 0 : rFindData.m_nSize = aPage.dataLength();
197 :
198 : // Leave.
199 0 : rFindData.m_nReserved = store::ntohl(aKey.m_nLow);
200 0 : return store_E_None;
201 0 : }
202 : }
203 :
204 0 : if (aKey.m_nLow == 0)
205 0 : break;
206 0 : aKey.m_nLow = store::htonl(store::ntohl(aKey.m_nLow) - 1);
207 0 : }
208 :
209 : // Finished.
210 0 : memset (&rFindData, 0, sizeof (storeFindData));
211 0 : return store_E_NoMoreFiles;
212 : }
213 :
214 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|