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