Files
haiku-beta6/src/apps/installer/CopyEngine.cpp
T

384 lines
10 KiB
C++
Raw Normal View History

/*
2006-01-23 17:36:24 +00:00
* Copyright 2005-2006, Jérôme DUVAL. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#include "CopyEngine.h"
#include "InstallerWindow.h"
2005-10-26 15:42:22 +00:00
#include "PartitionMenuItem.h"
#include "FSUndoRedo.h"
#include "FSUtils.h"
#include <Alert.h>
#include <DiskDeviceVisitor.h>
#include <DiskDeviceTypes.h>
#include <FindDirectory.h>
#include <Path.h>
#include <String.h>
#include <VolumeRoster.h>
2007-05-12 13:37:02 +00:00
#define COPY_TRACE
#ifdef COPY_TRACE
#define CALLED() printf("CALLED %s\n",__PRETTY_FUNCTION__)
2007-05-12 13:37:02 +00:00
#define ERR2(x, y...) fprintf(stderr, "CopyEngine: "x" %s\n", y, strerror(err))
#define ERR(x) fprintf(stderr, "CopyEngine: "x" %s\n", strerror(err))
#else
#define CALLED()
2007-05-12 13:37:02 +00:00
#define ERR(x)
#define ERR2(x, y...)
#endif
const char BOOT_PATH[] = "/boot";
extern void SizeAsString(off_t size, char *string);
2005-10-26 19:45:38 +00:00
2007-05-12 13:37:02 +00:00
class SourceVisitor : public BDiskDeviceVisitor
{
2007-05-12 13:37:02 +00:00
public:
SourceVisitor(BMenu *menu);
virtual bool Visit(BDiskDevice *device);
virtual bool Visit(BPartition *partition, int32 level);
private:
BMenu *fMenu;
};
class TargetVisitor : public BDiskDeviceVisitor
{
2007-05-12 13:37:02 +00:00
public:
TargetVisitor(BMenu *menu);
virtual bool Visit(BDiskDevice *device);
virtual bool Visit(BPartition *partition, int32 level);
private:
void MakeLabel(BPartition *partition, char *label, char *menuLabel);
BMenu *fMenu;
};
CopyEngine::CopyEngine(InstallerWindow *window)
: BLooper("copy_engine"),
2006-01-23 17:36:24 +00:00
fWindow(window),
fPackages(NULL),
fSpaceRequired(0)
{
fControl = new InstallerCopyLoopControl(window);
Run();
}
void
CopyEngine::MessageReceived(BMessage*msg)
{
CALLED();
switch (msg->what) {
case ENGINE_START:
2007-05-12 13:37:02 +00:00
status_t err = Start(fWindow->GetSourceMenu(), fWindow->GetTargetMenu());
if (err != B_OK)
ERR("Start failed");
break;
}
}
void
CopyEngine::SetStatusMessage(char *status)
{
BMessage msg(STATUS_MESSAGE);
msg.AddString("status", status);
BMessenger(fWindow).SendMessage(&msg);
}
void
CopyEngine::LaunchInitScript(BPath &path)
{
BPath bootPath;
find_directory(B_BEOS_BOOT_DIRECTORY, &bootPath);
BString command("/bin/sh ");
command += bootPath.Path();
command += "/InstallerInitScript ";
command += path.Path();
2007-05-12 13:37:02 +00:00
SetStatusMessage("Starting Installation.");
system(command.String());
}
void
CopyEngine::LaunchFinishScript(BPath &path)
{
BPath bootPath;
find_directory(B_BEOS_BOOT_DIRECTORY, &bootPath);
BString command("/bin/sh ");
command += bootPath.Path();
command += "/InstallerFinishScript ";
command += path.Path();
2007-05-12 13:37:02 +00:00
SetStatusMessage("Finishing Installation.");
system(command.String());
}
2007-05-12 13:37:02 +00:00
status_t
CopyEngine::Start(BMenu *srcMenu, BMenu *targetMenu)
{
CALLED();
2007-05-12 13:37:02 +00:00
status_t err = B_OK;
2006-01-19 18:15:01 +00:00
PartitionMenuItem *targetItem = (PartitionMenuItem *)targetMenu->FindMarked();
PartitionMenuItem *srcItem = (PartitionMenuItem *)srcMenu->FindMarked();
if (!srcItem || !targetItem) {
2007-05-12 13:37:02 +00:00
ERR("bad menu items\n");
return B_BAD_VALUE;
}
2007-05-12 13:37:02 +00:00
// check if target is initialized
// ask if init or mount as is
2006-01-19 18:15:01 +00:00
BPath targetDirectory;
BDiskDevice device;
BPartition *partition;
BVolume targetVolume;
2006-01-19 18:15:01 +00:00
if (fDDRoster.GetPartitionWithID(targetItem->ID(), &device, &partition) == B_OK) {
2007-05-12 13:37:02 +00:00
if (!partition->IsMounted()) {
if ((err = partition->Mount()) < B_OK) {
SetStatusMessage("The disk can't be mounted. Please choose a different disk.");
ERR("BPartition::Mount");
return err;
}
}
if ((err = partition->GetVolume(&targetVolume)) != B_OK) {
ERR("BPartition::GetVolume");
return err;
}
if ((err = partition->GetMountPoint(&targetDirectory)) != B_OK) {
ERR("BPartition::GetMountPoint");
return err;
}
2006-01-19 18:15:01 +00:00
} else if (fDDRoster.GetDeviceWithID(targetItem->ID(), &device) == B_OK) {
2007-05-12 13:37:02 +00:00
if (!device.IsMounted()) {
if ((err = device.Mount()) < B_OK) {
SetStatusMessage("The disk can't be mounted. Please choose a different disk.");
ERR("BDiskDevice::Mount");
return err;
}
}
if ((err = device.GetVolume(&targetVolume)) != B_OK) {
ERR("BDiskDevice::GetVolume");
return err;
}
if ((err = device.GetMountPoint(&targetDirectory)) != B_OK) {
ERR("BDiskDevice::GetMountPoint");
return err;
}
} else
return B_ERROR; // shouldn't happen
2006-01-19 18:15:01 +00:00
// check if target has enough space
2007-05-12 13:37:02 +00:00
if ((fSpaceRequired > 0 && targetVolume.FreeBytes() < fSpaceRequired)
&& ((new BAlert("", "The destination disk may not have enough space. Try choosing a different disk or \
choose to not install optional items.", "Try installing anyway", "Cancel", 0,
2007-05-12 13:37:02 +00:00
B_WIDTH_AS_USUAL, B_STOP_ALERT))->Go() != 0)) {
return B_OK;
}
2006-01-19 18:15:01 +00:00
BPath srcDirectory;
2007-05-12 13:37:02 +00:00
if (fDDRoster.GetPartitionWithID(srcItem->ID(), &device, &partition) == B_OK) {
if ((err = partition->GetMountPoint(&srcDirectory)) != B_OK) {
ERR("BPartition::GetMountPoint");
return err;
}
} else if (fDDRoster.GetDeviceWithID(srcItem->ID(), &device) == B_OK) {
if ((err = device.GetMountPoint(&srcDirectory)) != B_OK) {
ERR("BDiskDevice::GetMountPoint");
return err;
}
} else
return B_ERROR; // shouldn't happen
// check not installing on itself
if (strcmp(srcDirectory.Path(), targetDirectory.Path()) == 0) {
SetStatusMessage("You can't install the contents of a disk onto itself. Please choose a different disk.");
2007-05-12 13:37:02 +00:00
return B_OK;
}
2007-05-12 13:37:02 +00:00
// check not installing on boot volume
2007-05-12 13:37:02 +00:00
if ((strncmp(BOOT_PATH, targetDirectory.Path(), strlen(BOOT_PATH)) == 0)
&& ((new BAlert("", "Are you sure you want to install onto the current boot disk? \
The installer will have to reboot your machine if you proceed.", "OK", "Cancel", 0,
2007-05-12 13:37:02 +00:00
B_WIDTH_AS_USUAL, B_STOP_ALERT))->Go() != 0)) {
SetStatusMessage("Installation stopped.");
2007-05-12 13:37:02 +00:00
return B_OK;
}
2006-01-19 18:15:01 +00:00
LaunchInitScript(targetDirectory);
// copy source volume
2006-01-19 18:15:01 +00:00
BDirectory targetDir(targetDirectory.Path());
BDirectory srcDir(srcDirectory.Path());
2006-01-23 17:36:24 +00:00
CopyFolder(srcDir, targetDir);
// copy selected packages
if (fPackages) {
srcDirectory.Append(PACKAGES_DIRECTORY);
srcDir.SetTo(srcDirectory.Path());
BDirectory packageDir;
2006-01-23 17:36:24 +00:00
int32 count = fPackages->CountItems();
2007-05-12 13:37:02 +00:00
for (int32 i = 0; i < count; i++) {
2006-01-23 17:36:24 +00:00
Package *p = static_cast<Package*>(fPackages->ItemAt(i));
packageDir.SetTo(&srcDir, p->Folder());
CopyFolder(packageDir, targetDir);
}
}
LaunchFinishScript(targetDirectory);
BMessage msg(INSTALL_FINISHED);
BMessenger(fWindow).SendMessage(&msg);
2006-01-23 17:36:24 +00:00
}
void
CopyEngine::CopyFolder(BDirectory &srcDir, BDirectory &targetDir)
{
BEntry entry;
2007-05-12 13:37:02 +00:00
status_t err;
while (srcDir.GetNextEntry(&entry) == B_OK) {
2006-01-23 17:36:24 +00:00
char name[B_FILE_NAME_LENGTH];
entry.GetName(name);
if (strcmp(name, PACKAGES_DIRECTORY) == 0)
continue;
Undo undo;
2007-05-12 13:37:02 +00:00
err = FSCopyFolder(&entry, &targetDir, fControl, NULL, false, undo);
if (err != B_OK) {
BPath path;
entry.GetPath(&path);
2007-05-12 13:37:02 +00:00
ERR2("error while copying %s", path.Path());
}
}
}
void
CopyEngine::ScanDisksPartitions(BMenu *srcMenu, BMenu *targetMenu)
{
BDiskDevice device;
BPartition *partition = NULL;
printf("ScanDisksPartitions partitions begin\n");
SourceVisitor srcVisitor(srcMenu);
fDDRoster.VisitEachMountedPartition(&srcVisitor, &device, &partition);
printf("ScanDisksPartitions partitions begin\n");
TargetVisitor targetVisitor(targetMenu);
fDDRoster.VisitEachPartition(&targetVisitor, &device, &partition);
}
2006-01-23 17:36:24 +00:00
void
CopyEngine::SetPackagesList(BList *list)
{
if (fPackages)
delete fPackages;
fPackages = list;
}
SourceVisitor::SourceVisitor(BMenu *menu)
: fMenu(menu)
{
}
bool
SourceVisitor::Visit(BDiskDevice *device)
{
2007-05-12 13:37:02 +00:00
if (!device->ContentType() || strcmp(device->ContentType(), kPartitionTypeBFS) != 0)
return false;
BPath path;
2007-05-12 13:37:02 +00:00
if (device->GetPath(&path) == B_OK)
printf("SourceVisitor::Visit(BDiskDevice *) : %s type:%s, contentType:%s\n",
2005-10-26 19:45:38 +00:00
path.Path(), device->Type(), device->ContentType());
PartitionMenuItem *item = new PartitionMenuItem(NULL, device->ContentName(), NULL, new BMessage(SRC_PARTITION), device->ID());
if (device->IsMounted()) {
BPath mountPoint;
device->GetMountPoint(&mountPoint);
2007-05-12 13:37:02 +00:00
if (strcmp(BOOT_PATH, mountPoint.Path()) == 0)
item->SetMarked(true);
}
fMenu->AddItem(item);
return false;
}
bool
SourceVisitor::Visit(BPartition *partition, int32 level)
{
2007-05-12 13:37:02 +00:00
if (!partition->ContentType() || strcmp(partition->ContentType(), kPartitionTypeBFS) != 0)
return false;
BPath path;
2007-05-12 13:37:02 +00:00
if (partition->GetPath(&path) == B_OK)
printf("SourceVisitor::Visit(BPartition *) : %s\n", path.Path());
printf("SourceVisitor::Visit(BPartition *) : %s\n", partition->Name());
PartitionMenuItem *item = new PartitionMenuItem(NULL, partition->ContentName(), NULL, new BMessage(SRC_PARTITION), partition->ID());
if (partition->IsMounted()) {
BPath mountPoint;
partition->GetMountPoint(&mountPoint);
2007-05-12 13:37:02 +00:00
if (strcmp(BOOT_PATH, mountPoint.Path()) == 0)
item->SetMarked(true);
}
fMenu->AddItem(item);
return false;
}
TargetVisitor::TargetVisitor(BMenu *menu)
: fMenu(menu)
{
}
bool
TargetVisitor::Visit(BDiskDevice *device)
{
if (device->IsReadOnly() || device->IsReadOnlyMedia())
return false;
BPath path;
2007-05-12 13:37:02 +00:00
if (device->GetPath(&path) == B_OK)
printf("TargetVisitor::Visit(BDiskDevice *) : %s\n", path.Path());
2005-10-26 19:45:38 +00:00
char label[255], menuLabel[255];
MakeLabel(device, label, menuLabel);
fMenu->AddItem(new PartitionMenuItem(device->ContentName(), label, menuLabel, new BMessage(TARGET_PARTITION), device->ID()));
return false;
}
bool
TargetVisitor::Visit(BPartition *partition, int32 level)
{
if (partition->IsReadOnly())
return false;
BPath path;
2007-05-12 13:37:02 +00:00
if (partition->GetPath(&path) == B_OK)
printf("TargetVisitor::Visit(BPartition *) : %s\n", path.Path());
printf("TargetVisitor::Visit(BPartition *) : %s\n", partition->Name());
2005-10-26 19:45:38 +00:00
char label[255], menuLabel[255];
MakeLabel(partition, label, menuLabel);
fMenu->AddItem(new PartitionMenuItem(partition->ContentName(), label, menuLabel, new BMessage(TARGET_PARTITION), partition->ID()));
return false;
}
2005-10-26 19:45:38 +00:00
void
TargetVisitor::MakeLabel(BPartition *partition, char *label, char *menuLabel)
{
char size[15];
SizeAsString(partition->ContentSize(), size);
BPath path;
if (partition->Parent())
partition->Parent()->GetPath(&path);
2007-05-12 13:37:02 +00:00
2005-10-26 19:45:38 +00:00
sprintf(label, "%s - %s [%s] [%s partition:%li]", partition->ContentName(), size, partition->ContentType(),
2007-05-12 13:37:02 +00:00
path.Path(), partition->ID());
2005-10-26 19:45:38 +00:00
sprintf(menuLabel, "%s - %s [%s]", partition->ContentName(), size, partition->ContentType());
}