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