• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2024 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 #ifndef BASE_FILES_DRIVE_INFO_H_
6 #define BASE_FILES_DRIVE_INFO_H_
7 
8 #include <optional>
9 #include <string>
10 #include <string_view>
11 
12 #include "base/base_export.h"
13 #include "base/files/file_path.h"
14 #include "build/build_config.h"
15 
16 #if BUILDFLAG(IS_MAC)
17 #include <IOKit/IOKitLib.h>
18 #endif
19 
20 namespace base {
21 
22 // Used to hold information about either a drive, or of a combination of a
23 // partition residing on a drive and the drive itself, depending on how the
24 // object was constructed. In general, when calling GetFileDriveInfo(), this
25 // latter case is the one which should be considered. On MacOS, whole media can
26 // be queried by using by calling GetIOObjectDriveInfo() with an `IOObject`
27 // obtained via IOServiceGetMatchingService() with `kIOMediaWholeKey` set to
28 // `true`.
29 //
30 // Each of these parameters can fail to be retrieved from the OS, and so they
31 // can each be empty.
32 //
33 // If you add more fields to this structure (platform-specific fields are OK),
34 // make sure to update all functions that use it in
35 // file_util_{win|posix|apple}.{cc,mm}, too.
36 struct BASE_EXPORT DriveInfo {
37   DriveInfo();
38   DriveInfo(const DriveInfo&) = delete;
39   DriveInfo(DriveInfo&&) noexcept;
40   DriveInfo& operator=(const DriveInfo&) = delete;
41   DriveInfo& operator=(DriveInfo&&) noexcept;
42   ~DriveInfo();
43 
44   // Whether the drive has a seek penalty (i.e. is or is not a spinning disk).
45   std::optional<bool> has_seek_penalty;
46 
47 #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX) || \
48     BUILDFLAG(IS_CHROMEOS)
49   // Whether the drive is a removable drive.
50   //
51   // Note on macOS: that SSDs that are connected over USB that you can eject
52   // in finder are not necessarily removable and/or ejectable according to
53   // IOKitLib. The reason for this is unknown. The same SSD on Windows is
54   // non-ejectable (in explorer), and marked as non-removable here.
55   std::optional<bool> is_removable;
56 
57   // The size of the media, in bytes.
58   std::optional<int64_t> size_bytes;
59 #endif
60 #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
61   // Whether the drive is connected over USB.
62   std::optional<bool> is_usb;
63 #endif
64 #if BUILDFLAG(IS_MAC)
65   // Whether the drive is a CoreStorage volume.
66   std::optional<bool> is_core_storage;
67 
68   // Whether the drive is an APFS container.
69   std::optional<bool> is_apfs;
70 
71   // Whether the drive can be written to.
72   std::optional<bool> is_writable;
73 
74   // The BSD name is the filename at which the drive is found under /dev. For
75   // example, the 3rd partition of the 3rd disk is "disk3s3".
76   std::optional<std::string> bsd_name;
77 #endif
78 };
79 
80 // Returns information about the drive on which sits the given file. Also see
81 // `DriveInfo`.
82 BASE_EXPORT std::optional<DriveInfo> GetFileDriveInfo(
83     const FilePath& file_path);
84 
85 #if BUILDFLAG(IS_MAC)
86 // BSD name is the file found under `/dev`, not the full path including "/dev".
87 BASE_EXPORT std::optional<DriveInfo> GetBSDNameDriveInfo(
88     std::string_view bsd_name);
89 
90 // The IO Object is the underlying handle to the drive device. This function can
91 // be used if already iterating over drives matching certain characteristics.
92 // This function fails when the `io_object` does not conform to
93 // `kIOMediaClass`.
94 BASE_EXPORT std::optional<DriveInfo> GetIOObjectDriveInfo(
95     io_object_t io_object);
96 #endif
97 
98 }  // namespace base
99 
100 #endif  // BASE_FILES_DRIVE_INFO_H_
101