File tree Expand file tree Collapse file tree 4 files changed +46
-0
lines changed Expand file tree Collapse file tree 4 files changed +46
-0
lines changed Original file line number Diff line number Diff line change @@ -20,6 +20,8 @@ libc = { workspace = true }
20
20
rustix = { workspace = true }
21
21
serde = { workspace = true , features = [" derive" ] }
22
22
tracing = { workspace = true }
23
+ tempfile = { workspace = true }
24
+ cap-std-ext = { workspace = true }
23
25
24
26
[dev-dependencies ]
25
27
indoc = { workspace = true }
Original file line number Diff line number Diff line change @@ -22,6 +22,8 @@ use rustix::{
22
22
} ;
23
23
use serde:: Deserialize ;
24
24
25
+ pub mod tempmount;
26
+
25
27
/// Well known identifier for pid 1
26
28
pub const PID1 : Pid = const {
27
29
match Pid :: from_raw ( 1 ) {
Original file line number Diff line number Diff line change
1
+ use anyhow:: { Context , Result } ;
2
+
3
+ use camino:: Utf8Path ;
4
+ use cap_std_ext:: cap_std:: { ambient_authority, fs:: Dir } ;
5
+ use fn_error_context:: context;
6
+ use rustix:: mount:: { unmount, UnmountFlags } ;
7
+
8
+ pub struct TempMount {
9
+ pub dir : tempfile:: TempDir ,
10
+ pub fd : Dir ,
11
+ }
12
+
13
+ impl TempMount {
14
+ /// Mount device/partition on a tempdir which will be automatically unmounted on drop
15
+ #[ context( "Mounting {dev}" ) ]
16
+ pub fn mount_dev ( dev : & str ) -> Result < Self > {
17
+ let tempdir = tempfile:: TempDir :: new ( ) ?;
18
+
19
+ let utf8path = Utf8Path :: from_path ( tempdir. path ( ) )
20
+ . ok_or ( anyhow:: anyhow!( "Failed to convert path to UTF-8 Path" ) ) ?;
21
+
22
+ crate :: mount ( dev, utf8path) ?;
23
+
24
+ // There's a case here where if the following open fails, we won't unmount which should be
25
+ // unlikely
26
+ let fd = Dir :: open_ambient_dir ( tempdir. path ( ) , ambient_authority ( ) )
27
+ . with_context ( || format ! ( "Opening {:?}" , tempdir. path( ) ) ) ?;
28
+
29
+ Ok ( TempMount { dir : tempdir, fd } )
30
+ }
31
+ }
32
+
33
+ impl Drop for TempMount {
34
+ fn drop ( & mut self ) {
35
+ match unmount ( self . dir . path ( ) , UnmountFlags :: DETACH ) {
36
+ Ok ( _) => { }
37
+ Err ( e) => tracing:: warn!( "Failed to unmount tempdir: {e:?}" ) ,
38
+ }
39
+ }
40
+ }
You can’t perform that action at this time.
0 commit comments