forked from google/omaha
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisk.cc
More file actions
397 lines (334 loc) · 12.7 KB
/
Copy pathdisk.cc
File metadata and controls
397 lines (334 loc) · 12.7 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// Copyright 2004-2009 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ========================================================================
//
// Disk functions
#include "omaha/base/disk.h"
#include <winioctl.h>
#include "omaha/base/commontypes.h"
#include "omaha/base/const_config.h"
#include "omaha/base/debug.h"
#include "omaha/base/error.h"
#include "omaha/base/file.h"
#include "omaha/base/localization.h"
#include "omaha/base/logging.h"
#include "omaha/base/safe_format.h"
#include "omaha/base/shell.h"
#include "omaha/base/string.h"
#include "omaha/base/synchronized.h"
#include "omaha/base/system.h"
#include "omaha/base/timer.h"
#include "omaha/base/utils.h"
namespace omaha {
#define kNetdiskVendorId "netdisk"
// see also: http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q264203
#if _MSC_VER < 1400
// Not defined in the headers we have; from MSDN:
#define IOCTL_STORAGE_QUERY_PROPERTY \
CTL_CODE(IOCTL_STORAGE_BASE, 0x0500, METHOD_BUFFERED, FILE_ANY_ACCESS)
typedef struct _STORAGE_DEVICE_DESCRIPTOR {
ULONG Version;
ULONG Size;
UCHAR DeviceType;
UCHAR DeviceTypeModifier;
BOOLEAN RemovableMedia;
BOOLEAN CommandQueueing;
ULONG VendorIdOffset;
ULONG ProductIdOffset;
ULONG ProductRevisionOffset;
ULONG SerialNumberOffset;
STORAGE_BUS_TYPE BusType;
ULONG RawPropertiesLength;
UCHAR RawDeviceProperties[1];
} STORAGE_DEVICE_DESCRIPTOR, *PSTORAGE_DEVICE_DESCRIPTOR;
typedef enum _STORAGE_QUERY_TYPE {
PropertyStandardQuery = 0,
PropertyExistsQuery,
PropertyMaskQuery,
PropertyQueryMaxDefined
} STORAGE_QUERY_TYPE, *PSTORAGE_QUERY_TYPE;
typedef enum _STORAGE_PROPERTY_ID {
StorageDeviceProperty = 0,
StorageAdapterProperty,
StorageDeviceIdProperty
} STORAGE_PROPERTY_ID, *PSTORAGE_PROPERTY_ID;
typedef struct _STORAGE_PROPERTY_QUERY {
STORAGE_PROPERTY_ID PropertyId;
STORAGE_QUERY_TYPE QueryType;
UCHAR AdditionalParameters[1];
} STORAGE_PROPERTY_QUERY, *PSTORAGE_PROPERTY_QUERY;
// -------
#endif
#define kIoctlBufferSize 1024
#define kMaxDrivesCached 3
#define kMaxDriveLen 20
static TCHAR g_cache_drive[kMaxDrivesCached][kMaxDriveLen+1];
static bool g_cache_external[kMaxDrivesCached];
static int g_cache_pos;
static LLock g_cache_lock;
bool IsDiskExternal(const TCHAR *drive) {
ASSERT(drive, (L""));
ASSERT(lstrlen(drive) < kMaxDriveLen, (L""));
DisableThreadErrorUI disable_error_dialog_box;
{
__mutexScope(g_cache_lock);
for (int i = 0; i < kMaxDrivesCached; i++)
if (!lstrcmp(drive, g_cache_drive[i])) {
UTIL_LOG(L1, (L"cached disk ext %s %d", drive, g_cache_external[i]));
return g_cache_external[i];
}
}
#ifdef _DEBUG
Timer timer(true);
#endif
byte buffer[kIoctlBufferSize+1];
bool external = false;
HANDLE device = ::CreateFile(drive,
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
NULL,
NULL);
if (device == INVALID_HANDLE_VALUE) {
UTIL_LOG(L1, (L"disk external could not open drive %s", drive));
goto done;
}
STORAGE_DEVICE_DESCRIPTOR *device_desc;
STORAGE_PROPERTY_QUERY query;
DWORD out_bytes;
query.PropertyId = StorageDeviceProperty;
query.QueryType = PropertyStandardQuery;
*(query.AdditionalParameters) = 0;
device_desc = reinterpret_cast<STORAGE_DEVICE_DESCRIPTOR*>(buffer);
// should not be needed, but just to be safer
ZeroMemory(buffer, kIoctlBufferSize);
BOOL ok = ::DeviceIoControl(device,
IOCTL_STORAGE_QUERY_PROPERTY,
&query,
sizeof(STORAGE_PROPERTY_QUERY),
buffer,
kIoctlBufferSize,
&out_bytes,
(LPOVERLAPPED)NULL);
if (ok &&
device_desc->VendorIdOffset &&
stristr(reinterpret_cast<char*>(buffer + device_desc->VendorIdOffset),
kNetdiskVendorId)) {
external = true;
UTIL_LOG(L1, (L"ximeta netdisk %s", drive));
}
if (ok &&
(device_desc->BusType == BusTypeUsb ||
device_desc->BusType == BusType1394)) {
external = true;
}
if (!ok) {
UTIL_LOG(L1, (L"disk external ioctl failed %s", drive));
}
CloseHandle(device);
done:
UTIL_LOG(L1, (L"disk external %s %d time %s",
drive, external, String_DoubleToString(timer.GetMilliseconds(), 3)));
{
__mutexScope(g_cache_lock);
lstrcpyn(g_cache_drive[g_cache_pos], drive, kMaxDriveLen+1);
g_cache_external[g_cache_pos] = external;
if (++g_cache_pos >= kMaxDrivesCached) g_cache_pos = 0;
}
return external;
}
// find the first fixed local disk with at least the space requested
// confirms that we can create a directory on the drive
// returns the drive in the drive parameter
// returns E_FAIL if no drive with enough space could be found
HRESULT FindFirstLocalDriveWithEnoughSpace(const uint64 space_required,
CString *drive) {
ASSERT1(drive);
DisableThreadErrorUI disable_error_dialog_box;
const int kMaxNumDrives = 26;
static const size_t kBufLen = (STR_SIZE("c:\\\0") * kMaxNumDrives) + 1;
// obtain the fixed system drives
TCHAR buf[kBufLen];
DWORD str_len = ::GetLogicalDriveStrings(kBufLen, buf);
if (str_len > 0 && str_len < kBufLen) {
for (TCHAR* ptr = buf; *ptr != L'\0'; ptr += (lstrlen(ptr) + 1)) {
UINT drive_type = GetDriveType(ptr);
if (drive_type == DRIVE_FIXED) {
CString test_drive(ptr);
if (!IsDiskExternal(CString(L"\\\\?\\") + test_drive.Left(2))) {
uint64 free_disk_space = 0;
HRESULT hr = GetFreeDiskSpace(test_drive, &free_disk_space);
if (SUCCEEDED(hr) && space_required <= free_disk_space) {
CString temp_dir;
// confirm that we can create a directory on this drive
bool found = false;
while (!found) {
temp_dir = test_drive +
NOTRANSL(L"test") +
itostr(static_cast<uint32>(::GetTickCount()));
if (!File::Exists (temp_dir)) found = true;
}
if (SUCCEEDED(CreateDir(temp_dir, NULL))) {
VERIFY1(SUCCEEDED(DeleteDirectory(temp_dir)));
*drive = test_drive;
UTIL_LOG(L1, (L"drive %s enough space %d", test_drive.GetString(),
free_disk_space));
return S_OK;
}
}
}
}
}
}
return E_FAIL;
}
// Get free disk space of a drive containing the specified folder
HRESULT GetFreeDiskSpace(uint32 csidl, uint64* free_disk_space) {
ASSERT1(free_disk_space);
CString path;
RET_IF_FAILED(Shell::GetSpecialFolder(csidl, false, &path));
return GetFreeDiskSpace(path, free_disk_space);
}
// Get free disk space of a drive containing the specified folder
HRESULT GetFreeDiskSpace(const TCHAR* folder, uint64* free_disk_space) {
ASSERT1(folder && *folder);
ASSERT1(free_disk_space);
DisableThreadErrorUI disable_error_dialog_box;
CString drive(folder);
// (Stupid API used by System::GetDiskStatistics will work with any folder -
// as long as it EXISTS. Since the data storage folder might not exist yet
// (e.g., on a clean install) we'll just truncate it down to a drive letter.)
drive = drive.Left(3); // "X:\"
ASSERT1(String_EndsWith(drive, _T(":\\"), false));
// Get the free disk space available to this user on this drive
uint64 free_bytes_current_user = 0LL;
uint64 total_bytes_current_user = 0LL;
uint64 free_bytes_all_users = 0LL;
RET_IF_FAILED(System::GetDiskStatistics(drive,
&free_bytes_current_user,
&total_bytes_current_user,
&free_bytes_all_users));
*free_disk_space = std::min(free_bytes_current_user, free_bytes_all_users);
return S_OK;
}
// Has enough free disk space on a drive containing the specified folder
HRESULT HasEnoughFreeDiskSpace(uint32 csidl, uint64 disk_space_needed) {
uint64 free_disk_space = 0;
if (SUCCEEDED(GetFreeDiskSpace(csidl, &free_disk_space))) {
return (disk_space_needed <= free_disk_space) ?
S_OK : CI_E_NOT_ENOUGH_DISK_SPACE;
}
return S_OK;
}
// Has enough free disk space on a drive containing the specified folder
HRESULT HasEnoughFreeDiskSpace(const TCHAR* folder, uint64 disk_space_needed) {
uint64 free_disk_space = 0;
if (SUCCEEDED(GetFreeDiskSpace(folder, &free_disk_space))) {
return (disk_space_needed <= free_disk_space) ?
S_OK : CI_E_NOT_ENOUGH_DISK_SPACE;
}
return S_OK;
}
// The ::CreateFile() call will fail for mapped local drives (subst).
bool IsHotPluggable(const TCHAR* drive) {
ASSERT(drive, (L""));
// Disable potential error dialogs during this check
DisableThreadErrorUI disable_error_dialog_box;
//
// We set the default return value to true so that
// we treat the disk as hot-pluggable in case we
// don't know.
//
bool ret = true;
if (drive && lstrlen(drive) >= 2) {
CString volume_path(_T("\\\\.\\"));
// We don't want the trailing backslash.
volume_path.Append(drive, 2);
CHandle volume(::CreateFile(volume_path, GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL));
if (volume != INVALID_HANDLE_VALUE) {
STORAGE_HOTPLUG_INFO shi = {0};
shi.Size = sizeof(shi);
DWORD bytes_returned = 0;
if (::DeviceIoControl(volume, IOCTL_STORAGE_GET_HOTPLUG_INFO, NULL, 0,
&shi, sizeof(STORAGE_HOTPLUG_INFO), &bytes_returned,
NULL)) {
ret = (shi.DeviceHotplug != false);
} else {
UTIL_LOG(LW, (_T("[::DeviceIoControl failed][%u]"), ::GetLastError()));
}
} else {
UTIL_LOG(LW, (_T("[::CreateFile failed][%u]"), ::GetLastError()));
}
} else {
ASSERT(false, (L"Invalid path"));
}
return ret;
}
bool IsLargeDrive(const TCHAR* drive) {
ASSERT1(drive && *drive);
DisableThreadErrorUI disable_error_dialog_box;
ULARGE_INTEGER caller_free_bytes = {0};
ULARGE_INTEGER total_bytes = {0};
ULARGE_INTEGER total_free_bytes = {0};
if (!::GetDiskFreeSpaceEx(drive,
&caller_free_bytes,
&total_bytes,
&total_free_bytes)) {
HRESULT hr = HRESULTFromLastError();
UTIL_LOG(LEVEL_ERROR,
(_T("[IsLargeDrive - failed to GetDiskFreeSpaceEx][0x%x]"), hr));
return false;
}
return (total_bytes.QuadPart > kLargeDriveSize);
}
HRESULT DevicePathToDosPath(const TCHAR* device_path, CString* dos_path) {
ASSERT1(device_path);
ASSERT1(dos_path);
dos_path->Empty();
TCHAR drive_strings[MAX_PATH] = _T("");
if (!::GetLogicalDriveStrings(arraysize(drive_strings), drive_strings)) {
UTIL_LOG(L4, (_T("[DevicePathToDosPath-GetLogicalDriveStrings fail][0x%x]"),
HRESULTFromLastError()));
return HRESULTFromLastError();
}
// Drive strings are stored as a set of null terminated strings, with an
// extra null after the last string. Each drive string is of the form "C:\".
// We convert it to the form "C:", which is the format expected by
// ::QueryDosDevice().
TCHAR drive_colon[3] = _T(" :");
for (const TCHAR* next_drive_letter = drive_strings;
*next_drive_letter;
next_drive_letter += _tcslen(next_drive_letter) + 1) {
// Dos device of the form "C:".
*drive_colon = *next_drive_letter;
TCHAR device_name[MAX_PATH] = _T("");
if (!::QueryDosDevice(drive_colon, device_name, arraysize(device_name))) {
UTIL_LOG(LEVEL_ERROR, (_T("[QueryDosDevice failed][0x%x]"),
HRESULTFromLastError()));
continue;
}
size_t name_length = _tcslen(device_name);
if (_tcsnicmp(device_path, device_name, name_length) == 0) {
// Construct DOS path.
SafeCStringFormat(dos_path, _T("%s%s"),
drive_colon, device_path + name_length);
return S_OK;
}
}
return HRESULT_FROM_WIN32(ERROR_INVALID_DRIVE);
}
} // namespace omaha