xref: /haiku/src/apps/installer/CopyEngine.cpp (revision d081e691a0b4858c78af5955b5de5cf94e8d0f35)
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 				|| strcmp(name, VAR_DIRECTORY) == 0)) {
273 				continue;
274 			}
275 			err = FSCopyFolder(&entry, &targetDir, fControl, NULL, false, undo);
276 		} else {
277 			err = FSCopyFile(&entry, &statbuf, &targetDir, fControl, NULL,
278 				false, undo);
279 		}
280 		if (err != B_OK) {
281 			BPath path;
282 			entry.GetPath(&path);
283 			ERR2("error while copying %s", path.Path());
284 			return err;
285 		}
286 	}
287 
288 	return B_OK;
289 }
290 
291 
292 void
293 CopyEngine::ScanDisksPartitions(BMenu *srcMenu, BMenu *targetMenu)
294 {
295 	BDiskDevice device;
296 	BPartition *partition = NULL;
297 
298 	printf("ScanDisksPartitions partitions begin\n");
299 	SourceVisitor srcVisitor(srcMenu);
300 	fDDRoster.VisitEachMountedPartition(&srcVisitor, &device, &partition);
301 
302 	printf("ScanDisksPartitions partitions begin\n");
303 	TargetVisitor targetVisitor(targetMenu);
304 	fDDRoster.VisitEachPartition(&targetVisitor, &device, &partition);
305 }
306 
307 
308 void
309 CopyEngine::SetPackagesList(BList *list)
310 {
311 	delete fPackages;
312 	fPackages = list;
313 }
314 
315 
316 bool
317 CopyEngine::Cancel()
318 {
319 	return fControl->Cancel();
320 }
321 
322 
323 // #pragma mark -
324 
325 
326 SourceVisitor::SourceVisitor(BMenu *menu)
327 	: fMenu(menu)
328 {
329 }
330 
331 bool
332 SourceVisitor::Visit(BDiskDevice *device)
333 {
334 	if (!device->ContentType()
335 		|| strcmp(device->ContentType(), kPartitionTypeBFS) != 0)
336 		return false;
337 	BPath path;
338 	if (device->GetPath(&path) == B_OK)
339 		printf("SourceVisitor::Visit(BDiskDevice *) : %s type:%s, "
340 			"contentType:%s\n", path.Path(), device->Type(),
341 			device->ContentType());
342 	PartitionMenuItem *item = new PartitionMenuItem(NULL, device->ContentName(), NULL, new BMessage(SRC_PARTITION), device->ID());
343 	if (device->IsMounted()) {
344 		BPath mountPoint;
345 		device->GetMountPoint(&mountPoint);
346 		if (strcmp(BOOT_PATH, mountPoint.Path()) == 0)
347 			item->SetMarked(true);
348 	}
349 	fMenu->AddItem(item);
350 	return false;
351 }
352 
353 
354 bool
355 SourceVisitor::Visit(BPartition *partition, int32 level)
356 {
357 	if (!partition->ContentType()
358 		|| strcmp(partition->ContentType(), kPartitionTypeBFS) != 0)
359 		return false;
360 	BPath path;
361 	if (partition->GetPath(&path) == B_OK)
362 		printf("SourceVisitor::Visit(BPartition *) : %s\n", path.Path());
363 	printf("SourceVisitor::Visit(BPartition *) : %s\n", partition->Name());
364 	PartitionMenuItem *item = new PartitionMenuItem(NULL,
365 		partition->ContentName(), NULL, new BMessage(SRC_PARTITION),
366 		partition->ID());
367 	if (partition->IsMounted()) {
368 		BPath mountPoint;
369 		partition->GetMountPoint(&mountPoint);
370 		if (strcmp(BOOT_PATH, mountPoint.Path()) == 0)
371 			item->SetMarked(true);
372 	}
373 	fMenu->AddItem(item);
374 	return false;
375 }
376 
377 
378 // #pragma mark -
379 
380 
381 TargetVisitor::TargetVisitor(BMenu *menu)
382 	: fMenu(menu)
383 {
384 }
385 
386 
387 bool
388 TargetVisitor::Visit(BDiskDevice *device)
389 {
390 	if (device->IsReadOnly() || device->IsReadOnlyMedia())
391 		return false;
392 	BPath path;
393 	if (device->GetPath(&path) == B_OK)
394 		printf("TargetVisitor::Visit(BDiskDevice *) : %s\n", path.Path());
395 	char label[255], menuLabel[255];
396 	_MakeLabel(device, label, menuLabel);
397 	fMenu->AddItem(new PartitionMenuItem(device->ContentName(), label,
398 		menuLabel, new BMessage(TARGET_PARTITION), device->ID()));
399 	return false;
400 }
401 
402 
403 bool
404 TargetVisitor::Visit(BPartition *partition, int32 level)
405 {
406 	if (partition->IsReadOnly())
407 		return false;
408 	BPath path;
409 	if (partition->GetPath(&path) == B_OK)
410 		printf("TargetVisitor::Visit(BPartition *) : %s\n", path.Path());
411 	printf("TargetVisitor::Visit(BPartition *) : %s\n", partition->Name());
412 	char label[255], menuLabel[255];
413 	_MakeLabel(partition, label, menuLabel);
414 	fMenu->AddItem(new PartitionMenuItem(partition->ContentName(), label,
415 		menuLabel, new BMessage(TARGET_PARTITION), partition->ID()));
416 	return false;
417 }
418 
419 
420 void
421 TargetVisitor::_MakeLabel(BPartition *partition, char *label, char *menuLabel)
422 {
423 	char size[15];
424 	SizeAsString(partition->ContentSize(), size);
425 	BPath path;
426 	if (partition->Parent())
427 		partition->Parent()->GetPath(&path);
428 
429 	sprintf(label, "%s - %s [%s] [%s partition:%li]", partition->ContentName(),
430 		size, partition->ContentType(), path.Path(), partition->ID());
431 	sprintf(menuLabel, "%s - %s [%s]", partition->ContentName(), size,
432 		partition->ContentType());
433 
434 }
435 
436