xhfs 0.3.9

Bruteforce your way into storing files anywhere across an arbitrary combination of arbitrary devices.
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
use bytes::{Buf, Bytes};
use dav_server::{
    DavHandler,
    davpath::DavPath,
    fakels::FakeLs,
    fs::{
        DavDirEntry, DavFile, DavMetaData, FsError, FsFuture, FsResult, FsStream,
        GuardedFileSystem, OpenOptions, ReadDirMeta,
    },
};
use futures::{FutureExt, StreamExt, stream};
use hyper::{server::conn::http1, service::service_fn};
use hyper_util::rt::TokioIo;
use std::{
    convert::Infallible,
    fmt::Debug,
    net::SocketAddr,
    path::PathBuf,
    sync::Arc,
    time::{Duration, SystemTime, UNIX_EPOCH},
};
use tokio::{net::TcpListener, sync::Mutex};
use xhfs_core::xhfs::{
    WriteOption, XHFS,
    ds::{EntryStat, INodeKind, XHFSError},
};

macro_rules! read_only_guard {
    ($inp:expr) => {
        if $inp {
            return std::future::ready(Err(FsError::Forbidden)).boxed();
        }
    };
}
macro_rules! read_only_guard_raw {
    ($inp:expr) => {
        if $inp {
            return Err(FsError::Forbidden);
        }
    };
}

struct EConv(XHFSError);
impl From<EConv> for FsError {
    fn from(value: EConv) -> Self {
        tracing::error!("XHFS error occured: {}", value.0);
        match value.0 {
            XHFSError::Insufficient { .. } => FsError::TooLarge,
            XHFSError::Error { .. } => FsError::GeneralFailure,
        }
    }
}

#[derive(Debug, Clone)]
struct XHFSDirEntry(EntryStat);

#[derive(Debug, Clone)]
pub struct XHFSMetaData(EntryStat);

impl DavMetaData for XHFSMetaData {
    fn len(&self) -> u64 {
        self.0.size.unwrap_or(0) as u64
    }

    fn is_dir(&self) -> bool {
        matches!(self.0.kind, INodeKind::Directory)
    }

    fn modified(&self) -> FsResult<SystemTime> {
        Ok(UNIX_EPOCH + Duration::from_secs(self.0.mtime))
    }

    fn created(&self) -> FsResult<SystemTime> {
        Ok(UNIX_EPOCH + Duration::from_secs(self.0.ctime))
    }
}

impl DavDirEntry for XHFSDirEntry {
    fn name(&self) -> Vec<u8> {
        self.0.name.as_bytes().to_vec()
    }

    fn metadata(&self) -> FsFuture<'_, Box<dyn DavMetaData>> {
        async move {
            let meta = XHFSMetaData(self.0.clone());
            Ok(Box::new(meta) as Box<dyn DavMetaData>)
        }
        .boxed()
    }
}

pub struct XHFSFile {
    xhfs: Arc<XHFS>,
    path: PathBuf,
    offset: Mutex<u64>,
    chunk_size: usize,
    read_only: bool,
}

impl std::fmt::Debug for XHFSFile {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let current_pos = self.offset.try_lock().map(|g| *g).unwrap_or(0);
        f.debug_struct("XHFSFile")
            .field("path", &self.path)
            .field("current_offset", &current_pos)
            .finish()
    }
}

