firetrap 0.1.0

Modern, safe and extensible FTP server library for Rust
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
extern crate std;
extern crate bytes;
extern crate tokio;
extern crate tokio_io;
extern crate futures;
extern crate chrono;
extern crate path_abs;

use std::{fmt,result};
use std::path::{Path,PathBuf};
use std::time::SystemTime;

use self::futures::{Future, Stream};

use self::chrono::prelude::*;

/// Represents the Metadata of a file
pub trait Metadata {
    /// Returns the length (size) of the file.
    fn len(&self) -> u64;

    /// Returns `self.len() == 0`.
    fn is_empty(&self) -> bool;

    /// Returns true if the path is a directory.
    fn is_dir(&self) -> bool;

    /// Returns true if the path is a file.
    fn is_file(&self) -> bool;

    /// Returns the last modified time of the path.
    fn modified(&self) -> Result<SystemTime>;

    /// Returns the `gid` of the file.
    fn gid(&self) -> u32;

    /// Returns the `uid` of the file.
    fn uid(&self) -> u32;
}

/// Fileinfo contains the path and `Metadata` of a file.
///
/// [`Metadata`]: ./trait.Metadata.html
pub struct Fileinfo<P, M>
    where P: AsRef<Path>,
    M: Metadata,
{
    /// The full path to the file
    pub path: P,
    /// The file's metadata
    pub metadata: M,
}

impl<P, M> std::fmt::Display for Fileinfo<P, M>
    where P: AsRef<Path>,
    M: Metadata,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let modified: DateTime<Local> = DateTime::from(self.metadata.modified().unwrap_or(SystemTime::UNIX_EPOCH));
        write!(f, "{filetype}{permissions}     {owner} {group} {size} {modified} {path}",
               filetype = if self.metadata.is_dir() {
                   "d"
               } else {
                   "-"
               },
               // TODO: Don't hardcode permissions ;)
               permissions = "rwxr-xr-x",
               // TODO: Consider showing canonical names here
               owner = self.metadata.uid(),
               group = self.metadata.gid(),
               size = self.metadata.len(),
               modified = modified.format("%b %d %Y"),
               path = self.path.as_ref().components().last().unwrap().as_os_str().to_string_lossy(),
        )
    }
}

/// The `Storage` trait defines a common interface to different storage backends for our FTP
/// [`Server`], e.g. for a [`Filesystem`] or GCP buckets.
///
/// [`Server`]: ../server/struct.Server.html
/// [`filesystem`]: ./struct.Filesystem.html
pub trait StorageBackend {
    /// The concrete type of the Files returned by this StorageBackend.
    type File;
    /// The concrete type of the `Metadata` used by this StorageBackend.
    type Metadata;
    /// The concrete type of the error returned by this StorageBackend.
    type Error;

    /// Returns the `Metadata` for the given file.
    ///
    /// [`Metadata`]: ./trait.Metadata.html
    fn stat<P: AsRef<Path>>(&self, path: P) -> Box<Future<Item = Self::Metadata, Error = Self::Error> + Send>;

    /// Returns the list of files in the given directory.
    fn list<P: AsRef<Path>>(&self, path: P) -> Box<Stream<Item = Fileinfo<std::path::PathBuf, Self::Metadata>, Error = Self::Error> + Send> where <Self as StorageBackend>::Metadata: Metadata;

