Skip to content

Commit e7aa902

Browse files
ygerlachmtwebster
authored andcommitted
remove dependency to which, chmod and chown
1 parent 0b0c0cd commit e7aa902

File tree

6 files changed

+39
-85
lines changed

6 files changed

+39
-85
lines changed

src/Core/Main.vala

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ public class Main : GLib.Object{
314314
}
315315
else{
316316
//timeshift is running from system directory - update app_path
317-
this.app_path = get_cmd_path("timeshift");
317+
this.app_path = Environment.find_program_in_path("timeshift");
318318
}
319319

320320
// initialize lists -----------------
@@ -350,17 +350,13 @@ public class Main : GLib.Object{
350350
}
351351

352352
public bool check_dependencies(out string msg){
353-
354-
msg = "";
355-
356353
log_debug("Main: check_dependencies()");
357354

358-
string[] dependencies = { "rsync","/sbin/blkid","df","mount","umount","fuser","crontab","cp","rm","touch","ln","sync","which", "run-parts"}; //"shutdown","chroot",
355+
string[] dependencies = { "rsync","/sbin/blkid","df","mount","umount","fuser","crontab","cp","rm","touch","ln","sync", "run-parts"}; //"shutdown","chroot",
359356

360-
string path;
357+
msg = "";
361358
foreach(string cmd_tool in dependencies){
362-
path = get_cmd_path (cmd_tool);
363-
if ((path == null) || (path.length == 0)){
359+
if(!cmd_exists(cmd_tool)) {
364360
msg += " * " + cmd_tool + "\n";
365361
}
366362
}

src/Utility/CronTab.vala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,8 @@ public class CronTab : GLib.Object {
306306
}
307307

308308
file_write(file_path, sh);
309-
chown(file_path, "root", "root");
310-
chmod(file_path, "644");
309+
Posix.chown(file_path, 0, 0); // chown to root:root
310+
Posix.chmod(file_path, 0644);
311311

312312
log_msg(_("Added cron task") + ": %s".printf(file_path));
313313

src/Utility/OSDNotify.vala

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,15 @@ public class OSDNotify : GLib.Object {
5656
long seconds = 9999;
5757

5858
if (dt_last_notification != null){
59-
6059
DateTime dt_end = new DateTime.now_local();
6160
TimeSpan elapsed = dt_end.difference(dt_last_notification);
6261
seconds = (long)(elapsed * 1.0 / TimeSpan.SECOND);
6362
}
6463

6564
if (seconds > NOTIFICATION_INTERVAL){
66-
67-
if (cmd_exists("notify-send")){
68-
65+
66+
if (is_supported()){
67+
6968
string desktop_entry = "timeshift-gtk";
7069
string hint = "string:desktop-entry:%s".printf(desktop_entry);
7170

@@ -82,9 +81,6 @@ public class OSDNotify : GLib.Object {
8281
}
8382

8483
public static bool is_supported(){
85-
86-
string path = get_cmd_path("notify-send");
87-
88-
return (path != null) && (path.length > 0);
84+
return cmd_exists("notify-send");
8985
}
9086
}

src/Utility/TeeJee.FileSystem.vala

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -445,12 +445,6 @@ namespace TeeJee.FileSystem{
445445
return list;
446446
}
447447

448-
public bool chown(string dir_path, string user, string group = user){
449-
string cmd = "chown %s:%s -R '%s'".printf(user, group, escape_single_quote(dir_path));
450-
int status = exec_sync(cmd, null, null);
451-
return (status == 0);
452-
}
453-
454448
// misc --------------------
455449

456450
public string format_file_size (
@@ -506,11 +500,4 @@ namespace TeeJee.FileSystem{
506500

507501
return file_path.replace("'","'\\''");
508502
}
509-
510-
// dep: chmod
511-
public int chmod(string file, string permission){
512-
513-
string cmd = "chmod %s '%s'".printf(permission, escape_single_quote(file));
514-
return exec_sync (cmd, null, null);
515-
}
516503
}

src/Utility/TeeJee.Process.vala

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ namespace TeeJee.ProcessHelper{
4949

5050
TEMP_DIR = tempPlace + "/timeshift-" + random_string();
5151
dir_create(TEMP_DIR);
52-
chmod(TEMP_DIR, "0750");
52+
Posix.chmod(TEMP_DIR, 0750);
5353
exec_script_sync("echo 'ok'",out std_out,out std_err, true);
5454

5555
if ((std_out == null) || (std_out.strip() != "ok")){
@@ -223,7 +223,7 @@ namespace TeeJee.ProcessHelper{
223223
script.append ("echo ${exitCode} > status\n");
224224

225225
if ((sh_path == null) || (sh_path.length == 0)){
226-
sh_path = get_temp_file_path() + ".sh";
226+
sh_path = get_temp_file_path();
227227
}
228228

229229
try{
@@ -238,7 +238,7 @@ namespace TeeJee.ProcessHelper{
238238
data_stream.close();
239239

240240
// set execute permission
241-
chmod (sh_path, "u+x");
241+
Posix.chmod (sh_path, 0744);
242242

243243
return sh_path;
244244
}
@@ -259,32 +259,10 @@ namespace TeeJee.ProcessHelper{
259259
}
260260

261261
// find process -------------------------------
262-
263-
// dep: which
264-
public string get_cmd_path (string cmd_tool){
265-
266-
/* Returns the full path to a command */
267262

268-
try {
269-
int exitCode;
270-
string stdout, stderr;
271-
Process.spawn_command_line_sync("which " + cmd_tool, out stdout, out stderr, out exitCode);
272-
return stdout;
273-
}
274-
catch (Error e){
275-
log_error (e.message);
276-
return "";
277-
}
278-
}
279-
280-
public bool cmd_exists(string cmd_tool){
281-
string path = get_cmd_path (cmd_tool);
282-
if ((path == null) || (path.length == 0)){
283-
return false;
284-
}
285-
else{
286-
return true;
287-
}
263+
public static bool cmd_exists(string cmd_tool){
264+
string? path = Environment.find_program_in_path(cmd_tool);
265+
return (path != null) && (path.length > 0);
288266
}
289267

290268
// return the name of the executable of a given pid or self if pid is <= 0

src/Utility/TeeJee.System.vala

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,8 @@ namespace TeeJee.System{
164164

165165
// open -----------------------------
166166

167-
public bool xdg_open (string file, string user = ""){
168-
string path = get_cmd_path ("xdg-open");
169-
if ((path != null) && (path != "")){
167+
public static bool xdg_open (string file, string user = ""){
168+
if (cmd_exists("xdg-open")){
170169
string cmd = "xdg-open '%s'".printf(escape_single_quote(file));
171170
if (user.length > 0){
172171
cmd = "pkexec --user %s env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY ".printf(user) + cmd;
@@ -189,38 +188,36 @@ namespace TeeJee.System{
189188
We will first try using xdg-open and then check for specific file managers if it fails.
190189
*/
191190

192-
string path;
193-
int status;
194-
195-
if (xdg_open_try_first){
191+
bool xdgAvailable = cmd_exists("xdg-open");
192+
string escaped_dir_path = escape_single_quote(dir_path);
193+
int status = -1;
194+
195+
if (xdg_open_try_first && xdgAvailable){
196196
//try using xdg-open
197-
path = get_cmd_path ("xdg-open");
198-
if ((path != null)&&(path != "")){
199-
string cmd = "xdg-open '%s'".printf(escape_single_quote(dir_path));
200-
status = exec_script_async (cmd);
201-
return (status == 0);
202-
}
197+
string cmd = "xdg-open '%s'".printf(escaped_dir_path);
198+
status = exec_script_async (cmd);
199+
return (status == 0);
203200
}
204201

205202
foreach(string app_name in
206203
new string[]{ "nemo", "nautilus", "thunar", "io.elementary.files", "pantheon-files", "marlin", "dolphin" }){
207-
208-
path = get_cmd_path (app_name);
209-
if ((path != null)&&(path != "")){
210-
string cmd = "%s '%s'".printf(app_name, escape_single_quote(dir_path));
211-
status = exec_script_async (cmd);
212-
return (status == 0);
204+
if(!cmd_exists(app_name)) {
205+
continue;
206+
}
207+
208+
string cmd = "%s '%s'".printf(app_name, escaped_dir_path);
209+
status = exec_script_async (cmd);
210+
211+
if(status == 0) {
212+
return true;
213213
}
214214
}
215215

216-
if (xdg_open_try_first == false){
216+
if (!xdg_open_try_first && xdgAvailable){
217217
//try using xdg-open
218-
path = get_cmd_path ("xdg-open");
219-
if ((path != null)&&(path != "")){
220-
string cmd = "xdg-open '%s'".printf(escape_single_quote(dir_path));
221-
status = exec_script_async (cmd);
222-
return (status == 0);
223-
}
218+
string cmd = "xdg-open '%s'".printf(escaped_dir_path);
219+
status = exec_script_async (cmd);
220+
return (status == 0);
224221
}
225222

226223
return false;

0 commit comments

Comments
 (0)