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
use anyhow::anyhow;

use std::convert::TryInto;
use std::io::{Error as IoError, ErrorKind as IoErrorKind, Read, Seek, SeekFrom, Write};
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;

use crate::mem_fs::FileSystem as MemFileSystem;
use crate::{
    FileDescriptor, FileOpener, FileSystem, FsError, Metadata, OpenOptions, OpenOptionsConfig,
    ReadDir, VirtualFile,
};
use webc::{FsEntry, FsEntryType, OwnedFsEntryFile};

/// Custom file system wrapper to map requested file paths
#[derive(Debug)]
pub struct StaticFileSystem {
    pub package: String,
    pub volumes: Arc<webc::IndexMap<String, webc::Volume<'static>>>,
    pub memory: Arc<MemFileSystem>,
}

impl StaticFileSystem {
    pub fn init(bytes: &'static [u8], package: &str) -> Option<Self> {
        let volumes = Arc::new(webc::WebC::parse_volumes_from_fileblock(bytes).ok()?);
        let fs = Self {
            package: package.to_string(),
            volumes: volumes.clone(),
            memory: Arc::new(MemFileSystem::default()),
        };
        let volume_names = fs.volumes.keys().cloned().collect::<Vec<_>>();
        for volume_name in volume_names {
            let directories = volumes.get(&volume_name).unwrap().list_directories();
            for directory in directories {
                let _ = fs.create_dir(Path::new(&directory));
            }
        }
        Some(fs)
    }
}

/// Custom file opener, returns a WebCFile
#[derive(Debug)]
struct WebCFileOpener {
    pub package: String,
    pub volumes: Arc<webc::IndexMap<String, webc::Volume<'static>>>,
    pub memory: Arc<MemFileSystem>,
}

impl FileOpener for WebCFileOpener {
    fn open(
        &mut self,
        path: &Path,
        _conf: &OpenOptionsConfig,
    ) -> Result<Box<dyn VirtualFile + Send + Sync>, FsError> {
        match get_volume_name_opt(path) {
            Some(volume) => {
                let file = (*self.volumes)
                    .get(&volume)
                    .ok_or(FsError::EntityNotFound)?
                    .get_file_entry(&format!("{}", path.display()))
                    .map_err(|_e| FsError::EntityNotFound)?;

                Ok(Box::new(WebCFile {
                    package: self.package.clone(),
                    volume,
                    volumes: self.volumes.clone(),
                    path: path.to_path_buf(),
                    entry: file,
                    cursor: 0,
                }))
            }
            None => {
                for (volume, v) in self.volumes.iter() {
                    let entry = match v.get_file_entry(&format!("{}", path.display())) {
                        Ok(s) => s,
                        Err(_) => continue, // error
                    };

                    return Ok(Box::new(WebCFile {
                        package: self.package.clone(),
                        volume: volume.clone(),
                        volumes: self.volumes.clone(),
                        path: path.to_path_buf(),
                        entry,
                        cursor: 0,
                    }));
                }
                self.memory.new_open_options().open(path)
            }
        }
    }
}

#[derive(Debug)]
pub struct WebCFile {
    pub volumes: Arc<webc::IndexMap<String, webc::Volume<'static>>>,
    pub package: String,
    pub volume: String,
    pub path: PathBuf,
    pub entry: OwnedFsEntryFile,
    pub cursor: u64,
}

impl VirtualFile for WebCFile {
    fn last_accessed(&self) -> u64 {
        0
    }
    fn last_modified(&self) -> u64 {
        0
    }
    fn created_time(&self) -> u64 {
        0
    }
    fn size(&self) -> u64 {
        self.entry.get_len()
    }
    fn set_len(&mut self, _new_size: u64) -> Result<(), FsError> {
        Ok(())
    }
    fn unlink(&mut self) -> Result<(), FsError> {
        Ok(())
    }
    fn bytes_available(&self) -> Result<usize, FsError> {
        Ok(self.size().try_into().unwrap_or(u32::MAX as usize))
    }
    fn sync_to_disk(&self) -> Result<(), FsError> {
        Ok(())
    }
    fn get_fd(&self) -> Option<FileDescriptor> {
        None
    }
}

impl Read for WebCFile {
    fn read(&mut self, buf: &mut [u8]) -> Result<usize, IoError> {
        let bytes = self
            .volumes
            .get(&self.volume)
            .ok_or_else(|| {
                IoError::new(
                    IoErrorKind::NotFound,
                    anyhow!("Unknown volume {:?}", self.volume),
                )
            })?
            .get_file_bytes(&self.entry)
            .map_err(|e| IoError::new(IoErrorKind::NotFound, e))?;

        let cursor: usize = self.cursor.try_into().unwrap_or(u32::MAX as usize);
        let _start = cursor.min(bytes.len());
        let bytes = &bytes[cursor..];

        let mut len = 0;
        for (source, target) in bytes.iter().zip(buf.iter_mut()) {
            *target = *source;
            len += 1;
        }

        Ok(len)
    }
}

// WebC file is not writable, the FileOpener will return a MemoryFile for writing instead
// This code should never be executed (since writes are redirected to memory instead).
impl Write for WebCFile {
    fn write(&mut self, buf: &[u8]) -> Result<usize, IoError> {
        Ok(buf.len())
    }
    fn flush(&mut self) -> Result<(), IoError> {
        Ok(())
    }
}

