File: drive_info.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (109 lines) | stat: -rw-r--r-- 4,017 bytes parent folder | download | duplicates (5)
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_FILES_DRIVE_INFO_H_
#define BASE_FILES_DRIVE_INFO_H_

#include <stdint.h>
#include <time.h>

#include <optional>
#include <string>
#include <string_view>

#include "base/base_export.h"
#include "base/files/file_path.h"
#include "build/build_config.h"

#if BUILDFLAG(IS_MAC)
#include <IOKit/IOKitLib.h>
#endif

namespace base {

// Used to hold information about either a drive, or of a combination of a
// partition residing on a drive and the drive itself, depending on how the
// object was constructed. In general, when calling GetFileDriveInfo(), this
// latter case is the one which should be considered. On macOS, whole media can
// be queried by using by calling GetIOObjectDriveInfo() with an `IOObject`
// obtained via IOServiceGetMatchingService() with `kIOMediaWholeKey` set to
// `true`.
//
// Each of these parameters can fail to be retrieved from the OS, and so they
// can each be empty.
//
// If you add more fields to this structure (platform-specific fields are OK),
// make sure to update all functions that use it in
// file_util_{win|posix|mac|ios}.cc, too.
struct BASE_EXPORT DriveInfo {
  DriveInfo();
  DriveInfo(const DriveInfo&) = delete;
  DriveInfo(DriveInfo&&);
  DriveInfo& operator=(const DriveInfo&) = delete;
  DriveInfo& operator=(DriveInfo&&);
  ~DriveInfo();

  // Whether the drive has a seek penalty (i.e. is or is not a spinning disk).
  std::optional<bool> has_seek_penalty;

#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX) || \
    BUILDFLAG(IS_CHROMEOS)
  // Whether the drive is a "removable" drive.
  //
  // In macOS's IOKit API, a drive is "removable" if "the media is removable
  // from the drive mechanism" (e.g. DVD media), and can be further marked as
  // "ejectable" if it can be "eject[ed] from the drive mechanism under software
  // control" (also e.g. DVD media). If a drive is marked as being "removable"
  // as per IOKit, then `is_removable` is set to true.
  //
  // However, in the Finder, all drives connected via external I/O busses are
  // marked with an ⏏ icon to allow the user to initiate an unmount on the drive
  // in preparation for disconnection. Because the Finder offers that ⏏ action,
  // on macOS, such drives also have `is_removable` set to true.
  //
  // However, on Windows, drives in similar situations are not marked as
  // "ejectable" in Explorer, and thus `is_removable` is set to false in those
  // cases. For Windows, `is_removable` is a strict reflection of the
  // `RemovableMedia` flag in `STORAGE_DEVICE_DESCRIPTOR`.
  std::optional<bool> is_removable;

  // The size of the media, in bytes.
  std::optional<int64_t> size_bytes;
#endif
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
  // Whether the drive is connected over USB.
  std::optional<bool> is_usb;
#endif
#if BUILDFLAG(IS_MAC)
  // Whether the drive is a CoreStorage volume.
  std::optional<bool> is_core_storage;

  // Whether the drive is an APFS container.
  std::optional<bool> is_apfs;

  // Whether the drive can be written to.
  std::optional<bool> is_writable;

  // The BSD name is the filename at which the drive is found under /dev. For
  // example, the 3rd partition of the 3rd disk is "disk3s3".
  std::optional<std::string> bsd_name;
#endif
};

// Given a path to a file (following symlinks), returns information about the
// drive upon which sits that file. Returns nullopt if the file doesn't exist or
// if there is another error in looking up the drive.
BASE_EXPORT std::optional<DriveInfo> GetFileDriveInfo(
    const FilePath& file_path);

#if BUILDFLAG(IS_MAC)
// Given an IOObject of a drive's media, returns information about that drive.
// Returns nullopt if the IOObject does not conform to kIOMediaClass.
BASE_EXPORT std::optional<DriveInfo> GetIOObjectDriveInfo(
    io_object_t io_object);
#endif

}  // namespace base

#endif  // BASE_FILES_DRIVE_INFO_H_