xref: /haiku/src/apps/installer/WorkerThread.cpp (revision 15fb7d88e971c4d6c787c6a3a5c159afb1ebf77b)
1 /*
2  * Copyright 2009, Stephan Aßmus <superstippi@gmx.de>.
3  * Copyright 2005-2008, Jérôme DUVAL.
4  * All rights reserved. Distributed under the terms of the MIT License.
5  */
6 
7 #include "WorkerThread.h"
8 
9 #include <errno.h>
10 #include <stdio.h>
11 
12 #include <set>
13 #include <string>
14 #include <strings.h>
15 
16 #include <Alert.h>
17 #include <Autolock.h>
18 #include <Catalog.h>
19 #include <Directory.h>
20 #include <DiskDeviceVisitor.h>
21 #include <DiskDeviceTypes.h>
22 #include <FindDirectory.h>
23 #include <fs_index.h>
24 #include <Locale.h>
25 #include <Menu.h>
26 #include <MenuItem.h>
27 #include <Message.h>
28 #include <Messenger.h>
29 #include <Path.h>
30 #include <String.h>
31 #include <VolumeRoster.h>
32 
33 #include "AutoLocker.h"
34 #include "CopyEngine.h"
35 #include "InstallerDefs.h"
36 #include "PackageViews.h"
37 #include "PartitionMenuItem.h"
38 #include "ProgressReporter.h"
39 #include "StringForSize.h"
40 #include "UnzipEngine.h"
41 
42 
43 #define B_TRANSLATION_CONTEXT "InstallProgress"
44 
45 
46 //#define COPY_TRACE
47 #ifdef COPY_TRACE
48 #define CALLED() 		printf("CALLED %s\n",__PRETTY_FUNCTION__)
49 #define ERR2(x, y...)	fprintf(stderr, "WorkerThread: "x" %s\n", y, strerror(err))
50 #define ERR(x)			fprintf(stderr, "WorkerThread: "x" %s\n", strerror(err))
51 #else
52 #define CALLED()
53 #define ERR(x)
54 #define ERR2(x, y...)
55 #endif
56 
57 const char BOOT_PATH[] = "/boot";
58 
59 const uint32 MSG_START_INSTALLING = 'eSRT';
60 
61 
62 class SourceVisitor : public BDiskDeviceVisitor {
63 public:
64 	SourceVisitor(BMenu* menu);
65 	virtual bool Visit(BDiskDevice* device);
66 	virtual bool Visit(BPartition* partition, int32 level);
67 
68 private:
69 	BMenu* fMenu;
70 };
71 
72 
73 class TargetVisitor : public BDiskDeviceVisitor {
74 public:
75 	TargetVisitor(BMenu* menu);
76 	virtual bool Visit(BDiskDevice* device);
77 	virtual bool Visit(BPartition* partition, int32 level);
78 
79 private:
80 	BMenu* fMenu;
81 };
82 
83 
84 // #pragma mark - WorkerThread
85 
86 
87 class WorkerThread::EntryFilter : public CopyEngine::EntryFilter {
88 public:
89 	EntryFilter(const char* sourceDirectory)
90 		:
91 		fIgnorePaths(),
92 		fSourceDevice(-1)
93 	{
94 		try {
95 			fIgnorePaths.insert(kPackagesDirectoryPath);
96 			fIgnorePaths.insert(kSourcesDirectoryPath);
97 			fIgnorePaths.insert("rr_moved");
98 			fIgnorePaths.insert("boot.catalog");
99 			fIgnorePaths.insert("haiku-boot-floppy.image");
100 			fIgnorePaths.insert("system/var/swap");
101 			fIgnorePaths.insert("system/var/shared_memory");
102 			fIgnorePaths.insert("system/var/log/syslog");
103 			fIgnorePaths.insert("system/var/log/syslog.old");
104 
105 			fPackageFSRootPaths.insert("system");
106 			fPackageFSRootPaths.insert("home/config");
107 		} catch (std::bad_alloc&) {
108 		}
109 
110 		struct stat st;
111 		if (stat(sourceDirectory, &st) == 0)
112 			fSourceDevice = st.st_dev;
113 	}
114 
115 	virtual bool ShouldCopyEntry(const BEntry& entry, const char* path,
116 		const struct stat& statInfo, int32 level) const
117 	{
118 		if (S_ISBLK(statInfo.st_mode) || S_ISCHR(statInfo.st_mode)
119 				|| S_ISFIFO(statInfo.st_mode) || S_ISSOCK(statInfo.st_mode)) {
120 			printf("skipping '%s', it is a special file.\n", path);
121 			return false;
122 		}
123 
124 		if (fIgnorePaths.find(path) != fIgnorePaths.end()) {
125 			printf("ignoring '%s'.\n", path);
126 			return false;
127 		}
128 
129 		if (statInfo.st_dev != fSourceDevice) {
130 			// Allow that only for the root of the packagefs mounts, since
131 			// those contain directories that shine through from the
132 			// underlying volume.
133 			if (fPackageFSRootPaths.find(path) == fPackageFSRootPaths.end())
134 				return false;
135 		}
136 
137 		return true;
138 	}
139 
140 	virtual bool ShouldClobberFolder(const BEntry& entry, const char* path,
141 		const struct stat& statInfo, int32 level) const
142 	{
143 		if (level == 2 && S_ISDIR(statInfo.st_mode)
144 				&& strncmp("system/", path, 7) == 0
145 				&& strcmp("system/settings", path) != 0) {
146 			// Replace everything in "system" besides "settings"
147 			printf("clobbering '%s'.\n", path);
148 			return true;
149 		}
150 		return false;
151 	}
152 
153 private:
154 	typedef std::set<std::string> StringSet;
155 
156 			StringSet			fIgnorePaths;
157 			StringSet			fPackageFSRootPaths;
158 			dev_t				fSourceDevice;
159 };
160 
161 
162 // #pragma mark - WorkerThread
163 
164 
165 WorkerThread::WorkerThread(const BMessenger& owner)
166 	:
167 	BLooper("copy_engine"),
168 	fOwner(owner),
169 	fPackages(NULL),
170 	fSpaceRequired(0),
171 	fCancelSemaphore(-1)
172 {
173 	Run();
174 }
175 
176 
177 void
178 WorkerThread::MessageReceived(BMessage* message)
179 {
180 	CALLED();
181 
182 	switch (message->what) {
183 		case MSG_START_INSTALLING:
184 			_PerformInstall(message->GetInt32("source", -1),
185 				message->GetInt32("target", -1));
186 			break;
187 
188 		case MSG_WRITE_BOOT_SECTOR:
189 		{
190 			int32 id;
191 			if (message->FindInt32("id", &id) != B_OK) {
192 				_SetStatusMessage(B_TRANSLATE("Boot sector not written "
193 					"because of an internal error."));
194 				break;
195 			}
196 
197 			// TODO: Refactor with _PerformInstall()
198 			BPath targetDirectory;
199 			BDiskDevice device;
200 			BPartition* partition;
201 
202 			if (fDDRoster.GetPartitionWithID(id, &device, &partition) == B_OK) {
203 				if (!partition->IsMounted()) {
204 					if (partition->Mount() < B_OK) {
205 						_SetStatusMessage(B_TRANSLATE("The partition can't be "
206 							"mounted. Please choose a different partition."));
207 						break;
208 					}
209 				}
210 				if (partition->GetMountPoint(&targetDirectory) != B_OK) {
211 					_SetStatusMessage(B_TRANSLATE("The mount point could not "
212 						"be retrieved."));
213 					break;
214 				}
215 			} else if (fDDRoster.GetDeviceWithID(id, &device) == B_OK) {
216 				if (!device.IsMounted()) {
217 					if (device.Mount() < B_OK) {
218 						_SetStatusMessage(B_TRANSLATE("The disk can't be "
219 							"mounted. Please choose a different disk."));
220 						break;
221 					}
222 				}
223 				if (device.GetMountPoint(&targetDirectory) != B_OK) {
224 					_SetStatusMessage(B_TRANSLATE("The mount point could not "
225 						"be retrieved."));
226 					break;
227 				}
228 			}
229 
230 			if (_LaunchFinishScript(targetDirectory) != B_OK) {
231 				_SetStatusMessage(
232 					B_TRANSLATE("Error writing boot sector."));
233 				break;
234 			}
235 			_SetStatusMessage(
236 				B_TRANSLATE("Boot sector successfully written."));
237 		}
238 		default:
239 			BLooper::MessageReceived(message);
240 	}
241 }
242 
243 
244 
245 
246 void
247 WorkerThread::ScanDisksPartitions(BMenu *srcMenu, BMenu *targetMenu)
248 {
249 	// NOTE: This is actually executed in the window thread.
250 	BDiskDevice device;
251 	BPartition *partition = NULL;
252 
253 	printf("\nScanDisksPartitions source partitions begin\n");
254 	SourceVisitor srcVisitor(srcMenu);
255 	fDDRoster.VisitEachMountedPartition(&srcVisitor, &device, &partition);
256 
257 	printf("\nScanDisksPartitions target partitions begin\n");
258 	TargetVisitor targetVisitor(targetMenu);
259 	fDDRoster.VisitEachPartition(&targetVisitor, &device, &partition);
260 }
261 
262 
263 void
264 WorkerThread::SetPackagesList(BList *list)
265 {
266 	// Executed in window thread.
267 	BAutolock _(this);
268 
269 	delete fPackages;
270 	fPackages = list;
271 }
272 
273 
274 void
275 WorkerThread::StartInstall(partition_id sourcePartitionID,
276 	partition_id targetPartitionID)
277 {
278 	// Executed in window thread.
279 	BMessage message(MSG_START_INSTALLING);
280 	message.AddInt32("source", sourcePartitionID);
281 	message.AddInt32("target", targetPartitionID);
282 
283 	PostMessage(&message, this);
284 }
285 
286 
287 void
288 WorkerThread::WriteBootSector(BMenu* targetMenu)
289 {
290 	// Executed in window thread.
291 	CALLED();
292 
293 	PartitionMenuItem* item = (PartitionMenuItem*)targetMenu->FindMarked();
294 	if (item == NULL) {
295 		ERR("bad menu items\n");
296 		return;
297 	}
298 
299 	BMessage message(MSG_WRITE_BOOT_SECTOR);
300 	message.AddInt32("id", item->ID());
301 	PostMessage(&message, this);
302 }
303 
304 
305 // #pragma mark -
306 
307 
308 status_t
309 WorkerThread::_LaunchInitScript(BPath &path)
310 {
311 	BPath bootPath;
312 	find_directory(B_BEOS_BOOT_DIRECTORY, &bootPath);
313 	BString command("/bin/sh ");
314 	command += bootPath.Path();
315 	command += "/InstallerInitScript ";
316 	command += "\"";
317 	command += path.Path();
318 	command += "\"";
319 	_SetStatusMessage(B_TRANSLATE("Starting installation."));
320 	return system(command.String());
321 }
322 
323 
324 status_t
325 WorkerThread::_LaunchFinishScript(BPath &path)
326 {
327 	BPath bootPath;
328 	find_directory(B_BEOS_BOOT_DIRECTORY, &bootPath);
329 	BString command("/bin/sh ");
330 	command += bootPath.Path();
331 	command += "/InstallerFinishScript ";
332 	command += "\"";
333 	command += path.Path();
334 	command += "\"";
335 	_SetStatusMessage(B_TRANSLATE("Finishing installation."));
336 	return system(command.String());
337 }
338 
339 
340 status_t
341 WorkerThread::_PerformInstall(partition_id sourcePartitionID,
342 	partition_id targetPartitionID)
343 {
344 	CALLED();
345 
346 	BPath targetDirectory;
347 	BPath srcDirectory;
348 	BPath trashPath;
349 	BPath testPath;
350 	BDirectory targetDir;
351 	BDiskDevice device;
352 	BPartition* partition;
353 	BVolume targetVolume;
354 	status_t err = B_OK;
355 	int32 entries = 0;
356 	entry_ref testRef;
357 	const char* mountError = B_TRANSLATE("The disk can't be mounted. Please "
358 		"choose a different disk.");
359 
360 	if (sourcePartitionID < 0 || targetPartitionID < 0) {
361 		ERR("bad source or target partition ID\n");
362 		return _InstallationError(err);
363 	}
364 
365 	// check if target is initialized
366 	// ask if init or mount as is
367 	if (fDDRoster.GetPartitionWithID(targetPartitionID, &device,
368 			&partition) == B_OK) {
369 		if (!partition->IsMounted()) {
370 			if ((err = partition->Mount()) < B_OK) {
371 				_SetStatusMessage(mountError);
372 				ERR("BPartition::Mount");
373 				return _InstallationError(err);
374 			}
375 		}
376 		if ((err = partition->GetVolume(&targetVolume)) != B_OK) {
377 			ERR("BPartition::GetVolume");
378 			return _InstallationError(err);
379 		}
380 		if ((err = partition->GetMountPoint(&targetDirectory)) != B_OK) {
381 			ERR("BPartition::GetMountPoint");
382 			return _InstallationError(err);
383 		}
384 	} else if (fDDRoster.GetDeviceWithID(targetPartitionID, &device) == B_OK) {
385 		if (!device.IsMounted()) {
386 			if ((err = device.Mount()) < B_OK) {
387 				_SetStatusMessage(mountError);
388 				ERR("BDiskDevice::Mount");
389 				return _InstallationError(err);
390 			}
391 		}
392 		if ((err = device.GetVolume(&targetVolume)) != B_OK) {
393 			ERR("BDiskDevice::GetVolume");
394 			return _InstallationError(err);
395 		}
396 		if ((err = device.GetMountPoint(&targetDirectory)) != B_OK) {
397 			ERR("BDiskDevice::GetMountPoint");
398 			return _InstallationError(err);
399 		}
400 	} else
401 		return _InstallationError(err);  // shouldn't happen
402 
403 	// check if target has enough space
404 	if (fSpaceRequired > 0 && targetVolume.FreeBytes() < fSpaceRequired) {
405 		BAlert* alert = new BAlert("", B_TRANSLATE("The destination disk may "
406 			"not have enough space. Try choosing a different disk or choose "
407 			"to not install optional items."),
408 			B_TRANSLATE("Try installing anyway"), B_TRANSLATE("Cancel"), 0,
409 			B_WIDTH_AS_USUAL, B_STOP_ALERT);
410 		alert->SetShortcut(1, B_ESCAPE);
411 		if (alert->Go() != 0)
412 			return _InstallationError(err);
413 	}
414 
415 	if (fDDRoster.GetPartitionWithID(sourcePartitionID, &device, &partition)
416 			== B_OK) {
417 		if ((err = partition->GetMountPoint(&srcDirectory)) != B_OK) {
418 			ERR("BPartition::GetMountPoint");
419 			return _InstallationError(err);
420 		}
421 	} else if (fDDRoster.GetDeviceWithID(sourcePartitionID, &device) == B_OK) {
422 		if ((err = device.GetMountPoint(&srcDirectory)) != B_OK) {
423 			ERR("BDiskDevice::GetMountPoint");
424 			return _InstallationError(err);
425 		}
426 	} else
427 		return _InstallationError(err); // shouldn't happen
428 
429 	// check not installing on itself
430 	if (strcmp(srcDirectory.Path(), targetDirectory.Path()) == 0) {
431 		_SetStatusMessage(B_TRANSLATE("You can't install the contents of a "
432 			"disk onto itself. Please choose a different disk."));
433 		return _InstallationError(err);
434 	}
435 
436 	// check not installing on boot volume
437 	if (strncmp(BOOT_PATH, targetDirectory.Path(), strlen(BOOT_PATH)) == 0) {
438 		BAlert* alert = new BAlert("", B_TRANSLATE("Are you sure you want to "
439 			"install onto the current boot disk? The Installer will have to "
440 			"reboot your machine if you proceed."), B_TRANSLATE("OK"),
441 			B_TRANSLATE("Cancel"), 0, B_WIDTH_AS_USUAL, B_STOP_ALERT);
442 		alert->SetShortcut(1, B_ESCAPE);
443 		if (alert->Go() != 0) {
444 			_SetStatusMessage("Installation stopped.");
445 			return _InstallationError(err);
446 		}
447 	}
448 
449 	// check if target volume's trash dir has anything in it
450 	// (target volume w/ only an empty trash dir is considered
451 	// an empty volume)
452 	if (find_directory(B_TRASH_DIRECTORY, &trashPath, false,
453 		&targetVolume) == B_OK && targetDir.SetTo(trashPath.Path()) == B_OK) {
454 			while (targetDir.GetNextRef(&testRef) == B_OK) {
455 				// Something in the Trash
456 				entries++;
457 				break;
458 			}
459 	}
460 
461 	targetDir.SetTo(targetDirectory.Path());
462 
463 	// check if target volume otherwise has any entries
464 	while (entries == 0 && targetDir.GetNextRef(&testRef) == B_OK) {
465 		if (testPath.SetTo(&testRef) == B_OK && testPath != trashPath)
466 			entries++;
467 	}
468 
469 	if (entries != 0) {
470 		BAlert* alert = new BAlert("", B_TRANSLATE("The target volume is not "
471 			"empty. Are you sure you want to install anyway?\n\nNote: The "
472 			"'system' folder will be a clean copy from the source volume but "
473 			"will retain its settings folder, all other folders will be "
474 			"merged, whereas files and links that exist on both the source "
475 			"and target volume will be overwritten with the source volume "
476 			"version."),
477 			B_TRANSLATE("Install anyway"), B_TRANSLATE("Cancel"), 0,
478 			B_WIDTH_AS_USUAL, B_STOP_ALERT);
479 		alert->SetShortcut(1, B_ESCAPE);
480 		if (alert->Go() != 0) {
481 		// TODO: Would be cool to offer the option here to clean additional
482 		// folders at the user's choice.
483 			return _InstallationError(B_CANCELED);
484 		}
485 	}
486 
487 	// Begin actual installation
488 
489 	ProgressReporter reporter(fOwner, new BMessage(MSG_STATUS_MESSAGE));
490 	EntryFilter entryFilter(srcDirectory.Path());
491 	CopyEngine engine(&reporter, &entryFilter);
492 	BList unzipEngines;
493 
494 	err = _LaunchInitScript(targetDirectory);
495 	if (err != B_OK)
496 		return _InstallationError(err);
497 
498 	// Create the default indices which should always be present on a proper
499 	// boot volume. We don't care if the source volume does not have them.
500 	// After all, the user might be re-installing to another drive and may
501 	// want problems fixed along the way...
502 	err = _CreateDefaultIndices(targetDirectory);
503 	if (err != B_OK)
504 		return _InstallationError(err);
505 	// Mirror all the indices which are present on the source volume onto
506 	// the target volume.
507 	err = _MirrorIndices(srcDirectory, targetDirectory);
508 	if (err != B_OK)
509 		return _InstallationError(err);
510 
511 	// Let the engine collect information for the progress bar later on
512 	engine.ResetTargets(srcDirectory.Path());
513 	err = engine.CollectTargets(srcDirectory.Path(), fCancelSemaphore);
514 	if (err != B_OK)
515 		return _InstallationError(err);
516 
517 	// Collect selected packages also
518 	if (fPackages) {
519 		BPath pkgRootDir(srcDirectory.Path(), kPackagesDirectoryPath);
520 		int32 count = fPackages->CountItems();
521 		for (int32 i = 0; i < count; i++) {
522 			Package *p = static_cast<Package*>(fPackages->ItemAt(i));
523 			BPath packageDir(pkgRootDir.Path(), p->Folder());
524 			err = engine.CollectTargets(packageDir.Path(), fCancelSemaphore);
525 			if (err != B_OK)
526 				return _InstallationError(err);
527 		}
528 	}
529 
530 	// collect information about all zip packages
531 	err = _ProcessZipPackages(srcDirectory.Path(), targetDirectory.Path(),
532 		&reporter, unzipEngines);
533 	if (err != B_OK)
534 		return _InstallationError(err);
535 
536 	reporter.StartTimer();
537 
538 	// copy source volume
539 	err = engine.CopyFolder(srcDirectory.Path(), targetDirectory.Path(),
540 		fCancelSemaphore);
541 	if (err != B_OK)
542 		return _InstallationError(err);
543 
544 	// copy selected packages
545 	if (fPackages) {
546 		BPath pkgRootDir(srcDirectory.Path(), kPackagesDirectoryPath);
547 		int32 count = fPackages->CountItems();
548 		for (int32 i = 0; i < count; i++) {
549 			Package *p = static_cast<Package*>(fPackages->ItemAt(i));
550 			BPath packageDir(pkgRootDir.Path(), p->Folder());
551 			err = engine.CopyFolder(packageDir.Path(), targetDirectory.Path(),
552 				fCancelSemaphore);
553 			if (err != B_OK)
554 				return _InstallationError(err);
555 		}
556 	}
557 
558 	// Extract all zip packages. If an error occured, delete the rest of
559 	// the engines, but stop extracting.
560 	for (int32 i = 0; i < unzipEngines.CountItems(); i++) {
561 		UnzipEngine* engine = reinterpret_cast<UnzipEngine*>(
562 			unzipEngines.ItemAtFast(i));
563 		if (err == B_OK)
564 			err = engine->UnzipPackage();
565 		delete engine;
566 	}
567 	if (err != B_OK)
568 		return _InstallationError(err);
569 
570 	err = _LaunchFinishScript(targetDirectory);
571 	if (err != B_OK)
572 		return _InstallationError(err);
573 
574 	fOwner.SendMessage(MSG_INSTALL_FINISHED);
575 	return B_OK;
576 }
577 
578 
579 status_t
580 WorkerThread::_InstallationError(status_t error)
581 {
582 	BMessage statusMessage(MSG_RESET);
583 	if (error == B_CANCELED)
584 		_SetStatusMessage(B_TRANSLATE("Installation canceled."));
585 	else
586 		statusMessage.AddInt32("error", error);
587 	ERR("_PerformInstall failed");
588 	fOwner.SendMessage(&statusMessage);
589 	return error;
590 }
591 
592 
593 status_t
594 WorkerThread::_MirrorIndices(const BPath& sourceDirectory,
595 	const BPath& targetDirectory) const
596 {
597 	dev_t sourceDevice = dev_for_path(sourceDirectory.Path());
598 	if (sourceDevice < 0)
599 		return (status_t)sourceDevice;
600 	dev_t targetDevice = dev_for_path(targetDirectory.Path());
601 	if (targetDevice < 0)
602 		return (status_t)targetDevice;
603 	DIR* indices = fs_open_index_dir(sourceDevice);
604 	if (indices == NULL) {
605 		printf("%s: fs_open_index_dir(): (%d) %s\n", sourceDirectory.Path(),
606 			errno, strerror(errno));
607 		// Opening the index directory will fail for example on ISO-Live
608 		// CDs. The default indices have already been created earlier, so
609 		// we simply bail.
610 		return B_OK;
611 	}
612 	while (dirent* index = fs_read_index_dir(indices)) {
613 		if (strcmp(index->d_name, "name") == 0
614 			|| strcmp(index->d_name, "size") == 0
615 			|| strcmp(index->d_name, "last_modified") == 0) {
616 			continue;
617 		}
618 
619 		index_info info;
620 		if (fs_stat_index(sourceDevice, index->d_name, &info) != B_OK) {
621 			printf("Failed to mirror index %s: fs_stat_index(): (%d) %s\n",
622 				index->d_name, errno, strerror(errno));
623 			continue;
624 		}
625 
626 		uint32 flags = 0;
627 			// Flags are always 0 for the moment.
628 		if (fs_create_index(targetDevice, index->d_name, info.type, flags)
629 			!= B_OK) {
630 			if (errno == B_FILE_EXISTS)
631 				continue;
632 			printf("Failed to mirror index %s: fs_create_index(): (%d) %s\n",
633 				index->d_name, errno, strerror(errno));
634 			continue;
635 		}
636 	}
637 	fs_close_index_dir(indices);
638 	return B_OK;
639 }
640 
641 
642 status_t
643 WorkerThread::_CreateDefaultIndices(const BPath& targetDirectory) const
644 {
645 	dev_t targetDevice = dev_for_path(targetDirectory.Path());
646 	if (targetDevice < 0)
647 		return (status_t)targetDevice;
648 
649 	struct IndexInfo {
650 		const char* name;
651 		uint32_t	type;
652 	};
653 
654 	const IndexInfo defaultIndices[] = {
655 		{ "BEOS:APP_SIG", B_STRING_TYPE },
656 		{ "BEOS:LOCALE_LANGUAGE", B_STRING_TYPE },
657 		{ "BEOS:LOCALE_SIGNATURE", B_STRING_TYPE },
658 		{ "_trk/qrylastchange", B_INT32_TYPE },
659 		{ "_trk/recentQuery", B_INT32_TYPE },
660 		{ "be:deskbar_item_status", B_STRING_TYPE }
661 	};
662 
663 	uint32 flags = 0;
664 		// Flags are always 0 for the moment.
665 
666 	for (uint32 i = 0; i < sizeof(defaultIndices) / sizeof(IndexInfo); i++) {
667 		const IndexInfo& info = defaultIndices[i];
668 		if (fs_create_index(targetDevice, info.name, info.type, flags)
669 			!= B_OK) {
670 			if (errno == B_FILE_EXISTS)
671 				continue;
672 			printf("Failed to create index %s: fs_create_index(): (%d) %s\n",
673 				info.name, errno, strerror(errno));
674 			return errno;
675 		}
676 	}
677 
678 	return B_OK;
679 }
680 
681 
682 status_t
683 WorkerThread::_ProcessZipPackages(const char* sourcePath,
684 	const char* targetPath, ProgressReporter* reporter, BList& unzipEngines)
685 {
686 	// TODO: Put those in the optional packages list view
687 	// TODO: Implement mechanism to handle dependencies between these
688 	// packages. (Selecting one will auto-select others.)
689 	BPath pkgRootDir(sourcePath, kPackagesDirectoryPath);
690 	BDirectory directory(pkgRootDir.Path());
691 	BEntry entry;
692 	while (directory.GetNextEntry(&entry) == B_OK) {
693 		char name[B_FILE_NAME_LENGTH];
694 		if (entry.GetName(name) != B_OK)
695 			continue;
696 		int nameLength = strlen(name);
697 		if (nameLength <= 0)
698 			continue;
699 		char* nameExtension = name + nameLength - 4;
700 		if (strcasecmp(nameExtension, ".zip") != 0)
701 			continue;
702 		printf("found .zip package: %s\n", name);
703 
704 		UnzipEngine* unzipEngine = new(std::nothrow) UnzipEngine(reporter,
705 			fCancelSemaphore);
706 		if (unzipEngine == NULL || !unzipEngines.AddItem(unzipEngine)) {
707 			delete unzipEngine;
708 			return B_NO_MEMORY;
709 		}
710 		BPath path;
711 		entry.GetPath(&path);
712 		status_t ret = unzipEngine->SetTo(path.Path(), targetPath);
713 		if (ret != B_OK)
714 			return ret;
715 
716 		reporter->AddItems(unzipEngine->ItemsToUncompress(),
717 			unzipEngine->BytesToUncompress());
718 	}
719 
720 	return B_OK;
721 }
722 
723 
724 void
725 WorkerThread::_SetStatusMessage(const char *status)
726 {
727 	BMessage msg(MSG_STATUS_MESSAGE);
728 	msg.AddString("status", status);
729 	fOwner.SendMessage(&msg);
730 }
731 
732 
733 static void
734 make_partition_label(BPartition* partition, char* label, char* menuLabel,
735 	bool showContentType)
736 {
737 	char size[20];
738 	string_for_size(partition->Size(), size, sizeof(size));
739 
740 	BPath path;
741 	partition->GetPath(&path);
742 
743 	if (showContentType) {
744 		const char* type = partition->ContentType();
745 		if (type == NULL)
746 			type = B_TRANSLATE_COMMENT("Unknown Type", "Partition content type");
747 
748 		sprintf(label, "%s - %s [%s] (%s)", partition->ContentName(), size,
749 			path.Path(), type);
750 	} else {
751 		sprintf(label, "%s - %s [%s]", partition->ContentName(), size,
752 			path.Path());
753 	}
754 
755 	sprintf(menuLabel, "%s - %s", partition->ContentName(), size);
756 }
757 
758 
759 // #pragma mark - SourceVisitor
760 
761 
762 SourceVisitor::SourceVisitor(BMenu *menu)
763 	: fMenu(menu)
764 {
765 }
766 
767 bool
768 SourceVisitor::Visit(BDiskDevice *device)
769 {
770 	return Visit(device, 0);
771 }
772 
773 
774 bool
775 SourceVisitor::Visit(BPartition *partition, int32 level)
776 {
777 	BPath path;
778 	printf("SourceVisitor::Visit(BPartition *) : %s '%s'\n",
779 		(partition->GetPath(&path) == B_OK) ? path.Path() : "",
780 		partition->ContentName());
781 
782 	if (partition->ContentType() == NULL)
783 		return false;
784 
785 	bool isBootPartition = false;
786 	if (partition->IsMounted()) {
787 		BPath mountPoint;
788 		if (partition->GetMountPoint(&mountPoint) != B_OK)
789 			return false;
790 		isBootPartition = strcmp(BOOT_PATH, mountPoint.Path()) == 0;
791 	}
792 
793 	if (!isBootPartition
794 		&& strcmp(partition->ContentType(), kPartitionTypeBFS) != 0) {
795 		// Except only BFS partitions, except this is the boot partition
796 		// (ISO9660 with write overlay for example).
797 		return false;
798 	}
799 
800 	// TODO: We could probably check if this volume contains
801 	// the Haiku kernel or something. Does it make sense to "install"
802 	// from your BFS volume containing the music collection?
803 	// TODO: Then the check for BFS could also be removed above.
804 
805 	char label[255];
806 	char menuLabel[255];
807 	make_partition_label(partition, label, menuLabel, false);
808 	PartitionMenuItem* item = new PartitionMenuItem(partition->ContentName(),
809 		label, menuLabel, new BMessage(SOURCE_PARTITION), partition->ID());
810 	item->SetMarked(isBootPartition);
811 	fMenu->AddItem(item);
812 	return false;
813 }
814 
815 
816 // #pragma mark - TargetVisitor
817 
818 
819 TargetVisitor::TargetVisitor(BMenu *menu)
820 	: fMenu(menu)
821 {
822 }
823 
824 
825 bool
826 TargetVisitor::Visit(BDiskDevice *device)
827 {
828 	if (device->IsReadOnlyMedia())
829 		return false;
830 	return Visit(device, 0);
831 }
832 
833 
834 bool
835 TargetVisitor::Visit(BPartition *partition, int32 level)
836 {
837 	BPath path;
838 	if (partition->GetPath(&path) == B_OK)
839 		printf("TargetVisitor::Visit(BPartition *) : %s\n", path.Path());
840 	printf("TargetVisitor::Visit(BPartition *) : %s\n",
841 		partition->ContentName());
842 
843 	if (partition->ContentSize() < 20 * 1024 * 1024) {
844 		// reject partitions which are too small anyway
845 		// TODO: Could depend on the source size
846 		printf("  too small\n");
847 		return false;
848 	}
849 
850 	if (partition->CountChildren() > 0) {
851 		// Looks like an extended partition, or the device itself.
852 		// Do not accept this as target...
853 		printf("  no leaf partition\n");
854 		return false;
855 	}
856 
857 	// TODO: After running DriveSetup and doing another scan, it would
858 	// be great to pick the partition which just appeared!
859 
860 	bool isBootPartition = false;
861 	if (partition->IsMounted()) {
862 		BPath mountPoint;
863 		partition->GetMountPoint(&mountPoint);
864 		isBootPartition = strcmp(BOOT_PATH, mountPoint.Path()) == 0;
865 	}
866 
867 	// Only non-boot BFS partitions are valid targets, but we want to display the
868 	// other partitions as well, in order not to irritate the user.
869 	bool isValidTarget = isBootPartition == false
870 		&& partition->ContentType() != NULL
871 		&& strcmp(partition->ContentType(), kPartitionTypeBFS) == 0;
872 
873 	char label[255];
874 	char menuLabel[255];
875 	make_partition_label(partition, label, menuLabel, !isValidTarget);
876 	PartitionMenuItem* item = new PartitionMenuItem(partition->ContentName(),
877 		label, menuLabel, new BMessage(TARGET_PARTITION), partition->ID());
878 
879 	item->SetIsValidTarget(isValidTarget);
880 
881 
882 	fMenu->AddItem(item);
883 	return false;
884 }
885 
886