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