Skip to content
API Documentation
BastionGuard™
RsyncSpaceCheckTask.vala
Go to the documentation of this file.
1/*
2 * RsyncTask.vala
3 *
4 * Copyright 2012-2018 Tony George <teejeetech@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
20 *
21 *
22 */
23
24using TeeJee.Logging;
28using TeeJee.System;
29using TeeJee.Misc;
30
32
33 // settings
34 public bool delete_extra = true;
35 public bool delete_after = false;
36 public bool delete_excluded = false;
37 public bool relative = false;
38
39 public string exclude_from_file = "";
40 public string link_from_path = "";
41 public string source_path = "";
42 public string dest_path = "";
43 public bool verbose = true;
44 public bool io_nice = true;
45 public bool dry_run = false;
46
47 // regex
48 private Regex sent_bytes_regex;
49
50 // status
51 public int64 status_line_count = 0;
52 public int64 total_size = 0;
53
57
59 if (sent_bytes_regex != null){
60 return; // already initialized
61 }
62
63 try {
64 sent_bytes_regex = new Regex("""sent ([0-9,]+)[ \t]+bytes[ \t]+received""");
65 }
66 catch (Error e) {
67 log_error (e.message);
68 }
69 }
70
71 public override void prepare() {
72 base.prepare();
73
74 total_size = 0;
76 }
77
78 protected override string build_script() {
79 var cmd = "export LC_ALL=C.UTF-8\n";
80
81 cmd += "rsync -aii";
82
83 cmd += " --recursive";
84
85 if (verbose){
86 cmd += " --verbose";
87 }
88 else{
89 cmd += " --quiet";
90 }
91
92 if (delete_extra){
93 cmd += " --delete";
94 }
95
96 if (delete_after){
97 cmd += " --delete-after";
98 }
99
100 cmd += " --force"; // allow deletion of non-empty directories
101
102 cmd += " --stats";
103
104 cmd += " --sparse";
105
106 if (delete_excluded){
107 cmd += " --delete-excluded";
108 }
109
110 cmd += " --dry-run";
111
112 if (link_from_path.length > 0){
113 if (!link_from_path.has_suffix("/")){
114 link_from_path = "%s/".printf(link_from_path);
115 }
116
117 cmd += " --link-dest='%s'".printf(escape_single_quote(link_from_path));
118 }
119
120 if (exclude_from_file.length > 0){
121 cmd += " --exclude-from='%s'".printf(escape_single_quote(exclude_from_file));
122
124 cmd += " --delete-excluded";
125 }
126 }
127
129
131
132 cmd += " '%s/'".printf(escape_single_quote(source_path));
133
134 cmd += " '%s/'".printf(escape_single_quote(dest_path));
135
136 return cmd;
137 }
138
139 // execution ----------------------------
140
144
148
149 public bool update_progress_parse_console_output (string line) {
150 if ((line == null) || (line.length == 0)) {
151 return true;
152 }
153
155
156 if (prg_count_total > 0){
159 }
160
161 MatchInfo match;
162
163 if (sent_bytes_regex.match(line, 0, out match)) {
164 total_size = int64.parse(match.fetch(1).replace(",",""));
165 }
166 else{
167 //log_debug("not-matched: %s".printf(line));
168 }
169
170 return true;
171 }
172}
int64 prg_count
string err_line
int64 prg_count_total
double progress
string out_line
override string build_script()
override void parse_stderr_line(string err_line)
override void parse_stdout_line(string out_line)
bool update_progress_parse_console_output(string line)
string escape_single_quote(string file_path)
string remove_trailing_slash(string path)
void log_error(string message, bool highlight=false, bool is_warning=false)