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