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
use super::*;

use ::vfs04::*;

use ::zip::result::ZipError;
use ::zip::write::FileOptions;

use std::io::{self, Cursor};
use std::mem::replace;
use std::ops::Drop;
use std::sync::{Arc, Weak};



impl<IO: Write + Seek + Send + 'static> ZipWriteOnly<IO> {
    fn normalize_file<'s>(&self, orig: &'s str) -> VfsResult<&'s str> {
        if orig.contains('\\') || orig.ends_with('/') {
            return Err(VfsError::InvalidPath { path: orig.into() }); // Invalid path for file

        }
        let path = if orig.starts_with('/') { &orig[1..] } else { orig };
        if path.split('/').any(|c| c == "" || c == "." || c == "..") {
            return Err(VfsError::InvalidPath { path: orig.into() });
        }
        Ok(path)
    }

    fn normalize_path_dir<'s>(&self, orig: &'s str) -> VfsResult<(&'s str, bool)> {
        if orig == "" || orig == "/" {
            Ok(("", true)) // root dir

        } else if orig.ends_with('/') {
            Ok((self.normalize_file(&orig[..orig.len()-1])?, true))
        } else {
            Ok((self.normalize_file(orig)?, false))
        }
    }
}

impl<IO: Write + Seek + Send + 'static> FileSystem for ZipWriteOnly<IO> {
    fn create_dir(&self, path: &str) -> VfsResult<()> {
        let path = self.normalize_path_dir(path)?.0;
        let mut imp = self.imp.lock().unwrap();
        if path == "" {
            imp.dirs.insert(path.into());
            return Ok(());
        }
        #[allow(unreachable_patterns)] // zip 0.5.3 ..= 0.5.6 didn't have any extra cases

        match imp.writer.add_directory(path, FileOptions::default()) {
            Err(ZipError::FileNotFound)             => return Err(VfsError::FileNotFound { path: path.into() }),
            Err(ZipError::Io(e))                    => return Err(VfsError::IoError(e)),
            Err(ZipError::InvalidArchive(e))        => return Err(VfsError::IoError(io::Error::new(io::ErrorKind::InvalidData, e))),
            Err(ZipError::UnsupportedArchive(e))    => return Err(VfsError::IoError(io::Error::new(io::ErrorKind::InvalidData, e))),
            Err(other)                              => return Err(VfsError::Other { message: other.to_string() }),
            Ok(())                                  => {},
        }
        imp.dirs.insert(path.into());
        Ok(())
    }

    fn create_file(&self, path: &str) -> VfsResult<Box<dyn Write>> {
        let path = self.normalize_file(path)?;
        Ok(Box::new(ZipFileWriter {
            path:   path.into(),
            buffer: Cursor::new(Vec::new()),
            imp:    match self.weak {
                true    => ZipFileWriterRef::Weak(Arc::downgrade(&self.imp)),
                false   => ZipFileWriterRef::Strong(Arc::clone(&self.imp)),
            }
        }))
    }

    fn metadata(&self, path: &str) -> VfsResult<VfsMetadata> {
        let path = self.normalize_path_dir(path)?.0;
        if path == "" {
            Ok(VfsMetadata { file_type: VfsFileType::Directory, len: 0 })
        } else if self.imp.lock().unwrap().dirs.contains(path) {
            Ok(VfsMetadata { file_type: VfsFileType::Directory, len: 0 })
        } else {
            Err(VfsError::FileNotFound { path: path.into() })
        }
    }

    fn exists(&self, path: &str) -> bool {
        let path = match self.normalize_path_dir(path) {
            Err(_) => return false,
            Ok((path, _dir)) => path,
        };
        path == "" || self.imp.lock().unwrap().dirs.contains(path)
    }

    // these all involve reading, which zip::write::ZipWriter doesn't support

    fn read_dir     (&self, _path: &str)            -> VfsResult<Box<dyn Iterator<Item = String>>>  { Err(VfsError::NotSupported) }
    fn open_file    (&self, _path: &str)            -> VfsResult<Box<dyn SeekAndRead>>              { Err(VfsError::NotSupported) }
    fn append_file  (&self, _path: &str)            -> VfsResult<Box<dyn Write>>                    { Err(VfsError::NotSupported) }
    fn remove_file  (&self, _path: &str)            -> VfsResult<()>                                { Err(VfsError::NotSupported) }
    fn remove_dir   (&self, _path: &str)            -> VfsResult<()>                                { Err(VfsError::NotSupported) }
    fn copy_file    (&self, _src: &str, _dst: &str) -> VfsResult<()>                                { Err(VfsError::NotSupported) }
    fn move_file    (&self, _src: &str, _dst: &str) -> VfsResult<()>                                { Err(VfsError::NotSupported) }
    fn move_dir     (&self, _src: &str, _dst: &str) -> VfsResult<()>                                { Err(VfsError::NotSupported) }
}

