@@ -114,6 +114,12 @@ fn schedule_deletions<'a, I: Into<Cow<'a, Path>>, F: IntoIterator<Item = I>>(
114
114
115
115
#[ cfg( target_os = "linux" ) ]
116
116
mod compat {
117
+ use lockness_executor:: { LocknessExecutor , LocknessMessenger } ;
118
+ use rustix:: {
119
+ fs:: { AtFlags , CWD , FileType , Mode , OFlags , RawDir , openat, unlinkat} ,
120
+ io:: Errno ,
121
+ thread:: { UnshareFlags , unshare} ,
122
+ } ;
117
123
use std:: {
118
124
borrow:: Cow ,
119
125
cell:: LazyCell ,
@@ -131,12 +137,6 @@ mod compat {
131
137
sync:: Arc ,
132
138
} ;
133
139
134
- use rustix:: {
135
- fs:: { AtFlags , CWD , FileType , Mode , OFlags , RawDir , openat, unlinkat} ,
136
- io:: Errno ,
137
- thread:: { UnshareFlags , unshare} ,
138
- } ;
139
-
140
140
use crate :: {
141
141
Error ,
142
142
ops:: { IoErr , compat:: DirectoryOp , concat_cstrs, join_cstr_paths, path_buf_to_cstring} ,
@@ -147,34 +147,49 @@ mod compat {
147
147
}
148
148
149
149
impl ThreadState {
150
- #[ cfg_attr( feature = "tracing" , tracing:: instrument( level = "trace" ) ) ]
151
- fn new ( unshare_io : bool ) -> Result < Self , Error > {
152
- if unshare_io {
153
- unshare ( UnshareFlags :: FILES | UnshareFlags :: FS )
154
- . map_io_err ( || "Failed to unshare I/O." ) ?;
150
+ #[ cfg_attr(
151
+ feature = "tracing" ,
152
+ tracing:: instrument( level = "trace" , skip( messenger) )
153
+ ) ]
154
+ fn init ( messenger : & LocknessMessenger < Error > , unshare_io : bool ) -> Self {
155
+ let run = || -> Result < _ , Error > {
156
+ if unshare_io {
157
+ unshare ( UnshareFlags :: FILES | UnshareFlags :: FS )
158
+ . map_io_err ( || "Failed to unshare I/O." ) ?;
159
+ }
160
+ Ok ( ( ) )
161
+ } ;
162
+ if let Err ( e) = run ( ) {
163
+ messenger. send ( e) ;
155
164
}
156
- Ok ( Self {
165
+
166
+ Self {
157
167
buf : [ MaybeUninit :: uninit ( ) ; 8192 ] ,
158
- } )
168
+ }
159
169
}
160
170
}
161
171
162
- struct Impl {
163
- executor : LazyCell < LocknessExecutor < ThreadState > > ,
172
+ struct Impl < F , L > {
173
+ executor : LazyCell < LocknessExecutor < Error , F > , L > ,
164
174
}
165
175
166
176
pub fn remove_impl < ' a > ( ) -> impl DirectoryOp < Cow < ' a , Path > > {
167
177
let executor = LazyCell :: new ( || {
168
178
let unshare_io = env:: var_os ( "NO_UNSHARE" ) . is_none ( ) ;
169
179
LocknessExecutor :: builder ( )
170
- . thread_initializer ( || ThreadState :: new ( unshare_io) )
180
+ . message_type :: < Error > ( )
181
+ . thread_initializer ( move |m| ThreadState :: init ( m, unshare_io) )
171
182
. build ( )
172
183
} ) ;
173
184
174
185
Impl { executor }
175
186
}
176
187
177
- impl DirectoryOp < Cow < ' _ , Path > > for Impl {
188
+ impl <
189
+ F : ( FnMut ( & LocknessMessenger < Error > ) -> ThreadState ) + Send + ' static ,
190
+ L : FnOnce ( ) -> LocknessExecutor < Error , F > ,
191
+ > DirectoryOp < Cow < ' _ , Path > > for Impl < F , L >
192
+ {
178
193
#[ cfg_attr( feature = "tracing" , tracing:: instrument( level = "trace" , skip( self ) ) ) ]
179
194
fn run ( & self , dir : Cow < Path > ) -> Result < ( ) , Error > {
180
195
let Self { ref executor } = * self ;
@@ -184,7 +199,8 @@ mod compat {
184
199
path : path_buf_to_cstring ( dir. into_owned ( ) ) ?,
185
200
parent : None ,
186
201
} ;
187
- executor. spawn ( |executor, state| delete_dir ( node, state, executor) )
202
+ executor. spawn ( |ctx| delete_dir ( node, ctx) ) ;
203
+ Ok ( ( ) )
188
204
}
189
205
190
206
#[ cfg_attr( feature = "tracing" , tracing:: instrument( level = "trace" , skip( self ) ) ) ]
@@ -204,7 +220,10 @@ mod compat {
204
220
feature = "tracing" ,
205
221
tracing:: instrument( level = "info" , skip( state, executor) )
206
222
) ]
207
- fn delete_dir ( node : TreeNode , state : & mut ThreadState , executor : & LocknessExecutor ) {
223
+ fn delete_dir < F : ( FnMut ( & LocknessMessenger < Error > ) -> ThreadState ) + Send + ' static > (
224
+ node : TreeNode ,
225
+ ( state, executor) : ( & mut ThreadState , & LocknessExecutor < Error , F > ) ,
226
+ ) {
208
227
let run = || -> Result < ( ) , Error > {
209
228
let dir = openat (
210
229
CWD ,
@@ -226,11 +245,13 @@ mod compat {
226
245
feature = "tracing" ,
227
246
tracing:: instrument( level = "trace" , skip( dir, buf, executor) )
228
247
) ]
229
- fn delete_dir_contents (
248
+ fn delete_dir_contents <
249
+ F : ( FnMut ( & LocknessMessenger < Error > ) -> ThreadState ) + Send + ' static ,
250
+ > (
230
251
node : TreeNode ,
231
252
dir : OwnedFd ,
232
253
ThreadState { buf } : & mut ThreadState ,
233
- executor : & LocknessExecutor ,
254
+ executor : & LocknessExecutor < Error , F > ,
234
255
) -> Result < Option < TreeNode > , Error > {
235
256
enum Arcable < T > {
236
257
Raw ( T ) ,
@@ -303,7 +324,7 @@ mod compat {
303
324
path : concat_cstrs ( & node. path , file. file_name ( ) ) ,
304
325
parent : Some ( node. clone ( ) ) ,
305
326
} ;
306
- executor. spawn ( |executor , state | delete_dir ( node, state , executor ) ) ;
327
+ executor. spawn ( |ctx | delete_dir ( node, ctx ) ) ;
307
328
}
308
329
309
330
Ok ( Arcable :: into_inner ( node) )
0 commit comments