Skip to content

Commit bf37a53

Browse files
committed
remove rm dependency
1 parent 7cf0251 commit bf37a53

File tree

4 files changed

+74
-84
lines changed

4 files changed

+74
-84
lines changed

src/Core/Main.vala

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public class Main : GLib.Object{
206206
}
207207

208208
check_and_remove_timeshift_btrfs();
209-
209+
210210
// init log ------------------
211211

212212
try {
@@ -4375,18 +4375,20 @@ public class Main : GLib.Object{
43754375

43764376
string cmd = "umount '%s'".printf(escape_single_quote(mdir2));
43774377
int retval = exec_sync(cmd);
4378-
4379-
string cmd2 = "rmdir '%s'".printf(escape_single_quote(mdir2));
4380-
int retval2 = exec_sync(cmd2);
4381-
4382-
if (retval2 != 0){
4378+
4379+
if (retval != 0){
43834380
log_debug("E: Failed to unmount");
43844381
log_debug("Ret=%d".printf(retval));
43854382
//ignore
43864383
}
43874384
else{
43884385
log_debug("Unmounted successfully");
43894386
}
4387+
4388+
// delete directory
4389+
if(!dir_empty_delete(mdir2)) {
4390+
log_debug("E: Failed to delete %s".printf(mdir2));
4391+
}
43904392
}
43914393
}
43924394
}

src/Core/SnapshotRepo.vala

Lines changed: 6 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -882,40 +882,8 @@ public class SnapshotRepo : GLib.Object{
882882
return thr_success;
883883
}
884884

