1/*
2Open Tracker License
3
4Terms and Conditions
5
6Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
7
8Permission is hereby granted, free of charge, to any person obtaining a copy of
9this software and associated documentation files (the "Software"), to deal in
10the Software without restriction, including without limitation the rights to
11use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12of the Software, and to permit persons to whom the Software is furnished to do
13so, subject to the following conditions:
14
15The above copyright notice and this permission notice applies to all licensees
16and shall be included in all copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
23WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25Except as contained in this notice, the name of Be Incorporated shall not be
26used in advertising or otherwise to promote the sale, use or other dealings in
27this Software without prior written authorization from Be Incorporated.
28
29Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
30of Be Incorporated in the United States and other countries. Other brand product
31names are registered trademarks or trademarks of their respective holders.
32All rights reserved.
33*/
34
35//  A lot of the code in here wouldn't be needed if the destructor
36//  for BEntryList was virtual
37
38// TODO: get rid of all BEntryList API's in here, replace them with
39//       EntryListBase ones
40#ifndef _ENTRY_ITERATOR_H
41#define _ENTRY_ITERATOR_H
42
43
44#include <Directory.h>
45#include <ObjectList.h>
46
47#include "NodeWalker.h"
48
49
50namespace BPrivate {
51
52class EntryListBase : public BEntryList {
53	// this is what BEntryList should have been
54public:
55	EntryListBase();
56	virtual ~EntryListBase() {}
57
58	virtual status_t InitCheck() const;
59
60	virtual status_t GetNextEntry(BEntry* entry, bool traverse = false) = 0;
61	virtual status_t GetNextRef(entry_ref* ref) = 0;
62	virtual int32 GetNextDirents(struct dirent* buffer, size_t length,
63		int32 count = INT_MAX) = 0;
64
65	virtual status_t Rewind() = 0;
66	virtual int32 CountEntries() = 0;
67
68	static dirent* Next(dirent*);
69
70protected:
71	status_t fStatus;
72};
73
74
75class TWalkerWrapper : public EntryListBase {
76	// this is to be able to use TWalker polymorfically as BEntryListBase
77public:
78	TWalkerWrapper(BTrackerPrivate::TWalker* walker);
79	virtual ~TWalkerWrapper();
80
81	virtual status_t InitCheck() const;
82	virtual status_t GetNextEntry(BEntry* entry, bool traverse = false);
83	virtual status_t GetNextRef(entry_ref* ref);
84	virtual int32 GetNextDirents(struct dirent* buffer, size_t length,
85		int32 count = INT_MAX);
86	virtual status_t Rewind();
87	virtual int32 CountEntries();
88
89protected:
90	BTrackerPrivate::TWalker* fWalker;
91	status_t fStatus;
92};
93
94
95const int32 kDirentBufferSize = 10 * 1024;
96
97
98class CachedEntryIterator : public EntryListBase {
99public:
100	// takes any iterator and runs it through a cache of a specified size
101	// used to cluster entry_ref reads together, away from node accesses
102	//
103	// each chunk of iterators in the cache are then returned in an order,
104	// sorted by their i-node number -- this turns out to give quite a bit
105	// better performance over just using the order in which they show up
106	// using the default BEntryList iterator subclass
107
108	CachedEntryIterator(BEntryList* iterator, int32 numEntries,
109		bool sortInodes = false);
110		// CachedEntryIterator does not get to own the <iterator>
111	virtual ~CachedEntryIterator();
112
113	virtual status_t GetNextEntry(BEntry* entry, bool traverse = false);
114	virtual status_t GetNextRef(entry_ref* ref);
115	virtual int32 GetNextDirents(struct dirent* buffer, size_t length,
116		int32 count = INT_MAX);
117
118	virtual status_t Rewind();
119	virtual int32 CountEntries();
120
121	virtual void SetTo(BEntryList* iterator);
122		// CachedEntryIterator does not get to own the <iterator>
123
124private:
125	static int _CompareInodes(const dirent* ent1, const dirent* ent2);
126
127	BEntryList* fIterator;
128	entry_ref* fEntryRefBuffer;
129	int32 fCacheSize;
130	int32 fNumEntries;
131	int32 fIndex;
132
133	dirent* fDirentBuffer;
134	dirent* fCurrentDirent;
135	bool fSortInodes;
136	BObjectList<dirent>* fSortedList;
137
138	BEntry* fEntryBuffer;
139};
140
141
142class DirectoryEntryList : public EntryListBase {
143public:
144	DirectoryEntryList(const BDirectory &);
145
146	virtual status_t GetNextEntry(BEntry* entry, bool traverse = false);
147	virtual status_t GetNextRef(entry_ref* ref);
148	virtual int32 GetNextDirents(struct dirent* buffer, size_t length,
149		int32 count = INT_MAX);
150
151	virtual status_t Rewind();
152	virtual int32 CountEntries();
153
154private:
155	BDirectory fDirectory;
156};
157
158
159class CachedDirectoryEntryList : public CachedEntryIterator {
160	// this class is to work around not being able to delete
161	// BEntryList polymorfically - need to have a special
162	// caching entry list iterator for directories
163public:
164	CachedDirectoryEntryList(const BDirectory &);
165	virtual ~CachedDirectoryEntryList();
166
167private:
168	BDirectory fDirectory;
169};
170
171
172class EntryIteratorList : public EntryListBase {
173	// This wraps up several BEntryList style iterators and
174	// iterates them all, going from one to the other as it finishes
175	// up each of them
176public:
177	EntryIteratorList();
178	virtual ~EntryIteratorList();
179
180	void AddItem(BEntryList*);
181		// list gets to own walkers
182
183	virtual status_t GetNextEntry(BEntry* entry, bool traverse = false);
184	virtual status_t GetNextRef(entry_ref* ref);
185	virtual int32 GetNextDirents(struct dirent* buffer, size_t length,
186		int32 count = INT_MAX);
187
188	virtual status_t Rewind();
189	virtual int32 CountEntries();
190
191protected:
192	BObjectList<BEntryList> fList;
193	int32 fCurrentIndex;
194};
195
196
197class CachedEntryIteratorList : public CachedEntryIterator {
198public:
199	CachedEntryIteratorList(bool sortInodes = true);
200	void AddItem(BEntryList* list);
201
202protected:
203	EntryIteratorList fIteratorList;
204};
205
206} // namespace BPrivate
207
208using namespace BPrivate;
209
210
211#endif	// _ENTRY_ITERATOR_H
212