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 while " 471 "the existing 'settings' folder is retained. All other folders " 472 "will be merged, in which files and links that exist on both the " 473 "source and target volume will be overwritten with the source " 474 "volume 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 int32 count = fPackages->CountItems(); 518 for (int32 i = 0; i < count; i++) { 519 Package *p = static_cast<Package*>(fPackages->ItemAt(i)); 520 const BPath& pkgPath = p->Path(); 521 err = pkgPath.InitCheck(); 522 if (err != B_OK) 523 return _InstallationError(err); 524 err = engine.CollectTargets(pkgPath.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.Copy(srcDirectory.Path(), targetDirectory.Path(), 540 fCancelSemaphore); 541 if (err != B_OK) 542 return _InstallationError(err); 543 544 // copy selected packages 545 if (fPackages) { 546 int32 count = fPackages->CountItems(); 547 // FIXME: find_directory doesn't return the folder in the target volume, 548 // so we are hard coding this for now. 549 BPath targetPkgDir(targetDirectory.Path(), "system/packages"); 550 err = targetPkgDir.InitCheck(); 551 if (err != B_OK) 552 return _InstallationError(err); 553 for (int32 i = 0; i < count; i++) { 554 Package *p = static_cast<Package*>(fPackages->ItemAt(i)); 555 const BPath& pkgPath = p->Path(); 556 err = pkgPath.InitCheck(); 557 if (err != B_OK) 558 return _InstallationError(err); 559 BPath targetPath(targetPkgDir.Path(), pkgPath.Leaf()); 560 err = targetPath.InitCheck(); 561 if (err != B_OK) 562 return _InstallationError(err); 563 err = engine.Copy(pkgPath.Path(), targetPath.Path(), 564 fCancelSemaphore); 565 if (err != B_OK) 566 return _InstallationError(err); 567 } 568 } 569 570 // Extract all zip packages. If an error occured, delete the rest of 571 // the engines, but stop extracting. 572 for (int32 i = 0; i < unzipEngines.CountItems(); i++) { 573 UnzipEngine* engine = reinterpret_cast<UnzipEngine*>( 574 unzipEngines.ItemAtFast(i)); 575 if (err == B_OK) 576 err = engine->UnzipPackage(); 577 delete engine; 578 } 579 if (err != B_OK) 580 return _InstallationError(err); 581 582 err = _LaunchFinishScript(targetDirectory); 583 if (err != B_OK) 584 return _InstallationError(err); 585 586 fOwner.SendMessage(MSG_INSTALL_FINISHED); 587 return B_OK; 588 } 589 590 591 status_t 592 WorkerThread::_InstallationError(status_t error) 593 { 594 BMessage statusMessage(MSG_RESET); 595 if (error == B_CANCELED) 596 _SetStatusMessage(B_TRANSLATE("Installation canceled.")); 597 else 598 statusMessage.AddInt32("error", error); 599 ERR("_PerformInstall failed"); 600 fOwner.SendMessage(&statusMessage); 601 return error; 602 } 603 604 605 status_t 606 WorkerThread::_MirrorIndices(const BPath& sourceDirectory, 607 const BPath& targetDirectory) const 608 { 609 dev_t sourceDevice = dev_for_path(sourceDirectory.Path()); 610 if (sourceDevice < 0) 611 return (status_t)sourceDevice; 612 dev_t targetDevice = dev_for_path(targetDirectory.Path()); 613 if (targetDevice < 0) 614 return (status_t)targetDevice; 615 DIR* indices = fs_open_index_dir(sourceDevice); 616 if (indices == NULL) { 617 printf("%s: fs_open_index_dir(): (%d) %s\n", sourceDirectory.Path(), 618 errno, strerror(errno)); 619 // Opening the index directory will fail for example on ISO-Live 620 // CDs. The default indices have already been created earlier, so 621 // we simply bail. 622 return B_OK; 623 } 624 while (dirent* index = fs_read_index_dir(indices)) { 625 if (strcmp(index->d_name, "name") == 0 626 || strcmp(index->d_name, "size") == 0 627 || strcmp(index->d_name, "last_modified") == 0) { 628 continue; 629 } 630 631 index_info info; 632 if (fs_stat_index(sourceDevice, index->d_name, &info) != B_OK) { 633 printf("Failed to mirror index %s: fs_stat_index(): (%d) %s\n", 634 index->d_name, errno, strerror(errno)); 635 continue; 636 } 637 638 uint32 flags = 0; 639 // Flags are always 0 for the moment. 640 if (fs_create_index(targetDevice, index->d_name, info.type, flags) 641 != B_OK) { 642 if (errno == B_FILE_EXISTS) 643 continue; 644 printf("Failed to mirror index %s: fs_create_index(): (%d) %s\n", 645 index->d_name, errno, strerror(errno)); 646 continue; 647 } 648 } 649 fs_close_index_dir(indices); 650 return B_OK; 651 } 652 653 654 status_t 655 WorkerThread::_CreateDefaultIndices(const BPath& targetDirectory) const 656 { 657 dev_t targetDevice = dev_for_path(targetDirectory.Path()); 658 if (targetDevice < 0) 659 return (status_t)targetDevice; 660 661 struct IndexInfo { 662 const char* name; 663 uint32_t type; 664 }; 665 666 const IndexInfo defaultIndices[] = { 667 { "BEOS:APP_SIG", B_STRING_TYPE }, 668 { "BEOS:LOCALE_LANGUAGE", B_STRING_TYPE }, 669 { "BEOS:LOCALE_SIGNATURE", B_STRING_TYPE }, 670 { "_trk/qrylastchange", B_INT32_TYPE }, 671 { "_trk/recentQuery", B_INT32_TYPE }, 672 { "be:deskbar_item_status", B_STRING_TYPE } 673 }; 674 675 uint32 flags = 0; 676 // Flags are always 0 for the moment. 677 678 for (uint32 i = 0; i < sizeof(defaultIndices) / sizeof(IndexInfo); i++) { 679 const IndexInfo& info = defaultIndices[i]; 680 if (fs_create_index(targetDevice, info.name, info.type, flags) 681 != B_OK) { 682 if (errno == B_FILE_EXISTS) 683 continue; 684 printf("Failed to create index %s: fs_create_index(): (%d) %s\n", 685 info.name, errno, strerror(errno)); 686 return errno; 687 } 688 } 689 690 return B_OK; 691 } 692 693 694 status_t 695 WorkerThread::_ProcessZipPackages(const char* sourcePath, 696 const char* targetPath, ProgressReporter* reporter, BList& unzipEngines) 697 { 698 // TODO: Put those in the optional packages list view 699 // TODO: Implement mechanism to handle dependencies between these 700 // packages. (Selecting one will auto-select others.) 701 BPath pkgRootDir(sourcePath, kPackagesDirectoryPath); 702 BDirectory directory(pkgRootDir.Path()); 703 BEntry entry; 704 while (directory.GetNextEntry(&entry) == B_OK) { 705 char name[B_FILE_NAME_LENGTH]; 706 if (entry.GetName(name) != B_OK) 707 continue; 708 int nameLength = strlen(name); 709 if (nameLength <= 0) 710 continue; 711 char* nameExtension = name + nameLength - 4; 712 if (strcasecmp(nameExtension, ".zip") != 0) 713 continue; 714 printf("found .zip package: %s\n", name); 715 716 UnzipEngine* unzipEngine = new(std::nothrow) UnzipEngine(reporter, 717 fCancelSemaphore); 718 if (unzipEngine == NULL || !unzipEngines.AddItem(unzipEngine)) { 719 delete unzipEngine; 720 return B_NO_MEMORY; 721 } 722 BPath path; 723 entry.GetPath(&path); 724 status_t ret = unzipEngine->SetTo(path.Path(), targetPath); 725 if (ret != B_OK) 726 return ret; 727 728 reporter->AddItems(unzipEngine->ItemsToUncompress(), 729 unzipEngine->BytesToUncompress()); 730 } 731 732 return B_OK; 733 } 734 735 736 void 737 WorkerThread::_SetStatusMessage(const char *status) 738 { 739 BMessage msg(MSG_STATUS_MESSAGE); 740 msg.AddString("status", status); 741 fOwner.SendMessage(&msg); 742 } 743 744 745 static void 746 make_partition_label(BPartition* partition, char* label, char* menuLabel, 747 bool showContentType) 748 { 749 char size[20]; 750 string_for_size(partition->Size(), size, sizeof(size)); 751 752 BPath path; 753 partition->GetPath(&path); 754 755 if (showContentType) { 756 const char* type = partition->ContentType(); 757 if (type == NULL) 758 type = B_TRANSLATE_COMMENT("Unknown Type", "Partition content type"); 759 760 sprintf(label, "%s - %s [%s] (%s)", partition->ContentName(), size, 761 path.Path(), type); 762 } else { 763 sprintf(label, "%s - %s [%s]", partition->ContentName(), size, 764 path.Path()); 765 } 766 767 sprintf(menuLabel, "%s - %s", partition->ContentName(), size); 768 } 769 770 771 // #pragma mark - SourceVisitor 772 773 774 SourceVisitor::SourceVisitor(BMenu *menu) 775 : fMenu(menu) 776 { 777 } 778 779 bool 780 SourceVisitor::Visit(BDiskDevice *device) 781 { 782 return Visit(device, 0); 783 } 784 785 786 bool 787 SourceVisitor::Visit(BPartition *partition, int32 level) 788 { 789 BPath path; 790 791 if (partition->ContentType() == NULL) 792 return false; 793 794 bool isBootPartition = false; 795 if (partition->IsMounted()) { 796 BPath mountPoint; 797 if (partition->GetMountPoint(&mountPoint) != B_OK) 798 return false; 799 isBootPartition = strcmp(BOOT_PATH, mountPoint.Path()) == 0; 800 } 801 802 if (!isBootPartition 803 && strcmp(partition->ContentType(), kPartitionTypeBFS) != 0) { 804 // Except only BFS partitions, except this is the boot partition 805 // (ISO9660 with write overlay for example). 806 return false; 807 } 808 809 // TODO: We could probably check if this volume contains 810 // the Haiku kernel or something. Does it make sense to "install" 811 // from your BFS volume containing the music collection? 812 // TODO: Then the check for BFS could also be removed above. 813 814 char label[255]; 815 char menuLabel[255]; 816 make_partition_label(partition, label, menuLabel, false); 817 PartitionMenuItem* item = new PartitionMenuItem(partition->ContentName(), 818 label, menuLabel, new BMessage(SOURCE_PARTITION), partition->ID()); 819 item->SetMarked(isBootPartition); 820 fMenu->AddItem(item); 821 return false; 822 } 823 824 825 // #pragma mark - TargetVisitor 826 827 828 TargetVisitor::TargetVisitor(BMenu *menu) 829 : fMenu(menu) 830 { 831 } 832 833 834 bool 835 TargetVisitor::Visit(BDiskDevice *device) 836 { 837 if (device->IsReadOnlyMedia()) 838 return false; 839 return Visit(device, 0); 840 } 841 842 843 bool 844 TargetVisitor::Visit(BPartition *partition, int32 level) 845 { 846 if (partition->ContentSize() < 20 * 1024 * 1024) { 847 // reject partitions which are too small anyway 848 // TODO: Could depend on the source size 849 return false; 850 } 851 852 if (partition->CountChildren() > 0) { 853 // Looks like an extended partition, or the device itself. 854 // Do not accept this as target... 855 return false; 856 } 857 858 // TODO: After running DriveSetup and doing another scan, it would 859 // be great to pick the partition which just appeared! 860 861 bool isBootPartition = false; 862 if (partition->IsMounted()) { 863 BPath mountPoint; 864 partition->GetMountPoint(&mountPoint); 865 isBootPartition = strcmp(BOOT_PATH, mountPoint.Path()) == 0; 866 } 867 868 // Only writable non-boot BFS partitions are valid targets, but we want to 869 // display the other partitions as well, to inform the user that they are 870 // detected but somehow not appropriate. 871 bool isValidTarget = isBootPartition == false 872 && !partition->IsReadOnly() 873 && partition->ContentType() != NULL 874 && strcmp(partition->ContentType(), kPartitionTypeBFS) == 0; 875 876 char label[255]; 877 char menuLabel[255]; 878 make_partition_label(partition, label, menuLabel, !isValidTarget); 879 PartitionMenuItem* item = new PartitionMenuItem(partition->ContentName(), 880 label, menuLabel, new BMessage(TARGET_PARTITION), partition->ID()); 881 882 item->SetIsValidTarget(isValidTarget); 883 884 885 fMenu->AddItem(item); 886 return false; 887 } 888 889