xref: /haiku/src/kits/tracker/NodeWalker.h (revision 17889a8c70dbb3d59c1412f6431968753c767bab)
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 #ifndef _NODE_WALKER_H
35 #define _NODE_WALKER_H
36 
37 
38 #ifndef _BE_BUILD_H
39 #	include <BeBuild.h>
40 #endif
41 #include <Directory.h>
42 #include <Entry.h>
43 #include <EntryList.h>
44 #include <List.h>
45 #include <ObjectList.h>
46 #include <Query.h>
47 #include <Volume.h>
48 #include <VolumeRoster.h>
49 
50 
51 namespace BTrackerPrivate {
52 
53 class TWalker : public BEntryList {
54 	// adds a virtual destructor that is severely missing in BEntryList
55 	// BEntryList should never be used polymorphically because of that
56 public:
57 	virtual ~TWalker();
58 
59 	virtual status_t GetNextEntry(BEntry*, bool traverse = false) = 0;
60 	virtual status_t GetNextRef(entry_ref*) = 0;
61 	virtual int32 GetNextDirents(struct dirent*, size_t,
62 		int32 count = INT_MAX) = 0;
63 	virtual status_t Rewind() = 0;
64 	virtual int32 CountEntries() = 0;
65 };
66 
67 
68 class TNodeWalker : public TWalker {
69 	// TNodeWalker supports iterating a single volume, starting from a specified
70 	// entry; if passed a non-directory entry it returns just that one entry
71 public:
72 	TNodeWalker(bool includeTopDirectory);
73 	TNodeWalker(const char* path, bool includeTopDirectory);
74 	TNodeWalker(const entry_ref* ref, bool includeTopDirectory);
75 	TNodeWalker(const BDirectory* dir, bool includeTopDirectory);
76 	virtual ~TNodeWalker();
77 
78 	// Backwards compatibility with Tracker compiled for R5 (remove when this
79 	// gets integrated into the official release).
80 	TNodeWalker();
81 	TNodeWalker(const char* path);
82 	TNodeWalker(const entry_ref* ref);
83 	TNodeWalker(const BDirectory* dir);
84 
85 	virtual	status_t GetNextEntry(BEntry*, bool traverse = false);
86 	virtual	status_t GetNextRef(entry_ref*);
87 	virtual	int32 GetNextDirents(struct dirent*, size_t,
88 		int32 count = INT_MAX);
89 	virtual	status_t Rewind();
90 
91 protected:
92 	status_t PopDirCommon();
93 	void PushDirCommon(const entry_ref*);
94 
95 private:
96 	virtual	int32 CountEntries();
97 		// don't know how to do that, have just a fake stub here
98 
99 protected:
100 	BObjectList<BDirectory> fDirs;
101 	int32 fTopIndex;
102 	BDirectory* fTopDir;
103 	bool fIncludeTopDir;
104 	bool fOriginalIncludeTopDir;
105 
106 private:
107 	BEntry* fJustFile;
108 	BDirectory fOriginalDirCopy;
109 	BEntry* fOriginalJustFile;
110 		// keep around to support Rewind
111 };
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 
147 class TQueryWalker : public TWalker {
148 public:
149 	TQueryWalker(const char* predicate);
150 	virtual ~TQueryWalker();
151 
152 	// Does an in-fix walk of all entries
153 	virtual status_t GetNextEntry(BEntry*, bool traverse = false);
154 	virtual status_t GetNextRef(entry_ref*);
155 	virtual int32 GetNextDirents(struct dirent*, size_t,
156 		int32 count = INT_MAX);
157 
158 	virtual status_t NextVolume();
159 	// skips to the next volume
160 	virtual status_t Rewind();
161 
162 private:
163 	virtual int32 CountEntries();
164 		// can't count
165 
166 	BQuery fQuery;
167 	BVolumeRoster fVolRoster;
168 	BVolume fVol;
169 	bigtime_t fTime;
170 	const char* fPredicate;
171 
172 	typedef TQueryWalker _inherited;
173 };
174 
175 }	// namespace BTrackerPrivate
176 
177 using namespace BTrackerPrivate;
178 
179 
180 #endif	// _NODE_WALKER_H
181