prolink 0.3.0

Pioneer Pro DJ Link: browse and play from CDJs on the network, and serve a rekordbox medium to them
Documentation
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
// SPDX-License-Identifier: GPL-3.0-only

//! The read-only tree an NFS server hands out.
//!
//! Whatever backs it — a mounted USB stick, or a synthesised `/PIONEER/` layout
//! generated from some other library — the wire layer above should not have to
//! change, so it talks only to this.
//!
//! # Filehandles, and the one place a CDJ breaks the spec
//!
//! RFC 1094 says a filehandle is 32 *opaque* bytes that the client echoes back
//! verbatim. **A CDJ-2000NXS does not.** Handed a handle, it returns one whose
//! first twelve bytes match ours and whose remaining twenty it has rewritten
//! with its own file reference:
//!
//! ```text
//! served:   8a5edab282632443219e051e 4ade2d1d5bbc671c781051bf1437897cbdfea0f1
//! returned: 8a5edab282632443219e051e 03012d0000001b58000000000303010000000162
//!           |____ first 12 kept ____| |______ replaced by the player _______|
//! ```
//!
//! That fits the shape of a real player's own handles — a four-byte value
//! repeated three times followed by zeros — so the leading twelve bytes are
//! evidently the volume identity and the rest is the player's own file
//! reference, which it feels free to overwrite.
//!
//! A server that trusts the spec works perfectly for browsing and fails at
//! exactly the moment a DJ loads a track (F28). This table is therefore keyed on
//! [`FileHandleKey`], which is that twelve-byte prefix and a distinct type, so
//! "I looked the handle up correctly" is a property of the type rather than of
//! remembering to slice.
//!
//! # Two media need two subtrees
//!
//! A handle is a hash of its path. Two media sharing a root would mint identical
//! handles for the same relative path — the root most obviously — and after
//! truncation nothing would distinguish them. So each medium is grafted under
//! its own prefix, and [`Vfs::mount`] takes one.
//!
//! # Unicode, and why an exact match is not enough
//!
//! A name can reach us spelled differently from how the filesystem spells it,
//! and the two must still match.
//!
//! A rekordbox medium is **FAT32** — case-insensitive, case-preserving — and
//! `export.pdb` does not necessarily record a directory entry's case: on the
//! reference stick the database says `Gesaffelstein` where the directory is
//! `GESAFFELSTEIN`. The pdb also stores **NFC** where the filesystem reports
//! **NFD** (`カガミ`), because rekordbox wrote the two through different APIs.
//!
//! A player looking up the path the database gave it therefore asks for a name
//! that, byte for byte, is not the one in our listing, and an exact comparison
//! answers `NFSERR_NOENT` for a file that is plainly there. So: match exactly
//! first, fall back to comparing `NFC(name).casefold()`, and **always return the
//! handle for the name as stored** — hashing the requested spelling would mint a
//! handle that is not in the table, and every later use of it would come back
//! `NFSERR_STALE` (O6).

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use prolink_proto::rpc::nfs2::{FType, Fattr, FileHandle, FileHandleKey};
use sha2::{Digest, Sha256};
use unicode_normalization::UnicodeNormalization;

/// Fixed timestamp for synthesised attributes.
///
/// Deterministic on purpose: byte-identical replies across runs make a capture
/// diff meaningful.
const EPOCH: u32 = 1_600_000_000;

/// A file or a directory in the served tree.
#[derive(Clone, Debug)]
pub enum Node {
    /// A directory, with its children in the order they will be listed.
    Directory {
        /// Child names, in listing order.
        children: Vec<String>,
    },
    /// A file whose bytes are held in memory.
    Memory {
        /// The contents.
        data: Vec<u8>,
    },
    /// A file whose bytes stay on disk until a `READ` asks for them.
    ///
    /// This is what makes serving a mounted USB stick practical: reading a 60 GB
    /// library into memory to answer an 8 KB request is not an option.
    Disk {
        /// Where to read from.
        path: PathBuf,
        /// Its size, taken once at mount time.
        size: u64,
    },
}

