Module fs_extra::file [] [src]

This module include extra methods for works with files.

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

Example

Be careful when using this code, it's not being tested!
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 mut options = CopyOptions::new();
    options.buffer_size = 1;
    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

Options and flags which can be used to configure how a file will be copied or moved.

TransitProcess

A structure which include information about the current status of the copy or move file.

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 with recept information about process. This function will also copy the permission bits of the original file to the destination file.

move_file

Moves 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 file from one place to another with recept information about process. This function will also copy the permission bits of the original file to the destination file.

read_to_string

Read file content, placing him into String.

remove

Removes a file from the filesystem.

write_all

Write String content into inside target file.