Skip to content
API Documentation
BastionGuard™
DeleteFileTask.vala
Go to the documentation of this file.
1/*
2 * DeleteFileTask.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 string dest_path = "";
35 public bool verbose = true;
36 public bool io_nice = true;
37 public bool use_rsync = false;
38
39 //private
40 private string source_path = "";
41
42 // regex
43 private Gee.HashMap<string, Regex> regex_list;
44
45 // status
46 public int64 status_line_count = 0;
47 public int64 total_size = 0;
48 public string status_message = "";
49 public string time_remaining = "";
50
54
56 if (regex_list != null){
57 return; // already initialized
58 }
59
60 regex_list = new Gee.HashMap<string,Regex>();
61
62 try {
63
64 regex_list["rsync-deleted"] = new Regex(
65 """\*deleting[ \t]+(.*)""");
66
67 }
68 catch (Error e) {
69 log_error (e.message);
70 }
71 }
72
73 public override void prepare() {
74 base.prepare();
75
77 total_size = 0;
78 }
79
80 protected override string build_script() {
81 var cmd = "";
82
83 if (io_nice){
84 //cmd += "ionice -c2 -n7 ";
85 }
86
87 if (use_rsync){
88
89 cmd += "rsync -aii";
90
91 if (verbose){
92 cmd += " --verbose";
93 }
94 else{
95 cmd += " --quiet";
96 }
97
98 cmd += " --delete";
99
100 cmd += " --stats --relative";
101
102 source_path = "/tmp/%s_empty".printf(random_string());
104
107
108 cmd += " '%s/'".printf(escape_single_quote(source_path));
109 cmd += " '%s/'".printf(escape_single_quote(dest_path));
110 }
111 else{
112 cmd += "rm -rf";
113
114 if (verbose){
115 cmd += "v";
116 }
117
118 cmd += " '%s'".printf(escape_single_quote(dest_path));
119 }
120
121 return cmd;
122 }
123
124 // execution ----------------------------
125
129
133
134 public bool update_progress_parse_console_output (string line) {
135 if ((line == null) || (line.length == 0)) {
136 return true;
137 }
138
140
141 if (prg_count_total > 0){
144 }
145
146 MatchInfo match;
147 if (regex_list["rsync-deleted"].match(line, 0, out match)) {
148
149 //log_debug("matched: rsync-deleted:%s".printf(line));
150
151 status_line = match.fetch(1).split(" -> ")[0].strip();
152 }
153 else {
154
155 //log_debug("matched: else:%s".printf(line));
156
157 status_line = line.strip();
158 }
159
160 return true;
161 }
162}
int64 prg_count
string err_line
int64 prg_count_total
double progress
string status_line
string out_line
Gee.HashMap< string, Regex > regex_list
override void parse_stderr_line(string err_line)
override string build_script()
override void prepare()
bool update_progress_parse_console_output(string line)
void init_regular_expressions()
override void parse_stdout_line(string out_line)
string escape_single_quote(string file_path)
string remove_trailing_slash(string path)
bool dir_create(string dir_path, bool show_message=false)
void log_error(string message, bool highlight=false, bool is_warning=false)
string random_string(int length=8, string charset="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890")