impl DavFile for XHFSFile {
    fn read_bytes(&mut self, count: usize) -> FsFuture<'_, Bytes> {
        let xhfs = self.xhfs.clone();
        let path = self.path.clone();
        let chunk_size = self.chunk_size;
        tracing::info!("Reading bytes {count}");
        async move {
            let current_pos = {
                let offset_guard = self.offset.lock().await;
                *offset_guard
            };
            let mut stream = xhfs.fread_stream(path, chunk_size).await.map_err(|e| {
                tracing::error!("Error pulling chunk stream from block layer: {e:?}");
                FsError::NotFound
            })?;

            let mut skip_remaining = current_pos;
            let mut collected_bytes = vec![];
            while let Some(chunk_result) = stream.next().await {
                match chunk_result {
                    Ok(chunk) => {
                        let chunk_len = chunk.len() as u64;

                        if skip_remaining >= chunk_len {
                            skip_remaining -= chunk_len;
                            continue;
                        }

                        let start_idx = skip_remaining as usize;
                        skip_remaining = 0;

                        let available_in_chunk = chunk.len() - start_idx;
                        let needed = count - collected_bytes.len();
                        let take_len = std::cmp::min(available_in_chunk, needed);

                        collected_bytes.extend_from_slice(&chunk[start_idx..start_idx + take_len]);

                        if collected_bytes.len() >= count {
                            break;
                        }
                    }
                    Err(e) => {
                        tracing::error!("Underlying block device read failed: {e:?}");
                        return Err(FsError::GeneralFailure);
                    }
                }
            }

            {
                let mut offset_guard = self.offset.lock().await;
                *offset_guard += collected_bytes.len() as u64;
            }

            Ok(Bytes::from(collected_bytes))
        }
        .boxed()
    }

    fn seek(&mut self, pos: std::io::SeekFrom) -> FsFuture<'_, u64> {
        let xhfs = self.xhfs.clone();
        let path = self.path.clone();
        async move {
            let mut offset_guard = self.offset.lock().await;
            match pos {
                std::io::SeekFrom::Start(n) => {
                    *offset_guard = n;
                    Ok(n)
                }
                std::io::SeekFrom::Current(n) => {
                    let new_pos = (*offset_guard as i64).saturating_add(n);
                    if new_pos < 0 {
                        return Err(FsError::GeneralFailure);
                    }
                    *offset_guard = new_pos as u64;
                    Ok(*offset_guard)
                }
                std::io::SeekFrom::End(n) => match xhfs.stats(path, true).await {
                    Ok(Some(entry_stat)) => {
                        let total_size = entry_stat.size.unwrap_or(0) as i64;
                        let new_pos = total_size.saturating_add(n);
                        if new_pos < 0 {
                            return Err(FsError::GeneralFailure);
                        }
                        *offset_guard = new_pos as u64;
                        Ok(*offset_guard)
                    }
                    _ => Err(FsError::NotFound),
                },
            }
        }
        .boxed()
    }

    fn metadata(&mut self) -> FsFuture<'_, Box<dyn DavMetaData>> {
        let xhfs = self.xhfs.clone();
        let path = self.path.clone();
        async move {
            match xhfs.stats(path, true).await {
                Ok(Some(stat)) => Ok(Box::new(XHFSMetaData(stat)) as Box<dyn DavMetaData>),
                Ok(None) => Err(FsError::NotFound),
                Err(err) => {
                    tracing::error!("File metadata fetch failed: {err:?}");
                    Err(FsError::GeneralFailure)
                }
            }
        }
        .boxed()
    }

    fn write_bytes(&'_ mut self, buf: Bytes) -> FsFuture<'_, ()> {
        read_only_guard!(self.read_only);
        let xhfs = self.xhfs.clone();
        let path = self.path.clone();
        async move {
            xhfs.fappend(&path, buf.to_vec()).await.map_err(EConv)?;
            Ok(())
        }
        .boxed()
    }

    fn write_buf(&'_ mut self, mut buf: Box<dyn Buf + Send>) -> FsFuture<'_, ()> {
        read_only_guard!(self.read_only);
        tracing::debug!("Writting buf");
        let xhfs = self.xhfs.clone();
        let path = self.path.clone();
        async move {
            while buf.has_remaining() {
                let b = buf.chunk();
                let len = b.len();
                xhfs.fappend(&path, b.to_vec()).await.map_err(EConv)?;
                buf.advance(len);
            }
            Ok(())
        }
        .boxed()
    }

    fn flush(&mut self) -> FsFuture<'_, ()> {
        async move { Ok(()) }.boxed()
    }

    fn redirect_url(&'_ mut self) -> FsFuture<'_, Option<String>> {
        std::future::ready(Ok(None)).boxed()
    }
}

#[derive(Clone)]
struct XHFSAdapter {
    xhfs: Arc<XHFS>,
    read_only: bool,
    chunk_size: usize,
}

impl GuardedFileSystem<()> for XHFSAdapter {
    fn open<'a>(
        &'a self,
        path: &'a DavPath,
        options: OpenOptions,
        _creds: &'a (),
    ) -> FsFuture<'a, Box<dyn DavFile>> {
        tracing::warn!("open: {path:?}, options = {options:?}");
        let xhfs = self.xhfs.clone();
        let path_buf = path.as_pathbuf();
        let chunk_size = self.chunk_size;
        let read_only = self.read_only;
        async move {
            tracing::info!("WebDAV open {path_buf:?}");
            if options.write && options.truncate {
                read_only_guard_raw!(read_only);
                xhfs.fwrite(
                    &path_buf,
                    vec![],
                    WriteOption {
                        overwrite: options.truncate,
                    },
                )
                .await
                .map_err(EConv)?;
            }

            match xhfs.stats(path_buf.clone(), true).await {
                Ok(Some(stat)) => {
                    if matches!(stat.kind, INodeKind::Directory) {
                        tracing::warn!("Attempted to open directory {path_buf:?}");
                        return Err(FsError::Forbidden);
                    }
                    let file_handle = XHFSFile {
                        xhfs,
                        path: path_buf,
                        offset: tokio::sync::Mutex::new(0),
                        chunk_size,
                        read_only,
                    };
                    Ok(Box::new(file_handle) as Box<dyn DavFile>)
                }
                Ok(None) => {
                    tracing::warn!("File not found {path_buf:?}");
                    Err(FsError::NotFound)
                }
                Err(err) => {
                    tracing::error!("Metadata lookup failed {path_buf:?}: {err:?}");
                    Err(FsError::GeneralFailure)
                }
            }
        }
        .boxed()
    }

    fn metadata<'a>(
        &'a self,
        path: &'a DavPath,
        _creds: &'a (),
    ) -> FsFuture<'a, Box<dyn DavMetaData>> {
        tracing::warn!("metadata: {path:?}");
        let xhfs = self.xhfs.clone();
        let path_buf = path.as_pathbuf();
        async move {
            match xhfs.stats(path_buf, true).await {
                Ok(Some(entry_stat)) => {
                    let meta = XHFSMetaData(entry_stat);
                    Ok(Box::new(meta) as Box<dyn DavMetaData>)
                }
                Ok(None) => Err(FsError::NotFound),
                Err(e) => {
                    tracing::error!("Backend metadata lookup failure: {e:?}");
                    Err(FsError::GeneralFailure)
                }
            }
        }
        .boxed()
    }

    fn read_dir<'a>(
        &'a self,
        path: &'a DavPath,
        _meta: ReadDirMeta,
        _creds: &'a (),
    ) -> FsFuture<'a, FsStream<Box<dyn DavDirEntry>>> {
        tracing::warn!("ls: {path:?}");
        // TODO:
        // stream dir listing later in core
        let xhfs = self.xhfs.clone();
        let path_buf = path.as_pathbuf();
        async move {
            if path.file_name_bytes() == b".localized" {
                return Err(FsError::NotFound);
            }
            let item_names = xhfs.ls(path_buf.clone()).await.map_err(|e| {
                tracing::error!("Failed listing path collection targets: {e:?}");
                FsError::NotFound
            })?;

            let mut entries = vec![];
            for name in item_names {
                let mut entry_path = path_buf.clone();
                entry_path.push(&name);
                if let Ok(Some(entry_stat)) = xhfs.stats(entry_path, true).await {
                    entries.push(Box::new(XHFSDirEntry(entry_stat)) as Box<dyn DavDirEntry>);
                }
            }

            let stream_items = entries.into_iter().map(Ok);
            Ok(Box::pin(stream::iter(stream_items)) as FsStream<Box<dyn DavDirEntry>>)
        }
        .boxed()
    }

    fn symlink_metadata<'a>(
        &'a self,
        path: &'a DavPath,
        creds: &'a (),
    ) -> FsFuture<'a, Box<dyn DavMetaData>> {
        tracing::warn!("symlink metadata: {path:?}");
        self.metadata(path, creds)
    }

    fn create_dir<'a>(&'a self, path: &'a DavPath, _creds: &'a ()) -> FsFuture<'a, ()> {
        read_only_guard!(self.read_only);
        tracing::warn!("create dir: {path:?}");
        let xhfs = self.xhfs.clone();
        let path_buf = path.as_pathbuf();
        async move {
            xhfs.mkdir(path_buf, true).await.map_err(|err| {
                tracing::error!("mkdir failed: {err:?}");
                FsError::GeneralFailure
            })?;
            Ok(())
        }
        .boxed()
    }

    fn remove_dir<'a>(&'a self, path: &'a DavPath, _creds: &'a ()) -> FsFuture<'a, ()> {
        read_only_guard!(self.read_only);
        tracing::warn!("remove dir: {path:?}");
        let xhfs = self.xhfs.clone();
        let path_buf = path.as_pathbuf();
        async move {
            // TODO: native recursive
            // The default implementation is using a combination of ls and unlink
            xhfs.unlink(path_buf).await.map_err(|e| {
                tracing::error!("unlink folder failed: {e:?}");
                FsError::GeneralFailure
            })?;
            Ok(())
        }
        .boxed()
    }

    fn remove_file<'a>(&'a self, path: &'a DavPath, _creds: &'a ()) -> FsFuture<'a, ()> {
        read_only_guard!(self.read_only);
        let xhfs = self.xhfs.clone();
        let path_buf = path.as_pathbuf();
        async move {
            xhfs.unlink(path_buf).await.map_err(|e| {
                tracing::error!("unlink file failed: {e:?}");
                FsError::GeneralFailure
            })?;
            Ok(())
        }
        .boxed()
    }

    fn rename<'a>(
        &'a self,
        from: &'a DavPath,
        to: &'a DavPath,
        _creds: &'a (),
    ) -> FsFuture<'a, ()> {
        read_only_guard!(self.read_only);
        let xhfs = self.xhfs.clone();
        let from_path_buf = from.as_pathbuf();
        let to_path_buf = to.as_pathbuf();
        async move {
            xhfs.fmove(from_path_buf, to_path_buf).await.map_err(|e| {
                tracing::error!("fmove failed: {e:?}");
                FsError::GeneralFailure
            })?;
            Ok(())
        }
        .boxed()
    }

    fn copy<'a>(&'a self, from: &'a DavPath, to: &'a DavPath, _creds: &'a ()) -> FsFuture<'a, ()> {
        read_only_guard!(self.read_only);
        tracing::warn!("fcopy {from:?} => {to:?}");
        let xhfs = self.xhfs.clone();
        let from_path_buf = from.as_pathbuf();
        let to_path_buf = to.as_pathbuf();
        async move {
            xhfs.fcopy_stream(
                from_path_buf,
                to_path_buf,
                self.chunk_size,
                WriteOption { overwrite: true },
            )
            .await
            .map_err(EConv)?;
            Ok(())
        }
        .boxed()
    }

    fn have_props<'a>(
        &'a self,
        path: &'a DavPath,
        _creds: &'a (),
    ) -> std::pin::Pin<Box<dyn Future<Output = bool> + Send + 'a>> {
        tracing::warn!("Have props? {path}");
        Box::pin(std::future::ready(false))
    }

    fn patch_props<'a>(
        &'a self,
        path: &'a DavPath,
        dprop: Vec<(bool, dav_server::fs::DavProp)>,
        _creds: &'a (),
    ) -> FsFuture<'a, Vec<(hyper::StatusCode, dav_server::fs::DavProp)>> {
        tracing::warn!("Patch prop {path} {dprop:?}");
        async move { Err(FsError::Forbidden) }.boxed()
    }

    fn get_props<'a>(
        &'a self,
        path: &'a DavPath,
        b: bool,
        _creds: &'a (),
    ) -> FsFuture<'a, Vec<dav_server::fs::DavProp>> {
        tracing::warn!("Get props (plural) {path} {b}");
        async move { Err(FsError::Forbidden) }.boxed()
    }

    fn get_prop<'a>(
        &'a self,
        path: &'a DavPath,
        dprop: dav_server::fs::DavProp,
        _creds: &'a (),
    ) -> FsFuture<'a, Vec<u8>> {
        tracing::warn!("Get props {path} {dprop:?}");
        async move { Err(FsError::Forbidden) }.boxed()
    }

    fn get_quota<'a>(&'a self, _creds: &'a ()) -> FsFuture<'a, (u64, Option<u64>)> {
        let xhfs = self.xhfs.clone();
        async move {
            let total_capacity = xhfs.total_capacity().map_err(|e| {
                tracing::error!("Failed listing path collection targets: {e:?}");
                FsError::NotFound
            })?;
            let remaining = xhfs.total_remaining_capacity().await.map_err(|e| {
                tracing::error!("Failed calculating capacity: {e:?}");
                FsError::NotFound
            })?;
            Ok((total_capacity as u64, Some(remaining as u64)))
        }
        .boxed()
    }
}

