summaryrefslogtreecommitdiffstats
path: root/src/io/io_worker.rs
blob: cd9692922bee6c01ff4bcec37fa5febfdfd3dd42 (plain)
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
use std::fs;
use std::path;
use std::sync::mpsc;
use std::thread;

use super::rename_filename_conflict;

#[derive(Clone, Copy, Debug)]
pub enum FileOp {
    Cut,
    Copy,
}

#[derive(Clone, Debug)]
pub struct IOWorkerOptions {
    pub kind: FileOp,
    pub overwrite: bool,
    pub skip_exist: bool,
}

impl std::default::Default for IOWorkerOptions {
    fn default() -> Self {
        Self {
            kind: FileOp::Copy,
            overwrite: false,
            skip_exist: false,
        }
    }
}

#[derive(Debug)]
pub struct IOWorkerThread {
    pub options: IOWorkerOptions,
    pub paths: Vec<path::PathBuf>,
    pub dest: path::PathBuf,
}

impl IOWorkerThread {
    pub fn new(options: IOWorkerOptions, paths: Vec<path::PathBuf>, dest: path::PathBuf) -> Self {
        Self {
            options,
            paths,
            dest,
        }
    }

    pub fn start(&self, tx: mpsc::Sender<u64>) -> std::io::Result<u64> {
        match self.options.kind {
            FileOp::Cut => self.paste_cut(tx),
            FileOp::Copy => self.paste_copy(tx),
        }
    }

    fn paste_copy(&self, tx: mpsc::Sender<u64>) -> std::io::Result<u64> {
        let mut total = 0;
        for path in self.paths.iter() {
            total += self.recursive_copy(path.as_path(), self.dest.as_path())?;
            tx.send(total);
        }
        Ok(total)
    }

    fn recursive_copy(&self, src: &path::Path, dest: &path::Path) -> std::io::Result<u64> {
        let mut dest_buf = dest.to_path_buf();
        if let Some(s) = src.file_name() {
            dest_buf.push(s);
        }
        rename_filename_conflict(&mut dest_buf);
        let file_type = fs::symlink_metadata(src)?.file_type();
        if file_type.is_dir() {
            fs::create_dir(dest_buf.as_path())?;
            let mut total = 0;
            for entry in fs::read_dir(src)? {
                let entry = entry?;
                let entry_path = entry.path();
                total += self.recursive_copy(entry_path.as_path(), dest_buf.as_path())?;
            }
            Ok(total)
        } else if file_type.is_file() {
            fs::copy(src, dest_buf)
        } else if file_type.is_symlink() {
            let link_path = fs::read_link(src)?;
            std::os::unix::fs::symlink(link_path, dest_buf)?;
            Ok(0)
        } else {
            Ok(0)
        }
    }

    fn paste_cut(&self, tx: mpsc::Sender<u64>) -> std::io::Result<u64> {
        let mut total = 0;
        for path in self.paths.iter() {
            total += self.recursive_cut(path.as_path(), self.dest.as_path())?;
            tx.send(total);
        }
        Ok(total)
    }

    pub fn recursive_cut(&self, src: &path::Path, dest: &path::Path) -> std::io::Result<u64> {
        let mut dest_buf = dest.to_path_buf();
        if let Some(s) = src.file_name() {
            dest_buf.push(s);
        }
        rename_filename_conflict(&mut dest_buf);
        let metadata = fs::symlink_metadata(src)?;
        let file_type = metadata.file_type();
        if file_type.is_dir() {
            match fs::rename(src, dest_buf.as_path()) {
                Ok(_) => Ok(metadata.len()),
                Err(e) => {
                    let mut total = 0;
                    fs::create_dir(dest_buf.as_path())?;
                    for entry in fs::read_dir(src)? {
                        let entry = entry?;
                        let entry_path = entry.path();
                        total += self.recursive_cut(entry_path.as_path(), dest_buf.as_path())?;
                    }
                    fs::remove_dir(src)?;
                    Ok(total)
                }
            }
        } else if file_type.is_file() {
            if fs::rename(src, dest_buf.as_path()).is_err() {
                fs::copy(src, dest_buf.as_path())?;
                fs::remove_file(src)?;
            }
            Ok(metadata.len())
        } else if file_type.is_symlink() {
            let link_path = fs::read_link(src)?;
            std::os::unix::fs::symlink(link_path, dest_buf)?;
            fs::remove_file(src)?;
            Ok(metadata.len())
        } else {
            Ok(0)
        }
    }
}