impl Seek for WebCFile {
    fn seek(&mut self, pos: SeekFrom) -> Result<u64, IoError> {
        let self_size = self.size();
        match pos {
            SeekFrom::Start(s) => {
                self.cursor = s.min(self_size);
            }
            SeekFrom::End(e) => {
                let self_size_i64 = self_size.try_into().unwrap_or(i64::MAX);
                self.cursor = ((self_size_i64).saturating_add(e))
                    .min(self_size_i64)
                    .try_into()
                    .unwrap_or(i64::MAX as u64);
            }
            SeekFrom::Current(c) => {
                self.cursor = (self
                    .cursor
                    .saturating_add(c.try_into().unwrap_or(i64::MAX as u64)))
                .min(self_size);
            }
        }
        Ok(self.cursor)
    }
}

fn get_volume_name_opt<P: AsRef<Path>>(path: P) -> Option<String> {
    use std::path::Component::Normal;
    if let Some(Normal(n)) = path.as_ref().components().next() {
        if let Some(s) = n.to_str() {
            if s.ends_with(':') {
                return Some(s.replace(':', ""));
            }
        }
    }
    None
}

fn transform_into_read_dir<'a>(path: &Path, fs_entries: &[FsEntry<'a>]) -> crate::ReadDir {
    let entries = fs_entries
        .iter()
        .map(|e| crate::DirEntry {
            path: path.join(&*e.text),
            metadata: Ok(crate::Metadata {
                ft: translate_file_type(e.fs_type),
                accessed: 0,
                created: 0,
                modified: 0,
                len: e.get_len(),
            }),
        })
        .collect();

    crate::ReadDir::new(entries)
}

impl FileSystem for StaticFileSystem {
    fn read_dir(&self, path: &Path) -> Result<ReadDir, FsError> {
        let path = normalizes_path(path);
        for volume in self.volumes.values() {
            let read_dir_result = volume
                .read_dir(&path)
                .map(|o| transform_into_read_dir(Path::new(&path), o.as_ref()))
                .map_err(|_| FsError::EntityNotFound);

            match read_dir_result {
                Ok(o) => {
                    return Ok(o);
                }
                Err(_) => {
                    continue;
                }
            }
        }

        self.memory.read_dir(Path::new(&path))
    }
    fn create_dir(&self, path: &Path) -> Result<(), FsError> {
        let path = normalizes_path(path);
        let result = self.memory.create_dir(Path::new(&path));
        result
    }
    fn remove_dir(&self, path: &Path) -> Result<(), FsError> {
        let path = normalizes_path(path);
        let result = self.memory.remove_dir(Path::new(&path));
        if self
            .volumes
            .values()
            .find_map(|v| v.get_file_entry(&path).ok())
            .is_some()
        {
            Ok(())
        } else {
            result
        }
    }
    fn rename(&self, from: &Path, to: &Path) -> Result<(), FsError> {
        let from = normalizes_path(from);
        let to = normalizes_path(to);
        let result = self.memory.rename(Path::new(&from), Path::new(&to));
        if self
            .volumes
            .values()
            .find_map(|v| v.get_file_entry(&from).ok())
            .is_some()
        {
            Ok(())
        } else {
            result
        }
    }
    fn metadata(&self, path: &Path) -> Result<Metadata, FsError> {
        let path = normalizes_path(path);
        if let Some(fs_entry) = self
            .volumes
            .values()
            .find_map(|v| v.get_file_entry(&path).ok())
        {
            Ok(Metadata {
                ft: translate_file_type(FsEntryType::File),
                accessed: 0,
                created: 0,
                modified: 0,
                len: fs_entry.get_len(),
            })
        } else if let Some(_fs) = self.volumes.values().find_map(|v| v.read_dir(&path).ok()) {
            Ok(Metadata {
                ft: translate_file_type(FsEntryType::Dir),
                accessed: 0,
                created: 0,
                modified: 0,
                len: 0,
            })
        } else {
            self.memory.metadata(Path::new(&path))
        }
    }
    fn remove_file(&self, path: &Path) -> Result<(), FsError> {
        let path = normalizes_path(path);
        let result = self.memory.remove_file(Path::new(&path));
        if self
            .volumes
            .values()
            .find_map(|v| v.get_file_entry(&path).ok())
            .is_some()
        {
            Ok(())
        } else {
            result
        }
    }
    fn new_open_options(&self) -> OpenOptions {
        OpenOptions::new(Box::new(WebCFileOpener {
            package: self.package.clone(),
            volumes: self.volumes.clone(),
            memory: self.memory.clone(),
        }))
    }
    fn symlink_metadata(&self, path: &Path) -> Result<Metadata, FsError> {
        let path = normalizes_path(path);
        if let Some(fs_entry) = self
            .volumes
            .values()
            .find_map(|v| v.get_file_entry(&path).ok())
        {
            Ok(Metadata {
                ft: translate_file_type(FsEntryType::File),
                accessed: 0,
                created: 0,
                modified: 0,
                len: fs_entry.get_len(),
            })
        } else if self
            .volumes
            .values()
            .find_map(|v| v.read_dir(&path).ok())
            .is_some()
        {
            Ok(Metadata {
                ft: translate_file_type(FsEntryType::Dir),
                accessed: 0,
                created: 0,
                modified: 0,
                len: 0,
            })
        } else {
            self.memory.symlink_metadata(Path::new(&path))
        }
    }
}

fn normalizes_path(path: &Path) -> String {
    let path = format!("{}", path.display());
    if !path.starts_with('/') {
        format!("/{path}")
    } else {
        path
    }
}

fn translate_file_type(f: FsEntryType) -> crate::FileType {
    crate::FileType {
        dir: f == FsEntryType::Dir,
        file: f == FsEntryType::File,
        symlink: false,
        char_device: false,
        block_device: false,
        socket: false,
        fifo: false,
    }
}