xref: /haiku/src/kits/tracker/EntryIterator.h (revision 1754f053a373daad947badbcf867ce2d2f6fef3e)
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: 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 
50 namespace BPrivate {
51 
52 class EntryListBase : public BEntryList {
53 	// this is what BEntryList should have been
54 public:
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 
70 protected:
71 	status_t fStatus;
72 };
73 
74 
75 class TWalkerWrapper : public EntryListBase {
76 	// this is to be able to use TWalker polymorfically as BEntryListBase
77 public:
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 
89 protected:
90 	BTrackerPrivate::TWalker* fWalker;
91 	status_t fStatus;
92 };
93 
94 
95 const int32 kDirentBufferSize = B_PAGE_SIZE * 2;
96 
97 
98 class CachedEntryIterator : public EntryListBase {
99 public:
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 
124 private:
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 
142 class DirectoryEntryList : public EntryListBase {
143 public:
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 
154 private:
155 	BDirectory fDirectory;
156 };
157 
158 
159 class 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
163 public:
164 	CachedDirectoryEntryList(const BDirectory &);
165 	virtual ~CachedDirectoryEntryList();
166 
167 private:
168 	BDirectory fDirectory;
169 };
170 
171 
172 class 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
176 public:
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 
191 protected:
192 	BObjectList<BEntryList> fList;
193 	int32 fCurrentIndex;
194 };
195 
196 
197 class CachedEntryIteratorList : public CachedEntryIterator {
198 public:
199 	CachedEntryIteratorList(bool sortInodes = true);
200 	void AddItem(BEntryList* list);
201 
202 protected:
203 	EntryIteratorList fIteratorList;
204 };
205 
206 } // namespace BPrivate
207 
208 using namespace BPrivate;
209 
210 
211 #endif	// _ENTRY_ITERATOR_H
212