1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
/* $Id: HostDriveImpl.cpp $ */
/** @file
* VirtualBox Main - IHostDrive implementation, VBoxSVC.
*/
/*
* Copyright (C) 2013-2022 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, in version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses>.
*
* SPDX-License-Identifier: GPL-3.0-only
*/
#define LOG_GROUP LOG_GROUP_MAIN_HOSTDRIVE
#include "Global.h"
#include "HostDriveImpl.h"
#include "HostDrivePartitionImpl.h"
#include "LoggingNew.h"
#include "VirtualBoxImpl.h"
#include <iprt/dvm.h>
#include <iprt/err.h>
#include <iprt/file.h>
#include <iprt/path.h>
#include <iprt/vfs.h>
#include <VBox/com/Guid.h>
/*
* HostDrive implementation.
*/
DEFINE_EMPTY_CTOR_DTOR(HostDrive)
HRESULT HostDrive::FinalConstruct()
{
return BaseFinalConstruct();
}
void HostDrive::FinalRelease()
{
uninit();
BaseFinalRelease();
}
/**
* Initializes the instance.
*/
HRESULT HostDrive::initFromPathAndModel(const com::Utf8Str &drivePath, const com::Utf8Str &driveModel)
{
LogFlowThisFunc(("\n"));
AssertReturn(!drivePath.isEmpty(), E_INVALIDARG);
/* Enclose the state transition NotReady->InInit->Ready */
AutoInitSpan autoInitSpan(this);
AssertReturn(autoInitSpan.isOk(), E_FAIL);
m.partitioningType = PartitioningType_MBR;
m.drivePath = drivePath;
m.model = driveModel;
m.partitions.clear();
/*
* Try open the drive so we can extract futher details,
* like the size, sector size and partitions.
*/
HRESULT hrc = E_FAIL;
RTFILE hRawFile = NIL_RTFILE;
int vrc = RTFileOpen(&hRawFile, drivePath.c_str(), RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
if (RT_SUCCESS(vrc))
{
vrc = RTFileQuerySize(hRawFile, &m.cbDisk);
int vrc2 = RTFileQuerySectorSize(hRawFile, &m.cbSector);
if (RT_FAILURE(vrc2))
vrc = vrc2;
if (RT_SUCCESS(vrc))
{
/*
* Hand it to DVM.
*/
RTVFSFILE hVfsFile = NIL_RTVFSFILE;
vrc = RTVfsFileFromRTFile(hRawFile, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE /*fOpen*/,
true /*fLeaveOpen*/, &hVfsFile);
if (RT_SUCCESS(vrc))
{
RTDVM hVolMgr = NIL_RTDVM;
vrc = RTDvmCreate(&hVolMgr, hVfsFile, m.cbSector, 0 /*fFlags*/);
if (RT_SUCCESS(vrc))
{
vrc = RTDvmMapOpen(hVolMgr);
if (RT_SUCCESS(vrc))
{
hrc = S_OK;
/*
* Get details.
*/
switch (RTDvmMapGetFormatType(hVolMgr))
{
case RTDVMFORMATTYPE_GPT:
m.partitioningType = PartitioningType_GPT;
break;
case RTDVMFORMATTYPE_MBR:
m.partitioningType = PartitioningType_MBR;
break;
case RTDVMFORMATTYPE_BSD_LABEL:
AssertMsgFailed(("TODO\n"));
break;
default:
AssertFailed();
}
RTUUID Uuid = RTUUID_INITIALIZE_NULL;
if (RT_SUCCESS(RTDvmMapQueryDiskUuid(hVolMgr, &Uuid)))
m.uuid = Uuid;
/*
* Enumerate volumes and tuck them into the partitions list..
*/
uint32_t const cVolumes = RTDvmMapGetValidVolumes(hVolMgr);
RTDVMVOLUME hVol = NIL_RTDVMVOLUME;
for (uint32_t i = 0; i < cVolumes; i++)
{
/* Enumeration cruft: */
RTDVMVOLUME hVolNext = NIL_RTDVMVOLUME;
if (i == 0)
vrc = RTDvmMapQueryFirstVolume(hVolMgr, &hVolNext);
else
vrc = RTDvmMapQueryNextVolume(hVolMgr, hVol, &hVolNext);
AssertRCBreakStmt(vrc, hrc = Global::vboxStatusCodeToCOM(vrc));
uint32_t cRefs = RTDvmVolumeRelease(hVol);
Assert(cRefs != UINT32_MAX); RT_NOREF(cRefs);
hVol = hVolNext;
/* Instantiate a new partition object and add it to the list: */
ComObjPtr<HostDrivePartition> ptrHostPartition;
hrc = ptrHostPartition.createObject();
if (SUCCEEDED(hrc))
hrc = ptrHostPartition->initFromDvmVol(hVol);
if (SUCCEEDED(hrc))
try
{
m.partitions.push_back(ptrHostPartition);
}
catch (std::bad_alloc &)
{
AssertFailedBreakStmt(hrc = E_OUTOFMEMORY);
}
}
RTDvmVolumeRelease(hVol);
}
else
hrc = Global::vboxStatusCodeToCOM(vrc);
RTDvmRelease(hVolMgr);
}
else
hrc = Global::vboxStatusCodeToCOM(vrc);
RTVfsFileRelease(hVfsFile);
}
else
hrc = Global::vboxStatusCodeToCOM(vrc);
}
else /* VERR_IO_NOT_READ / STATUS_NO_MEDIA_IN_DEVICE is likely for card readers on windows. */
hrc = Global::vboxStatusCodeToCOM(vrc);
RTFileClose(hRawFile);
}
else
{
/*
* We don't use the Global::vboxStatusCodeToCOM(vrc) here
* because RTFileOpen can return some error which causes
* the assertion and breaks original idea of returning
* the object in the limited state.
*/
if ( vrc == VERR_RESOURCE_BUSY
|| vrc == VERR_ACCESS_DENIED)
hrc = E_ACCESSDENIED;
else
hrc = VBOX_E_IPRT_ERROR;
}
/* Confirm a successful initialization */
if (SUCCEEDED(hrc))
autoInitSpan.setSucceeded();
else
autoInitSpan.setLimited(hrc);
return S_OK;
}
/**
* Uninitializes the instance.
* Called either from FinalRelease() or by the parent when it gets destroyed.
*/
void HostDrive::uninit()
{
LogFlowThisFunc(("\n"));
/* Enclose the state transition Ready->InUninit->NotReady */
AutoUninitSpan autoUninitSpan(this);
if (autoUninitSpan.uninitDone())
return;
m.drivePath.setNull();
m.partitions.clear();
}
/*********************************************************************************************************************************
* IHostDrive properties *
*********************************************************************************************************************************/
HRESULT HostDrive::getPartitioningType(PartitioningType_T *aPartitioningType)
{
*aPartitioningType = m.partitioningType;
return S_OK;
}
HRESULT HostDrive::getDrivePath(com::Utf8Str &aDrivePath)
{
aDrivePath = m.drivePath;
return S_OK;
}
HRESULT HostDrive::getUuid(com::Guid &aUuid)
{
aUuid = m.uuid;
return S_OK;
}
HRESULT HostDrive::getSectorSize(ULONG *aSectorSize)
{
*aSectorSize = m.cbSector;
return S_OK;
}
HRESULT HostDrive::getSize(LONG64 *aSize)
{
*aSize = (LONG64)m.cbDisk;
if (*aSize < 0)
*aSize = INT64_MAX;
return S_OK;
}
HRESULT HostDrive::getModel(com::Utf8Str &aModel)
{
return aModel.assignEx(m.model);
}
HRESULT HostDrive::getPartitions(std::vector<ComPtr<IHostDrivePartition> > &aPartitions)
{
aPartitions = m.partitions;
return S_OK;
}
/* vi: set tabstop=4 shiftwidth=4 expandtab: */
|