impl Node {
    /// Whether this is a directory.
    pub fn is_dir(&self) -> bool {
        matches!(self, Self::Directory { .. })
    }

    /// The size an NFS `fattr` should report.
    pub fn size(&self) -> u64 {
        match self {
            Self::Directory { .. } => 0,
            Self::Memory { data } => data.len().try_into().unwrap_or(u64::MAX),
            Self::Disk { size, .. } => *size,
        }
    }
}

/// One entry in the table: its path, its node, and its handle.
#[derive(Clone, Debug)]
struct Entry {
    /// The path this entry's handle was derived from.
    path: String,
    node: Node,
}

/// A read-only tree addressable by filehandle.
#[derive(Debug, Default)]
pub struct Vfs {
    entries: BTreeMap<FileHandleKey, Entry>,
}

impl Vfs {
    /// An empty tree with just a root directory.
    pub fn new() -> Self {
        let mut vfs = Self {
            entries: BTreeMap::new(),
        };
        vfs.insert(
            "/",
            Node::Directory {
                children: Vec::new(),
            },
        );
        vfs
    }

    /// The handle for a path.
    ///
    /// A truncated SHA-256, so the same tree yields the same handles and a
    /// client's cached root handle survives a restart of this process.
    pub fn handle_for(path: &str) -> FileHandle {
        let digest = Sha256::digest(path.as_bytes());
        let mut handle = [0u8; FileHandle::LEN];
        for (slot, byte) in handle.iter_mut().zip(digest.iter()) {
            *slot = *byte;
        }
        FileHandle(handle)
    }

    /// The root directory's handle, which is what a `MNT` reply carries.
    pub fn root(&self) -> FileHandle {
        Self::handle_for("/")
    }

    /// Graft a real directory into the tree under `prefix`.
    ///
    /// Only the structure and the file sizes are walked up front; contents are
    /// read per request. Give each medium its own prefix — see the module
    /// documentation for why sharing one would make their handles collide.
    ///
    /// A file that cannot be read or that vanishes between the walk and the
    /// `stat` is skipped: that costs one file, where propagating the error would
    /// cost the whole medium.
    pub fn mount(&mut self, prefix: &str, directory: &Path) -> std::io::Result<usize> {
        let prefix = prefix.trim_matches('/');
        let mount_point = format!("/{prefix}");
        // The mount point itself has to exist before anything can be added to
        // it, and it has to be a child of the root or a `LOOKUP` from the root
        // handle — which is where every walk starts — will not find it.
        self.ensure_parents(&mount_point);
        if !self
            .entries
            .contains_key(&Self::handle_for(&mount_point).key())
        {
            self.insert(
                &mount_point,
                Node::Directory {
                    children: Vec::new(),
                },
            );
        }
        if let Some((parent, name)) = split(&mount_point) {
            self.add_child(&parent, &name);
        }

        let mut mounted = 0;
        let mut stack = vec![(directory.to_path_buf(), mount_point)];

        while let Some((disk, virtual_path)) = stack.pop() {
            let Ok(listing) = std::fs::read_dir(&disk) else {
                continue;
            };
            let mut entries: Vec<_> = listing.flatten().collect();
            entries.sort_by_key(std::fs::DirEntry::file_name);

            for entry in entries {
                let name = entry.file_name().to_string_lossy().into_owned();
                let child_path = join(&virtual_path, &name);
                let Ok(metadata) = entry.metadata() else {
                    continue;
                };

                if metadata.is_dir() {
                    self.insert(
                        &child_path,
                        Node::Directory {
                            children: Vec::new(),
                        },
                    );
                    self.add_child(&virtual_path, &name);
                    stack.push((entry.path(), child_path));
                } else if metadata.is_file() {
                    self.insert(
                        &child_path,
                        Node::Disk {
                            path: entry.path(),
                            size: metadata.len(),
                        },
                    );
                    self.add_child(&virtual_path, &name);
                    mounted += 1;
                }
            }
        }
        Ok(mounted)
    }

