roo_io
API Documentation for roo_io
Loading...
Searching...
No Matches
filesystem.h
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4
6#include "roo_io/fs/mount.h"
8#include "roo_io/status.h"
9
10namespace roo_io {
11
12// A high-level abstract class representing a mountable filesystem (e.g. an SD
13// card slot).
15 public:
16 virtual ~Filesystem() = default;
17
18 // Result of checkMediaPresence().
20 // Media (e.g. SD card) is known to be absent.
22
23 // Media (e.g. SD card) is known to be present.
25
26 // Media presence cannot be determined.
28 };
29
30 // Decides the level of access allowed for new mounts. Does not affect
31 // existing mounts.
33 // New mounts can both read and write (as long as the filesystem supports
34 // that).
36
37 // New mounts cannot write to the file system.
39
40 // New mounts are disallowed.
42 };
43
44 // Decides how aggressive the filesystem gets unmounted when no longer used.
46 // Unmount as soon as all mount objects go out of scope.
48
49 // Keep mounted until explicit call to unmount.
51 };
52
53 // Returns a new mount for the filesystem, or error status. The error can be:
54 // * kNoMedia, when mount fails because there is no media (e.g. no SD card
55 // inserted),
56 // * kGenericMountError, if the cause of error is unknown.
57 //
58 // Multiple mount objects can be independently created. They are all backed by
59 // the same mounted filesystem. The filesystem gets mounted when the first
60 // mount object is created.
61 //
62 // Generally you don't need to, and shouldn't, explicitly unmount the
63 // filesystem. If `unmountingPolicy()' is 'kEagerUnmount', the filesystem gets
64 // automatically unmounted when the last mount object gets out of scope.
65 // If that policy is 'kLazyUnmount', the filesystem is kept mounted, to be
66 // reused by future created mount objects.
67 Mount mount();
68
69 // Returns true if the filesystem is currently mounted.
70 bool isMounted() const { return !mount_.expired(); }
71
72 // Returns true if there are any user-created live mounts referencing the
73 // filesystem. If unmounting policy is 'kEagerUnmount', it is always the same
74 // as 'isMounted()'. With 'kLazyUnmount', 'isUnUse()' can be false while
75 // 'isMounted()' is still true.
76 bool isInUse() const {
77 return mount_.use_count() > (unmounting_policy_ == kUnmountEagerly ? 0 : 1);
78 }
79
80 // Checks whether media is present. Does not require the filesystem to be
81 // mounted, and does not cause it to become mounted. Some implementations may
82 // not be able to determine media presence; they can return
83 // kMediaPresenceUnknown in such case.
85
86 // Returns the current value of the mounting policy.
87 MountingPolicy mountingPolicy() const { return mounting_policy_; }
88
89 // Sets the mounting policy, which decides what do to when `mount()` gets
90 // called.
94
95 // Returns the current value of the unmounting policy.
96 UnmountingPolicy unmountingPolicy() const { return unmounting_policy_; }
97
98 // Sets unmounting policy, which decides how aggressively to unmount the
99 // filesystem when it stops being used.
100 //
101 // If switching from lazy to eager, and no mount objects currently exist, the
102 // filesystem gets unmounted. Otherwise, the call has no immediate effect.
104
105 // Invalidates all existing mount objects, and unmounts the filesystem
106 // immediately. The mounts will return 'kNotMounted' from any subsequent
107 // calls.
108 //
109 // New mounts can still be created, as long as the mounting policy allows it.
110 //
111 // Prefer relying on automount. This method is indended for use in special
112 // circumstances, such as forceful shutdown.
113 void forceUnmount();
114
115 protected:
117 : mounting_policy_(kMountReadWrite),
118 unmounting_policy_(kUnmountEagerly) {}
119
121 std::function<void()> unmount_fn) = 0;
122
123 virtual void unmountImpl() = 0;
124
125 private:
126 std::weak_ptr<MountImpl> mount_;
127
128 // Used when unmounting_policy_ == kLazyUnmount, to keep the mount alive.
129 std::shared_ptr<MountImpl> lazy_unmount_;
130
131 MountingPolicy mounting_policy_;
132 UnmountingPolicy unmounting_policy_;
133};
134
135// Returns a pointer into path that points past the last '/' in the path.
136const char* GetFileName(const char* path);
137
138} // namespace roo_io
bool isMounted() const
Definition filesystem.h:70
virtual MediaPresence checkMediaPresence()=0
virtual void unmountImpl()=0
void setMountingPolicy(MountingPolicy mounting_policy)
Definition filesystem.h:91
virtual ~Filesystem()=default
void setUnmountingPolicy(UnmountingPolicy unmounting_policy)
virtual MountImpl::MountResult mountImpl(std::function< void()> unmount_fn)=0
MountingPolicy mountingPolicy() const
Definition filesystem.h:87
bool isInUse() const
Definition filesystem.h:76
UnmountingPolicy unmountingPolicy() const
Definition filesystem.h:96
Definition byte.h:6
const char * GetFileName(const char *path)
roo::basic_string_view< CharT, Traits > basic_string_view
Definition string_view.h:8