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
// Copyright 2020 Martin Pool.

// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

//! Access to an archive on the local filesystem.

use std::convert::TryInto;
use std::fs::{create_dir, File};
use std::io;
use std::io::prelude::*;
use std::path::{Path, PathBuf};

use crate::transport::{DirEntry, Metadata, Transport};

#[derive(Clone, Debug)]
pub struct LocalTransport {
    /// Root directory for this transport.
    root: PathBuf,
}

impl LocalTransport {
    pub fn new(path: &Path) -> Self {
        LocalTransport {
            root: path.to_owned(),
        }
    }

    pub fn full_path(&self, relpath: &str) -> PathBuf {
        debug_assert!(!relpath.contains("/../"), "path must not contain /../");
        self.root.join(relpath)
    }
}

impl Transport for LocalTransport {
    fn iter_dir_entries(
        &self,
        relpath: &str,
    ) -> io::Result<Box<dyn Iterator<Item = io::Result<DirEntry>>>> {
        // Archives should never normally contain non-UTF-8 (or even non-ASCII) filenames, but
        // let's pass them back as lossy UTF-8 so they can be reported at a higher level, for
        // example during validation.
        let relpath = relpath.to_owned();
        Ok(Box::new(self.full_path(&relpath).read_dir()?.map(
            move |i| {
                i.and_then(|de| {
                    Ok(DirEntry {
                        name: de.file_name().to_string_lossy().into(),
                        kind: de.file_type()?.into(),
                    })
                })
            },
        )))
    }

    fn read_file(&self, relpath: &str, out_buf: &mut Vec<u8>) -> io::Result<()> {
        out_buf.truncate(0);
        // read_to_end reads in gradually increasing parts, but here we can probably read one large
        // buffer.
        let mut file = File::open(&self.full_path(relpath))?;
        let prefetch_len: usize = file.metadata()?.len().try_into().unwrap();
        out_buf.resize(prefetch_len, 0);
        let actual_len = file.read(out_buf)?;
        out_buf.truncate(actual_len);
        Ok(())
    }

    fn exists(&self, relpath: &str) -> io::Result<bool> {
        Ok(self.full_path(relpath).exists())
    }

    fn box_clone(&self) -> Box<dyn Transport> {
        Box::new(self.clone())
    }

    fn create_dir(&self, relpath: &str) -> io::Result<()> {
        create_dir(self.full_path(&relpath)).or_else(|err| {
            if err.kind() == io::ErrorKind::AlreadyExists {
                Ok(())
            } else {
                Err(err)
            }
        })
    }

    fn write_file(&self, relpath: &str, content: &[u8]) -> io::Result<()> {
        let full_path = self.full_path(relpath);
        let dir = full_path.parent().unwrap();
        let mut temp = tempfile::Builder::new()
            .prefix(crate::TMP_PREFIX)
            .tempfile_in(dir)?;
        if let Err(err) = temp.write_all(content) {
            let _ = temp.close();
            return Err(err);
        }
        if let Err(persist_error) = temp.persist(&full_path) {
            let _ = persist_error.file.close()?;
            Err(persist_error.error)
        } else {
            Ok(())
        }
    }

    fn remove_file(&self, relpath: &str) -> io::Result<()> {
        std::fs::remove_file(self.full_path(relpath))
    }

    fn remove_dir(&self, relpath: &str) -> io::Result<()> {
        std::fs::remove_dir(self.full_path(relpath))
    }

    fn remove_dir_all(&self, relpath: &str) -> io::Result<()> {
        std::fs::remove_dir_all(self.full_path(relpath))
    }

    fn sub_transport(&self, relpath: &str) -> Box<dyn Transport> {
        Box::new(LocalTransport {
            root: self.root.join(relpath),
        })
    }

    fn metadata(&self, relpath: &str) -> io::Result<Metadata> {
        let fsmeta = self.root.join(relpath).metadata()?;
        Ok(Metadata { len: fsmeta.len() })
    }
}

impl AsRef<dyn Transport> for LocalTransport {
    fn as_ref(&self) -> &(dyn Transport + 'static) {
        self
    }
}

#[cfg(test)]
mod test {
    use assert_fs::prelude::*;
    use predicates::prelude::*;

    use super::*;
    use crate::kind::Kind;

    #[test]
    fn read_file() {
        let temp = assert_fs::TempDir::new().unwrap();
        let content: &str = "the ribs of the disaster";
        let filename = "poem.txt";

        temp.child(filename).write_str(content).unwrap();

        let transport = LocalTransport::new(temp.path());
        let mut buf = Vec::new();
        transport.read_file(&filename, &mut buf).unwrap();
        assert_eq!(buf, content.as_bytes());

        temp.close().unwrap();
    }

