xref: /haiku/src/kits/tracker/NodeWalker.h (revision 323b65468e5836bb27a5e373b14027d902349437)
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 #ifndef WALKER_H
36 #define WALKER_H
37 
38 #ifndef _BE_BUILD_H
39 #include <BeBuild.h>
40 #endif
41 #include <VolumeRoster.h>
42 #include <Volume.h>
43 #include <List.h>
44 #include <EntryList.h>
45 #include <Directory.h>
46 #include <Entry.h>
47 #include <Query.h>
48 
49 #include "ObjectList.h"
50 
51 namespace BTrackerPrivate {
52 
53 
54 class TWalker : public BEntryList {
55 	// adds a virtual destructor that is severely missing in BEntryList
56 	// BEntryList should never be used polymorphically because of that
57 
58 public:
59 	virtual ~TWalker();
60 
61 	virtual	status_t GetNextEntry(BEntry *, bool traverse = false) = 0;
62 	virtual	status_t GetNextRef(entry_ref *) = 0;
63 	virtual	int32 GetNextDirents(struct dirent *, size_t,
64 		int32 count = INT_MAX) = 0;
65 	virtual	status_t Rewind() = 0;
66 	virtual	int32 CountEntries() = 0;
67 };
68 
69 class TNodeWalker : public TWalker {
70 // TNodeWalker supports iterating a single volume, starting from a specified
71 // entry; if passed a non-directory entry it returns just that one entry
72 public:
73 	TNodeWalker(bool includeTopDirectory);
74 	TNodeWalker(const char *path, bool includeTopDirectory);
75 	TNodeWalker(const entry_ref *ref, bool includeTopDirectory);
76 	TNodeWalker(const BDirectory *dir, bool includeTopDirectory);
77 	virtual ~TNodeWalker();
78 
79 	// Backwards compatibility with Tracker compiled for R5 (remove when this
80 	// gets integrated into the official release).
81 	TNodeWalker();
82 	TNodeWalker(const char *path);
83 	TNodeWalker(const entry_ref *ref);
84 	TNodeWalker(const BDirectory *dir);
85 
86 	virtual	status_t GetNextEntry(BEntry *, bool traverse = false);
87 	virtual	status_t GetNextRef(entry_ref *);
88 	virtual	int32 GetNextDirents(struct dirent *, size_t,
89 		int32 count = INT_MAX);
90 	virtual	status_t Rewind();
91 
92 protected:
93 	status_t PopDirCommon();
94 	void PushDirCommon(const entry_ref *);
95 
96 private:
97 	virtual	int32 CountEntries();
98 		// don't know how to do that, have just a fake stub here
99 
100 protected:
101 	BObjectList<BDirectory> fDirs;
102 	int32 fTopIndex;
103 	BDirectory *fTopDir;
104 	bool fIncludeTopDir;
105 	bool fOriginalIncludeTopDir;
106 
107 private:
108 	BEntry *fJustFile;
109 	BDirectory fOriginalDirCopy;
110 	BEntry *fOriginalJustFile;
111 		// keep around to support Rewind
112 };
113 
114 class TVolWalker : public TNodeWalker {
115 // TNodeWalker supports iterating over all the mounted volumes;
116 // non-attribute and read-only volumes may optionaly be filtered out
117 public:
118 	TVolWalker(bool knows_attr = true, bool writable = true,
119 		bool includeTopDirectory = true);
120 	virtual ~TVolWalker();
121 
122 	virtual	status_t GetNextEntry(BEntry *, bool traverse = false);
123 	virtual	status_t GetNextRef(entry_ref *);
124 	virtual	int32 GetNextDirents(struct dirent *, size_t,
125 		int32 count = INT_MAX);
126 	virtual	status_t Rewind();
127 
128 	virtual	status_t NextVolume();
129 		// skips to the next volume
130 		// Note: it would be cool to return const BVolume *
131 		// that way a subclass could implement a volume filter -
132 		// it would just override, call inherited for as long as there
133 		// are volumes and it does not like them
134 		// we would have to give up the status_t then, which might be
135 		// ok
136 
137 private:
138 	BVolumeRoster fVolRoster;
139 	BVolume fVol;
140 	bool fKnowsAttr;
141 	bool fWritable;
142 
143 	typedef	TNodeWalker _inherited;
144 };
145 
146 class TQueryWalker : public TWalker {
147 public:
148 	TQueryWalker(const char *predicate);
149 	virtual ~TQueryWalker();
150 
151 	// Does an in-fix walk of all entries
152 	virtual	status_t GetNextEntry(BEntry *, bool traverse = false);
153 	virtual	status_t GetNextRef(entry_ref *);
154 	virtual	int32 GetNextDirents(struct dirent *, size_t,
155 		int32 count = INT_MAX);
156 
157 	virtual	status_t NextVolume();
158 	// skips to the next volume
159 	virtual	status_t Rewind();
160 
161 private:
162 	virtual	int32 CountEntries();
163 	// can't count
164 
165 	BQuery fQuery;
166 	BVolumeRoster fVolRoster;
167 	BVolume fVol;
168 	bigtime_t fTime;
169 	const char *fPredicate;
170 
171 	typedef	TQueryWalker _inherited;
172 };
173 
174 }	// namespace BTrackerPrivate
175 
176 using namespace BTrackerPrivate;
177 
178 #endif	// WALKER_H
179