    /// Returns some bytes that make up a directory listing that can immediately be send to the
    /// client.
    fn list_fmt<P: AsRef<Path>>(&self, path: P) -> Box<Future<Item = std::io::Cursor<Vec<u8>>, Error = std::io::Error> + Send>
        where <Self as StorageBackend>::Metadata: Metadata + 'static,
              <Self as StorageBackend>::Error: Send + 'static,
    {

        let res = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));

        let stream: Box<Stream<Item = Fileinfo<std::path::PathBuf, Self::Metadata>, Error = Self::Error> + Send> = self.list(path);
        let res_work = res.clone();
        let fut = stream.for_each(move |file: Fileinfo<std::path::PathBuf, Self::Metadata>| {
            let mut res = res_work.lock().unwrap();
            let fmt = format!("{}\r\n", file);
            let fmt_vec = fmt.into_bytes();
            res.extend_from_slice(&fmt_vec);
            Ok(())
        }).and_then(|_| {
            Ok(())
        }).
        map(move |_| {
            std::sync::Arc::try_unwrap(res).expect("failed try_unwrap").into_inner().unwrap()
        }).map(move |res| {
            std::io::Cursor::new(res)
        }).map_err(|_| {
            std::io::Error::new(std::io::ErrorKind::Other, "shut up")
        });

        Box::new(fut)
    }

    /// Returns some bytes that make up a NLST directory listing (only the basename) that can
    /// immediately be send to the client.
    fn nlst<P: AsRef<Path>>(&self, path: P) -> Box<Future<Item = std::io::Cursor<Vec<u8>>, Error = std::io::Error> + Send>
        where <Self as StorageBackend>::Metadata: Metadata + 'static,
              <Self as StorageBackend>::Error: Send + 'static,
    {
        let res = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));

        let stream: Box<Stream<Item = Fileinfo<std::path::PathBuf, Self::Metadata>, Error = Self::Error> + Send> = self.list(path);
        let res_work = res.clone();
        let fut = stream.for_each(move |file: Fileinfo<std::path::PathBuf, Self::Metadata>| {
            let mut res = res_work.lock().unwrap();
            let fmt = format!("{}\r\n", file.path.file_name().unwrap_or(std::ffi::OsStr::new("")).to_str().unwrap_or(""));
            let fmt_vec = fmt.into_bytes();
            res.extend_from_slice(&fmt_vec);
            Ok(())
        }).and_then(|_| {
            Ok(())
        }).
        map(move |_| {
            std::sync::Arc::try_unwrap(res).expect("failed try_unwrap").into_inner().unwrap()
        }).map(move |res| {
            std::io::Cursor::new(res)
        }).map_err(|_| {
            std::io::Error::new(std::io::ErrorKind::Other, "shut up")
        });

        Box::new(fut)
    }

    /// Returns the content of the given file.
    // TODO: Future versions of Rust will probably allow use to use `impl Future<...>` here. Use it
    // if/when available. By that time, also see if we can replace Self::File with the AsyncRead
    // Trait.
    fn get<P: AsRef<Path>>(&self, path: P) -> Box<Future<Item = Self::File, Error = Self::Error> + Send>;

    /// Write the given bytes to the given file.
    // TODO: Get rid of 'static requirement her
    fn put<P: AsRef<Path>, R: self::tokio::prelude::AsyncRead + Send + 'static>(&self, bytes: R, path: P) -> Box<Future<Item = u64, Error = Self::Error> + Send>;

    /// Delete the given file.
    fn del<P: AsRef<Path>>(&self, path: P) -> Box<Future<Item = (), Error = Self::Error> + Send>;

    /// Create the given directory.
    fn mkd<P: AsRef<Path>>(&self, path: P) -> Box<Future<Item = (), Error = Self::Error> + Send>;
}

/// StorageBackend that uses a local filesystem, like a traditional FTP server.
pub struct Filesystem {
    root: PathBuf,
}

/// Returns the canonical path corresponding to the input path, sequences like '../' resolved.
///
/// I may decide to make this part of just the Filesystem implementation, because strictly speaking
/// '../' is only special on the context of a filesystem. Then again, FTP does kind of imply a
/// filesystem... hmm...
fn canonicalize<P: AsRef<Path>>(path: P) -> Result<PathBuf> {
    use self::path_abs::PathAbs;
    let p = PathAbs::new(path)?;
    Ok(p.as_path().to_path_buf())
}

impl Filesystem {
    /// Create a new Filesytem backend, with the given root. No operations can take place outside
    /// of the root. For example, when the `Filesystem` root is set to `/srv/ftp`, and a client
    /// asks for `hello.txt`, the server will send it `/srv/ftp/hello.txt`.
    pub fn new<P: Into<PathBuf>>(root: P) -> Self {
        Filesystem {
            root: root.into(),
        }
    }

