xref: /haiku/src/apps/installer/WorkerThread.cpp (revision ae0a10cad3999b13cbfa47a3d947a5219d2d90f4)
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) 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 private:
141 	typedef std::set<std::string> StringSet;
142 
143 			StringSet			fIgnorePaths;
144 			StringSet			fPackageFSRootPaths;
145 			dev_t				fSourceDevice;
146 };
147 
148 
149 // #pragma mark - WorkerThread
150 
151 
152 WorkerThread::WorkerThread(const BMessenger& owner)
153 	:
154 	BLooper("copy_engine"),
155 	fOwner(owner),
156 	fPackages(NULL),
157 	fSpaceRequired(0),
158 	fCancelSemaphore(-1)
159 {
160 	Run();
161 }
162 
163 
164 void
165 WorkerThread::MessageReceived(BMessage* message)
166 {
167 	CALLED();
168 
169 	switch (message->what) {
170 		case MSG_START_INSTALLING:
171 			_PerformInstall(message->GetInt32("source", -1),
172 				message->GetInt32("target", -1));
173 			break;
174 
175 		case MSG_WRITE_BOOT_SECTOR:
176 		{
177 			int32 id;
178 			if (message->FindInt32("id", &id) != B_OK) {
179 				_SetStatusMessage(B_TRANSLATE("Boot sector not written "
180 					"because of an internal error."));
181 				break;
182 			}
183 
184 			// TODO: Refactor with _PerformInstall()
185 			BPath targetDirectory;
186 			BDiskDevice device;
187 			BPartition* partition;
188 
189 			if (fDDRoster.GetPartitionWithID(id, &device, &partition) == B_OK) {
190 				if (!partition->IsMounted()) {
191 					if (partition->Mount() < B_OK) {
192 						_SetStatusMessage(B_TRANSLATE("The partition can't be "
193 							"mounted. Please choose a different partition."));
194 						break;
195 					}
196 				}
197 				if (partition->GetMountPoint(&targetDirectory) != B_OK) {
198 					_SetStatusMessage(B_TRANSLATE("The mount point could not "
199 						"be retrieved."));
200 					break;
201 				}
202 			} else if (fDDRoster.GetDeviceWithID(id, &device) == B_OK) {
203 				if (!device.IsMounted()) {
204 					if (device.Mount() < B_OK) {
205 						_SetStatusMessage(B_TRANSLATE("The disk can't be "
206 							"mounted. Please choose a different disk."));
207 						break;
208 					}
209 				}
210 				if (device.GetMountPoint(&targetDirectory) != B_OK) {
211 					_SetStatusMessage(B_TRANSLATE("The mount point could not "
212 						"be retrieved."));
213 					break;
214 				}
215 			}
216 
217 			if (_WriteBootSector(targetDirectory) != B_OK) {
218 				_SetStatusMessage(
219 					B_TRANSLATE("Error writing boot sector."));
220 				break;
221 			}
222 			_SetStatusMessage(
223 				B_TRANSLATE("Boot sector successfully written."));
224 		}
225 		default:
226 			BLooper::MessageReceived(message);
227 	}
228 }
229 
230 
231 
232 
233 void
234 WorkerThread::ScanDisksPartitions(BMenu *srcMenu, BMenu *targetMenu)
235 {
236 	// NOTE: This is actually executed in the window thread.
237 	BDiskDevice device;
238 	BPartition *partition = NULL;
239 
240 	SourceVisitor srcVisitor(srcMenu);
241 	fDDRoster.VisitEachMountedPartition(&srcVisitor, &device, &partition);
242 
243 	TargetVisitor targetVisitor(targetMenu);
244 	fDDRoster.VisitEachPartition(&targetVisitor, &device, &partition);
245 }
246 
247 
248 void
249 WorkerThread::SetPackagesList(BList *list)
250 {
251 	// Executed in window thread.
252 	BAutolock _(this);
253 
254 	delete fPackages;
255 	fPackages = list;
256 }
257 
258 
259 void
260 WorkerThread::StartInstall(partition_id sourcePartitionID,
261 	partition_id targetPartitionID)
262 {
263 	// Executed in window thread.
264 	BMessage message(MSG_START_INSTALLING);
265 	message.AddInt32("source", sourcePartitionID);
266 	message.AddInt32("target", targetPartitionID);
267 
268 	PostMessage(&message, this);
269 }
270 
271 
272 void
273 WorkerThread::WriteBootSector(BMenu* targetMenu)
274 {
275 	// Executed in window thread.
276 	CALLED();
277 
278 	PartitionMenuItem* item = (PartitionMenuItem*)targetMenu->FindMarked();
279 	if (item == NULL) {
280 		ERR("bad menu items\n");
281 		return;
282 	}
283 
284 	BMessage message(MSG_WRITE_BOOT_SECTOR);
285 	message.AddInt32("id", item->ID());
286 	PostMessage(&message, this);
287 }
288 
289 
290 // #pragma mark -
291 
292 
293 status_t
294 WorkerThread::_WriteBootSector(BPath &path)
295 {
296 	BPath bootPath;
297 	find_directory(B_BEOS_BOOT_DIRECTORY, &bootPath);
298 	BString command;
299 	command.SetToFormat("makebootable \"%s\"", path.Path());
300 	_SetStatusMessage(B_TRANSLATE("Writing bootsector."));
301 	return system(command.String());
302 }
303 
304 
305 status_t
306 WorkerThread::_LaunchFinishScript(BPath &path)
307 {
308 	_SetStatusMessage(B_TRANSLATE("Finishing installation."));
309 
310 	BString command;
311 	command.SetToFormat("mkdir -p \"%s/system/cache/tmp\"", path.Path());
312 	if (system(command.String()) != 0)
313 		return B_ERROR;
314 
315 	// Ask for first boot processing of all the packages copied into the new
316 	// installation, since by just copying them the normal package processing
317 	// isn't done.  package_daemon will detect the magic file and do it.
318 	command.SetToFormat("echo 'First Boot written by Installer.' > "
319 		"\"%s/system/packages/administrative/FirstBootProcessingNeeded\"",
320 		path.Path());
321 	if (system(command.String()) != 0)
322 		return B_ERROR;
323 
324 	command.SetToFormat("rm -f \"%s/home/Desktop/Installer\"", path.Path());
325 	return system(command.String());
326 }
327 
328 
329 status_t
330 WorkerThread::_PerformInstall(partition_id sourcePartitionID,
331 	partition_id targetPartitionID)
332 {
333 	CALLED();
334 
335 	BPath targetDirectory;
336 	BPath srcDirectory;
337 	BPath trashPath;
338 	BPath testPath;
339 	BDirectory targetDir;
340 	BDiskDevice device;
341 	BPartition* partition;
342 	BVolume targetVolume;
343 	status_t err = B_OK;
344 	int32 entries = 0;
345 	entry_ref testRef;
346 	const char* mountError = B_TRANSLATE("The disk can't be mounted. Please "
347 		"choose a different disk.");
348 
349 	if (sourcePartitionID < 0 || targetPartitionID < 0) {
350 		ERR("bad source or target partition ID\n");
351 		return _InstallationError(err);
352 	}
353 
354 	// check if target is initialized
355 	// ask if init or mount as is
356 	if (fDDRoster.GetPartitionWithID(targetPartitionID, &device,
357 			&partition) == B_OK) {
358 		if (!partition->IsMounted()) {
359 			if ((err = partition->Mount()) < B_OK) {
360 				_SetStatusMessage(mountError);
361 				ERR("BPartition::Mount");
362 				return _InstallationError(err);
363 			}
364 		}
365 		if ((err = partition->GetVolume(&targetVolume)) != B_OK) {
366 			ERR("BPartition::GetVolume");
367 			return _InstallationError(err);
368 		}
369 		if ((err = partition->GetMountPoint(&targetDirectory)) != B_OK) {
370 			ERR("BPartition::GetMountPoint");
371 			return _InstallationError(err);
372 		}
373 	} else if (fDDRoster.GetDeviceWithID(targetPartitionID, &device) == B_OK) {
374 		if (!device.IsMounted()) {
375 			if ((err = device.Mount()) < B_OK) {
376 				_SetStatusMessage(mountError);
377 				ERR("BDiskDevice::Mount");
378 				return _InstallationError(err);
379 			}
380 		}
381 		if ((err = device.GetVolume(&targetVolume)) != B_OK) {
382 			ERR("BDiskDevice::GetVolume");
383 			return _InstallationError(err);
384 		}
385 		if ((err = device.GetMountPoint(&targetDirectory)) != B_OK) {
386 			ERR("BDiskDevice::GetMountPoint");
387 			return _InstallationError(err);
388 		}
389 	} else
390 		return _InstallationError(err);  // shouldn't happen
391 
392 	// check if target has enough space
393 	if (fSpaceRequired > 0 && targetVolume.FreeBytes() < fSpaceRequired) {
394 		BAlert* alert = new BAlert("", B_TRANSLATE("The destination disk may "
395 			"not have enough space. Try choosing a different disk or choose "
396 			"to not install optional items."),
397 			B_TRANSLATE("Try installing anyway"), B_TRANSLATE("Cancel"), 0,
398 			B_WIDTH_AS_USUAL, B_STOP_ALERT);
399 		alert->SetShortcut(1, B_ESCAPE);
400 		if (alert->Go() != 0)
401 			return _InstallationError(err);
402 	}
403 
404 	if (fDDRoster.GetPartitionWithID(sourcePartitionID, &device, &partition)
405 			== B_OK) {
406 		if ((err = partition->GetMountPoint(&srcDirectory)) != B_OK) {
407 			ERR("BPartition::GetMountPoint");
408 			return _InstallationError(err);
409 		}
410 	} else if (fDDRoster.GetDeviceWithID(sourcePartitionID, &device) == B_OK) {
411 		if ((err = device.GetMountPoint(&srcDirectory)) != B_OK) {
412 			ERR("BDiskDevice::GetMountPoint");
413 			return _InstallationError(err);
414 		}
415 	} else
416 		return _InstallationError(err); // shouldn't happen
417 
418 	// check not installing on itself
419 	if (strcmp(srcDirectory.Path(), targetDirectory.Path()) == 0) {
420 		_SetStatusMessage(B_TRANSLATE("You can't install the contents of a "
421 			"disk onto itself. Please choose a different disk."));
422 		return _InstallationError(err);
423 	}
424 
425 	// check not installing on boot volume
426 	if (strncmp(BOOT_PATH, targetDirectory.Path(), strlen(BOOT_PATH)) == 0) {
427 		BAlert* alert = new BAlert("", B_TRANSLATE("Are you sure you want to "
428 			"install onto the current boot disk? The Installer will have to "
429 			"reboot your machine if you proceed."), B_TRANSLATE("OK"),
430 			B_TRANSLATE("Cancel"), 0, B_WIDTH_AS_USUAL, B_STOP_ALERT);
431 		alert->SetShortcut(1, B_ESCAPE);
432 		if (alert->Go() != 0) {
433 			_SetStatusMessage("Installation stopped.");
434 			return _InstallationError(err);
435 		}
436 	}
437 
438 	// check if target volume's trash dir has anything in it
439 	// (target volume w/ only an empty trash dir is considered
440 	// an empty volume)
441 	if (find_directory(B_TRASH_DIRECTORY, &trashPath, false,
442 		&targetVolume) == B_OK && targetDir.SetTo(trashPath.Path()) == B_OK) {
443 			while (targetDir.GetNextRef(&testRef) == B_OK) {
444 				// Something in the Trash
445 				entries++;
446 				break;
447 			}
448 	}
449 
450 	targetDir.SetTo(targetDirectory.Path());
451 
452 	// check if target volume otherwise has any entries
453 	while (entries == 0 && targetDir.GetNextRef(&testRef) == B_OK) {
454 		if (testPath.SetTo(&testRef) == B_OK && testPath != trashPath)
455 			entries++;
456 	}
457 
458 	if (entries != 0) {
459 		BAlert* alert = new BAlert("", B_TRANSLATE("The target volume is not "
460 			"empty. If it already contains a Haiku installation, it will be "
461 			"overwritten. This will remove all installed software.\n\n"
462 			"If you want to upgrade your system without removing installed "
463 			"software, see the Haiku User Guide's topic on the application "
464 			"\"SoftwareUpdater\" for update instructions.\n\n"
465 			"Are you sure you want to continue the installation?"),
466 			B_TRANSLATE("Install anyway"), B_TRANSLATE("Cancel"), 0,
467 			B_WIDTH_AS_USUAL, B_STOP_ALERT);
468 		alert->SetShortcut(1, B_ESCAPE);
469 		if (alert->Go() != 0) {
470 		// TODO: Would be cool to offer the option here to clean additional
471 		// folders at the user's choice.
472 			return _InstallationError(B_CANCELED);
473 		}
474 		err = _PrepareCleanInstall(targetDirectory);
475 		if (err != B_OK)
476 			return _InstallationError(err);
477 	}
478 
479 	// Begin actual installation
480 
481 	ProgressReporter reporter(fOwner, new BMessage(MSG_STATUS_MESSAGE));
482 	EntryFilter entryFilter(srcDirectory.Path());
483 	CopyEngine engine(&reporter, &entryFilter);
484 	BList unzipEngines;
485 
486 	// Create the default indices which should always be present on a proper
487 	// boot volume. We don't care if the source volume does not have them.
488 	// After all, the user might be re-installing to another drive and may
489 	// want problems fixed along the way...
490 	err = _CreateDefaultIndices(targetDirectory);
491 	if (err != B_OK)
492 		return _InstallationError(err);
493 	// Mirror all the indices which are present on the source volume onto
494 	// the target volume.
495 	err = _MirrorIndices(srcDirectory, targetDirectory);
496 	if (err != B_OK)
497 		return _InstallationError(err);
498 
499 	// Let the engine collect information for the progress bar later on
500 	engine.ResetTargets(srcDirectory.Path());
501 	err = engine.CollectTargets(srcDirectory.Path(), fCancelSemaphore);
502 	if (err != B_OK)
503 		return _InstallationError(err);
504 
505 	// Collect selected packages also
506 	if (fPackages) {
507 		int32 count = fPackages->CountItems();
508 		for (int32 i = 0; i < count; i++) {
509 			Package *p = static_cast<Package*>(fPackages->ItemAt(i));
510 			const BPath& pkgPath = p->Path();
511 			err = pkgPath.InitCheck();
512 			if (err != B_OK)
513 				return _InstallationError(err);
514 			err = engine.CollectTargets(pkgPath.Path(), fCancelSemaphore);
515 			if (err != B_OK)
516 				return _InstallationError(err);
517 		}
518 	}
519 
520 	// collect information about all zip packages
521 	err = _ProcessZipPackages(srcDirectory.Path(), targetDirectory.Path(),
522 		&reporter, unzipEngines);
523 	if (err != B_OK)
524 		return _InstallationError(err);
525 
526 	reporter.StartTimer();
527 
528 	// copy source volume
529 	err = engine.Copy(srcDirectory.Path(), targetDirectory.Path(),
530 		fCancelSemaphore);
531 	if (err != B_OK)
532 		return _InstallationError(err);
533 
534 	// copy selected packages
535 	if (fPackages) {
536 		int32 count = fPackages->CountItems();
537 		// FIXME: find_directory doesn't return the folder in the target volume,
538 		// so we are hard coding this for now.
539 		BPath targetPkgDir(targetDirectory.Path(), "system/packages");
540 		err = targetPkgDir.InitCheck();
541 		if (err != B_OK)
542 			return _InstallationError(err);
543 		for (int32 i = 0; i < count; i++) {
544 			Package *p = static_cast<Package*>(fPackages->ItemAt(i));
545 			const BPath& pkgPath = p->Path();
546 			err = pkgPath.InitCheck();
547 			if (err != B_OK)
548 				return _InstallationError(err);
549 			BPath targetPath(targetPkgDir.Path(), pkgPath.Leaf());
550 			err = targetPath.InitCheck();
551 			if (err != B_OK)
552 				return _InstallationError(err);
553 			err = engine.Copy(pkgPath.Path(), targetPath.Path(),
554 				fCancelSemaphore);
555 			if (err != B_OK)
556 				return _InstallationError(err);
557 		}
558 	}
559 
560 	// Extract all zip packages. If an error occured, delete the rest of
561 	// the engines, but stop extracting.
562 	for (int32 i = 0; i < unzipEngines.CountItems(); i++) {
563 		UnzipEngine* engine = reinterpret_cast<UnzipEngine*>(
564 			unzipEngines.ItemAtFast(i));
565 		if (err == B_OK)
566 			err = engine->UnzipPackage();
567 		delete engine;
568 	}
569 	if (err != B_OK)
570 		return _InstallationError(err);
571 
572 	err = _WriteBootSector(targetDirectory);
573 	if (err != B_OK)
574 		return _InstallationError(err);
575 
576 	err = _LaunchFinishScript(targetDirectory);
577 	if (err != B_OK)
578 		return _InstallationError(err);
579 
580 	fOwner.SendMessage(MSG_INSTALL_FINISHED);
581 	return B_OK;
582 }
583 
584 
585 status_t
586 WorkerThread::_PrepareCleanInstall(const BPath& targetDirectory) const
587 {
588 	// When a target volume has files (other than the trash), the /system
589 	// folder will be purged, except for the /system/settings subdirectory.
590 	BPath systemPath(targetDirectory.Path(), "system", true);
591 	status_t ret = systemPath.InitCheck();
592 	if (ret != B_OK)
593 		return ret;
594 
595 	BEntry systemEntry(systemPath.Path());
596 	ret = systemEntry.InitCheck();
597 	if (ret != B_OK)
598 		return ret;
599 	if (!systemEntry.Exists())
600 		// target does not exist, done
601 		return B_OK;
602 	if (!systemEntry.IsDirectory())
603 		// the system entry is a file or a symlink
604 		return systemEntry.Remove();
605 
606 	BDirectory systemDirectory(&systemEntry);
607 	ret = systemDirectory.InitCheck();
608 	if (ret != B_OK)
609 		return ret;
610 
611 	BEntry subEntry;
612 	char fileName[B_FILE_NAME_LENGTH];
613 	while (systemDirectory.GetNextEntry(&subEntry) == B_OK) {
614 		ret = subEntry.GetName(fileName);
615 		if (ret != B_OK)
616 			return ret;
617 
618 		if (subEntry.IsDirectory() && strcmp(fileName, "settings") == 0) {
619 			// Keep the settings folder
620 			continue;
621 		} else if (subEntry.IsDirectory()) {
622 			ret = CopyEngine::RemoveFolder(subEntry);
623 			if (ret != B_OK)
624 				return ret;
625 		} else {
626 			ret = subEntry.Remove();
627 			if (ret != B_OK)
628 				return ret;
629 		}
630 	}
631 
632 	return B_OK;
633 }
634 
635 
636 status_t
637 WorkerThread::_InstallationError(status_t error)
638 {
639 	BMessage statusMessage(MSG_RESET);
640 	if (error == B_CANCELED)
641 		_SetStatusMessage(B_TRANSLATE("Installation canceled."));
642 	else
643 		statusMessage.AddInt32("error", error);
644 	ERR("_PerformInstall failed");
645 	fOwner.SendMessage(&statusMessage);
646 	return error;
647 }
648 
649 
650 status_t
651 WorkerThread::_MirrorIndices(const BPath& sourceDirectory,
652 	const BPath& targetDirectory) const
653 {
654 	dev_t sourceDevice = dev_for_path(sourceDirectory.Path());
655 	if (sourceDevice < 0)
656 		return (status_t)sourceDevice;
657 	dev_t targetDevice = dev_for_path(targetDirectory.Path());
658 	if (targetDevice < 0)
659 		return (status_t)targetDevice;
660 	DIR* indices = fs_open_index_dir(sourceDevice);
661 	if (indices == NULL) {
662 		printf("%s: fs_open_index_dir(): (%d) %s\n", sourceDirectory.Path(),
663 			errno, strerror(errno));
664 		// Opening the index directory will fail for example on ISO-Live
665 		// CDs. The default indices have already been created earlier, so
666 		// we simply bail.
667 		return B_OK;
668 	}
669 	while (dirent* index = fs_read_index_dir(indices)) {
670 		if (strcmp(index->d_name, "name") == 0
671 			|| strcmp(index->d_name, "size") == 0
672 			|| strcmp(index->d_name, "last_modified") == 0) {
673 			continue;
674 		}
675 
676 		index_info info;
677 		if (fs_stat_index(sourceDevice, index->d_name, &info) != B_OK) {
678 			printf("Failed to mirror index %s: fs_stat_index(): (%d) %s\n",
679 				index->d_name, errno, strerror(errno));
680 			continue;
681 		}
682 
683 		uint32 flags = 0;
684 			// Flags are always 0 for the moment.
685 		if (fs_create_index(targetDevice, index->d_name, info.type, flags)
686 			!= B_OK) {
687 			if (errno == B_FILE_EXISTS)
688 				continue;
689 			printf("Failed to mirror index %s: fs_create_index(): (%d) %s\n",
690 				index->d_name, errno, strerror(errno));
691 			continue;
692 		}
693 	}
694 	fs_close_index_dir(indices);
695 	return B_OK;
696 }
697 
698 
699 status_t
700 WorkerThread::_CreateDefaultIndices(const BPath& targetDirectory) const
701 {
702 	dev_t targetDevice = dev_for_path(targetDirectory.Path());
703 	if (targetDevice < 0)
704 		return (status_t)targetDevice;
705 
706 	struct IndexInfo {
707 		const char* name;
708 		uint32_t	type;
709 	};
710 
711 	const IndexInfo defaultIndices[] = {
712 		{ "BEOS:APP_SIG", B_STRING_TYPE },
713 		{ "BEOS:LOCALE_LANGUAGE", B_STRING_TYPE },
714 		{ "BEOS:LOCALE_SIGNATURE", B_STRING_TYPE },
715 		{ "_trk/qrylastchange", B_INT32_TYPE },
716 		{ "_trk/recentQuery", B_INT32_TYPE },
717 		{ "be:deskbar_item_status", B_STRING_TYPE }
718 	};
719 
720 	uint32 flags = 0;
721 		// Flags are always 0 for the moment.
722 
723 	for (uint32 i = 0; i < sizeof(defaultIndices) / sizeof(IndexInfo); i++) {
724 		const IndexInfo& info = defaultIndices[i];
725 		if (fs_create_index(targetDevice, info.name, info.type, flags)
726 			!= B_OK) {
727 			if (errno == B_FILE_EXISTS)
728 				continue;
729 			printf("Failed to create index %s: fs_create_index(): (%d) %s\n",
730 				info.name, errno, strerror(errno));
731 			return errno;
732 		}
733 	}
734 
735 	return B_OK;
736 }
737 
738 
739 status_t
740 WorkerThread::_ProcessZipPackages(const char* sourcePath,
741 	const char* targetPath, ProgressReporter* reporter, BList& unzipEngines)
742 {
743 	// TODO: Put those in the optional packages list view
744 	// TODO: Implement mechanism to handle dependencies between these
745 	// packages. (Selecting one will auto-select others.)
746 	BPath pkgRootDir(sourcePath, kPackagesDirectoryPath);
747 	BDirectory directory(pkgRootDir.Path());
748 	BEntry entry;
749 	while (directory.GetNextEntry(&entry) == B_OK) {
750 		char name[B_FILE_NAME_LENGTH];
751 		if (entry.GetName(name) != B_OK)
752 			continue;
753 		int nameLength = strlen(name);
754 		if (nameLength <= 0)
755 			continue;
756 		char* nameExtension = name + nameLength - 4;
757 		if (strcasecmp(nameExtension, ".zip") != 0)
758 			continue;
759 		printf("found .zip package: %s\n", name);
760 
761 		UnzipEngine* unzipEngine = new(std::nothrow) UnzipEngine(reporter,
762 			fCancelSemaphore);
763 		if (unzipEngine == NULL || !unzipEngines.AddItem(unzipEngine)) {
764 			delete unzipEngine;
765 			return B_NO_MEMORY;
766 		}
767 		BPath path;
768 		entry.GetPath(&path);
769 		status_t ret = unzipEngine->SetTo(path.Path(), targetPath);
770 		if (ret != B_OK)
771 			return ret;
772 
773 		reporter->AddItems(unzipEngine->ItemsToUncompress(),
774 			unzipEngine->BytesToUncompress());
775 	}
776 
777 	return B_OK;
778 }
779 
780 
781 void
782 WorkerThread::_SetStatusMessage(const char *status)
783 {
784 	BMessage msg(MSG_STATUS_MESSAGE);
785 	msg.AddString("status", status);
786 	fOwner.SendMessage(&msg);
787 }
788 
789 
790 static void
791 make_partition_label(BPartition* partition, char* label, char* menuLabel,
792 	bool showContentType)
793 {
794 	char size[20];
795 	string_for_size(partition->Size(), size, sizeof(size));
796 
797 	BPath path;
798 	partition->GetPath(&path);
799 
800 	if (showContentType) {
801 		const char* type = partition->ContentType();
802 		if (type == NULL)
803 			type = B_TRANSLATE_COMMENT("Unknown Type", "Partition content type");
804 
805 		sprintf(label, "%s - %s [%s] (%s)", partition->ContentName(), size,
806 			path.Path(), type);
807 	} else {
808 		sprintf(label, "%s - %s [%s]", partition->ContentName(), size,
809 			path.Path());
810 	}
811 
812 	sprintf(menuLabel, "%s - %s", partition->ContentName(), size);
813 }
814 
815 
816 // #pragma mark - SourceVisitor
817 
818 
819 SourceVisitor::SourceVisitor(BMenu *menu)
820 	: fMenu(menu)
821 {
822 }
823 
824 bool
825 SourceVisitor::Visit(BDiskDevice *device)
826 {
827 	return Visit(device, 0);
828 }
829 
830 
831 bool
832 SourceVisitor::Visit(BPartition *partition, int32 level)
833 {
834 	BPath path;
835 
836 	if (partition->ContentType() == NULL)
837 		return false;
838 
839 	bool isBootPartition = false;
840 	if (partition->IsMounted()) {
841 		BPath mountPoint;
842 		if (partition->GetMountPoint(&mountPoint) != B_OK)
843 			return false;
844 		isBootPartition = strcmp(BOOT_PATH, mountPoint.Path()) == 0;
845 	}
846 
847 	if (!isBootPartition
848 		&& strcmp(partition->ContentType(), kPartitionTypeBFS) != 0) {
849 		// Except only BFS partitions, except this is the boot partition
850 		// (ISO9660 with write overlay for example).
851 		return false;
852 	}
853 
854 	// TODO: We could probably check if this volume contains
855 	// the Haiku kernel or something. Does it make sense to "install"
856 	// from your BFS volume containing the music collection?
857 	// TODO: Then the check for BFS could also be removed above.
858 
859 	char label[255];
860 	char menuLabel[255];
861 	make_partition_label(partition, label, menuLabel, false);
862 	PartitionMenuItem* item = new PartitionMenuItem(partition->ContentName(),
863 		label, menuLabel, new BMessage(SOURCE_PARTITION), partition->ID());
864 	item->SetMarked(isBootPartition);
865 	fMenu->AddItem(item);
866 	return false;
867 }
868 
869 
870 // #pragma mark - TargetVisitor
871 
872 
873 TargetVisitor::TargetVisitor(BMenu *menu)
874 	: fMenu(menu)
875 {
876 }
877 
878 
879 bool
880 TargetVisitor::Visit(BDiskDevice *device)
881 {
882 	if (device->IsReadOnlyMedia())
883 		return false;
884 	return Visit(device, 0);
885 }
886 
887 
888 bool
889 TargetVisitor::Visit(BPartition *partition, int32 level)
890 {
891 	if (partition->ContentSize() < 20 * 1024 * 1024) {
892 		// reject partitions which are too small anyway
893 		// TODO: Could depend on the source size
894 		return false;
895 	}
896 
897 	if (partition->CountChildren() > 0) {
898 		// Looks like an extended partition, or the device itself.
899 		// Do not accept this as target...
900 		return false;
901 	}
902 
903 	// TODO: After running DriveSetup and doing another scan, it would
904 	// be great to pick the partition which just appeared!
905 
906 	bool isBootPartition = false;
907 	if (partition->IsMounted()) {
908 		BPath mountPoint;
909 		partition->GetMountPoint(&mountPoint);
910 		isBootPartition = strcmp(BOOT_PATH, mountPoint.Path()) == 0;
911 	}
912 
913 	// Only writable non-boot BFS partitions are valid targets, but we want to
914 	// display the other partitions as well, to inform the user that they are
915 	// detected but somehow not appropriate.
916 	bool isValidTarget = isBootPartition == false
917 		&& !partition->IsReadOnly()
918 		&& partition->ContentType() != NULL
919 		&& strcmp(partition->ContentType(), kPartitionTypeBFS) == 0;
920 
921 	char label[255];
922 	char menuLabel[255];
923 	make_partition_label(partition, label, menuLabel, !isValidTarget);
924 	PartitionMenuItem* item = new PartitionMenuItem(partition->ContentName(),
925 		label, menuLabel, new BMessage(TARGET_PARTITION), partition->ID());
926 
927 	item->SetIsValidTarget(isValidTarget);
928 
929 
930 	fMenu->AddItem(item);
931 	return false;
932 }
933 
934