xref: /haiku/src/kits/tracker/FSUtils.h (revision 91054f1d38dd7827c0f0ba9490c213775ec7b471)
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	FS_UTILS_H
36 #define	FS_UTILS_H
37 
38 #include <FindDirectory.h>
39 #include <List.h>
40 #include <Point.h>
41 #include <StorageDefs.h>
42 
43 #include <vector>
44 
45 #include "Model.h"
46 #include "ObjectList.h"
47 
48 // Note - APIs/code in FSUtils.h and FSUtils.cpp is slated for a major cleanup
49 // 	-- in other words, you will find a lot of ugly cruft in here
50 
51 class BDirectory;
52 class BEntry;
53 class BList;
54 class BFile;
55 
56 namespace BPrivate {
57 
58 class BInfoWindow;
59 
60 class CopyLoopControl {
61 	// controls the copy engine; may be overriden to specify how conflicts are
62 	// handled, etc.
63 	// Installer has it's own subclass
64 	public:
65 		virtual ~CopyLoopControl();
66 		virtual bool FileError(const char *message, const char *name, status_t error,
67 			bool allowContinue) = 0;
68 			// inform that a file error occurred while copying <name>
69 			// returns true if user decided to continue
70 
71 		virtual void UpdateStatus(const char *name, entry_ref ref, int32 count,
72 			bool optional = false) = 0;
73 
74 		virtual bool CheckUserCanceled() = 0;
75 			// returns true if canceled
76 
77 		enum OverwriteMode {
78 			kSkip,				// do not replace, go to next entry
79 			kReplace,			// remove entry before copying new one
80 			kMerge				// for folders: leave existing folder, update contents leaving
81 								//  nonconflicting items
82 								// for files: save original attributes on file.
83 		};
84 
85 		virtual OverwriteMode OverwriteOnConflict(const BEntry *srcEntry,
86 			const char *destName, const BDirectory *destDir, bool srcIsDir,
87 			bool dstIsDir) = 0;
88 			// override to always overwrite, never overwrite, let user decide,
89 			// compare dates, etc.
90 
91 		virtual bool SkipEntry(const BEntry *, bool file) = 0;
92 			// override to prevent copying of a given file or directory
93 
94 		virtual void ChecksumChunk(const char *block, size_t size);
95 			// during a file copy, this is called every time a chunk of data
96 			// is copied.  Users may override to keep a running checksum.
97 
98 		virtual bool ChecksumFile(const entry_ref *);
99 			// This is called when a file is finished copying.  Users of this
100 			// class may override to verify that the checksum they've been
101 			// computing in ChecksumChunk matches.  If this returns true,
102 			// the copy will continue.  If false, if will abort.
103 
104 		virtual bool SkipAttribute(const char *attributeName);
105 		virtual bool PreserveAttribute(const char *attributeName);
106 };
107 
108 
109 class TrackerCopyLoopControl : public CopyLoopControl {
110 	// this is the Tracker copy - specific version of CopyLoopControl
111 	public:
112 		TrackerCopyLoopControl(thread_id);
113 		virtual ~TrackerCopyLoopControl() {}
114 
115 		virtual bool FileError(const char *message, const char *name, status_t error,
116 			bool allowContinue);
117 			// inform that a file error occurred while copying <name>
118 			// returns true if user decided to continue
119 
120 		virtual void UpdateStatus(const char *name, entry_ref ref, int32 count,
121 			bool optional = false);
122 
123 		virtual bool CheckUserCanceled();
124 			// returns true if canceled
125 
126 		virtual OverwriteMode OverwriteOnConflict(const BEntry *srcEntry,
127 			const char *destName, const BDirectory *destDir, bool srcIsDir,
128 			bool dstIsDir);
129 
130 		virtual bool SkipEntry(const BEntry *, bool file);
131 			// override to prevent copying of a given file or directory
132 
133 		virtual bool SkipAttribute(const char *attributeName);
134 
135 	private:
136 		thread_id fThread;
137 };
138 
139 
140 inline
141 TrackerCopyLoopControl::TrackerCopyLoopControl(thread_id thread)
142 	: fThread(thread)
143 {
144 }
145 
146 #define B_DESKTOP_DIR_NAME "Desktop"
147 
148 #ifndef _IMPEXP_TRACKER
149 #define _IMPEXP_TRACKER
150 #endif
151 _IMPEXP_TRACKER status_t FSCopyAttributesAndStats(BNode *, BNode *);
152 
153 _IMPEXP_TRACKER void FSDuplicate(BObjectList<entry_ref> *srcList, BList *pointList);
154 _IMPEXP_TRACKER void FSMoveToFolder(BObjectList<entry_ref> *srcList, BEntry *, uint32 moveMode,
155 	BList *pointList = NULL);
156 _IMPEXP_TRACKER void FSMakeOriginalName(char *name, BDirectory *destDir, const char *suffix);
157 _IMPEXP_TRACKER bool FSIsTrashDir(const BEntry *);
158 _IMPEXP_TRACKER bool FSIsPrintersDir(const BEntry *);
159 _IMPEXP_TRACKER bool FSIsDeskDir(const BEntry *);
160 _IMPEXP_TRACKER bool FSIsHomeDir(const BEntry *);
161 _IMPEXP_TRACKER void FSMoveToTrash(BObjectList<entry_ref> *srcList, BList *pointList = NULL,
162 	bool async = true);
163 	// Deprecated
164 
165 void FSDeleteRefList(BObjectList<entry_ref> *, bool, bool confirm = true);
166 void FSDelete(entry_ref *, bool, bool confirm = true);
167 void FSRestoreRefList(BObjectList<entry_ref> *list, bool async);
168 
169 _IMPEXP_TRACKER status_t FSLaunchItem(const entry_ref *application, const BMessage *refsReceived,
170 	bool async, bool openWithOK);
171 	// Preferred way of launching; only pass an actual application in <application>, not
172 	// a document; to open documents with the preferred app, pase 0 in <application> and
173 	// stuff all the document refs into <refsReceived>
174 	// Consider having silent mode that does not show alerts, just returns error code
175 
176 _IMPEXP_TRACKER status_t FSOpenWith(BMessage *listOfRefs);
177 	// runs the Open With window; pas a list of refs
178 
179 _IMPEXP_TRACKER void FSEmptyTrash();
180 _IMPEXP_TRACKER status_t FSCreateNewFolderIn(const node_ref *destDir, entry_ref *newRef,
181 	node_ref *new_node);
182 _IMPEXP_TRACKER void FSCreateTrashDirs();
183 _IMPEXP_TRACKER status_t FSGetTrashDir(BDirectory *trashDir, dev_t volume);
184 _IMPEXP_TRACKER status_t FSGetDeskDir(BDirectory *deskDir, dev_t volume);
185 _IMPEXP_TRACKER status_t FSRecursiveCalcSize(BInfoWindow *, BDirectory *,
186 	off_t *runningSize, int32 *fileCount, int32 *dirCount);
187 
188 bool FSInTrashDir(const entry_ref *);
189 
190 // doesn't need to be exported
191 bool FSGetPoseLocation(const BNode *node, BPoint *point);
192 status_t FSSetPoseLocation(BEntry *entry, BPoint point);
193 status_t FSSetPoseLocation(ino_t destDirInode, BNode *destNode, BPoint point);
194 status_t FSGetBootDeskDir(BDirectory *deskDir);
195 
196 status_t FSGetOriginalPath(BEntry *entry, BPath *path);
197 
198 enum ReadAttrResult {
199 	kReadAttrFailed,
200 	kReadAttrNativeOK,
201 	kReadAttrForeignOK
202 };
203 
204 ReadAttrResult ReadAttr(const BNode *, const char *hostAttrName, const char *foreignAttrName,
205 	type_code , off_t , void *, size_t , void (*swapFunc)(void *) = 0,
206 	bool isForeign = false);
207 	// Endian swapping ReadAttr call; endianness is determined by trying first the
208 	// native attribute name, then the foreign one; an endian swapping function can
209 	// be passed, if null data won't be swapped; if <isForeign> set the foreign endianness
210 	// will be read directly without first trying the native one
211 
212 ReadAttrResult GetAttrInfo(const BNode *, const char *hostAttrName, const char *foreignAttrName,
213 	type_code * = NULL, size_t * = NULL);
214 
215 status_t FSCreateNewFolder(const entry_ref *);
216 status_t FSRecursiveCreateFolder(const char *path);
217 void FSMakeOriginalName(BString &name, const BDirectory *destDir, const char *suffix = 0);
218 
219 status_t TrackerLaunch(const entry_ref *app, bool async);
220 status_t TrackerLaunch(const BMessage *refs, bool async, bool okToRunOpenWith = true);
221 status_t TrackerLaunch(const entry_ref *app, const BMessage *refs, bool async,
222 	bool okToRunOpenWith = true);
223 status_t LaunchBrokenLink(const char *, const BMessage *);
224 
225 status_t FSFindTrackerSettingsDir(BPath *, bool autoCreate = true);
226 
227 bool FSIsDeskDir(const BEntry *, dev_t);
228 
229 bool ConfirmChangeIfWellKnownDirectory(const BEntry *entry, const char *action,
230 	bool dontAsk = false, int32 *confirmedAlready = NULL);
231 
232 // Deprecated calls use newer calls above instead
233 _IMPEXP_TRACKER void FSLaunchItem(const entry_ref *, BMessage * = NULL, int32 workspace = -1);
234 _IMPEXP_TRACKER status_t FSLaunchItem(const entry_ref *, BMessage *,
235 	int32 workspace, bool asynch);
236 _IMPEXP_TRACKER void FSOpenWithDocuments(const entry_ref *executableToLaunch,
237 	BMessage *documentEntryRefs);
238 _IMPEXP_TRACKER status_t FSLaunchUsing(const entry_ref *ref, BMessage *listOfRefs);
239 
240 
241 // some extra directory_which values
242 // move these to FindDirectory.h
243 const uint32 B_USER_MAIL_DIRECTORY = 3500;
244 const uint32 B_USER_QUERIES_DIRECTORY = 3501;
245 const uint32 B_USER_PEOPLE_DIRECTORY = 3502;
246 const uint32 B_USER_DOWNLOADS_DIRECTORY = 3503;
247 const uint32 B_USER_DESKBAR_APPS_DIRECTORY = 3504;
248 const uint32 B_USER_DESKBAR_PREFERENCES_DIRECTORY = 3505;
249 const uint32 B_USER_DESKBAR_DEVELOP_DIRECTORY = 3506;
250 
251 const int32 B_BOOT_DISK = 10000000;
252 	// map /boot into the directory_which enum for convenience
253 
254 class WellKnowEntryList {
255 	// matches up names, id's and node_refs of well known entries in the
256 	// system hierarchy
257 	public:
258 		struct WellKnownEntry {
259 			WellKnownEntry(const node_ref *node, directory_which which, const char *name)
260 				:
261 				node(*node),
262 				which(which),
263 				name(name)
264 			{
265 			}
266 
267 			// mwcc needs these explicitly to use vector
268 			WellKnownEntry(const WellKnownEntry &clone)
269 				:
270 				node(clone.node),
271 				which(clone.which),
272 				name(clone.name)
273 			{
274 			}
275 
276 			WellKnownEntry()
277 			{
278 			}
279 
280 			node_ref node;
281 			directory_which which;
282 			const char *name;
283 		};
284 
285 		static directory_which Match(const node_ref *);
286 		static const WellKnownEntry *MatchEntry(const node_ref *);
287 		static void Quit();
288 
289 	private:
290 		const WellKnownEntry *MatchEntryCommon(const node_ref *);
291 		WellKnowEntryList();
292 		void AddOne(directory_which, const char *name);
293 		void AddOne(directory_which, const char *path, const char *name);
294 		void AddOne(directory_which, directory_which base, const char *extension,
295 			const char *name);
296 
297 		std::vector<WellKnownEntry> entries;
298 		static WellKnowEntryList *self;
299 };
300 
301 #if B_BEOS_VERSION_DANO
302 #undef _IMPEXP_TRACKER
303 #endif
304 
305 } // namespace BPrivate
306 
307 using namespace BPrivate;
308 
309 #endif	/* FS_UTILS_H */
310