    /// Returns the full, absolute and canonical path corresponding to the (relative to FTP root)
    /// input path, resolving symlinks and sequences like '../'.
    fn full_path<P: AsRef<Path>>(&self, path: P) -> Result<PathBuf> {
        // `path.join(other_path)` replaces `path` with `other_path` if `other_path` is absolute,
        // so we have to check for it.
        let path = path.as_ref();
        let full_path = if path.starts_with("/") {
            self.root.join(path.strip_prefix("/").unwrap())
        } else {
            self.root.join(path)
        };

        // TODO: Use `?` operator here, when we can use `impl Future`
        let real_full_path = match canonicalize(full_path) {
            Ok(path) => path,
            Err(e) => return Err(e),
        };

        if real_full_path.starts_with(&self.root) {
            Ok(real_full_path)
        } else {
            Err(Error::PathError)
        }
    }
}

impl StorageBackend for Filesystem {
    type File =  self::tokio::fs::File;
    type Metadata = std::fs::Metadata;
    type Error = Error;

    fn stat<P: AsRef<Path>>(&self, path: P) -> Box<Future<Item = Self::Metadata, Error = Self::Error> + Send> {
        let full_path = match self.full_path(path) {
            Ok(path) => path,
            Err(err) => return Box::new(futures::future::err(err)),
        };
        // TODO: Some more useful error reporting
        Box::new(tokio::fs::symlink_metadata(full_path).map_err(|_| Error::IOError))
    }

    fn list<P: AsRef<Path>>(&self, path: P) -> Box<Stream<Item = Fileinfo<std::path::PathBuf, Self::Metadata>, Error = Self::Error> + Send>
        where <Self as StorageBackend>::Metadata: Metadata
    {
        // TODO: Use `?` operator here when we can use `impl Future`
        let full_path = match self.full_path(path) {
            Ok(path) => path,
            Err(e) => return Box::new(futures::future::err(e).into_stream()),
        };

        let prefix = self.root.clone();

        let fut = tokio::fs::read_dir(full_path).flatten_stream().filter_map(move |dir_entry| {
            let prefix = prefix.clone();
            let path = dir_entry.path();
            let relpath = path.strip_prefix(prefix).unwrap();
            let relpath = std::path::PathBuf::from(relpath);
            match std::fs::metadata(dir_entry.path()) {
                Ok(stat)    => Some(Fileinfo{path: relpath, metadata: stat}),
                Err(_)      => None,
            }
        });

        // TODO: Some more useful error reporting
        Box::new(fut.map_err(|_| Error::IOError))
    }

    fn get<P: AsRef<Path>>(&self, path: P) -> Box<Future<Item = self::tokio::fs::File, Error = Self::Error> + Send> {
        let full_path = match self.full_path(path) {
            Ok(path) => path,
            Err(e) => return Box::new(futures::future::err(e)),
        };
        // TODO: Some more useful error reporting
        Box::new(self::tokio::fs::file::File::open(full_path).map_err(|_| Error::IOError))
    }

    fn put<P: AsRef<Path>, R: self::tokio::prelude::AsyncRead + Send + 'static>(&self, bytes: R, path: P) -> Box<Future<Item = u64, Error = Self::Error> + Send> {
        // TODO: Add permission checks
        let path = path.as_ref();
        let full_path = if path.starts_with("/") {
            self.root.join(path.strip_prefix("/").unwrap())
        } else {
            self.root.join(path)
        };

        let fut = self::tokio::fs::file::File::create(full_path)
            .and_then(|f| {
                self::tokio_io::io::copy(bytes, f)
            })
            .map(|(n, _, _)| n)
            // TODO: Some more useful error reporting
            .map_err(|_| Error::IOError);
        Box::new(fut)
    }

    fn del<P: AsRef<Path>>(&self, path: P) -> Box<Future<Item = (), Error = Self::Error> + Send> {
        let full_path = match self.full_path(path) {
            Ok(path) => path,
            Err(e) => return Box::new(futures::future::err(e)),
        };
        Box::new(self::tokio::fs::remove_file(full_path).map_err(|_| Error::IOError))
    }

    fn mkd<P: AsRef<Path>>(&self, path: P) -> Box<Future<Item = (), Error = Self::Error> + Send> {
        let full_path = match self.full_path(path) {
            Ok(path) => path,
            Err(e) => return Box::new(futures::future::err(e)),
        };

        Box::new(self::tokio::fs::create_dir(full_path).map_err(|e| {println!("error: {}", e); Error::IOError}))
    }
}

