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
use crate::{CoreError, CoreState, LbError, LbResult, Requester};
use lockbook_shared::account::Account;
use lockbook_shared::document_repo::DocumentService;
use lockbook_shared::file::File;
use lockbook_shared::file_like::FileLike;
use lockbook_shared::file_metadata::FileType;
use lockbook_shared::filename::NameComponents;
use lockbook_shared::lazy::LazyStaged1;
use lockbook_shared::signed_file::SignedFile;
use lockbook_shared::tree_like::TreeLike;
use lockbook_shared::{symkey, SharedError};
use std::collections::HashSet;
use std::fs;
use std::fs::OpenOptions;
use std::io::Write;
use std::path::{Path, PathBuf};
use uuid::Uuid;

pub enum ImportStatus {
    CalculatedTotal(usize),
    StartingItem(String),
    FinishedItem(File),
}

impl<Client: Requester, Docs: DocumentService> CoreState<Client, Docs> {
    pub(crate) fn import_files<F: Fn(ImportStatus)>(
        &mut self, sources: &[PathBuf], dest: Uuid, update_status: &F,
    ) -> LbResult<()> {
        update_status(ImportStatus::CalculatedTotal(get_total_child_count(sources)?));

        let tree = self.db.base_metadata.stage(&self.db.local_metadata);
        let parent = tree.find(&dest)?;
        if !parent.is_folder() {
            return Err(CoreError::FileNotFolder.into());
        }

        for disk_path in sources {
            self.import_file_recursively(disk_path, &dest, update_status)?;
        }

        self.cleanup()?;

        Ok(())
    }

    pub(crate) fn export_file(
        &mut self, id: Uuid, destination: PathBuf, edit: bool,
        export_progress: Option<Box<dyn Fn(ExportFileInfo)>>,
    ) -> LbResult<()> {
        if destination.is_file() {
            return Err(CoreError::DiskPathInvalid.into());
        }

        let mut tree = (&self.db.base_metadata)
            .to_staged(&self.db.local_metadata)
            .to_lazy();

        let file = tree.find(&id)?.clone();

        let account = self.db.account.get().ok_or(CoreError::AccountNonexistent)?;

        Self::export_file_recursively(
            &self.docs,
            account,
            &mut tree,
            &file,
            &destination,
            edit,
            &export_progress,
        )?;

        Ok(())
    }

    fn export_file_recursively<Base, Local>(
        docs: &impl DocumentService, account: &Account, tree: &mut LazyStaged1<Base, Local>,
        this_file: &Base::F, disk_path: &Path, edit: bool,
        export_progress: &Option<Box<dyn Fn(ExportFileInfo)>>,
    ) -> LbResult<()>
    where
        Base: TreeLike<F = SignedFile>,
        Local: TreeLike<F = Base::F>,
    {
        let dest_with_new = disk_path.join(tree.name_using_links(this_file.id(), account)?);

        if let Some(ref func) = export_progress {
            func(ExportFileInfo {
                disk_path: disk_path.to_path_buf(),
                lockbook_path: tree.id_to_path(this_file.id(), account)?,
            })
        }

        match this_file.file_type() {
            FileType::Folder => {
                let children = tree.children(this_file.id())?;
                fs::create_dir(dest_with_new.clone()).map_err(LbError::from)?;

                for id in children {
                    if !tree.calculate_deleted(&id)? {
                        let file = tree.find(&id)?.clone();
                        Self::export_file_recursively(
                            docs,
                            account,
                            tree,
                            &file,
                            &dest_with_new,
                            edit,
                            export_progress,
                        )?;
                    }
                }
            }
            FileType::Document => {
                let mut file = if edit {
                    OpenOptions::new()
                        .write(true)
                        .create(true)
                        .truncate(true)
                        .open(dest_with_new)
                } else {
                    OpenOptions::new()
                        .write(true)
                        .create_new(true)
                        .open(dest_with_new)
                }
                .map_err(LbError::from)?;

                let doc = tree.read_document(docs, this_file.id(), account)?;

                file.write_all(doc.as_slice()).map_err(LbError::from)?;
            }
            FileType::Link { target } => {
                if !tree.calculate_deleted(&target)? {
                    let file = tree.find(&target)?.clone();
                    Self::export_file_recursively(
                        docs,
                        account,
                        tree,
                        &file,
                        disk_path,
                        edit,
                        export_progress,
                    )?;
                }
            }
        }

        Ok(())
    }