    /// Remove a mounted subtree, and the mount point itself.
    ///
    /// Returns how many entries went. A prefix that was never mounted is not an
    /// error: a caller unmounting a slot does not always know whether anything
    /// was grafted into it.
    ///
    /// The handles under it become unresolvable, which is the point — a deck
    /// still holding one gets `NFSERR_STALE`, which is what a real player
    /// answers for a stick that has been pulled, and is what makes it let go.
    pub fn unmount(&mut self, prefix: &str) -> usize {
        let prefix = prefix.trim_matches('/');
        let mount_point = format!("/{prefix}");
        let under = format!("{mount_point}/");

        let doomed: Vec<FileHandleKey> = self
            .entries
            .iter()
            .filter(|(_, entry)| entry.path == mount_point || entry.path.starts_with(&under))
            .map(|(key, _)| *key)
            .collect();
        for key in &doomed {
            self.entries.remove(key);
        }

        // The parent still lists it as a child, and a READDIR that names an
        // entry LOOKUP then cannot find reads to a deck as a corrupt medium
        // rather than an absent one.
        if let Some((parent, name)) = split(&mount_point) {
            self.remove_child(&parent, &name);
        }
        doomed.len()
    }

    /// Drop a name from a directory's listing.
    fn remove_child(&mut self, directory: &str, name: &str) {
        if let Some(entry) = self.entries.get_mut(&Self::handle_for(directory).key())
            && let Node::Directory { children } = &mut entry.node
        {
            children.retain(|child| child != name);
        }
    }

    /// Add a file held in memory, creating any directories it needs.
    pub fn add_file(&mut self, path: &str, data: Vec<u8>) {
        self.ensure_parents(path);
        if let Some((parent, name)) = split(path) {
            self.add_child(&parent, &name);
        }
        self.insert(path, Node::Memory { data });
    }

    /// The node a handle names, or `None` — which is `NFSERR_STALE`.
    ///
    /// Matches on [`FileHandleKey`] only, because a CDJ rewrites the rest.
    pub fn resolve(&self, handle: FileHandle) -> Option<&Node> {
        self.entries.get(&handle.key()).map(|entry| &entry.node)
    }

    /// The path a handle names, for logging and for building child paths.
    pub fn path_of(&self, handle: FileHandle) -> Option<&str> {
        self.entries
            .get(&handle.key())
            .map(|entry| entry.path.as_str())
    }

    /// Walk one path component.
    ///
    /// Returns the handle **for the name as stored**, never for the name as
    /// asked for; see the module documentation.
    pub fn lookup(&self, directory: FileHandle, name: &str) -> Option<(FileHandle, &Node)> {
        let parent = self.entries.get(&directory.key())?;
        let Node::Directory { children } = &parent.node else {
            return None;
        };

        // The exact hit is what almost every lookup takes, so a genuinely
        // case-sensitive backing tree still resolves two names differing only in
        // case correctly. Only on a miss do we fall back.
        let stored = children
            .iter()
            .find(|child| child.as_str() == name)
            .or_else(|| children.iter().find(|child| fold(child) == fold(name)))?;

        let path = join(&parent.path, stored);
        let handle = Self::handle_for(&path);
        Some((handle, &self.entries.get(&handle.key())?.node))
    }

    /// The children of a directory, in listing order.
    pub fn read_dir(&self, directory: FileHandle) -> Option<&[String]> {
        match self.resolve(directory)? {
            Node::Directory { children } => Some(children),
            _ => None,
        }
    }

    /// Read a byte range.
    ///
    /// Returns `None` for a handle that names nothing or names a directory. A
    /// file that vanished mid-session — the medium was ejected — reads as empty
    /// rather than taking the server down.
    pub fn read(&self, handle: FileHandle, offset: u64, count: usize) -> Option<Vec<u8>> {
        match self.resolve(handle)? {
            Node::Directory { .. } => None,
            Node::Memory { data } => {
                let start = usize::try_from(offset)
                    .unwrap_or(usize::MAX)
                    .min(data.len());
                let end = start.saturating_add(count).min(data.len());
                Some(data.get(start..end).unwrap_or_default().to_vec())
            }
            Node::Disk { path, .. } => Some(read_range(path, offset, count)),
        }
    }

    /// Synthesise the attributes an NFS reply carries.
    ///
    /// `fileid` is the handle's leading word, which is what a real player puts
    /// there — true in every observed reply, so deriving it this way is
    /// consistent with the hardware for free.
    pub fn attributes(&self, handle: FileHandle) -> Option<Fattr> {
        let node = self.resolve(handle)?;
        let size = u32::try_from(node.size()).unwrap_or(u32::MAX);
        Some(Fattr {
            ftype: if node.is_dir() {
                FType::DIR
            } else {
                FType::REG
            },
            mode: if node.is_dir() { 0o040_755 } else { 0o100_644 },
            nlink: if node.is_dir() { 2 } else { 1 },
            uid: 0,
            gid: 0,
            size,
            blocksize: 512,
            rdev: 0,
            blocks: size / 512 + u32::from(size % 512 != 0),
            fsid: 1,
            fileid: handle.fileid(),
            atime_sec: EPOCH,
            atime_usec: 0,
            mtime_sec: EPOCH,
            mtime_usec: 0,
            ctime_sec: EPOCH,
            ctime_usec: 0,
        })
    }

    /// How many entries the tree holds.
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Whether the tree holds nothing but its root.
    pub fn is_empty(&self) -> bool {
        self.entries.len() <= 1
    }

    fn insert(&mut self, path: &str, node: Node) {
        let handle = Self::handle_for(path);
        self.entries.insert(
            handle.key(),
            Entry {
                path: path.to_owned(),
                node,
            },
        );
    }

    fn ensure_parents(&mut self, path: &str) {
        let mut walked = String::new();
        let components: Vec<&str> = path.split('/').filter(|part| !part.is_empty()).collect();
        let Some((_, parents)) = components.split_last() else {
            return;
        };
        for component in parents {
            let parent = if walked.is_empty() {
                "/".to_owned()
            } else {
                walked.clone()
            };
            walked = join(&parent, component);
            if !self.entries.contains_key(&Self::handle_for(&walked).key()) {
                self.insert(
                    &walked,
                    Node::Directory {
                        children: Vec::new(),
                    },
                );
            }
            self.add_child(&parent, component);
        }
    }

    fn add_child(&mut self, directory: &str, name: &str) {
        let key = Self::handle_for(directory).key();
        if let Some(Entry {
            node: Node::Directory { children },
            ..
        }) = self.entries.get_mut(&key)
            && !children.iter().any(|child| child == name)
        {
            children.push(name.to_owned());
        }
    }
}

/// The key two spellings of the same name must agree on.
fn fold(name: &str) -> String {
    name.nfc().collect::<String>().to_lowercase()
}

fn join(parent: &str, name: &str) -> String {
    if parent == "/" {
        format!("/{name}")
    } else {
        format!("{parent}/{name}")
    }
}

fn split(path: &str) -> Option<(String, String)> {
    let (parent, name) = path.rsplit_once('/')?;
    Some((
        if parent.is_empty() {
            "/".to_owned()
        } else {
            parent.to_owned()
        },
        name.to_owned(),
    ))
}

fn read_range(path: &Path, offset: u64, count: usize) -> Vec<u8> {
    use std::io::{Read, Seek, SeekFrom};
    let Ok(mut file) = std::fs::File::open(path) else {
        return Vec::new();
    };
    if file.seek(SeekFrom::Start(offset)).is_err() {
        return Vec::new();
    }
    let mut buffer = vec![0u8; count];
    let mut filled = 0;
    while filled < count {
        match file.read(buffer.get_mut(filled..).unwrap_or_default()) {
            Ok(0) | Err(_) => break,
            Ok(read) => filled += read,
        }
    }
    buffer.truncate(filled);
    buffer
}

#[cfg(test)]
mod tests {
    use super::*;

    fn tree() -> Vfs {
        let mut vfs = Vfs::new();
        vfs.add_file("/C/PIONEER/rekordbox/export.pdb", b"pdb".to_vec());
        vfs.add_file("/C/Contents/GESAFFELSTEIN/track.mp3", b"audio".to_vec());
        vfs.add_file(
            "/C/Contents/\u{30ab}\u{3099}\u{30ab}\u{3099}\u{30df}.mp3",
            b"nfd".to_vec(),
        );
        vfs
    }

    #[test]
    fn unmounting_takes_the_subtree_and_the_name_its_parent_lists() {
        // A stick is pulled. Every handle under it has to stop resolving, so a
        // deck still holding one gets NFSERR_STALE -- which is exactly what a
        // real player answers, and what makes the deck let go.
        let mut vfs = tree();
        let root = vfs.root();
        assert!(
            vfs.read_dir(root)
                .is_some_and(|names| names.contains(&"C".to_owned())),
            "the mount point is listed before it goes"
        );
        let file = Vfs::handle_for("/C/Contents/GESAFFELSTEIN/track.mp3");
        assert!(vfs.resolve(file).is_some());

        let removed = vfs.unmount("C");
        assert!(removed > 0, "something was there");
        assert!(
            vfs.resolve(file).is_none(),
            "a handle under a pulled medium must stop resolving"
        );
        assert!(
            vfs.resolve(Vfs::handle_for("/C")).is_none(),
            "and so must the mount point itself"
        );
        assert!(
            !vfs.read_dir(root)
                .is_some_and(|names| names.contains(&"C".to_owned())),
            "a READDIR that names an entry LOOKUP cannot find reads as a corrupt \
             medium rather than an absent one"
        );

        assert_eq!(
            vfs.unmount("C"),
            0,
            "unmounting an empty slot is not an error: a caller does not always \
             know whether anything was there"
        );
    }

    #[test]
    fn a_handle_survives_a_cdj_rewriting_its_tail() {
        // A CDJ keeps the first twelve bytes and overwrites the other twenty
        // with its own file reference (F28).
        let vfs = tree();
        let served = Vfs::handle_for("/C/PIONEER");
        let mut returned = served;
        returned.0[12..].copy_from_slice(&[0xab; 20]);

        assert_ne!(served, returned, "the bytes really are different");
        assert!(vfs.resolve(returned).is_some(), "and it must still resolve");
        assert_eq!(vfs.path_of(returned), Some("/C/PIONEER"));
    }

    #[test]
    fn two_media_do_not_share_a_handle() {
        // A handle is a hash of the path, so a shared root would mint identical
        // handles for the same relative path.
        assert_ne!(Vfs::handle_for("/C/PIONEER"), Vfs::handle_for("/B/PIONEER"));
        assert_ne!(Vfs::handle_for("/C"), Vfs::handle_for("/B"));
    }

    #[test]
    fn a_lookup_walks_one_component() {
        let vfs = tree();
        let (contents, node) = vfs.lookup(Vfs::handle_for("/C"), "Contents").unwrap();
        assert!(node.is_dir());
        assert_eq!(vfs.path_of(contents), Some("/C/Contents"));
    }

    #[test]
    fn a_name_differing_only_in_case_still_resolves() {
        // export.pdb says `Gesaffelstein` where the directory is
        // `GESAFFELSTEIN`; a FAT32 driver does not notice and neither may we.
        let vfs = tree();
        let contents = Vfs::handle_for("/C/Contents");
        let (handle, _) = vfs
            .lookup(contents, "Gesaffelstein")
            .expect("the fold must match");
        assert_eq!(
            vfs.path_of(handle),
            Some("/C/Contents/GESAFFELSTEIN"),
            "the handle must be for the name as stored, or every later use is STALE",
        );
    }

    #[test]
    fn a_name_differing_only_in_normalisation_still_resolves() {
        // The pdb stores NFC where the filesystem reports NFD, because
        // rekordbox wrote them through different APIs.
        let vfs = tree();
        let composed = "\u{30ac}\u{30ac}\u{30df}.mp3";
        let decomposed = "\u{30ab}\u{3099}\u{30ab}\u{3099}\u{30df}.mp3";
        assert_ne!(
            composed.as_bytes(),
            decomposed.as_bytes(),
            "genuinely different bytes"
        );

        let (handle, _) = vfs
            .lookup(Vfs::handle_for("/C/Contents"), composed)
            .expect("NFC must find NFD");
        assert_eq!(
            vfs.path_of(handle),
            Some(format!("/C/Contents/{decomposed}").as_str())
        );
    }

    #[test]
    fn an_exact_match_wins_over_a_folded_one() {
        let mut vfs = Vfs::new();
        vfs.add_file("/x/TRACK.mp3", b"upper".to_vec());
        vfs.add_file("/x/track.mp3", b"lower".to_vec());
        let (handle, _) = vfs.lookup(Vfs::handle_for("/x"), "track.mp3").unwrap();
        assert_eq!(
            vfs.read(handle, 0, 16).as_deref(),
            Some(b"lower".as_slice())
        );
    }

    #[test]
    fn an_unknown_handle_is_stale_not_a_panic() {
        let vfs = tree();
        assert!(vfs.resolve(Vfs::handle_for("/nowhere")).is_none());
        assert!(vfs.read(Vfs::handle_for("/nowhere"), 0, 16).is_none());
    }

    #[test]
    fn a_read_past_the_end_is_short_not_an_error() {
        let vfs = tree();
        let handle = Vfs::handle_for("/C/PIONEER/rekordbox/export.pdb");
        assert_eq!(
            vfs.read(handle, 0, 8192).as_deref(),
            Some(b"pdb".as_slice())
        );
        assert_eq!(vfs.read(handle, 2, 8192).as_deref(), Some(b"b".as_slice()));
        assert_eq!(vfs.read(handle, 99, 8192).as_deref(), Some(b"".as_slice()));
    }

    #[test]
    fn a_directory_cannot_be_read() {
        let vfs = tree();
        assert!(vfs.read(Vfs::handle_for("/C"), 0, 16).is_none());
    }

    #[test]
    fn attributes_take_their_fileid_from_the_handle() {
        let vfs = tree();
        let handle = Vfs::handle_for("/C/PIONEER/rekordbox/export.pdb");
        let attributes = vfs.attributes(handle).unwrap();
        assert_eq!(attributes.fileid, handle.fileid());
        assert_eq!(attributes.ftype, FType::REG);
        assert_eq!(attributes.size, 3);
    }

    #[test]
    fn a_mounted_directory_is_walkable() {
        let root = std::env::temp_dir().join(format!("prolink-vfs-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&root);
        std::fs::create_dir_all(root.join("PIONEER/rekordbox")).unwrap();
        std::fs::write(root.join("PIONEER/rekordbox/export.pdb"), b"real bytes").unwrap();

        let mut vfs = Vfs::new();
        let mounted = vfs.mount("C", &root).unwrap();
        assert_eq!(mounted, 1);

        let (pioneer, _) = vfs.lookup(Vfs::handle_for("/C"), "PIONEER").unwrap();
        let (rekordbox, _) = vfs.lookup(pioneer, "rekordbox").unwrap();
        let (pdb, node) = vfs.lookup(rekordbox, "export.pdb").unwrap();
        assert!(!node.is_dir());
        assert_eq!(vfs.read(pdb, 5, 5).as_deref(), Some(b"bytes".as_slice()));

        std::fs::remove_dir_all(&root).unwrap();
    }
}