1/*
2 * Copyright 2011, Axel D��rfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
5
6
7#include "BootDrive.h"
8
9#include <DiskDevice.h>
10#include <DiskDeviceRoster.h>
11#include <Volume.h>
12#include <VolumeRoster.h>
13
14
15BootDrive::BootDrive(const char* path)
16	:
17	fPath(path)
18{
19}
20
21
22BootDrive::~BootDrive()
23{
24}
25
26
27BootMenu*
28BootDrive::InstalledMenu(const BootMenuList& menus) const
29{
30	for (int32 i = 0; i < menus.CountItems(); i++) {
31		BootMenu* menu = menus.ItemAt(i);
32		if (menu->IsInstalled(*this))
33			return menu;
34	}
35	return NULL;
36}
37
38
39status_t
40BootDrive::CanMenuBeInstalled(const BootMenuList& menus) const
41{
42	status_t status = B_ERROR;
43
44	for (int32 i = 0; i < menus.CountItems(); i++) {
45		status = menus.ItemAt(i)->CanBeInstalled(*this);
46		if (status == B_OK)
47			return status;
48	}
49	return status;
50}
51
52
53/*!	Adds all boot menus from the list \a from that support the drive to \a to.
54*/
55void
56BootDrive::AddSupportedMenus(const BootMenuList& from, BootMenuList& to)
57{
58	for (int32 i = 0; i < from.CountItems(); i++) {
59		BootMenu* menu = from.ItemAt(i);
60		if (menu->CanBeInstalled(*this))
61			to.AddItem(menu);
62	}
63}
64
65
66const char*
67BootDrive::Path() const
68{
69	return fPath.Path();
70}
71
72
73bool
74BootDrive::IsBootDrive() const
75{
76	BVolumeRoster volumeRoster;
77	BVolume volume;
78	if (volumeRoster.GetBootVolume(&volume) != B_OK)
79		return false;
80
81	BDiskDeviceRoster roster;
82	BDiskDevice device;
83	if (roster.FindPartitionByVolume(volume, &device, NULL) == B_OK) {
84		BPath path;
85		if (device.GetPath(&path) == B_OK && path == fPath)
86			return true;
87	}
88
89	return false;
90}
91
92
93status_t
94BootDrive::GetDiskDevice(BDiskDevice& device) const
95{
96	BDiskDeviceRoster roster;
97	return roster.GetDeviceForPath(fPath.Path(), &device);
98}
99