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
use std::env;
use std::fs;
use std::fs::File;
use std::io::prelude::*;
use std::path::PathBuf;
use tar::Builder;
use super::log_exit;
use crate::file::get_jinx_files;
use crate::service::get_jinx_proxy_service;
use crate::service::JinxService;
pub fn create_jinx_proxy_tar() {
let jinx_service = get_jinx_proxy_service();
let jinx_files = get_jinx_files();
let excluded = vec!["jinx_conf.json".to_string(), ".jinx.tar.gz".to_string(), "letsencrypt".to_string()];
_write_tar(&jinx_service, &excluded, Some(jinx_files.jinx_home));
}
pub fn get_jinx_proxy_tar() -> Vec<u8> {
let jinx_service = get_jinx_proxy_service();
get_tar(&jinx_service)
}
pub fn get_tar(jinx_service: &JinxService) -> Vec<u8> {
let jinx_files = get_jinx_files();
let tar_file_path = format!(
"{}/{}.jinx.tar.gz",
jinx_files.jinx_home, &jinx_service.name
);
let mut tar_file = File::open(&tar_file_path).expect("[TARGZ] Failed to open tar file");
let mut tar_buffer = vec![];
tar_file
.read_to_end(&mut tar_buffer)
.expect("[TARGZ] Failed to read tar file");
tar_buffer
}
pub fn write_tar(jinx_service: &JinxService, excluded: &Vec<String>, directory: Option<String>) {
_write_tar(&jinx_service, &excluded, directory);
}
fn _write_tar(jinx_service: &JinxService, excluded: &Vec<String>, directory: Option<String>) {
let mut dir = env::current_dir().expect("[TARGZ] Failed to get current directory");
if directory.is_some() {
dir = PathBuf::from(directory.expect("Failed to get directory"));
}
let jinx_files = get_jinx_files();
let tar_file_path = format!(
"{}/{}.jinx.tar.gz",
jinx_files.jinx_home, &jinx_service.name
);
let tar_file = File::create(&tar_file_path).expect("[TARGZ] Failed to create tar file");
let mut tar_builder = Builder::new(tar_file);
let paths = fs::read_dir(&dir).expect("Failed to read jinx directory");
'path: for path in paths {
let p = path.expect("Failed to get path");
let file_path = p.path();
let file_name = match p.file_name().into_string() {
Err(_) => log_exit!("[TARGZ] Failed to convert file name"),
Ok(name) => name,
};
for ex in excluded.iter() {
if file_name.contains(ex) {
continue 'path;
}
}
let meta = match p.metadata() {
Err(_err) => log_exit!("[TARGZ] Failed to get file metadata"),
Ok(meta) => meta,
};
if meta.is_dir() {
tar_builder
.append_dir_all(&file_name, &file_name)
.expect("[TARGZ] Failed to append to tar file");
} else {
tar_builder
.append_path_with_name(&file_path, &file_name)
.expect("TARGZ] Failed to append to tar file");
}
}
}