-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisk_manager.h
109 lines (85 loc) · 3.75 KB
/
disk_manager.h
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 2012 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CROS_DISKS_DISK_MANAGER_H_
#define CROS_DISKS_DISK_MANAGER_H_
#include <libudev.h>
#include <memory>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#include <base/functional/callback.h>
#include <gtest/gtest_prod.h>
#include "cros-disks/disk.h"
#include "cros-disks/mount_manager.h"
namespace cros_disks {
class DeviceEjector;
class DiskMonitor;
class Mounter;
class Platform;
class SandboxedProcessFactory;
// The DiskManager is responsible for mounting removable media.
//
// This class is designed to run within a single-threaded GMainLoop application
// and should not be considered thread safe.
class DiskManager : public MountManager {
public:
DiskManager(const std::string& mount_root,
Platform* platform,
Metrics* metrics,
brillo::ProcessReaper* process_reaper,
DiskMonitor* disk_monitor,
DeviceEjector* device_ejector,
const SandboxedProcessFactory* test_sandbox_factory = nullptr);
DiskManager(const DiskManager&) = delete;
DiskManager& operator=(const DiskManager&) = delete;
~DiskManager() override;
// Initializes the disk manager and registers default filesystems.
// Returns true on success.
bool Initialize() override;
// Returns true if mounting |source_path| is supported.
bool CanMount(const std::string& source_path) const override;
MountSourceType GetMountSourceType() const final {
return MOUNT_SOURCE_REMOVABLE_DEVICE;
}
// Unmounts all mounted paths.
void UnmountAll() override;
protected:
// Mounts |source_path| to |mount_path| as |filesystem_type| with |options|.
std::unique_ptr<MountPoint> DoMount(const std::string& source_path,
const std::string& filesystem_type,
const std::vector<std::string>& options,
const base::FilePath& mount_path,
MountError* error) override;
// Returns a suggested mount path for a source path.
std::string SuggestMountPath(const std::string& source_path) const override;
// Returns true to reserve a mount path on errors due to unknown or
// unsupported filesystems.
bool ShouldReserveMountPathOnError(MountError error_type) const override;
private:
// Ejects media for the device |device_file|. Return true if the eject process
// has started or |eject_device_on_unmount_| is false, or false if the eject
// process failed.
bool EjectDevice(const std::string& device_file);
// If |disk| is an optical disk, wrap |mount_point| in a wrapper that ejects
// the disk on a successful unmount. If |disk| is not an optical disk, returns
// |mount_point|. This is exposed as a function to allow ejecting behaviour to
// be tested.
std::unique_ptr<MountPoint> MaybeWrapMountPointForEject(
std::unique_ptr<MountPoint> mount_point, const Disk& disk);
DiskMonitor* const disk_monitor_;
DeviceEjector* const device_ejector_;
const SandboxedProcessFactory* const test_sandbox_factory_;
// Set to true if devices should be ejected upon unmount.
bool eject_device_on_unmount_;
// Mapping of filesystem types to corresponding mounters.
std::unordered_map<std::string, std::unique_ptr<Mounter>> mounters_;
FRIEND_TEST(DiskManagerTest, DoMountDiskWithNonexistentSourcePath);
FRIEND_TEST(DiskManagerTest, EjectDevice);
FRIEND_TEST(DiskManagerTest, EjectDeviceWhenUnmountFailed);
FRIEND_TEST(DiskManagerTest, EjectDeviceWhenExplicitlyDisabled);
FRIEND_TEST(DiskManagerTest, EjectDeviceWhenReleased);
};
} // namespace cros_disks
#endif // CROS_DISKS_DISK_MANAGER_H_