    #[test]
    fn read_metadata() {
        let temp = assert_fs::TempDir::new().unwrap();
        let content: &str = "the ribs of the disaster";
        let filename = "poem.txt";
        temp.child(filename).write_str(content).unwrap();

        let transport = LocalTransport::new(temp.path());

        assert_eq!(transport.metadata(&filename).unwrap(), Metadata { len: 24 });
        assert!(transport.metadata("nopoem").is_err());
    }

    #[test]
    fn read_with_non_empty_buffer() {
        let mut buf = b"already has some stuff".to_vec();
        let temp = assert_fs::TempDir::new().unwrap();
        let desired = b"content from file";
        let filename = "test.txt";
        temp.child(filename).write_binary(desired).unwrap();
        let transport = LocalTransport::new(temp.path());
        transport.read_file(&filename, &mut buf).unwrap();
        assert_eq!(
            String::from_utf8_lossy(&buf),
            String::from_utf8_lossy(desired)
        );
        temp.close().unwrap();
    }

    #[test]
    fn list_directory() {
        let temp = assert_fs::TempDir::new().unwrap();
        temp.child("root file").touch().unwrap();
        temp.child("subdir").create_dir_all().unwrap();
        temp.child("subdir")
            .child("subfile")
            .write_str("Morning coffee")
            .unwrap();

        let transport = Transport::new(&temp.path().to_string_lossy()).unwrap();
        let mut root_list: Vec<_> = transport
            .iter_dir_entries(".")
            .unwrap()
            .map(io::Result::unwrap)
            .collect();
        assert_eq!(root_list.len(), 2);
        root_list.sort();

        assert_eq!(
            root_list[0],
            DirEntry {
                name: "root file".to_owned(),
                kind: Kind::File,
            }
        );

        // Len is unpredictable for directories, so check the other fields.
        assert_eq!(root_list[1].name, "subdir");
        assert_eq!(root_list[1].kind, Kind::Dir);

        assert_eq!(transport.exists("root file").unwrap(), true);
        assert_eq!(transport.exists("nuh-uh").unwrap(), false);

        let subdir_list: Vec<_> = transport
            .iter_dir_entries("subdir")
            .unwrap()
            .map(io::Result::unwrap)
            .collect();
        assert_eq!(
            subdir_list,
            vec![DirEntry {
                name: "subfile".to_owned(),
                kind: Kind::File,
            }]
        );

        temp.close().unwrap();
    }

    #[test]
    fn write_file() {
        // TODO: Maybe test some error cases of failing to write.
        let temp = assert_fs::TempDir::new().unwrap();
        let transport = Transport::new(&temp.path().to_string_lossy()).unwrap();

        transport.create_dir("subdir").unwrap();
        transport
            .write_file("subdir/subfile", b"Must I paint you a picture?")
            .unwrap();

        temp.child("subdir").assert(predicate::path::is_dir());
        temp.child("subdir")
            .child("subfile")
            .assert("Must I paint you a picture?");

        temp.close().unwrap();
    }

    #[test]
    fn create_existing_dir() {
        let temp = assert_fs::TempDir::new().unwrap();
        let transport = Transport::new(&temp.path().to_string_lossy()).unwrap();

        transport.create_dir("aaa").unwrap();
        transport.create_dir("aaa").unwrap();
        transport.create_dir("aaa").unwrap();

        temp.close().unwrap();
    }

    #[test]
    fn sub_transport() {
        let temp = assert_fs::TempDir::new().unwrap();
        let transport = Transport::new(&temp.path().to_string_lossy()).unwrap();

        transport.create_dir("aaa").unwrap();
        transport.create_dir("aaa/bbb").unwrap();

        let sub_transport = transport.sub_transport("aaa");
        let sub_list: Vec<DirEntry> = sub_transport
            .iter_dir_entries("")
            .unwrap()
            .map(Result::unwrap)
            .collect();

        assert_eq!(sub_list.len(), 1);
        assert_eq!(sub_list[0].name, "bbb");

        temp.close().unwrap();
    }

    #[test]
    fn remove_dir_all() -> std::io::Result<()> {
        let temp = assert_fs::TempDir::new().unwrap();
        let transport = Transport::new(&temp.path().to_string_lossy()).unwrap();

        transport.create_dir("aaa")?;
        transport.create_dir("aaa/bbb")?;
        transport.create_dir("aaa/bbb/ccc")?;

        transport.remove_dir_all("aaa")?;
        Ok(())
    }
}