-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathDeleteFileTask.vala
More file actions
157 lines (119 loc) · 3.34 KB
/
DeleteFileTask.vala
File metadata and controls
157 lines (119 loc) · 3.34 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
/*
* DeleteFileTask.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 DeleteFileTask : AsyncTask{
// settings
public string dest_path = "";
public bool verbose = true;
public bool use_rsync = false;
//private
private string source_path = "";
// regex
private Gee.HashMap<string, Regex> regex_list;
// status
public int64 status_line_count = 0;
public int64 total_size = 0;
public string status_message = "";
public string time_remaining = "";
public DeleteFileTask(){
init_regular_expressions();
}
private void init_regular_expressions(){
if (regex_list != null){
return; // already initialized
}
regex_list = new Gee.HashMap<string,Regex>();
try {
regex_list["rsync-deleted"] = new Regex(
"""\*deleting[ \t]+(.*)""");
}
catch (Error e) {
log_error (e.message);
}
}
public override void prepare() {
base.prepare();
status_line_count = 0;
total_size = 0;
}
protected override string build_script() {
var cmd = "";
if (use_rsync){
cmd += "rsync -aii";
if (verbose){
cmd += " --verbose";
}
else{
cmd += " --quiet";
}
cmd += " --delete";
cmd += " --stats --relative";
source_path = "/tmp/%s_empty".printf(random_string());
dir_create(source_path);
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));
}
else{
cmd += "rm -rf";
if (verbose){
cmd += "v";
}
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 (regex_list["rsync-deleted"].match(line, 0, out match)) {
//log_debug("matched: rsync-deleted:%s".printf(line));
status_line = match.fetch(1).split(" -> ")[0].strip();
}
else {
//log_debug("matched: else:%s".printf(line));
status_line = line.strip();
}
return true;
}
}