drive_metrics_provider_win.cc 1.3 KB

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