enum ZipFileWriterRef<IO: Write + Seek + Send + 'static> {
    Weak(Weak<Mutex<Imp<IO>>>),
    Strong(Arc<Mutex<Imp<IO>>>),
}

struct ZipFileWriter<IO: Write + Seek + Send + 'static> {
    path:   String,
    buffer: Cursor<Vec<u8>>,
    imp:    ZipFileWriterRef<IO>,
}

impl<IO: Write + Seek + Send> Write for ZipFileWriter<IO> {
    // Forward all the Write methods I can to the underlying buffer

    fn write                (&mut self, buf: &[u8])             -> io::Result<usize>    { self.buffer.write(buf) }
    fn flush                (&mut self)                         -> io::Result<()>       { self.buffer.flush() }
    fn write_all            (&mut self, buf: &[u8])             -> io::Result<()>       { self.buffer.write_all(buf) }
    fn write_fmt            (&mut self, fmt: fmt::Arguments<'_>)-> io::Result<()>       { self.buffer.write_fmt(fmt) }

    // unstable or missing in 1.34.0

    //fn write_vectored       (&mut self, bufs: &[IoSlice<'_>])   -> io::Result<usize>    { self.buffer.write_vectored(bufs) }

    //fn is_write_vectored    (&self)                             -> bool             { self.buffer.is_write_vectored() }

    //fn write_all_vectored   (&mut self, mut bufs: &mut [IoSlice<'_>]) -> io::Result<()> { self.buffer.write_all_vectored(bufs) }

}

impl<IO: Write + Seek + Send> Drop for ZipFileWriter<IO> {
    fn drop(&mut self) {
        let path    = replace(&mut self.path, String::new());
        let buffer  = replace(&mut self.buffer, Cursor::new(Vec::new())).into_inner();
        let imp     = match replace(&mut self.imp, ZipFileWriterRef::Weak(Weak::default())) {
            ZipFileWriterRef::Strong(s) => s,
            ZipFileWriterRef::Weak(w) => match w.upgrade() {
                Some(s) => s,
                None => return,
            }
        };
        let mut imp = imp.lock().unwrap();
        if imp.writer.start_file(path, zip::write::FileOptions::default()).is_err() { return; }
        let _ = imp.writer.write_all(&buffer[..]);
    }
}

#[cfg(test)] mod tests {
    use crate::*;
    use super::{VfsError, VfsFileType, VfsPath, VfsResult};
    use std::fs::{create_dir_all, File};

    #[test] fn copy_early_vfs_zip() {
        let _ = create_dir_all("target/tmp");
        let src = VfsPath::new(ZipReadOnly::new_strict(File::open("test/data/early-vfs-zip.zip").unwrap()).unwrap());
        let dst = VfsPath::new(ZipWriteOnly::new_weak(File::create("target/tmp/early-vfs-zip-copy.zip").unwrap()).unwrap());
        let copied = copy_dir_merge(&dst, &src).unwrap();
        assert_eq!(copied, 16);
    }

    /// NOTE: https://github.com/MaulingMonkey/vfs-zip/issues/1

    fn copy_dir_merge(dst: &VfsPath, src: &VfsPath) -> VfsResult<usize> {
        let mut n = 0;

        if !src.exists() { return Err(VfsError::FileNotFound { path: src.as_str().into() }); }
        if !dst.exists() { dst.create_dir()?; n += 1; }

        for src in src.read_dir()? {
            let dst = dst.join(src.filename().as_str())?;
            match src.metadata()?.file_type {
                VfsFileType::Directory  => n += copy_dir_merge(&dst, &src)?,
                VfsFileType::File       => { src.copy_file(&dst)?; n += 1 },
            }
        }
        Ok(n)
    }
}