    fn import_file_recursively<F: Fn(ImportStatus)>(
        &mut self, disk_path: &Path, dest: &Uuid, update_status: &F,
    ) -> LbResult<()> {
        let mut tree = (&self.db.base_metadata)
            .to_staged(&mut self.db.local_metadata)
            .to_lazy();
        let account = self.db.account.get().ok_or(CoreError::AccountNonexistent)?;

        update_status(ImportStatus::StartingItem(format!("{}", disk_path.display())));

        let mut disk_paths_with_destinations = vec![(PathBuf::from(disk_path), *dest)];
        loop {
            let (disk_path, dest) = match disk_paths_with_destinations.pop() {
                None => break,
                Some((disk_path, dest)) => (disk_path, dest),
            };

            if !disk_path.exists() {
                return Err(CoreError::DiskPathInvalid.into());
            }

            let disk_file_name = disk_path
                .file_name()
                .and_then(|name| name.to_str())
                .ok_or(CoreError::DiskPathInvalid)?;

            let ftype = match disk_path.is_file() {
                true => FileType::Document,
                false => FileType::Folder,
            };

            let file_name =
                Self::generate_non_conflicting_name(&mut tree, account, &dest, disk_file_name)?;

            let id = tree.create(
                Uuid::new_v4(),
                symkey::generate_key(),
                &dest,
                &file_name,
                ftype,
                account,
            )?;

            let file = tree.decrypt(account, &id, &mut self.db.pub_key_lookup)?;

            tree = if ftype == FileType::Document {
                let doc = fs::read(&disk_path).map_err(LbError::from)?;

                let encrypted_document = tree.update_document(&id, &doc, account)?;
                let hmac = tree.find(&id)?.document_hmac();
                self.docs.insert(&id, hmac, &encrypted_document)?;

                update_status(ImportStatus::FinishedItem(file));
                tree
            } else {
                update_status(ImportStatus::FinishedItem(file));

                let entries = fs::read_dir(disk_path).map_err(LbError::from)?;

                for entry in entries {
                    let child_path = entry.map_err(LbError::from)?.path();
                    disk_paths_with_destinations.push((child_path.clone(), id));
                }

                tree
            };
        }

        Ok(())
    }

    fn generate_non_conflicting_name<Base, Local>(
        tree: &mut LazyStaged1<Base, Local>, account: &Account, parent: &Uuid, proposed_name: &str,
    ) -> LbResult<String>
    where
        Base: TreeLike<F = SignedFile>,
        Local: TreeLike<F = Base::F>,
    {
        let maybe_siblings = tree.children(parent)?;
        let mut new_name = NameComponents::from(proposed_name);

        let mut siblings = HashSet::new();

        for id in maybe_siblings {
            if !tree.calculate_deleted(&id)? {
                siblings.insert(id);
            }
        }

        let siblings: Result<Vec<String>, SharedError> = siblings
            .iter()
            .map(|id| tree.name_using_links(id, account))
            .collect();
        let siblings = siblings?;

        loop {
            if !siblings.iter().any(|name| *name == new_name.to_name()) {
                return Ok(new_name.to_name());
            }
            new_name = new_name.generate_next();
        }
    }
}

fn get_total_child_count(paths: &[PathBuf]) -> LbResult<usize> {
    let mut count = 0;
    for p in paths {
        count += get_child_count(p)?;
    }
    Ok(count)
}

fn get_child_count(path: &Path) -> LbResult<usize> {
    let mut count = 1;
    if path.is_dir() {
        let children = fs::read_dir(path).map_err(LbError::from)?;
        for maybe_child in children {
            let child_path = maybe_child.map_err(LbError::from)?.path();

            count += get_child_count(&child_path)?;
        }
    }
    Ok(count)
}

pub struct ExportFileInfo {
    pub disk_path: PathBuf,
    pub lockbook_path: String,
}