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 SourceVisitor srcVisitor(srcMenu); 254 fDDRoster.VisitEachMountedPartition(&srcVisitor, &device, &partition); 255 256 TargetVisitor targetVisitor(targetMenu); 257 fDDRoster.VisitEachPartition(&targetVisitor, &device, &partition); 258 } 259 260 261 void 262 WorkerThread::SetPackagesList(BList *list) 263 { 264 // Executed in window thread. 265 BAutolock _(this); 266 267 delete fPackages; 268 fPackages = list; 269 } 270 271 272 void 273 WorkerThread::StartInstall(partition_id sourcePartitionID, 274 partition_id targetPartitionID) 275 { 276 // Executed in window thread. 277 BMessage message(MSG_START_INSTALLING); 278 message.AddInt32("source", sourcePartitionID); 279 message.AddInt32("target", targetPartitionID); 280 281 PostMessage(&message, this); 282 } 283 284 285 void 286 WorkerThread::WriteBootSector(BMenu* targetMenu) 287 { 288 // Executed in window thread. 289 CALLED(); 290 291 PartitionMenuItem* item = (PartitionMenuItem*)targetMenu->FindMarked(); 292 if (item == NULL) { 293 ERR("bad menu items\n"); 294 return; 295 } 296 297 BMessage message(MSG_WRITE_BOOT_SECTOR); 298 message.AddInt32("id", item->ID()); 299 PostMessage(&message, this); 300 } 301 302 303 // #pragma mark - 304 305 306 status_t 307 WorkerThread::_LaunchInitScript(BPath &path) 308 { 309 BPath bootPath; 310 find_directory(B_BEOS_BOOT_DIRECTORY, &bootPath); 311 BString command("/bin/sh "); 312 command += bootPath.Path(); 313 command += "/InstallerInitScript "; 314 command += "\""; 315 command += path.Path(); 316 command += "\""; 317 _SetStatusMessage(B_TRANSLATE("Starting installation.")); 318 return system(command.String()); 319 } 320 321 322 status_t 323 WorkerThread::_LaunchFinishScript(BPath &path) 324 { 325 BPath bootPath; 326 find_directory(B_BEOS_BOOT_DIRECTORY, &bootPath); 327 BString command("/bin/sh "); 328 command += bootPath.Path(); 329 command += "/InstallerFinishScript "; 330 command += "\""; 331 command += path.Path(); 332 command += "\""; 333 _SetStatusMessage(B_TRANSLATE("Finishing installation.")); 334 return system(command.String()); 335 } 336 337 338 status_t 339 WorkerThread::_PerformInstall(partition_id sourcePartitionID, 340 partition_id targetPartitionID) 341 { 342 CALLED(); 343 344 BPath targetDirectory; 345 BPath srcDirectory; 346 BPath trashPath; 347 BPath testPath; 348 BDirectory targetDir; 349 BDiskDevice device; 350 BPartition* partition; 351 BVolume targetVolume; 352 status_t err = B_OK; 353 int32 entries = 0; 354 entry_ref testRef; 355 const char* mountError = B_TRANSLATE("The disk can't be mounted. Please " 356 "choose a different disk."); 357 358 if (sourcePartitionID < 0 || targetPartitionID < 0) { 359 ERR("bad source or target partition ID\n"); 360 return _InstallationError(err); 361 } 362 363 // check if target is initialized 364 // ask if init or mount as is 365 if (fDDRoster.GetPartitionWithID(targetPartitionID, &device, 366 &partition) == B_OK) { 367 if (!partition->IsMounted()) { 368 if ((err = partition->Mount()) < B_OK) { 369 _SetStatusMessage(mountError); 370 ERR("BPartition::Mount"); 371 return _InstallationError(err); 372 } 373 } 374 if ((err = partition->GetVolume(&targetVolume)) != B_OK) { 375 ERR("BPartition::GetVolume"); 376 return _InstallationError(err); 377 } 378 if ((err = partition->GetMountPoint(&targetDirectory)) != B_OK) { 379 ERR("BPartition::GetMountPoint"); 380 return _InstallationError(err); 381 } 382 } else if (fDDRoster.GetDeviceWithID(targetPartitionID, &device) == B_OK) { 383 if (!device.IsMounted()) { 384 if ((err = device.Mount()) < B_OK) { 385 _SetStatusMessage(mountError); 386 ERR("BDiskDevice::Mount"); 387 return _InstallationError(err); 388 } 389 } 390 if ((err = device.GetVolume(&targetVolume)) != B_OK) { 391 ERR("BDiskDevice::GetVolume"); 392 return _InstallationError(err); 393 } 394 if ((err = device.GetMountPoint(&targetDirectory)) != B_OK) { 395 ERR("BDiskDevice::GetMountPoint"); 396 return _InstallationError(err); 397 } 398 } else 399 return _InstallationError(err); // shouldn't happen 400 401 // check if target has enough space 402 if (fSpaceRequired > 0 && targetVolume.FreeBytes() < fSpaceRequired) { 403 BAlert* alert = new BAlert("", B_TRANSLATE("The destination disk may " 404 "not have enough space. Try choosing a different disk or choose " 405 "to not install optional items."), 406 B_TRANSLATE("Try installing anyway"), B_TRANSLATE("Cancel"), 0, 407 B_WIDTH_AS_USUAL, B_STOP_ALERT); 408 alert->SetShortcut(1, B_ESCAPE); 409 if (alert->Go() != 0) 410 return _InstallationError(err); 411 } 412 413 if (fDDRoster.GetPartitionWithID(sourcePartitionID, &device, &partition) 414 == B_OK) { 415 if ((err = partition->GetMountPoint(&srcDirectory)) != B_OK) { 416 ERR("BPartition::GetMountPoint"); 417 return _InstallationError(err); 418 } 419 } else if (fDDRoster.GetDeviceWithID(sourcePartitionID, &device) == B_OK) { 420 if ((err = device.GetMountPoint(&srcDirectory)) != B_OK) { 421 ERR("BDiskDevice::GetMountPoint"); 422 return _InstallationError(err); 423 } 424 } else 425 return _InstallationError(err); // shouldn't happen 426 427 // check not installing on itself 428 if (strcmp(srcDirectory.Path(), targetDirectory.Path()) == 0) { 429 _SetStatusMessage(B_TRANSLATE("You can't install the contents of a " 430 "disk onto itself. Please choose a different disk.")); 431 return _InstallationError(err); 432 } 433 434 // check not installing on boot volume 435 if (strncmp(BOOT_PATH, targetDirectory.Path(), strlen(BOOT_PATH)) == 0) { 436 BAlert* alert = new BAlert("", B_TRANSLATE("Are you sure you want to " 437 "install onto the current boot disk? The Installer will have to " 438 "reboot your machine if you proceed."), B_TRANSLATE("OK"), 439 B_TRANSLATE("Cancel"), 0, B_WIDTH_AS_USUAL, B_STOP_ALERT); 440 alert->SetShortcut(1, B_ESCAPE); 441 if (alert->Go() != 0) { 442 _SetStatusMessage("Installation stopped."); 443 return _InstallationError(err); 444 } 445 } 446 447 // check if target volume's trash dir has anything in it 448 // (target volume w/ only an empty trash dir is considered 449 // an empty volume) 450 if (find_directory(B_TRASH_DIRECTORY, &trashPath, false, 451 &targetVolume) == B_OK && targetDir.SetTo(trashPath.Path()) == B_OK) { 452 while (targetDir.GetNextRef(&testRef) == B_OK) { 453 // Something in the Trash 454 entries++; 455 break; 456 } 457 } 458 459 targetDir.SetTo(targetDirectory.Path()); 460 461 // check if target volume otherwise has any entries 462 while (entries == 0 && targetDir.GetNextRef(&testRef) == B_OK) { 463 if (testPath.SetTo(&testRef) == B_OK && testPath != trashPath) 464 entries++; 465 } 466 467 if (entries != 0) { 468 BAlert* alert = new BAlert("", B_TRANSLATE("The target volume is not " 469 "empty. Are you sure you want to install anyway?\n\nNote: The " 470 "'system' folder will be a clean copy from the source volume but " 471 "will retain its settings folder, all other folders will be " 472 "merged, whereas files and links that exist on both the source " 473 "and target volume will be overwritten with the source volume " 474 "version."), 475 B_TRANSLATE("Install anyway"), B_TRANSLATE("Cancel"), 0, 476 B_WIDTH_AS_USUAL, B_STOP_ALERT); 477 alert->SetShortcut(1, B_ESCAPE); 478 if (alert->Go() != 0) { 479 // TODO: Would be cool to offer the option here to clean additional 480 // folders at the user's choice. 481 return _InstallationError(B_CANCELED); 482 } 483 } 484 485 // Begin actual installation 486 487 ProgressReporter reporter(fOwner, new BMessage(MSG_STATUS_MESSAGE)); 488 EntryFilter entryFilter(srcDirectory.Path()); 489 CopyEngine engine(&reporter, &entryFilter); 490 BList unzipEngines; 491 492 err = _LaunchInitScript(targetDirectory); 493 if (err != B_OK) 494 return _InstallationError(err); 495 496 // Create the default indices which should always be present on a proper 497 // boot volume. We don't care if the source volume does not have them. 498 // After all, the user might be re-installing to another drive and may 499 // want problems fixed along the way... 500 err = _CreateDefaultIndices(targetDirectory); 501 if (err != B_OK) 502 return _InstallationError(err); 503 // Mirror all the indices which are present on the source volume onto 504 // the target volume. 505 err = _MirrorIndices(srcDirectory, targetDirectory); 506 if (err != B_OK) 507 return _InstallationError(err); 508 509 // Let the engine collect information for the progress bar later on 510 engine.ResetTargets(srcDirectory.Path()); 511 err = engine.CollectTargets(srcDirectory.Path(), fCancelSemaphore); 512 if (err != B_OK) 513 return _InstallationError(err); 514 515 // Collect selected packages also 516 if (fPackages) { 517 BPath pkgRootDir(srcDirectory.Path(), kPackagesDirectoryPath); 518 int32 count = fPackages->CountItems(); 519 for (int32 i = 0; i < count; i++) { 520 Package *p = static_cast<Package*>(fPackages->ItemAt(i)); 521 BPath packageDir(pkgRootDir.Path(), p->Folder()); 522 err = engine.CollectTargets(packageDir.Path(), fCancelSemaphore); 523 if (err != B_OK) 524 return _InstallationError(err); 525 } 526 } 527 528 // collect information about all zip packages 529 err = _ProcessZipPackages(srcDirectory.Path(), targetDirectory.Path(), 530 &reporter, unzipEngines); 531 if (err != B_OK) 532 return _InstallationError(err); 533 534 reporter.StartTimer(); 535 536 // copy source volume 537 err = engine.CopyFolder(srcDirectory.Path(), targetDirectory.Path(), 538 fCancelSemaphore); 539 if (err != B_OK) 540 return _InstallationError(err); 541 542 // copy selected packages 543 if (fPackages) { 544 BPath pkgRootDir(srcDirectory.Path(), kPackagesDirectoryPath); 545 int32 count = fPackages->CountItems(); 546 for (int32 i = 0; i < count; i++) { 547 Package *p = static_cast<Package*>(fPackages->ItemAt(i)); 548 BPath packageDir(pkgRootDir.Path(), p->Folder()); 549 err = engine.CopyFolder(packageDir.Path(), targetDirectory.Path(), 550 fCancelSemaphore); 551 if (err != B_OK) 552 return _InstallationError(err); 553 } 554 } 555 556 // Extract all zip packages. If an error occured, delete the rest of 557 // the engines, but stop extracting. 558 for (int32 i = 0; i < unzipEngines.CountItems(); i++) { 559 UnzipEngine* engine = reinterpret_cast<UnzipEngine*>( 560 unzipEngines.ItemAtFast(i)); 561 if (err == B_OK) 562 err = engine->UnzipPackage(); 563 delete engine; 564 } 565 if (err != B_OK) 566 return _InstallationError(err); 567 568 err = _LaunchFinishScript(targetDirectory); 569 if (err != B_OK) 570 return _InstallationError(err); 571 572 fOwner.SendMessage(MSG_INSTALL_FINISHED); 573 return B_OK; 574 } 575 576 577 status_t 578 WorkerThread::_InstallationError(status_t error) 579 { 580 BMessage statusMessage(MSG_RESET); 581 if (error == B_CANCELED) 582 _SetStatusMessage(B_TRANSLATE("Installation canceled.")); 583 else 584 statusMessage.AddInt32("error", error); 585 ERR("_PerformInstall failed"); 586 fOwner.SendMessage(&statusMessage); 587 return error; 588 } 589 590 591 status_t 592 WorkerThread::_MirrorIndices(const BPath& sourceDirectory, 593 const BPath& targetDirectory) const 594 { 595 dev_t sourceDevice = dev_for_path(sourceDirectory.Path()); 596 if (sourceDevice < 0) 597 return (status_t)sourceDevice; 598 dev_t targetDevice = dev_for_path(targetDirectory.Path()); 599 if (targetDevice < 0) 600 return (status_t)targetDevice; 601 DIR* indices = fs_open_index_dir(sourceDevice); 602 if (indices == NULL) { 603 printf("%s: fs_open_index_dir(): (%d) %s\n", sourceDirectory.Path(), 604 errno, strerror(errno)); 605 // Opening the index directory will fail for example on ISO-Live 606 // CDs. The default indices have already been created earlier, so 607 // we simply bail. 608 return B_OK; 609 } 610 while (dirent* index = fs_read_index_dir(indices)) { 611 if (strcmp(index->d_name, "name") == 0 612 || strcmp(index->d_name, "size") == 0 613 || strcmp(index->d_name, "last_modified") == 0) { 614 continue; 615 } 616 617 index_info info; 618 if (fs_stat_index(sourceDevice, index->d_name, &info) != B_OK) { 619 printf("Failed to mirror index %s: fs_stat_index(): (%d) %s\n", 620 index->d_name, errno, strerror(errno)); 621 continue; 622 } 623 624 uint32 flags = 0; 625 // Flags are always 0 for the moment. 626 if (fs_create_index(targetDevice, index->d_name, info.type, flags) 627 != B_OK) { 628 if (errno == B_FILE_EXISTS) 629 continue; 630 printf("Failed to mirror index %s: fs_create_index(): (%d) %s\n", 631 index->d_name, errno, strerror(errno)); 632 continue; 633 } 634 } 635 fs_close_index_dir(indices); 636 return B_OK; 637 } 638 639 640 status_t 641 WorkerThread::_CreateDefaultIndices(const BPath& targetDirectory) const 642 { 643 dev_t targetDevice = dev_for_path(targetDirectory.Path()); 644 if (targetDevice < 0) 645 return (status_t)targetDevice; 646 647 struct IndexInfo { 648 const char* name; 649 uint32_t type; 650 }; 651 652 const IndexInfo defaultIndices[] = { 653 { "BEOS:APP_SIG", B_STRING_TYPE }, 654 { "BEOS:LOCALE_LANGUAGE", B_STRING_TYPE }, 655 { "BEOS:LOCALE_SIGNATURE", B_STRING_TYPE }, 656 { "_trk/qrylastchange", B_INT32_TYPE }, 657 { "_trk/recentQuery", B_INT32_TYPE }, 658 { "be:deskbar_item_status", B_STRING_TYPE } 659 }; 660 661 uint32 flags = 0; 662 // Flags are always 0 for the moment. 663 664 for (uint32 i = 0; i < sizeof(defaultIndices) / sizeof(IndexInfo); i++) { 665 const IndexInfo& info = defaultIndices[i]; 666 if (fs_create_index(targetDevice, info.name, info.type, flags) 667 != B_OK) { 668 if (errno == B_FILE_EXISTS) 669 continue; 670 printf("Failed to create index %s: fs_create_index(): (%d) %s\n", 671 info.name, errno, strerror(errno)); 672 return errno; 673 } 674 } 675 676 return B_OK; 677 } 678 679 680 status_t 681 WorkerThread::_ProcessZipPackages(const char* sourcePath, 682 const char* targetPath, ProgressReporter* reporter, BList& unzipEngines) 683 { 684 // TODO: Put those in the optional packages list view 685 // TODO: Implement mechanism to handle dependencies between these 686 // packages. (Selecting one will auto-select others.) 687 BPath pkgRootDir(sourcePath, kPackagesDirectoryPath); 688 BDirectory directory(pkgRootDir.Path()); 689 BEntry entry; 690 while (directory.GetNextEntry(&entry) == B_OK) { 691 char name[B_FILE_NAME_LENGTH]; 692 if (entry.GetName(name) != B_OK) 693 continue; 694 int nameLength = strlen(name); 695 if (nameLength <= 0) 696 continue; 697 char* nameExtension = name + nameLength - 4; 698 if (strcasecmp(nameExtension, ".zip") != 0) 699 continue; 700 printf("found .zip package: %s\n", name); 701 702 UnzipEngine* unzipEngine = new(std::nothrow) UnzipEngine(reporter, 703 fCancelSemaphore); 704 if (unzipEngine == NULL || !unzipEngines.AddItem(unzipEngine)) { 705 delete unzipEngine; 706 return B_NO_MEMORY; 707 } 708 BPath path; 709 entry.GetPath(&path); 710 status_t ret = unzipEngine->SetTo(path.Path(), targetPath); 711 if (ret != B_OK) 712 return ret; 713 714 reporter->AddItems(unzipEngine->ItemsToUncompress(), 715 unzipEngine->BytesToUncompress()); 716 } 717 718 return B_OK; 719 } 720 721 722 void 723 WorkerThread::_SetStatusMessage(const char *status) 724 { 725 BMessage msg(MSG_STATUS_MESSAGE); 726 msg.AddString("status", status); 727 fOwner.SendMessage(&msg); 728 } 729 730 731 static void 732 make_partition_label(BPartition* partition, char* label, char* menuLabel, 733 bool showContentType) 734 { 735 char size[20]; 736 string_for_size(partition->Size(), size, sizeof(size)); 737 738 BPath path; 739 partition->GetPath(&path); 740 741 if (showContentType) { 742 const char* type = partition->ContentType(); 743 if (type == NULL) 744 type = B_TRANSLATE_COMMENT("Unknown Type", "Partition content type"); 745 746 sprintf(label, "%s - %s [%s] (%s)", partition->ContentName(), size, 747 path.Path(), type); 748 } else { 749 sprintf(label, "%s - %s [%s]", partition->ContentName(), size, 750 path.Path()); 751 } 752 753 sprintf(menuLabel, "%s - %s", partition->ContentName(), size); 754 } 755 756 757 // #pragma mark - SourceVisitor 758 759 760 SourceVisitor::SourceVisitor(BMenu *menu) 761 : fMenu(menu) 762 { 763 } 764 765 bool 766 SourceVisitor::Visit(BDiskDevice *device) 767 { 768 return Visit(device, 0); 769 } 770 771 772 bool 773 SourceVisitor::Visit(BPartition *partition, int32 level) 774 { 775 BPath path; 776 777 if (partition->ContentType() == NULL) 778 return false; 779 780 bool isBootPartition = false; 781 if (partition->IsMounted()) { 782 BPath mountPoint; 783 if (partition->GetMountPoint(&mountPoint) != B_OK) 784 return false; 785 isBootPartition = strcmp(BOOT_PATH, mountPoint.Path()) == 0; 786 } 787 788 if (!isBootPartition 789 && strcmp(partition->ContentType(), kPartitionTypeBFS) != 0) { 790 // Except only BFS partitions, except this is the boot partition 791 // (ISO9660 with write overlay for example). 792 return false; 793 } 794 795 // TODO: We could probably check if this volume contains 796 // the Haiku kernel or something. Does it make sense to "install" 797 // from your BFS volume containing the music collection? 798 // TODO: Then the check for BFS could also be removed above. 799 800 char label[255]; 801 char menuLabel[255]; 802 make_partition_label(partition, label, menuLabel, false); 803 PartitionMenuItem* item = new PartitionMenuItem(partition->ContentName(), 804 label, menuLabel, new BMessage(SOURCE_PARTITION), partition->ID()); 805 item->SetMarked(isBootPartition); 806 fMenu->AddItem(item); 807 return false; 808 } 809 810 811 // #pragma mark - TargetVisitor 812 813 814 TargetVisitor::TargetVisitor(BMenu *menu) 815 : fMenu(menu) 816 { 817 } 818 819 820 bool 821 TargetVisitor::Visit(BDiskDevice *device) 822 { 823 if (device->IsReadOnlyMedia()) 824 return false; 825 return Visit(device, 0); 826 } 827 828 829 bool 830 TargetVisitor::Visit(BPartition *partition, int32 level) 831 { 832 if (partition->ContentSize() < 20 * 1024 * 1024) { 833 // reject partitions which are too small anyway 834 // TODO: Could depend on the source size 835 return false; 836 } 837 838 if (partition->CountChildren() > 0) { 839 // Looks like an extended partition, or the device itself. 840 // Do not accept this as target... 841 return false; 842 } 843 844 // TODO: After running DriveSetup and doing another scan, it would 845 // be great to pick the partition which just appeared! 846 847 bool isBootPartition = false; 848 if (partition->IsMounted()) { 849 BPath mountPoint; 850 partition->GetMountPoint(&mountPoint); 851 isBootPartition = strcmp(BOOT_PATH, mountPoint.Path()) == 0; 852 } 853 854 // Only writable non-boot BFS partitions are valid targets, but we want to 855 // display the other partitions as well, to inform the user that they are 856 // detected but somehow not appropriate. 857 bool isValidTarget = isBootPartition == false 858 && !partition->IsReadOnly() 859 && partition->ContentType() != NULL 860 && strcmp(partition->ContentType(), kPartitionTypeBFS) == 0; 861 862 char label[255]; 863 char menuLabel[255]; 864 make_partition_label(partition, label, menuLabel, !isValidTarget); 865 PartitionMenuItem* item = new PartitionMenuItem(partition->ContentName(), 866 label, menuLabel, new BMessage(TARGET_PARTITION), partition->ID()); 867 868 item->SetIsValidTarget(isValidTarget); 869 870 871 fMenu->AddItem(item); 872 return false; 873 } 874 875