pub async fn webdav_main(
    addr: String,
    port: u16,
    xhfs: Arc<XHFS>,
    read_only: bool,
) -> eyre::Result<()> {
    println!("-------");
    println!("{}", xhfs.format_headers_report().await?);
    println!("-------");
    let addr = format!("{addr}:{port}").parse::<SocketAddr>()?;
    let xhfs_adapter = Box::new(XHFSAdapter {
        xhfs,
        read_only,
        chunk_size: 64 * 1024, // standard 64KB disk read windows
    });
    let dav_server = DavHandler::builder()
        .filesystem(xhfs_adapter)
        .locksystem(FakeLs::new())
        .build_handler();

    let listener = TcpListener::bind(addr).await.unwrap();
    println!("Listening for WebDAV requests at {addr}");
    loop {
        let (stream, _) = listener.accept().await.unwrap();
        let dav_server = dav_server.clone();
        let io = TokioIo::new(stream);
        tokio::task::spawn(async move {
            if let Err(err) = http1::Builder::new()
                .serve_connection(
                    io,
                    service_fn({
                        move |req| {
                            let dav_server = dav_server.clone();
                            async move { Ok::<_, Infallible>(dav_server.handle(req).await) }
                        }
                    }),
                )
                .await
            {
                eprintln!("Error executing HTTP session processing context: {err:?}");
            }
        });
    }
}