xref: /haiku/src/kits/tracker/EntryIterator.h (revision 893988af824e65e49e55f517b157db8386e8002b)
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 	static int _CompareInodes(const dirent *ent1, const dirent *ent2);
121 
122 	BEntryList *fIterator;
123 	entry_ref *fEntryRefBuffer;
124 	int32 fCacheSize;
125 	int32 fNumEntries;
126 	int32 fIndex;
127 
128 	dirent *fDirentBuffer;
129 	dirent *fCurrentDirent;
130 	bool fSortInodes;
131 	BObjectList<dirent> *fSortedList;
132 
133 	BEntry *fEntryBuffer;
134 };
135 
136 class DirectoryEntryList : public EntryListBase {
137 public:
138 	DirectoryEntryList(const BDirectory &);
139 
140 	virtual status_t GetNextEntry(BEntry *entry, bool traverse = false);
141 	virtual status_t GetNextRef(entry_ref *ref);
142 	virtual int32 GetNextDirents(struct dirent *buffer, size_t length,
143 		int32 count = INT_MAX);
144 
145 	virtual status_t Rewind();
146 	virtual int32 CountEntries();
147 
148 private:
149 	BDirectory fDir;
150 };
151 
152 class CachedDirectoryEntryList : public CachedEntryIterator {
153 	// this class is to work around not being able to delete
154 	// BEntryList polymorfically - need to have a special
155 	// caching entry list iterator for directories
156 public:
157 	CachedDirectoryEntryList(const BDirectory &);
158 	virtual ~CachedDirectoryEntryList();
159 
160 private:
161 	BDirectory fDir;
162 };
163 
164 class EntryIteratorList : public EntryListBase {
165 	// This wraps up several BEntryList style iterators and
166 	// iterates them all, going from one to the other as it finishes
167 	// up each of them
168 public:
169 	EntryIteratorList();
170 	virtual ~EntryIteratorList();
171 
172 	void AddItem(BEntryList *);
173 		// list gets to own walkers
174 
175 	virtual status_t GetNextEntry(BEntry *entry, bool traverse = false);
176 	virtual status_t GetNextRef(entry_ref *ref);
177 	virtual int32 GetNextDirents(struct dirent *buffer, size_t length,
178 		int32 count = INT_MAX);
179 
180 	virtual status_t Rewind();
181 	virtual int32 CountEntries();
182 
183 protected:
184 	BObjectList<BEntryList> fList;
185 	int32 fCurrentIndex;
186 };
187 
188 class CachedEntryIteratorList : public CachedEntryIterator {
189 public:
190 	CachedEntryIteratorList(bool sortInodes = true);
191 	void AddItem(BEntryList *list);
192 
193 protected:
194 	EntryIteratorList fIteratorList;
195 };
196 
197 } // namespace BPrivate
198 
199 using namespace BPrivate;
200 
201 #endif
202