use std::os::unix::fs::MetadataExt;
impl Metadata for std::fs::Metadata {
    fn len(&self) -> u64 {
        self.len()
    }

    fn is_empty(&self) -> bool {
        self.len() == 0
    }

    fn is_dir(&self) -> bool {
        self.is_dir()
    }

    fn is_file(&self) -> bool {
        self.is_file()
    }

    fn modified(&self) -> Result<SystemTime> {
        self.modified().map_err(|e| e.into())
    }

    fn gid(&self) -> u32 {
        MetadataExt::gid(self)
    }

    fn uid(&self) -> u32 {
        MetadataExt::uid(self)
    }
}

#[derive(Debug, PartialEq)]
/// The `Error` variants that can be produced by the [`StorageBackend`] implementations.
///
/// [`StorageBackend`]: ./trait.StorageBackend.html
pub enum Error {
    /// An IO Error
    IOError,
    /// Path error
    PathError,
}

impl Error {
    fn description_str(&self) -> &'static str {
        ""
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(&self.description_str())
    }
}

impl std::error::Error for Error {
    fn description(&self) -> &str {
        self.description_str()
    }
}

impl From<std::io::Error> for Error {
    fn from(_err: std::io::Error) -> Error {
        Error::IOError
    }
}

impl From<path_abs::Error> for Error {
    fn from(_err: path_abs::Error) -> Error {
        Error::PathError
    }
}

type Result<T> = result::Result<T, Error>;

#[cfg(test)]
mod tests {
    extern crate tempfile;

    use super::*;
    use std::fs::File;

    use std::io::prelude::*;

    #[test]
    fn fs_stat() {
        let root = std::env::temp_dir();

        // Create a temp file and get it's metadata
        let file = tempfile::NamedTempFile::new_in(&root).unwrap();
        let path = file.path().clone();
        let file = file.as_file();
        let meta = file.metadata().unwrap();

        // Create a filesystem StorageBackend with the directory containing our temp file as root
        let fs = Filesystem::new(&root);

        // Since the filesystem backend is based on futures, we need a runtime to run it
        let mut rt = tokio::runtime::Runtime::new().unwrap();
        let filename = path.file_name().unwrap();
        let my_meta = rt.block_on(fs.stat(filename)).unwrap();

        assert_eq!(meta.is_dir(), my_meta.is_dir());
        assert_eq!(meta.is_file(), my_meta.is_file());
        assert_eq!(meta.len(), my_meta.len());
        assert_eq!(meta.modified().unwrap(), my_meta.modified().unwrap());
    }

    #[test]
    fn fs_list() {
        // Create a temp directory and create some files in it
        let root = tempfile::tempdir().unwrap();
        let file = tempfile::NamedTempFile::new_in(&root.path()).unwrap();
        let path = file.path().clone();
        let relpath = path.strip_prefix(&root.path()).unwrap();
        let file = file.as_file();
        let meta = file.metadata().unwrap();

        // Create a filesystem StorageBackend with our root dir
        let fs = Filesystem::new(&root.path());

        // Since the filesystem backend is based on futures, we need a runtime to run it
        let mut rt = tokio::runtime::Runtime::new().unwrap();
        let my_list = rt.block_on(fs.list("/").collect()).unwrap();

        assert_eq!(my_list.len(), 1);

        let my_fileinfo = &my_list[0];
        assert_eq!(my_fileinfo.path, relpath);
        assert_eq!(my_fileinfo.metadata.is_dir(), meta.is_dir());
        assert_eq!(my_fileinfo.metadata.is_file(), meta.is_file());
        assert_eq!(my_fileinfo.metadata.len(), meta.len());
        assert_eq!(my_fileinfo.metadata.modified().unwrap(), meta.modified().unwrap());
    }

