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