-
-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathUtils.vala
More file actions
252 lines (212 loc) · 8.11 KB
/
Copy pathUtils.vala
File metadata and controls
252 lines (212 loc) · 8.11 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
* Copyright (c) 2011-2020 elementary, Inc. (https://elementary.io)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 3, as published by the Free Software Foundation.
*
* 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 Lesser 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
*/
namespace Terminal.Utils {
public string? sanitize_path (string _path, string shell_location, bool add_file_scheme = true) {
/* Remove trailing whitespace, ensure scheme, substitute leading "~" and "..", remove extraneous "/" */
string scheme = "", path = "";
var parts_scheme = _path.split ("://", 2);
if (parts_scheme.length == 2) {
scheme = parts_scheme[0] + "://";
path = parts_scheme[1];
} else if (parts_scheme.length == 1 ) {
if (add_file_scheme) {
scheme = "file://";
}
path = _path;
} else {
critical ("Invalid path");
return null;
}
path = Uri.unescape_string (path);
if (path == null) {
return null;
}
path = strip_uri (path);
do {
path = path.replace ("//", "/");
} while (path.contains ("//"));
// If just basename of file then assume in current shell location
if (!path.contains (Path.DIR_SEPARATOR_S)) {
path = string.join (Path.DIR_SEPARATOR_S, ".", path);
}
var parts_sep = path.split (Path.DIR_SEPARATOR_S, 3);
var index = 0;
while (parts_sep[index] == null && index < parts_sep.length - 1) {
index++;
}
if (parts_sep[index] == "~") {
parts_sep[index] = Environment.get_home_dir ();
} else if (parts_sep[index] == ".") {
parts_sep[index] = shell_location;
} else if (parts_sep[index] == "..") {
parts_sep[index] = construct_parent_path (shell_location);
}
var result = scheme + string.joinv (Path.DIR_SEPARATOR_S, parts_sep).replace ("//", "/");
return result;
}
/*** Simplified version of PF.FileUtils function, with fewer checks ***/
public string get_parent_path_from_path (string path) {
if (path.length < 2) {
return Path.DIR_SEPARATOR_S;
}
StringBuilder string_builder = new StringBuilder (path);
if (path.has_suffix (Path.DIR_SEPARATOR_S)) {
string_builder.erase (string_builder.str.length - 1, -1);
}
int last_separator = string_builder.str.last_index_of (Path.DIR_SEPARATOR_S);
if (last_separator < 0) {
last_separator = 0;
}
string_builder.erase (last_separator, -1);
return string_builder.str + Path.DIR_SEPARATOR_S;
}
private string construct_parent_path (string path) {
if (path.length < 2) {
return Path.DIR_SEPARATOR_S;
}
var sb = new StringBuilder (path);
if (path.has_suffix (Path.DIR_SEPARATOR_S)) {
sb.erase (sb.str.length - 1, -1);
}
int last_separator = sb.str.last_index_of (Path.DIR_SEPARATOR_S);
if (last_separator < 0) {
last_separator = 0;
}
sb.erase (last_separator, -1);
string parent_path = sb.str + Path.DIR_SEPARATOR_S;
return parent_path;
}
private string? strip_uri (string? _uri) {
string uri = _uri;
/* Strip off any trailing spaces, newlines or carriage returns */
if (_uri != null) {
uri = uri.strip ();
uri = uri.replace ("\n", "");
uri = uri.replace ("\r", "");
}
return uri;
}
/**
* Checks a string for possible unsafe contents before pasting
*
* @param clipboard contents containing terminal commands
*
* @param return localized explanation of risk
*
* @return true if safe, false if unsafe.
*/
public bool is_safe_paste (string text, out string[]? msg_array) {
string[] msgs = {};
var newline_index = text.index_of ("\n"); // First occurrence of new line
bool embedded_newline = newline_index >= 0 && newline_index < text.length - 1;
if (embedded_newline || "&" in text || "|" in text || ";" in text ) {
msgs += _("The pasted text may contain multiple commands");
}
if ("sudo " in text || "doas " in text || "run0 " in text || "pkexec " in text || "su " in text) {
msgs += _("The pasted text may be trying to gain administrative access");
}
string[] skip_commands = {
"--assume-yes",
"-f",
"--force",
"--interactive=never",
"-y",
"--yes"
};
var words = text.split (" ");
foreach (unowned var skip_command in skip_commands) {
if (skip_command in words) {
msgs += _("The pasted text includes a command to skip warnings and confirmations");
break;
}
}
if (msgs.length > 0) {
msg_array = msgs;
return false;
} else {
msg_array = null;
return true;
}
}
public SimpleAction action_from_group (string action_name, SimpleActionGroup action_group) {
return ((SimpleAction) action_group.lookup_action (action_name));
}
public bool valid_local_uri (string s, out string path) {
var scheme = Uri.peek_scheme (s);
path = "";
string absolute_uri;
if (scheme == null || scheme == "") {
absolute_uri = "file:///" + s;
} else if (scheme != "file") {
return false;
} else {
absolute_uri = s;
}
try {
if (!Uri.is_valid (absolute_uri, PARSE_RELAXED)) {
return false;
}
var file = GLib.File.new_for_uri (absolute_uri);
var type = file.query_file_type (NONE);
if (type == DIRECTORY) {
path = file.get_path ();
} else if (type == REGULAR) {
path = file.get_parent ().get_path ();
} else {
return false;
}
return true;
} catch (Error e) {
warning ("Error parsing uri - %s", e.message);
}
return false;
}
public static AppInfo? get_default_app_for_uri (string? uri) {
if (uri == null) {
return null;
}
AppInfo? appinfo = null;
var scheme = Uri.parse_scheme (uri);
if (scheme != null) {
appinfo = AppInfo.get_default_for_uri_scheme (scheme);
}
if (appinfo == null) {
bool uncertain;
/* Guess content type from filename if possible */
//TODO Get content type from actual file (if exists)
var ctype = ContentType.guess (uri, null, out uncertain);
if (!uncertain) {
appinfo = AppInfo.get_default_for_type (ctype, true);
}
if (appinfo == null) {
var file = File.new_for_commandline_arg (uri);
try {
var info = file.query_info (FileAttribute.STANDARD_CONTENT_TYPE,
FileQueryInfoFlags.NOFOLLOW_SYMLINKS, null);
if (info.has_attribute (FileAttribute.STANDARD_CONTENT_TYPE)) {
appinfo = AppInfo.get_default_for_type (
info.get_attribute_string (FileAttribute.STANDARD_CONTENT_TYPE), true);
}
} catch (Error e) {
warning ("Could not get file info %s", e.message);
}
}
}
return appinfo;
}
}