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