885-
public static bool delete_directory_recursive(string dir) {
886-
File f = File.new_for_path(dir);
887-
if(f.query_exists()) {
888-
try {
889-
FileEnumerator enumerator = f.enumerate_children(FileAttribute.STANDARD_NAME, FileQueryInfoFlags.NOFOLLOW_SYMLINKS);
890-
FileInfo info;
891-
while ((info = enumerator.next_file()) != null) {
892-
string name = info.get_name();
893-
if(info.get_file_type() == FileType.DIRECTORY) {
894-
895-
// ignore . and ..
896-
if(name == "." || name == "..") {
897-
continue;
898-
}
899-
900-
if(!delete_directory_recursive(dir + "/" + name)) {
901-
return false;
902-
}
903-
} else {
904-
File file = File.new_for_path(dir + "/" + name);
905-
file.delete();
906-
}
907-
}
908-
f.delete();
909-
} catch(Error err) {
910-
log_error("Can not enumerate folder '%s'".printf(dir));
911-
return false;
912-
}
913-
}
914-
return true;
915-
}
916-
917885
private void delete_directory_thread(){
918-
thr_success = delete_directory_recursive(thr_args1);
886+
thr_success = TeeJee.FileSystem.dir_delete_recursive(thr_args1);
919887

920888
if (thr_success) {
921889
log_msg(_("Removed") + ": '%s'".printf(thr_args1));
@@ -964,32 +932,18 @@ public class SnapshotRepo : GLib.Object{
964932
}
965933

966934
public void cleanup_symlink_dir(string tag){
967-
string cmd = "";
968-
string std_out;
969-
string std_err;
970-
int ret_val;
971-
972935
try{
973936
string path = "%s-%s".printf(snapshots_path, tag);
974-
var f = File.new_for_path(path);
975-
if (f.query_exists()){
976-
cmd = "rm -rf \"%s\"".printf(path + "/");
977-
978-
if (LOG_COMMANDS) { log_debug(cmd); }
979-
980-
Process.spawn_command_line_sync(cmd, out std_out, out std_err, out ret_val);
981-
if (ret_val != 0){
982-
log_error (std_err);
983-
log_error(_("Failed to delete symlinks") + ": 'snapshots-%s'".printf(tag));
984-
return;
985-
}
937+
if(!TeeJee.FileSystem.dir_delete_recursive(path)) {
938+
log_error(_("Failed to delete symlinks") + ": '%s'".printf(path));
986939
}
987940

941+
File f = File.new_for_path(path);
988942
f.make_directory_with_parents();
989943
}
990944
catch (Error e) {
991-
log_error (e.message);
992-
}
945+
log_error (e.message);
946+
}
993947
}
994948

995949
}

src/Utility/DeleteFileTask.vala

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public class DeleteFileTask : AsyncTask{
6969
log_error (e.message);
7070
}
7171
}
72-
72+
7373
public void prepare() {
7474
string script_text = build_script();
7575
log_debug(script_text);
@@ -113,13 +113,10 @@ public class DeleteFileTask : AsyncTask{
113113
cmd += " '%s/'".printf(escape_single_quote(dest_path));
114114
}
115115
else{
116-
cmd += "rm";
116+
cmd += "rm -rf";
117117

118118
if (verbose){
119-
cmd += " -rfv";
120-
}
121-
else{
122-
cmd += " -rf";
119+
cmd += "v";
123120
}
124121

125122
cmd += " '%s'".printf(escape_single_quote(dest_path));

src/Utility/TeeJee.FileSystem.vala

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -298,33 +298,70 @@ namespace TeeJee.FileSystem{
298298
}
299299
}
300300

301-
public bool dir_delete (string dir_path, bool show_message = false){
302-
303-
/* Recursively deletes directory along with contents */
304-
305-
if (!dir_exists(dir_path)){
306-
return true;
301+
// delete an empty directory
302+
public static bool dir_empty_delete(string path) {
303+
File dirf = File.new_for_path(path);
304+
try {
305+
if (dirf.query_file_type(FileQueryInfoFlags.NONE) == FileType.DIRECTORY) {
306+
return dirf.delete(); // only succeeds, if the dir is empty
307+
}
308+
} catch(Error ioe) {
309+
if(ioe.matches(IOError.quark(), IOError.NOT_FOUND)) {
310+
// directory does not exist
311+
return true;
312+
}
307313
}
308-
309-
string cmd = "rm -rf '%s'".printf(escape_single_quote(dir_path));
310-
311-
log_debug(cmd);
312-
313-
string std_out, std_err;
314-
int status = exec_sync(cmd, out std_out, out std_err);
315-
314+
return false;
315+
}
316+
317+
public static bool dir_delete_recursive(string dir) {
318+
File f = File.new_for_path(dir);
319+
if(f.query_exists()) {
320+
try {
321+
FileEnumerator enumerator = f.enumerate_children(FileAttribute.STANDARD_NAME, FileQueryInfoFlags.NOFOLLOW_SYMLINKS);
322+
FileInfo info;
323+
while ((info = enumerator.next_file()) != null) {
324+
string name = info.get_name();
325+
if(info.get_file_type() == FileType.DIRECTORY) {
326+
327+
// ignore . and ..
328+
if(name == "." || name == "..") {
329+
continue;
330+
}
331+
332+
if(!dir_delete_recursive(dir + "/" + name)) {
333+
return false;
334+
}
335+
} else {
336+
File file = File.new_for_path(dir + "/" + name);
337+
file.delete();
338+
}
339+
}
340+
f.delete();
341+
} catch(Error err) {
342+
log_error(_("Can not enumerate folder %s").printf(dir));
343+
return false;
344+
}
345+
}
346+
return true;
347+
}
348+
349+
public static bool dir_delete(string dir_path, bool show_message = false) {
350+
351+
/* Recursively deletes directory along with contents */
352+
353+
bool status = dir_delete_recursive(dir_path);
354+
316355
if (show_message){
317-
if (status == 0){
356+
if (status){
318357
log_msg(_("Deleted directory") + ": %s".printf(dir_path));
319358
}
320359
else{
321360
log_error(_("Failed to delete directory") + ": %s".printf(dir_path));
322-
log_error(std_out);
323-
log_error(std_err);
324361
}
325362
}
326-
327-
return (status == 0);
363+
364+
return status;
328365
}
329366

330367
public bool dir_is_empty (string dir_path){
@@ -336,7 +373,7 @@ namespace TeeJee.FileSystem{
336373
var dir = File.parse_name (dir_path);
337374
if (dir.query_exists()) {
338375
FileInfo info;
339-
var enu = dir.enumerate_children ("%s".printf(FileAttribute.STANDARD_NAME), 0);
376+
var enu = dir.enumerate_children (FileAttribute.STANDARD_NAME, 0);
340377
while ((info = enu.next_file()) != null) {
341378
is_empty = false;
342379
break;

0 commit comments

Comments
 (0)