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