forked from linuxmint/timeshift
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRsyncSpaceCheckTask.vala
More file actions
171 lines (131 loc) · 3.81 KB
/
RsyncSpaceCheckTask.vala
File metadata and controls
171 lines (131 loc) · 3.81 KB
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
/*
* RsyncTask.vala
*
* Copyright 2012-2018 Tony George <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
using TeeJee.Logging;
using TeeJee.FileSystem;
using TeeJee.JsonHelper;
using TeeJee.ProcessHelper;
using TeeJee.System;
using TeeJee.Misc;
public class RsyncSpaceCheckTask : AsyncTask{
// settings
public bool delete_extra = true;
public bool delete_after = false;
public bool delete_excluded = false;
public bool relative = false;
public string exclude_from_file = "";
public string link_from_path = "";
public string source_path = "";
public string dest_path = "";
public bool verbose = true;
public bool dry_run = false;
// regex
private Regex sent_bytes_regex;
// status
public int64 status_line_count = 0;
public int64 total_size = 0;
public RsyncSpaceCheckTask(){
init_regular_expressions();
}
private void init_regular_expressions(){
if (sent_bytes_regex != null){
return; // already initialized
}
try {
sent_bytes_regex = new Regex("""sent ([0-9,]+)[ \t]+bytes[ \t]+received""");
}
catch (Error e) {
log_error (e.message);
}
}
public override void prepare() {
base.prepare();
total_size = 0;
status_line_count = 0;
}
protected override string build_script() {
var cmd = "export LC_ALL=C.UTF-8\n";
cmd += "rsync -aii";
cmd += " --recursive";
if (verbose){
cmd += " --verbose";
}
else{
cmd += " --quiet";
}
if (delete_extra){
cmd += " --delete";
}
if (delete_after){
cmd += " --delete-after";
}
cmd += " --force"; // allow deletion of non-empty directories
cmd += " --stats";
cmd += " --sparse";
if (delete_excluded){
cmd += " --delete-excluded";
}
cmd += " --dry-run";
if (link_from_path.length > 0){
if (!link_from_path.has_suffix("/")){
link_from_path = "%s/".printf(link_from_path);
}
cmd += " --link-dest='%s'".printf(escape_single_quote(link_from_path));
}
if (exclude_from_file.length > 0){
cmd += " --exclude-from='%s'".printf(escape_single_quote(exclude_from_file));
if (delete_extra && delete_excluded){
cmd += " --delete-excluded";
}
}
source_path = remove_trailing_slash(source_path);
dest_path = remove_trailing_slash(dest_path);
cmd += " '%s/'".printf(escape_single_quote(source_path));
cmd += " '%s/'".printf(escape_single_quote(dest_path));
return cmd;
}
// execution ----------------------------
public override void parse_stdout_line(string out_line){
update_progress_parse_console_output(out_line);
}
public override void parse_stderr_line(string err_line){
update_progress_parse_console_output(err_line);
}
public bool update_progress_parse_console_output (string line) {
if ((line == null) || (line.length == 0)) {
return true;
}
status_line_count++;
if (prg_count_total > 0){
prg_count = status_line_count;
progress = (prg_count * 1.0) / prg_count_total;
}
MatchInfo match;
if (sent_bytes_regex.match(line, 0, out match)) {
total_size = int64.parse(match.fetch(1).replace(",",""));
}
else{
//log_debug("not-matched: %s".printf(line));
}
return true;
}
}