1 /* 2 * Copyright 2005-2008, Jérôme DUVAL. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 #include "CopyEngine.h" 7 #include "InstallerWindow.h" 8 #include "PartitionMenuItem.h" 9 #include "FSUndoRedo.h" 10 #include "FSUtils.h" 11 #include <Alert.h> 12 #include <DiskDeviceVisitor.h> 13 #include <DiskDeviceTypes.h> 14 #include <FindDirectory.h> 15 #include <Path.h> 16 #include <String.h> 17 #include <VolumeRoster.h> 18 19 //#define COPY_TRACE 20 #ifdef COPY_TRACE 21 #define CALLED() printf("CALLED %s\n",__PRETTY_FUNCTION__) 22 #define ERR2(x, y...) fprintf(stderr, "CopyEngine: "x" %s\n", y, strerror(err)) 23 #define ERR(x) fprintf(stderr, "CopyEngine: "x" %s\n", strerror(err)) 24 #else 25 #define CALLED() 26 #define ERR(x) 27 #define ERR2(x, y...) 28 #endif 29 30 const char BOOT_PATH[] = "/boot"; 31 32 extern void SizeAsString(off_t size, char *string); 33 34 class SourceVisitor : public BDiskDeviceVisitor 35 { 36 public: 37 SourceVisitor(BMenu *menu); 38 virtual bool Visit(BDiskDevice *device); 39 virtual bool Visit(BPartition *partition, int32 level); 40 private: 41 BMenu *fMenu; 42 }; 43 44 45 class TargetVisitor : public BDiskDeviceVisitor 46 { 47 public: 48 TargetVisitor(BMenu *menu); 49 virtual bool Visit(BDiskDevice *device); 50 virtual bool Visit(BPartition *partition, int32 level); 51 private: 52 void _MakeLabel(BPartition *partition, char *label, char *menuLabel); 53 BMenu *fMenu; 54 }; 55 56 57 CopyEngine::CopyEngine(InstallerWindow *window) 58 : BLooper("copy_engine"), 59 fWindow(window), 60 fPackages(NULL), 61 fSpaceRequired(0) 62 { 63 fControl = new InstallerCopyLoopControl(window); 64 Run(); 65 } 66 67 68 void 69 CopyEngine::MessageReceived(BMessage*msg) 70 { 71 CALLED(); 72 switch (msg->what) { 73 case ENGINE_START: 74 { 75 status_t err = Start(fWindow->GetSourceMenu(), 76 fWindow->GetTargetMenu()); 77 if (err != B_OK) { 78 ERR("Start failed"); 79 SetStatusMessage("Installation aborted."); 80 BMessenger(fWindow).SendMessage(RESET_INSTALL); 81 } else { 82 BMessenger(fWindow).SendMessage(INSTALL_FINISHED); 83 } 84 break; 85 } 86 } 87 } 88 89 90 void 91 CopyEngine::SetStatusMessage(char *status) 92 { 93 BMessage msg(STATUS_MESSAGE); 94 msg.AddString("status", status); 95 BMessenger(fWindow).SendMessage(&msg); 96 } 97 98 99 void 100 CopyEngine::LaunchInitScript(BPath &path) 101 { 102 BPath bootPath; 103 find_directory(B_BEOS_BOOT_DIRECTORY, &bootPath); 104 BString command("/bin/sh "); 105 command += bootPath.Path(); 106 command += "/InstallerInitScript "; 107 command += path.Path(); 108 SetStatusMessage("Starting Installation."); 109 system(command.String()); 110 } 111 112 113 void 114 CopyEngine::LaunchFinishScript(BPath &path) 115 { 116 BPath bootPath; 117 find_directory(B_BEOS_BOOT_DIRECTORY, &bootPath); 118 BString command("/bin/sh "); 119 command += bootPath.Path(); 120 command += "/InstallerFinishScript "; 121 command += path.Path(); 122 SetStatusMessage("Finishing Installation."); 123 system(command.String()); 124 } 125 126 127 status_t 128 CopyEngine::Start(BMenu *srcMenu, BMenu *targetMenu) 129 { 130 CALLED(); 131 132 fControl->Reset(); 133 134 status_t err = B_OK; 135 PartitionMenuItem *targetItem = (PartitionMenuItem *)targetMenu->FindMarked(); 136 PartitionMenuItem *srcItem = (PartitionMenuItem *)srcMenu->FindMarked(); 137 if (!srcItem || !targetItem) { 138 ERR("bad menu items\n"); 139 return B_BAD_VALUE; 140 } 141 142 // check if target is initialized 143 // ask if init or mount as is 144 145 BPath targetDirectory; 146 BDiskDevice device; 147 BPartition *partition; 148 BVolume targetVolume; 149 150 if (fDDRoster.GetPartitionWithID(targetItem->ID(), &device, &partition) == B_OK) { 151 if (!partition->IsMounted()) { 152 if ((err = partition->Mount()) < B_OK) { 153 SetStatusMessage("The disk can't be mounted. Please choose a " 154 "different disk."); 155 ERR("BPartition::Mount"); 156 return err; 157 } 158 } 159 if ((err = partition->GetVolume(&targetVolume)) != B_OK) { 160 ERR("BPartition::GetVolume"); 161 return err; 162 } 163 if ((err = partition->GetMountPoint(&targetDirectory)) != B_OK) { 164 ERR("BPartition::GetMountPoint"); 165 return err; 166 } 167 } else if (fDDRoster.GetDeviceWithID(targetItem->ID(), &device) == B_OK) { 168 if (!device.IsMounted()) { 169 if ((err = device.Mount()) < B_OK) { 170 SetStatusMessage("The disk can't be mounted. Please choose a " 171 "different disk."); 172 ERR("BDiskDevice::Mount"); 173 return err; 174 } 175 } 176 if ((err = device.GetVolume(&targetVolume)) != B_OK) { 177 ERR("BDiskDevice::GetVolume"); 178 return err; 179 } 180 if ((err = device.GetMountPoint(&targetDirectory)) != B_OK) { 181 ERR("BDiskDevice::GetMountPoint"); 182 return err; 183 } 184 } else 185 return B_ERROR; // shouldn't happen 186 187 // check if target has enough space 188 if ((fSpaceRequired > 0 && targetVolume.FreeBytes() < fSpaceRequired) 189 && ((new BAlert("", "The destination disk may not have enough space. " 190 "Try choosing a different disk or choose to not install optional " 191 "items.", "Try installing anyway", "Cancel", 0, 192 B_WIDTH_AS_USUAL, B_STOP_ALERT))->Go() != 0)) { 193 BMessenger(fWindow).SendMessage(RESET_INSTALL); 194 return B_OK; 195 } 196 197 BPath srcDirectory; 198 if (fDDRoster.GetPartitionWithID(srcItem->ID(), &device, &partition) == B_OK) { 199 if ((err = partition->GetMountPoint(&srcDirectory)) != B_OK) { 200 ERR("BPartition::GetMountPoint"); 201 return err; 202 } 203 } else if (fDDRoster.GetDeviceWithID(srcItem->ID(), &device) == B_OK) { 204 if ((err = device.GetMountPoint(&srcDirectory)) != B_OK) { 205 ERR("BDiskDevice::GetMountPoint"); 206 return err; 207 } 208 } else 209 return B_ERROR; // shouldn't happen 210 211 // check not installing on itself 212 if (strcmp(srcDirectory.Path(), targetDirectory.Path()) == 0) { 213 SetStatusMessage("You can't install the contents of a disk onto " 214 "itself. Please choose a different disk."); 215 BMessenger(fWindow).SendMessage(RESET_INSTALL); 216 return B_OK; 217 } 218 219 // check not installing on boot volume 220 if ((strncmp(BOOT_PATH, targetDirectory.Path(), strlen(BOOT_PATH)) == 0) 221 && ((new BAlert("", "Are you sure you want to install onto the " 222 "current boot disk? The installer will have to reboot your " 223 "machine if you proceed.", "OK", "Cancel", 0, 224 B_WIDTH_AS_USUAL, B_STOP_ALERT))->Go() != 0)) { 225 SetStatusMessage("Installation stopped."); 226 BMessenger(fWindow).SendMessage(RESET_INSTALL); 227 return B_OK; 228 } 229 230 LaunchInitScript(targetDirectory); 231 232 // copy source volume 233 BDirectory targetDir(targetDirectory.Path()); 234 BDirectory srcDir(srcDirectory.Path()); 235 err = CopyFolder(srcDir, targetDir); 236 237 if (err != B_OK) 238 return err; 239 240 // copy selected packages 241 if (fPackages) { 242 srcDirectory.Append(PACKAGES_DIRECTORY); 243 srcDir.SetTo(srcDirectory.Path()); 244 BDirectory packageDir; 245 int32 count = fPackages->CountItems(); 246 for (int32 i = 0; i < count; i++) { 247 if (fControl->CheckUserCanceled()) 248 return B_CANCELED; 249 Package *p = static_cast<Package*>(fPackages->ItemAt(i)); 250 packageDir.SetTo(&srcDir, p->Folder()); 251 err = CopyFolder(packageDir, targetDir); 252 if (err != B_OK) 253 break; 254 } 255 } 256 257 if (err != B_OK) 258 return err; 259 260 if (fControl->CheckUserCanceled()) 261 return B_CANCELED; 262 263 LaunchFinishScript(targetDirectory); 264 265 return B_OK; 266 } 267 268 269 status_t 270 CopyEngine::CopyFolder(BDirectory &srcDir, BDirectory &targetDir) 271 { 272 BEntry entry; 273 status_t err; 274 while (srcDir.GetNextEntry(&entry) == B_OK 275 && !fControl->CheckUserCanceled()) { 276 StatStruct statbuf; 277 entry.GetStat(&statbuf); 278 279 Undo undo; 280 if (S_ISDIR(statbuf.st_mode)) { 281 char name[B_FILE_NAME_LENGTH]; 282 if (entry.GetName(name) == B_OK 283 && strcmp(name, PACKAGES_DIRECTORY) == 0) { 284 continue; 285 } 286 err = FSCopyFolder(&entry, &targetDir, fControl, NULL, false, undo); 287 } else { 288 err = FSCopyFile(&entry, &statbuf, &targetDir, fControl, NULL, 289 false, undo); 290 } 291 if (err != B_OK) { 292 BPath path; 293 entry.GetPath(&path); 294 ERR2("error while copying %s", path.Path()); 295 return err; 296 } 297 } 298 299 return B_OK; 300 } 301 302 303 void 304 CopyEngine::ScanDisksPartitions(BMenu *srcMenu, BMenu *targetMenu) 305 { 306 BDiskDevice device; 307 BPartition *partition = NULL; 308 309 printf("ScanDisksPartitions partitions begin\n"); 310 SourceVisitor srcVisitor(srcMenu); 311 fDDRoster.VisitEachMountedPartition(&srcVisitor, &device, &partition); 312 313 printf("ScanDisksPartitions partitions begin\n"); 314 TargetVisitor targetVisitor(targetMenu); 315 fDDRoster.VisitEachPartition(&targetVisitor, &device, &partition); 316 } 317 318 319 void 320 CopyEngine::SetPackagesList(BList *list) 321 { 322 delete fPackages; 323 fPackages = list; 324 } 325 326 327 bool 328 CopyEngine::Cancel() 329 { 330 return fControl->Cancel(); 331 } 332 333 334 // #pragma mark - 335 336 337 SourceVisitor::SourceVisitor(BMenu *menu) 338 : fMenu(menu) 339 { 340 } 341 342 bool 343 SourceVisitor::Visit(BDiskDevice *device) 344 { 345 if (!device->ContentType() 346 || strcmp(device->ContentType(), kPartitionTypeBFS) != 0) 347 return false; 348 BPath path; 349 if (device->GetPath(&path) == B_OK) 350 printf("SourceVisitor::Visit(BDiskDevice *) : %s type:%s, " 351 "contentType:%s\n", path.Path(), device->Type(), 352 device->ContentType()); 353 PartitionMenuItem *item = new PartitionMenuItem(NULL, device->ContentName(), NULL, new BMessage(SRC_PARTITION), device->ID()); 354 if (device->IsMounted()) { 355 BPath mountPoint; 356 device->GetMountPoint(&mountPoint); 357 if (strcmp(BOOT_PATH, mountPoint.Path()) == 0) 358 item->SetMarked(true); 359 } 360 fMenu->AddItem(item); 361 return false; 362 } 363 364 365 bool 366 SourceVisitor::Visit(BPartition *partition, int32 level) 367 { 368 if (!partition->ContentType() 369 || strcmp(partition->ContentType(), kPartitionTypeBFS) != 0) 370 return false; 371 BPath path; 372 if (partition->GetPath(&path) == B_OK) 373 printf("SourceVisitor::Visit(BPartition *) : %s\n", path.Path()); 374 printf("SourceVisitor::Visit(BPartition *) : %s\n", partition->Name()); 375 PartitionMenuItem *item = new PartitionMenuItem(NULL, 376 partition->ContentName(), NULL, new BMessage(SRC_PARTITION), 377 partition->ID()); 378 if (partition->IsMounted()) { 379 BPath mountPoint; 380 partition->GetMountPoint(&mountPoint); 381 if (strcmp(BOOT_PATH, mountPoint.Path()) == 0) 382 item->SetMarked(true); 383 } 384 fMenu->AddItem(item); 385 return false; 386 } 387 388 389 // #pragma mark - 390 391 392 TargetVisitor::TargetVisitor(BMenu *menu) 393 : fMenu(menu) 394 { 395 } 396 397 398 bool 399 TargetVisitor::Visit(BDiskDevice *device) 400 { 401 if (device->IsReadOnly() || device->IsReadOnlyMedia()) 402 return false; 403 BPath path; 404 if (device->GetPath(&path) == B_OK) 405 printf("TargetVisitor::Visit(BDiskDevice *) : %s\n", path.Path()); 406 char label[255], menuLabel[255]; 407 _MakeLabel(device, label, menuLabel); 408 fMenu->AddItem(new PartitionMenuItem(device->ContentName(), label, 409 menuLabel, new BMessage(TARGET_PARTITION), device->ID())); 410 return false; 411 } 412 413 414 bool 415 TargetVisitor::Visit(BPartition *partition, int32 level) 416 { 417 if (partition->IsReadOnly()) 418 return false; 419 BPath path; 420 if (partition->GetPath(&path) == B_OK) 421 printf("TargetVisitor::Visit(BPartition *) : %s\n", path.Path()); 422 printf("TargetVisitor::Visit(BPartition *) : %s\n", partition->Name()); 423 char label[255], menuLabel[255]; 424 _MakeLabel(partition, label, menuLabel); 425 fMenu->AddItem(new PartitionMenuItem(partition->ContentName(), label, 426 menuLabel, new BMessage(TARGET_PARTITION), partition->ID())); 427 return false; 428 } 429 430 431 void 432 TargetVisitor::_MakeLabel(BPartition *partition, char *label, char *menuLabel) 433 { 434 char size[15]; 435 SizeAsString(partition->ContentSize(), size); 436 BPath path; 437 if (partition->Parent()) 438 partition->Parent()->GetPath(&path); 439 440 sprintf(label, "%s - %s [%s] [%s partition:%li]", partition->ContentName(), 441 size, partition->ContentType(), path.Path(), partition->ID()); 442 sprintf(menuLabel, "%s - %s [%s]", partition->ContentName(), size, 443 partition->ContentType()); 444 445 } 446 447