1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/metrics/drive_metrics_provider.h"
6
7 #include <windows.h>
8
9 #include <winioctl.h>
10
11 #include <vector>
12
13 #include "base/files/file.h"
14 #include "base/files/file_path.h"
15
16 namespace metrics {
17
18 // static
HasSeekPenalty(const base::FilePath & path,bool * has_seek_penalty)19 bool DriveMetricsProvider::HasSeekPenalty(const base::FilePath& path,
20 bool* has_seek_penalty) {
21 std::vector<base::FilePath::StringType> components = path.GetComponents();
22
23 base::File volume(base::FilePath(L"\\\\.\\" + components[0]),
24 base::File::FLAG_OPEN);
25 if (!volume.IsValid())
26 return false;
27
28 STORAGE_PROPERTY_QUERY query = {};
29 query.QueryType = PropertyStandardQuery;
30 query.PropertyId = StorageDeviceSeekPenaltyProperty;
31
32 DEVICE_SEEK_PENALTY_DESCRIPTOR result;
33 DWORD bytes_returned;
34
35 BOOL success = DeviceIoControl(
36 volume.GetPlatformFile(), IOCTL_STORAGE_QUERY_PROPERTY, &query,
37 sizeof(query), &result, sizeof(result), &bytes_returned, nullptr);
38
39 if (success == FALSE || bytes_returned < sizeof(result))
40 return false;
41
42 *has_seek_penalty = result.IncursSeekPenalty != FALSE;
43 return true;
44 }
45
46 } // namespace metrics
47