summaryrefslogtreecommitdiffstats
path: root/chrome/browser/metrics/drive_metrics_provider_win.cc
blob: 70cf3228efaf74432e6eed604b2b858557b6f44c (plain)
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
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/metrics/drive_metrics_provider.h"

#include <windows.h>
#include <ntddscsi.h>
#include <winioctl.h>
#include <vector>

#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/strings/stringprintf.h"
#include "base/win/windows_version.h"

namespace {

// Semi-copy of similarly named struct from ata.h in WinDDK.
struct IDENTIFY_DEVICE_DATA {
  USHORT UnusedWords[217];
  USHORT NominalMediaRotationRate;
  USHORT MoreUnusedWords[38];
};
COMPILE_ASSERT(sizeof(IDENTIFY_DEVICE_DATA) == 512, IdentifyDeviceDataSize);

struct AtaRequest {
  ATA_PASS_THROUGH_EX query;
  IDENTIFY_DEVICE_DATA result;
};

}  // namespace

// static
bool DriveMetricsProvider::HasSeekPenalty(const base::FilePath& path,
                                          bool* has_seek_penalty) {
  std::vector<base::FilePath::StringType> components;
  path.GetComponents(&components);

  int flags = base::File::FLAG_OPEN;
  bool win7_or_higher = base::win::GetVersion() >= base::win::VERSION_WIN7;
  if (!win7_or_higher)
    flags |= base::File::FLAG_READ | base::File::FLAG_WRITE;

  base::File volume(base::FilePath(L"\\\\.\\" + components[0]), flags);
  if (!volume.IsValid())
    return false;

  if (win7_or_higher) {
    STORAGE_PROPERTY_QUERY query = {};
    query.QueryType = PropertyStandardQuery;
    query.PropertyId = StorageDeviceSeekPenaltyProperty;

    DEVICE_SEEK_PENALTY_DESCRIPTOR result;
    DWORD bytes_returned;
    BOOL success = DeviceIoControl(volume.GetPlatformFile(),
                                   IOCTL_STORAGE_QUERY_PROPERTY,
                                   &query, sizeof(query),
                                   &result, sizeof(result),
                                   &bytes_returned, NULL);
    if (success == FALSE || bytes_returned < sizeof(result))
      return false;

    *has_seek_penalty = result.IncursSeekPenalty != FALSE;
  } else {
    AtaRequest request = {};
    request.query.AtaFlags = ATA_FLAGS_DATA_IN;
    request.query.CurrentTaskFile[6] = ID_CMD;
    request.query.DataBufferOffset = sizeof(request.query);
    request.query.DataTransferLength = sizeof(request.result);
    request.query.Length = sizeof(request.query);
    request.query.TimeOutValue = 10;

    DWORD bytes_returned;
    BOOL success = DeviceIoControl(volume.GetPlatformFile(),
                                   IOCTL_ATA_PASS_THROUGH,
                                   &request, sizeof(request),
                                   &request, sizeof(request),
                                   &bytes_returned, NULL);
    if (success == FALSE || bytes_returned < sizeof(request) ||
        request.query.CurrentTaskFile[0]) {
      return false;
    }

    *has_seek_penalty = request.result.NominalMediaRotationRate != 1;
  }

  return true;
}