Module file

Source
Expand description

This module includes additional methods for working with files.

One of the distinguishing features is receipt information about process work with files.

§Example

use std::path::Path;
use std::{thread, time};
use std::sync::mpsc::{self, TryRecvError};

extern crate fs_extra;
use fs_extra::file::*;
use fs_extra::error::*;

fn example_copy() -> Result<()> {
    let path_from = Path::new("./temp");
    let path_to = path_from.join("out");
    let test_file = (path_from.join("test_file.txt"), path_to.join("test_file.txt"));


    fs_extra::dir::create_all(&path_from, true)?;
    fs_extra::dir::create_all(&path_to, true)?;

    write_all(&test_file.0, "test_data")?;
    assert!(test_file.0.exists());
    assert!(!test_file.1.exists());


    let options = CopyOptions {
        buffer_size: 1,
        ..Default::default()
    }
    let (tx, rx) = mpsc::channel();
    thread::spawn(move || {
        let handler = |process_info: TransitProcess| {
            tx.send(process_info).unwrap();
            thread::sleep(time::Duration::from_millis(500));
        };
        copy_with_progress(&test_file.0, &test_file.1, &options, handler).unwrap();
        assert!(test_file.0.exists());
        assert!(test_file.1.exists());

    });
    loop {
        match rx.try_recv() {
            Ok(process_info) => {
                println!("{} of {} bytes",
                         process_info.copied_bytes,
                         process_info.total_bytes);
            }
            Err(TryRecvError::Disconnected) => {
                println!("finished");
                break;
            }
            Err(TryRecvError::Empty) => {}
        }
    }
    Ok(())

}


fn main() {
    example_copy();
}

Structs§

CopyOptions
TransitProcess
A structure which stores information about the current status of a file that’s copied or moved. .

Functions§

copy
Copies the contents of one file to another. This function will also copy the permission bits of the original file to the destination file.
copy_with_progress
Copies the contents of one file to another file with information about progress. This function will also copy the permission bits of the original file to the destination file.
move_file
Moves a file from one place to another. This function will also copy the permission bits of the original file to the destination file.
move_file_with_progress
Moves a file from one place to another with information about progress. This function will also copy the permission bits of the original file to the destination file.
read_to_string
Read file contents, placing them into String.
remove
Removes a file from the filesystem.
write_all
Write String content into file.