    #[test]
    fn fs_list_fmt() {
        // Create a temp directory and create some files in it
        let root = tempfile::tempdir().unwrap();
        let file = tempfile::NamedTempFile::new_in(&root.path()).unwrap();
        let path = file.path().clone();
        let relpath = path.strip_prefix(&root.path()).unwrap();

        // Create a filesystem StorageBackend with our root dir
        let fs = Filesystem::new(&root.path());

        // Since the filesystem backend is based on futures, we need a runtime to run it
        let mut rt = tokio::runtime::Runtime::new().unwrap();
        let my_list = rt.block_on(fs.list_fmt("/")).unwrap();

        let my_list = std::string::String::from_utf8(my_list.into_inner()).unwrap();

        assert!(my_list.contains(relpath.to_str().unwrap()));
    }

    #[test]
    fn fs_get() {
        let root = std::env::temp_dir();

        let mut file = tempfile::NamedTempFile::new_in(&root).unwrap();
        let path = file.path().to_owned();

        // Write some data to our test file
        let data = b"Koen was here\n";
        file.write_all(data).unwrap();

        let filename = path.file_name().unwrap();
        let fs = Filesystem::new(&root);

        // Since the filesystem backend is based on futures, we need a runtime to run it
        let mut rt = tokio::runtime::Runtime::new().unwrap();
        let mut my_file = rt.block_on(fs.get(filename)).unwrap();
        let mut my_content = Vec::new();
        rt.block_on(
            self::futures::future::lazy(move || {
                self::tokio::prelude::AsyncRead::read_to_end(&mut my_file, &mut my_content).unwrap();
                assert_eq!(data.as_ref(), &*my_content);
                // We need a `Err` branch because otherwise the compiler can't infer the `E` type,
                // and I'm not sure where/how to annotate it.
                if true {
                    Ok(())
                } else {
                    Err(())
                }
            })
        ).unwrap();
    }

    #[test]
    fn fs_put() {
        let root = std::env::temp_dir();
        let orig_content = b"hallo";
        let fs = Filesystem::new(&root);

        // Since the Filesystem StorageBackend is based on futures, we need a runtime to run them
        // to completion
        let mut rt = tokio::runtime::Runtime::new().unwrap();

        rt.block_on(fs.put(orig_content.as_ref(), "greeting.txt")).expect("Failed to `put` file");

        let mut written_content = Vec::new();
        let mut f = File::open(root.join("greeting.txt")).unwrap();
        f.read_to_end(&mut written_content).unwrap();

        assert_eq!(orig_content, written_content.as_slice());
    }

    #[test]
    fn fileinfo_fmt() {
        struct MockMetadata{};
        impl Metadata for MockMetadata {
            fn len(&self) -> u64 { 5 }
            fn is_empty(&self) -> bool { false }
            fn is_dir(&self) -> bool { false }
            fn is_file(&self) -> bool { true }
            fn modified(&self) -> Result<SystemTime> { Ok(std::time::SystemTime::UNIX_EPOCH) }
            fn uid(&self) -> u32 { 1 }
            fn gid(&self) -> u32 { 2 }
        }

        let dir = std::env::temp_dir();
        let meta = MockMetadata{};
        let fileinfo = Fileinfo{path: dir.to_str().unwrap(), metadata: meta};
        let my_format = format!("{}", fileinfo);
        let format = format!("-rwxr-xr-x     1 2 5 Jan 01 1970 {}", dir.strip_prefix("/").unwrap().to_str().unwrap());
        assert_eq!(my_format, format);
    }

    #[test]
    fn fs_mkd() {
        let root = tempfile::TempDir::new().unwrap().into_path();
        let fs = Filesystem::new(&root);
        let new_dir_name = "bla";

        // Since the Filesystem StorageBackend is based on futures, we need a runtime to run them
        // to completion
        let mut rt = tokio::runtime::Runtime::new().unwrap();

        rt.block_on(fs.mkd(new_dir_name)).expect("Failed to mkd");

        let full_path = root.join(new_dir_name);
        let metadata = std::fs::metadata(full_path).unwrap();
        assert!(metadata.is_dir());
    }
}