summaryrefslogtreecommitdiff
path: root/src/cmd/add_folder.rs
blob: 82df1f9bd952bf903e69de2e5d0261adecc55073 (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
use std::fs::create_dir;
use std::path::PathBuf;

use protobuf::Message as _;

use crate::error::Result;
use crate::pb3::jwebmail::{AddFolderReq, AddFolderResp};

pub fn add_folder(mut p: PathBuf, req: &[u8]) -> Result<Vec<u8>> {
    let r = AddFolderReq::parse_from_bytes(req)?;
    let mut resp = AddFolderResp::new();

    let mut folder = ".".to_owned();
    folder.push_str(&r.name);
    p.push(folder);

    if p.is_dir() {
        resp.status = 1;
        return resp.write_to_bytes().map_err(|e| e.into());
    }

    create_dir(&p)?;

    p.push("tmp");
    create_dir(&p)?;

    p.pop();
    p.push("new");
    create_dir(&p)?;

    p.pop();
    p.push("cur");
    create_dir(p)?;

    resp.status = 0;
    resp.write_to_bytes().map_err(|e| e.into())
}