Branch libreoffice-5-0-4
[LibreOffice.git] / store / source / stordir.cxx
blob77ea3f0afd47af79bb1f1d0b029c12aec880e4d8
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "stordir.hxx"
22 #include <sal/types.h>
24 #include <rtl/textcvt.h>
25 #include <rtl/ref.hxx>
27 #include <osl/mutex.hxx>
29 #include "store/types.h"
30 #include "object.hxx"
32 #include "storbase.hxx"
33 #include "stordata.hxx"
34 #include "storpage.hxx"
36 using namespace store;
38 /*========================================================================
40 * OStore... internals.
42 *======================================================================*/
44 * __store_convertTextToUnicode.
46 inline sal_Size __store_convertTextToUnicode (
47 rtl_TextToUnicodeConverter hConverter,
48 const sal_Char *pSrcBuffer, sal_Size nSrcLength,
49 sal_Unicode *pDstBuffer, sal_Size nDstLength)
51 sal_uInt32 nCvtInfo = 0;
52 sal_Size nCvtBytes = 0;
53 return rtl_convertTextToUnicode (
54 hConverter, 0,
55 pSrcBuffer, nSrcLength,
56 pDstBuffer, nDstLength,
57 OSTRING_TO_OUSTRING_CVTFLAGS,
58 &nCvtInfo, &nCvtBytes);
61 /*========================================================================
63 * OStoreDirectory_Impl implementation.
65 *======================================================================*/
66 const sal_uInt32 OStoreDirectory_Impl::m_nTypeId = sal_uInt32(0x89191107);
69 * OStoreDirectory_Impl.
71 OStoreDirectory_Impl::OStoreDirectory_Impl()
72 : m_xManager (),
73 m_aDescr (0, 0, 0),
74 m_nPath (0),
75 m_hTextCvt (NULL)
79 * ~OStoreDirectory_Impl.
81 OStoreDirectory_Impl::~OStoreDirectory_Impl()
83 if (m_xManager.is())
85 if (m_aDescr.m_nAddr != STORE_PAGE_NULL)
86 m_xManager->releasePage (m_aDescr);
88 rtl_destroyTextToUnicodeConverter (m_hTextCvt);
92 * isKindOf.
94 bool OStoreDirectory_Impl::isKindOf (sal_uInt32 nTypeId)
96 return (nTypeId == m_nTypeId);
100 * create.
102 storeError OStoreDirectory_Impl::create (
103 OStorePageManager *pManager,
104 rtl_String *pPath,
105 rtl_String *pName,
106 storeAccessMode eMode)
108 rtl::Reference<OStorePageManager> xManager (pManager);
109 if (!xManager.is())
110 return store_E_InvalidAccess;
112 if (!(pPath && pName))
113 return store_E_InvalidParameter;
115 OStoreDirectoryPageObject aPage;
116 storeError eErrCode = xManager->iget (
117 aPage, STORE_ATTRIB_ISDIR,
118 pPath, pName, eMode);
119 if (eErrCode != store_E_None)
120 return eErrCode;
122 if (!(aPage.attrib() & STORE_ATTRIB_ISDIR))
123 return store_E_NotDirectory;
125 inode_holder_type xNode (aPage.get());
126 eErrCode = xManager->acquirePage (xNode->m_aDescr, store_AccessReadOnly);
127 if (eErrCode != store_E_None)
128 return eErrCode;
130 // Evaluate iteration path.
131 m_nPath = aPage.path();
132 m_nPath = rtl_crc32 (m_nPath, "/", 1);
134 // Save page manager, and descriptor.
135 m_xManager = xManager;
136 m_aDescr = xNode->m_aDescr;
138 return store_E_None;
142 * iterate.
144 storeError OStoreDirectory_Impl::iterate (storeFindData &rFindData)
146 if (!m_xManager.is())
147 return store_E_InvalidAccess;
149 storeError eErrCode = store_E_NoMoreFiles;
150 if (!rFindData.m_nReserved)
151 return eErrCode;
153 // Acquire exclusive access.
154 osl::MutexGuard aGuard (*m_xManager);
156 // Check TextConverter.
157 if (m_hTextCvt == NULL)
158 m_hTextCvt = rtl_createTextToUnicodeConverter(RTL_TEXTENCODING_UTF8);
160 // Setup iteration key.
161 OStorePageKey aKey (rFindData.m_nReserved, m_nPath);
163 // Iterate.
164 for (;;)
166 OStorePageLink aLink;
167 eErrCode = m_xManager->iterate (aKey, aLink, rFindData.m_nAttrib);
168 if (!((eErrCode == store_E_None) && (aKey.m_nHigh == store::htonl(m_nPath))))
169 break;
171 if (!(rFindData.m_nAttrib & STORE_ATTRIB_ISLINK))
173 // Load page.
174 OStoreDirectoryPageObject aPage;
175 eErrCode = m_xManager->loadObjectAt (aPage, aLink.location());
176 if (eErrCode == store_E_None)
178 inode_holder_type xNode (aPage.get());
180 // Setup FindData.
181 sal_Char *p = xNode->m_aNameBlock.m_pData;
182 sal_Size n = rtl_str_getLength (p);
183 sal_Size k = rFindData.m_nLength;
185 n = __store_convertTextToUnicode (
186 m_hTextCvt, p, n,
187 rFindData.m_pszName, STORE_MAXIMUM_NAMESIZE - 1);
188 if (k > n)
190 k = (k - n) * sizeof(sal_Unicode);
191 memset (&rFindData.m_pszName[n], 0, k);
194 rFindData.m_nLength = n;
195 rFindData.m_nAttrib |= aPage.attrib();
196 rFindData.m_nSize = aPage.dataLength();
198 // Leave.
199 rFindData.m_nReserved = store::ntohl(aKey.m_nLow);
200 return store_E_None;
204 if (aKey.m_nLow == 0)
205 break;
206 aKey.m_nLow = store::htonl(store::ntohl(aKey.m_nLow) - 1);
209 // Finished.
210 memset (&rFindData, 0, sizeof (storeFindData));
211 return store_E_NoMoreFiles;
214 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */