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
//! This is a bridge of [`vfs`] and TAR files.

#![warn(missing_docs)]

use stable_deref_trait::StableDeref;
#[allow(unused_imports)]
use std::{
    borrow::Cow,
    collections::HashMap,
    fmt::Debug,
    fs::File,
    io::{Cursor, Write},
    ops::Deref,
    path::{Iter, Path},
};
use tar_parser2::*;
use vfs::{error::VfsErrorKind, *};

/// A readonly tar archive filesystem.
#[derive(Debug)]
pub struct TarFS<F: StableDeref<Target = [u8]>> {
    #[allow(dead_code)]
    file: F,
    root: DirTree,
}

impl<F: StableDeref<Target = [u8]>> TarFS<F> {
    /// Create [`TarFS`] from a specified file or buffer.
    pub fn new(file: F) -> VfsResult<Self> {
        // SAFETY: the entries won't live longer than mmap
        let (_, entries) = parse_tar(unsafe { &*(file.deref() as *const [u8]) })
            .map_err(|e| VfsErrorKind::Other(e.to_string()))?;
        let root = DirTreeBuilder::default().build(&entries);
        Ok(Self { file, root })
    }

    fn find_entry(&self, path: &str) -> Option<EntryRef> {
        let mut path: Cow<Path> = strip_path(path).into();
        loop {
            let res = Self::find_entry_impl(&self.root, path.iter());
            if let Some(EntryRef::Link(p)) = res {
                path = Self::read_link(path, p);
            } else {
                return res;
            }
        }
    }

    fn find_entry_impl<'a>(dir: &'a DirTree, mut path: Iter) -> Option<EntryRef<'a>> {
        let next_path = match path.next() {
            Some(str) => str.to_string_lossy(),
            None => return Some(EntryRef::Directory(dir)),
        };
        if let Some(entry) = dir.get(next_path.as_ref()) {
            match entry {
                Entry::File(buf) => {
                    debug_assert!(path.next().is_none());
                    Some(EntryRef::File(buf))
                }
                Entry::Directory(dir) => Self::find_entry_impl(dir, path),
                Entry::Link(p) => {
                    debug_assert!(path.next().is_none());
                    Some(EntryRef::Link(p))
                }
            }
        } else {
            None
        }
    }

    fn read_link<'a>(path: Cow<Path>, target: &'a str) -> Cow<'a, Path> {
        if let Some(target) = target.strip_prefix('/') {
            Path::new(target).into()
        } else {
            let mut path = path.into_owned();
            path.pop();
            let target_components = Path::new(target).iter();
            for c in target_components {
                if c == ".." {
                    path.pop();
                } else {
                    path.push(c);
                }
            }
            path.into()
        }
    }
}

#[cfg(feature = "mmap")]
use memmap2::{Mmap, MmapOptions};

#[cfg(feature = "mmap")]
impl TarFS<Mmap> {
    /// Create [`TarFS`] from the archive path.
    pub fn new_mmap(p: impl AsRef<Path>) -> VfsResult<Self> {
        Self::from_std_file(&File::open(p)?)
    }

    /// Create [`TarFS`] from [`File`].
    /// Note that the filesystem is still valid after the [`File`] being dropped.
    pub fn from_std_file(f: &File) -> VfsResult<Self> {
        // SAFETY: mmap with COW
        let file = unsafe { MmapOptions::new().map_copy_read_only(f) }?;
        TarFS::new(file)
    }

    /// Get the reference of the inner [`Mmap`].
    pub fn as_inner(&self) -> &Mmap {
        &self.file
    }

    /// Get the inner [`Mmap`].
    pub fn into_inner(self) -> Mmap {
        self.file
    }
}

impl<F: StableDeref<Target = [u8]> + Debug + Send + Sync + 'static> FileSystem for TarFS<F> {
    fn read_dir(&self, path: &str) -> VfsResult<Box<dyn Iterator<Item = String> + Send>> {
        let dir = if path.is_empty() {
            &self.root
        } else {
            match self.find_entry(path) {
                Some(EntryRef::Directory(dir)) => dir,
                _ => return Err(VfsErrorKind::FileNotFound.into()),
            }
        };
        Ok(Box::new(
            dir.keys()
                .map(|s| s.to_string())
                .collect::<Vec<_>>()
                .into_iter(),
        ))
    }

    fn create_dir(&self, _path: &str) -> VfsResult<()> {
        Err(VfsErrorKind::NotSupported.into())
    }

    fn open_file(&self, path: &str) -> VfsResult<Box<dyn SeekAndRead + Send>> {
        match self.find_entry(path) {
            Some(EntryRef::File(buf)) => Ok(Box::new(Cursor::new(buf))),
            _ => Err(VfsErrorKind::FileNotFound.into()),
        }
    }

    fn create_file(&self, _path: &str) -> VfsResult<Box<dyn Write + Send>> {
        Err(VfsErrorKind::NotSupported.into())
    }

    fn append_file(&self, _path: &str) -> VfsResult<Box<dyn Write + Send>> {
        Err(VfsErrorKind::NotSupported.into())
    }

    fn metadata(&self, path: &str) -> VfsResult<VfsMetadata> {
        match self.find_entry(path) {
            Some(e) => match e {
                EntryRef::File(buf) => Ok(VfsMetadata {
                    file_type: VfsFileType::File,
                    len: buf.len() as u64,
                }),
                EntryRef::Directory(_) => Ok(VfsMetadata {
                    file_type: VfsFileType::Directory,
                    len: 0,
                }),
                EntryRef::Link(_) => unreachable!(),
            },
            None => Err(VfsErrorKind::FileNotFound.into()),
        }
    }

    fn exists(&self, path: &str) -> VfsResult<bool> {
        Ok(self.find_entry(path).is_some())
    }

    fn remove_file(&self, _path: &str) -> VfsResult<()> {
        Err(VfsErrorKind::NotSupported.into())
    }

    fn remove_dir(&self, _path: &str) -> VfsResult<()> {
        Err(VfsErrorKind::NotSupported.into())
    }
}

#[derive(Debug)]
enum Entry {
    File(&'static [u8]),
    Directory(DirTree),
    Link(&'static str),
}

#[derive(Debug)]
enum EntryRef<'a> {
    File(&'static [u8]),
    Directory(&'a DirTree),
    Link(&'static str),
}

type DirTree = HashMap<String, Entry>;

#[derive(Debug, Default)]
struct DirTreeBuilder {
    root: DirTree,
    longname: Option<Cow<'static, str>>,
    longlink: Option<&'static str>,
    realsize: Option<u64>,
}

impl DirTreeBuilder {
    pub fn build(mut self, entries: &[TarEntry<'static>]) -> DirTree {
        for entry in entries {
            match entry.header.typeflag {
                // Don't handle directory diff.
                TypeFlag::Directory | TypeFlag::GnuDirectory => {
                    let name = self.get_name(entry);
                    self.insert_dir(Path::new(name.deref()));
                }
                // Treat links as redirects.
                TypeFlag::HardLink | TypeFlag::SymbolicLink => {
                    let name = self.get_name(entry);
                    let target = self.longlink.take().unwrap_or(entry.header.linkname);
                    self.insert_link(Path::new(name.deref()), target)
                }
                // Handle long name.
                TypeFlag::GnuLongName => {
                    debug_assert!(entry.header.size > 1);
                    if let Ok((_, name)) = parse_long_name(entry.contents) {
                        debug_assert!(self.longname.is_none());
                        self.longname = Some(Cow::Borrowed(name));
                    }
                }
                // Handle long link name.
                TypeFlag::GnuLongLink => {
                    debug_assert!(entry.header.size > 1);
                    if let Ok((_, target)) = parse_long_name(entry.contents) {
                        debug_assert!(self.longlink.is_none());
                        self.longlink = Some(target);
                    }
                }
                // Handle PAX.
                TypeFlag::Pax => {
                    if let Ok((_, pax)) = parse_pax(entry.contents) {
                        if let Some(name) = pax.get("path") {
                            debug_assert!(self.longname.is_none());
                            self.longname = Some(Cow::Borrowed(name));
                        }
                        if let Some(target) = pax.get("linkpath") {
                            debug_assert!(self.longlink.is_none());
                            self.longlink = Some(target);
                        }
                        if let Some(size) = pax.get("size") {
                            debug_assert!(self.realsize.is_none());
                            self.realsize = size.parse().ok();
                        }
                    }
                }
                // The file-specific settings should not appear in global PAX.
                // GNU volume header should be ignored.
                TypeFlag::PaxGlobal | TypeFlag::GnuVolumeHeader => {}
                // A POSIX-compliant impl must treat any unrecognized typeflag as normal file.
                _ => {
                    let name = self.get_name(entry);
                    let size = self.realsize.take().unwrap_or(entry.header.size) as usize;
                    self.insert_file(Path::new(name.deref()), &entry.contents[..size])
                }
            }
        }
        self.root
    }

    fn get_name(&mut self, entry: &TarEntry<'static>) -> Cow<'static, str> {
        self.longname
            .take()
            .unwrap_or_else(|| Self::get_full_name(entry))
    }

    fn get_full_name(entry: &TarEntry<'static>) -> Cow<'static, str> {
        if let ExtraHeader::UStar(ustar) = &entry.header.ustar {
            if let UStarExtraHeader::Posix(header) = &ustar.extra {
                if !header.prefix.is_empty() {
                    return Cow::Owned(format!("{}/{}", header.prefix, entry.header.name));
                }
            }
        };
        Cow::Borrowed(entry.header.name)
    }

    fn insert_dir(&mut self, path: &Path) -> &mut DirTree {
        let path = path.iter();
        let mut current = &mut self.root;
        for p in path {
            let entry = current
                .entry(p.to_string_lossy().into_owned())
                .or_insert_with(|| Entry::Directory(DirTree::new()));
            current = if let Entry::Directory(dir) = entry {
                dir
            } else {
                unreachable!()
            };
        }
        current
    }

    fn insert_file(&mut self, path: &Path, buf: &'static [u8]) {
        let current = if let Some(parent) = path.parent() {
            self.insert_dir(parent)
        } else {
            &mut self.root
        };
        if let Some(filename) = path.file_name() {
            current.insert(filename.to_string_lossy().into_owned(), Entry::File(buf));
        }
    }

    fn insert_link(&mut self, path: &Path, target: &'static str) {
        let current = if let Some(parent) = path.parent() {
            self.insert_dir(parent)
        } else {
            &mut self.root
        };
        if let Some(filename) = path.file_name() {
            current.insert(filename.to_string_lossy().into_owned(), Entry::Link(target));
        }
    }
}

/// [`Path`] doesn't iterate well with the prefix `/`.
fn strip_path(path: &str) -> &Path {
    Path::new(path.strip_prefix('/').unwrap_or(path))
}

#[cfg(test)]
mod test {
    use crate::TarFS;
    use tempfile::tempfile;
    use vfs::VfsPath;

    #[test]
    fn basic() {
        let file = tempfile().unwrap();
        let mut archive = tar::Builder::new(file);
        archive.append_dir_all("src", "src").unwrap();
        let file = archive.into_inner().unwrap();

        let fs = TarFS::from_std_file(&file).unwrap();
        let root = VfsPath::from(fs);
        let mut files = root
            .join("src")
            .unwrap()
            .read_dir()
            .unwrap()
            .map(|p| p.filename())
            .collect::<Vec<_>>();
        files.sort();
        assert_eq!(&files, &["lib.rs"]);

        let mut buffer = String::new();
        root.join("src/lib.rs")
            .unwrap()
            .open_file()
            .unwrap()
            .read_to_string(&mut buffer)
            .unwrap();
        let real_content = std::fs::read_to_string("src/lib.rs").unwrap();
        assert_eq!(buffer, real_content);
    }

    #[test]
    fn long() {
        let name = "a".repeat(1024);

        let file = tempfile().unwrap();
        let mut archive = tar::Builder::new(file);
        archive.append_path_with_name("src/lib.rs", &name).unwrap();
        let file = archive.into_inner().unwrap();

        let fs = TarFS::from_std_file(&file).unwrap();
        let root = VfsPath::from(fs);

        let mut buffer = String::new();
        root.join(name)
            .unwrap()
            .open_file()
            .unwrap()
            .read_to_string(&mut buffer)
            .unwrap();
        let real_content = std::fs::read_to_string("src/lib.rs").unwrap();
        assert_eq!(buffer, real_content);
    }

    #[test]
    fn link() {
        let name = "a".repeat(1024);
        let link_name = "b".repeat(1024);

        let file = tempfile().unwrap();
        let mut archive = tar::Builder::new(file);
        archive.append_path_with_name("src/lib.rs", &name).unwrap();
        {
            let mut header = tar::Header::new_gnu();
            header.set_entry_type(tar::EntryType::Symlink);
            archive.append_link(&mut header, &link_name, &name).unwrap();
        }
        let file = archive.into_inner().unwrap();

        let fs = TarFS::from_std_file(&file).unwrap();
        let root = VfsPath::from(fs);

        let mut buffer = String::new();
        root.join(link_name)
            .unwrap()
            .open_file()
            .unwrap()
            .read_to_string(&mut buffer)
            .unwrap();
        let real_content = std::fs::read_to_string("src/lib.rs").unwrap();
        assert_eq!(buffer, real_content);
    }

    #[test]
    fn ustar() {
        let name = format!("{}/{}", "a".repeat(80), "b".repeat(80));
        let link_name = format!("{}/{}", "c".repeat(80), "d".repeat(80));

        let file = tempfile().unwrap();
        let mut archive = tar::Builder::new(file);
        {
            let mut header = tar::Header::new_ustar();
            let file = std::fs::File::open("src/lib.rs").unwrap();
            let size = file.metadata().unwrap().len();
            header.set_size(size);
            archive.append_data(&mut header, &name, file).unwrap();
        }
        {
            let mut header = tar::Header::new_ustar();
            header.set_entry_type(tar::EntryType::Symlink);
            archive
                .append_link(&mut header, &link_name, format!("../{name}"))
                .unwrap();
        }
        let file = archive.into_inner().unwrap();

        let fs = TarFS::from_std_file(&file).unwrap();
        let root = VfsPath::from(fs);

        let mut buffer = String::new();
        root.join(link_name)
            .unwrap()
            .open_file()
            .unwrap()
            .read_to_string(&mut buffer)
            .unwrap();
        let real_content = std::fs::read_to_string("src/lib.rs").unwrap();
        assert_eq!(buffer, real_content);
    }
}