@@ -16,12 +16,12 @@ use dav_server::{
16
16
} ,
17
17
} ;
18
18
use futures:: FutureExt ;
19
-
20
19
use rustic_core:: {
21
20
repofile:: Node ,
22
21
vfs:: { FilePolicy , OpenFile , Vfs } ,
23
22
IndexedFull , Repository ,
24
23
} ;
24
+ use tokio:: task:: spawn_blocking;
25
25
26
26
fn now ( ) -> SystemTime {
27
27
static NOW : OnceLock < SystemTime > = OnceLock :: new ( ) ;
@@ -55,7 +55,7 @@ pub struct WebDavFS<P, S> {
55
55
inner : Arc < DavFsInner < P , S > > ,
56
56
}
57
57
58
- impl < P , S : IndexedFull > WebDavFS < P , S > {
58
+ impl < P : Send + Sync + ' static , S : IndexedFull + Send + Sync + ' static > WebDavFS < P , S > {
59
59
/// Create a new [`WebDavFS`] instance.
60
60
///
61
61
/// # Arguments
@@ -94,11 +94,17 @@ impl<P, S: IndexedFull> WebDavFS<P, S> {
94
94
/// The [`Node`] at the specified path
95
95
///
96
96
/// [`Tree`]: crate::repofile::Tree
97
- fn node_from_path ( & self , path : & DavPath ) -> Result < Node , FsError > {
98
- self . inner
99
- . vfs
100
- . node_from_path ( & self . inner . repo , & path. as_pathbuf ( ) )
101
- . map_err ( |_| FsError :: GeneralFailure )
97
+ async fn node_from_path ( & self , path : & DavPath ) -> Result < Node , FsError > {
98
+ let inner = self . inner . clone ( ) ;
99
+ let path = path. as_pathbuf ( ) ;
100
+ spawn_blocking ( move || {
101
+ inner
102
+ . vfs
103
+ . node_from_path ( & inner. repo , & path)
104
+ . map_err ( |_| FsError :: GeneralFailure )
105
+ } )
106
+ . await
107
+ . map_err ( |_| FsError :: GeneralFailure ) ?
102
108
}
103
109
104
110
/// Get a list of [`Node`]s from the specified directory path.
@@ -116,11 +122,17 @@ impl<P, S: IndexedFull> WebDavFS<P, S> {
116
122
/// The list of [`Node`]s at the specified path
117
123
///
118
124
/// [`Tree`]: crate::repofile::Tree
119
- fn dir_entries_from_path ( & self , path : & DavPath ) -> Result < Vec < Node > , FsError > {
120
- self . inner
121
- . vfs
122
- . dir_entries_from_path ( & self . inner . repo , & path. as_pathbuf ( ) )
123
- . map_err ( |_| FsError :: GeneralFailure )
125
+ async fn dir_entries_from_path ( & self , path : & DavPath ) -> Result < Vec < Node > , FsError > {
126
+ let inner = self . inner . clone ( ) ;
127
+ let path = path. as_pathbuf ( ) ;
128
+ spawn_blocking ( move || {
129
+ inner
130
+ . vfs
131
+ . dir_entries_from_path ( & inner. repo , & path)
132
+ . map_err ( |_| FsError :: GeneralFailure )
133
+ } )
134
+ . await
135
+ . map_err ( |_| FsError :: GeneralFailure ) ?
124
136
}
125
137
}
126
138
@@ -141,7 +153,7 @@ impl<P: Debug + Send + Sync + 'static, S: IndexedFull + Debug + Send + Sync + 's
141
153
142
154
fn symlink_metadata < ' a > ( & ' a self , davpath : & ' a DavPath ) -> FsFuture < ' _ , Box < dyn DavMetaData > > {
143
155
async move {
144
- let node = self . node_from_path ( davpath) ?;
156
+ let node = self . node_from_path ( davpath) . await ?;
145
157
let meta: Box < dyn DavMetaData > = Box :: new ( DavFsMetaData ( node) ) ;
146
158
Ok ( meta)
147
159
}
@@ -154,7 +166,7 @@ impl<P: Debug + Send + Sync + 'static, S: IndexedFull + Debug + Send + Sync + 's
154
166
_meta : ReadDirMeta ,
155
167
) -> FsFuture < ' _ , FsStream < Box < dyn DavDirEntry > > > {
156
168
async move {
157
- let entries = self . dir_entries_from_path ( davpath) ?;
169
+ let entries = self . dir_entries_from_path ( davpath) . await ?;
158
170
let entry_iter = entries. into_iter ( ) . map ( |e| {
159
171
let entry: Box < dyn DavDirEntry > = Box :: new ( DavFsDirEntry ( e) ) ;
160
172
Ok ( entry)
@@ -180,19 +192,25 @@ impl<P: Debug + Send + Sync + 'static, S: IndexedFull + Debug + Send + Sync + 's
180
192
return Err ( FsError :: Forbidden ) ;
181
193
}
182
194
183
- let node = self . node_from_path ( path) ?;
195
+ let node = self . node_from_path ( path) . await ?;
184
196
if matches ! ( self . inner. file_policy, FilePolicy :: Forbidden ) {
185
197
return Err ( FsError :: Forbidden ) ;
186
198
}
187
199
188
- let open = self
189
- . inner
190
- . repo
191
- . open_file ( & node)
192
- . map_err ( |_err| FsError :: GeneralFailure ) ?;
200
+ let inner = self . inner . clone ( ) ;
201
+ let node_copy = node. clone ( ) ;
202
+ let open = spawn_blocking ( move || {
203
+ inner
204
+ . repo
205
+ . open_file ( & node_copy)
206
+ . map_err ( |_err| FsError :: GeneralFailure )
207
+ } )
208
+ . await
209
+ . map_err ( |_| FsError :: GeneralFailure ) ??;
210
+
193
211
let file: Box < dyn DavFile > = Box :: new ( DavFsFile {
194
212
node,
195
- open,
213
+ open : Arc :: new ( open ) ,
196
214
fs : self . inner . clone ( ) ,
197
215
seek : 0 ,
198
216
} ) ;
@@ -239,7 +257,7 @@ struct DavFsFile<P, S> {
239
257
node : Node ,
240
258
241
259
/// The [`OpenFile`] for this file
242
- open : OpenFile ,
260
+ open : Arc < OpenFile > ,
243
261
244
262
/// The [`DavFsInner`] this file belongs to
245
263
fs : Arc < DavFsInner < P , S > > ,
@@ -254,7 +272,9 @@ impl<P, S> Debug for DavFsFile<P, S> {
254
272
}
255
273
}
256
274
257
- impl < P : Debug + Send + Sync , S : IndexedFull + Debug + Send + Sync > DavFile for DavFsFile < P , S > {
275
+ impl < P : Debug + Send + Sync + ' static , S : IndexedFull + Debug + Send + Sync + ' static > DavFile
276
+ for DavFsFile < P , S >
277
+ {
258
278
fn metadata ( & mut self ) -> FsFuture < ' _ , Box < dyn DavMetaData > > {
259
279
async move {
260
280
let meta: Box < dyn DavMetaData > = Box :: new ( DavFsMetaData ( self . node . clone ( ) ) ) ;
@@ -272,12 +292,17 @@ impl<P: Debug + Send + Sync, S: IndexedFull + Debug + Send + Sync> DavFile for D
272
292
}
273
293
274
294
fn read_bytes ( & mut self , count : usize ) -> FsFuture < ' _ , Bytes > {
295
+ let fs = self . fs . clone ( ) ;
296
+ let seek = self . seek ;
297
+ let open = self . open . clone ( ) ;
275
298
async move {
276
- let data = self
277
- . fs
278
- . repo
279
- . read_file_at ( & self . open , self . seek , count)
280
- . map_err ( |_err| FsError :: GeneralFailure ) ?;
299
+ let data = spawn_blocking ( move || {
300
+ fs. repo
301
+ . read_file_at ( & open, seek, count)
302
+ . map_err ( |_err| FsError :: GeneralFailure )
303
+ } )
304
+ . await
305
+ . map_err ( |_| FsError :: GeneralFailure ) ??;
281
306
self . seek += data. len ( ) ;
282
307
Ok ( data)
283
308
}
0 commit comments