diff --git a/src/Core/Main.vala b/src/Core/Main.vala index 519a4862..10be87a1 100644 --- a/src/Core/Main.vala +++ b/src/Core/Main.vala @@ -101,9 +101,6 @@ public class Main : GLib.Object{ //global vars for controlling threads public bool thr_success = false; - public bool thread_estimate_running = false; - public bool thread_estimate_success = false; - public bool thread_restore_running = false; public bool thread_restore_success = false; @@ -227,7 +224,7 @@ public class Main : GLib.Object{ file.delete (); } - dos_log = new DataOutputStream (file.create(FileCreateFlags.REPLACE_DESTINATION)); + TeeJee.Logging.dos_log = new DataOutputStream (file.create(FileCreateFlags.REPLACE_DESTINATION)); if (LOG_DEBUG || gui_mode){ log_debug(_("Session log file") + ": %s".printf(log_file)); } @@ -885,7 +882,8 @@ public class Main : GLib.Object{ } - public bool save_exclude_list_for_backup(string output_path){ + // returns the file path on success, null on failure + private string? save_exclude_list_for_backup(string output_path){ log_debug("Main: save_exclude_list_for_backup()"); @@ -899,7 +897,10 @@ public class Main : GLib.Object{ } string list_file = path_combine(output_path, "exclude.list"); - return file_write(list_file, txt); + if (file_write(list_file, txt)) { + return list_file; + } + return null; } public bool save_exclude_list_for_restore(string output_path){ @@ -1411,11 +1412,9 @@ public class Main : GLib.Object{ // save exclude list ---------------- - bool ok = save_exclude_list_for_backup(snapshot_path); - - string exclude_from_file = path_combine(snapshot_path, "exclude.list"); + string? exclude_from_file = save_exclude_list_for_backup(snapshot_path); - if (!ok){ + if (null == exclude_from_file){ log_error("Failed to save exclude list"); return 0; } @@ -1526,11 +1525,9 @@ public class Main : GLib.Object{ // save exclude list ---------------- - bool ok = save_exclude_list_for_backup(snapshot_path); - - string exclude_from_file = path_combine(snapshot_path, "exclude.list"); + string? exclude_from_file = save_exclude_list_for_backup(snapshot_path); - if (!ok){ + if (null == exclude_from_file){ log_error(_("Failed to save exclude list")); return null; } @@ -1557,7 +1554,7 @@ public class Main : GLib.Object{ task.delete_extra = true; task.delete_excluded = true; task.delete_after = false; - + if (app_mode.length > 0){ // console mode task.io_nice = true; @@ -1578,7 +1575,7 @@ public class Main : GLib.Object{ stdout.printf("\r"); stdout.flush(); - + if (task.total_size == 0){ log_error(_("rsync returned an error")); log_error(_("Failed to create new snapshot")); @@ -3872,8 +3869,9 @@ public class Main : GLib.Object{ return ok; } - - public uint64 estimate_system_size(){ + + public delegate void progressCallback(); + public uint64 estimate_system_size(progressCallback? callback = null) { log_debug("estimate_system_size()"); @@ -3884,105 +3882,45 @@ public class Main : GLib.Object{ return 0; } - try { - thread_estimate_running = true; - thr_success = false; - new Thread.try ("estimate-system-size", () => {estimate_system_size_thread();}); - } catch (Error e) { - thread_estimate_running = false; - thr_success = false; - log_error (e.message); - } - - while (thread_estimate_running){ - gtk_do_events (); - Thread.usleep((ulong) GLib.TimeSpan.MILLISECOND * 100); - } - - save_app_config(); - - log_debug("estimate_system_size(): ok"); - - return Main.first_snapshot_size; - } - - public void estimate_system_size_thread(){ - thread_estimate_running = true; - - string cmd = ""; - string std_out; - string std_err; - int ret_val; - uint64 required_space = 0; - int64 file_count = 0; - - try{ + // create a RsyncTask in dry_run mode to estimate the amount of work needed to do + string dir_empty = path_combine(TEMP_DIR, "empty"); + dir_create(dir_empty); - log_debug("Using temp dir '%s'".printf(TEMP_DIR)); + task = new RsyncTask(); - string file_exclude_list = path_combine(TEMP_DIR, "exclude.list"); - var f = File.new_for_path(file_exclude_list); - if (f.query_exists()){ - f.delete(); - } + task.dest_path = dir_empty; + task.exclude_from_file = save_exclude_list_for_backup(TEMP_DIR) ?? "";; - string file_log = path_combine(TEMP_DIR, "rsync.log"); - f = File.new_for_path(file_log); - if (f.query_exists()){ - f.delete(); - } - - string dir_empty = path_combine(TEMP_DIR, "empty"); - f = File.new_for_path(dir_empty); - if (!f.query_exists()){ - dir_create(dir_empty); - } + task.delete_extra = true; + task.delete_excluded = true; + task.delete_after = false; - save_exclude_list_for_backup(TEMP_DIR); + if (app_mode.length > 0){ + // console mode + task.io_nice = true; + } - cmd = "export LC_ALL=C.UTF-8 ; rsync -ai --delete --numeric-ids --relative --stats --dry-run --delete-excluded --exclude-from='%s' /. '%s' &> '%s'".printf(file_exclude_list, dir_empty, file_log); + task.dry_run = true; - log_debug(cmd); - ret_val = exec_script_sync(cmd, out std_out, out std_err); + task.execute(); - if (file_exists(file_log)){ - cmd = "cat '%s' | awk '/Total file size/ {print $4}'".printf(file_log); - ret_val = exec_script_sync(cmd, out std_out, out std_err); - if (ret_val == 0){ - required_space = long.parse(std_out.replace(",","").strip()); - file_count = file_line_count(file_log) ?? 0; - - thr_success = true; - } - else{ - log_error (_("Failed to estimate system size")); - log_error (std_err); - thr_success = false; - } - } - else{ - log_error (_("Failed to estimate system size")); - log_error (std_err); - log_error (std_out); - thr_success = false; - } - } - catch(Error e){ - log_error (e.message); - thr_success = false; + while (task.status == AppStatus.RUNNING){ + Thread.usleep((ulong) GLib.TimeSpan.MILLISECOND * 16); // ~60fps + if(callback != null) callback(); + gtk_do_events(); } - if ((required_space == 0) && (sys_root != null)){ - required_space = sys_root.used_bytes; + Main.first_snapshot_size = task.total_size; + Main.first_snapshot_count = task.count_created; + if ((task.total_size == 0) && (sys_root != null)) { + Main.first_snapshot_size = sys_root.used_bytes; } - Main.first_snapshot_size = required_space; - Main.first_snapshot_count = file_count; - - log_debug("First snapshot size: %s".printf(format_file_size(required_space))); - log_debug("File count: %lld".printf(first_snapshot_count)); + save_app_config(); - thread_estimate_running = false; + log_debug("estimate_system_size(): ok"); + + return Main.first_snapshot_size; } // btrfs diff --git a/src/Gtk/EstimateBox.vala b/src/Gtk/EstimateBox.vala index f006e4af..b94941bf 100644 --- a/src/Gtk/EstimateBox.vala +++ b/src/Gtk/EstimateBox.vala @@ -41,8 +41,6 @@ class EstimateBox : Gtk.Box{ private Gtk.ProgressBar progressbar; private Gtk.Window parent_window; - private bool thread_is_running = false; - public EstimateBox (Gtk.Window _parent_window) { log_debug("EstimateBox: EstimateBox()"); @@ -70,62 +68,33 @@ class EstimateBox : Gtk.Box{ //progressbar progressbar = new Gtk.ProgressBar(); + progressbar.pulse_step = 0.01; //progressbar.set_size_request(-1,25); - //progressbar.pulse_step = 0.1; add (progressbar); log_debug("EstimateBox: EstimateBox(): exit"); } - public void estimate_system_size(){ + public void estimate_system_size() { if (Main.first_snapshot_size > 0){ log_debug("EstimateBox: size > 0"); return; } - - progressbar.fraction = 0.0; // start the estimation if not already running - if (!App.thread_estimate_running){ - log_debug("EstimateBox: thread started"); - - try { - thread_is_running = true; - new Thread.try ("estimate-system-size", () => {estimate_system_size_thread();}); - } - catch (Error e) { - thread_is_running = false; - log_error (e.message); - } - } + log_debug("EstimateBox: thread started"); - // wait for completion and increment progressbar - while (thread_is_running){ - - if (progressbar.fraction < 98.0){ - - progressbar.fraction += 0.005; + #if XAPP + XApp.set_window_progress_pulse(parent_window, true); + #endif + progressbar.pulse(); - #if XAPP - XApp.set_window_progress(parent_window, (int)(progressbar.fraction * 100.0)); - #endif - } - - gtk_do_events(); - sleep(100); - } + App.estimate_system_size(progressbar.pulse); #if XAPP - XApp.set_window_progress(parent_window, 0); + XApp.set_window_progress_pulse(parent_window, false); #endif } - - private void estimate_system_size_thread(){ - - App.estimate_system_size(); - log_debug("EstimateBox: thread finished"); - thread_is_running = false; - } } diff --git a/src/Utility/AsyncTask.vala b/src/Utility/AsyncTask.vala index 125c1b63..5fec490a 100644 --- a/src/Utility/AsyncTask.vala +++ b/src/Utility/AsyncTask.vala @@ -36,7 +36,6 @@ public abstract class AsyncTask : GLib.Object{ private DataInputStream dis_out = null; private DataInputStream dis_err = null; protected DataOutputStream dos_log = null; - protected bool is_terminated = false; private bool stdout_is_open = false; private bool stderr_is_open = false; @@ -49,10 +48,7 @@ public abstract class AsyncTask : GLib.Object{ protected string script_file = ""; protected string working_dir = ""; - protected string log_file = ""; - public bool background_mode = false; - // public public AppStatus status = AppStatus.NOT_STARTED; @@ -60,15 +56,11 @@ public abstract class AsyncTask : GLib.Object{ public GLib.Mutex status_line_mutex; public int exit_code = 0; - public string error_msg = ""; public GLib.Timer timer; public double progress = 0.0; public double percent = 0.0; public int64 prg_count = 0; public int64 prg_count_total = 0; - public int64 prg_bytes = 0; - public int64 prg_bytes_total = 0; - public string eta = ""; // signals public signal void stdout_line_read(string line); @@ -104,25 +96,30 @@ public abstract class AsyncTask : GLib.Object{ protected AsyncTask(){ working_dir = TEMP_DIR + "/" + timestamp_for_path(); script_file = path_combine(working_dir, "script.sh"); - log_file = path_combine(working_dir, "task.log"); status_line_mutex = GLib.Mutex(); dir_create(working_dir); } - - public bool begin(){ + public virtual void prepare() { + string script_text = build_script(); + log_debug(script_text); + save_bash_script_temp(script_text, script_file); + + log_debug("AsyncTask:prepare(): saved: %s".printf(script_file)); + } + + protected abstract string build_script(); + + protected virtual bool begin() { status = AppStatus.RUNNING; bool has_started = true; - is_terminated = false; finish_called = false; - + prg_count = 0; - prg_bytes = 0; - error_msg = ""; - + string[] spawn_args = new string[1]; spawn_args[0] = script_file; @@ -145,42 +142,30 @@ public abstract class AsyncTask : GLib.Object{ out output_fd, out error_fd); - set_priority(); - log_debug("AsyncTask: child_pid: %d".printf(child_pid)); // create stream readers - UnixOutputStream uos_in = new UnixOutputStream(input_fd, false); - UnixInputStream uis_out = new UnixInputStream(output_fd, false); - UnixInputStream uis_err = new UnixInputStream(error_fd, false); + UnixOutputStream uos_in = new UnixOutputStream(input_fd, true); + UnixInputStream uis_out = new UnixInputStream(output_fd, true); + UnixInputStream uis_err = new UnixInputStream(error_fd, true); dos_in = new DataOutputStream(uos_in); dis_out = new DataInputStream(uis_out); dis_err = new DataInputStream(uis_err); dis_out.newline_type = DataStreamNewlineType.ANY; dis_err.newline_type = DataStreamNewlineType.ANY; - // create log file - if (log_file.length > 0){ - var file = File.new_for_path(log_file); - if (file.query_exists()){ - file.delete(); - } - var file_stream = file.create (FileCreateFlags.REPLACE_DESTINATION); - dos_log = new DataOutputStream (file_stream); - } - try { //start thread for reading output stream - new Thread.try ("async-task-stdout-reader", () => {read_stdout();}); - } catch (Error e) { + new Thread.try ("async-task-stdout-reader", read_stdout); + } catch (GLib.Error e) { log_error ("AsyncTask.begin():create_thread:read_stdout()"); log_error (e.message); } try { //start thread for reading error stream - new Thread.try ("async-task-stderr-reader", () => {read_stderr();}); - } catch (Error e) { + new Thread.try ("async-task-stderr-reader", read_stderr); + } catch (GLib.Error e) { log_error ("AsyncTask.begin():create_thread:read_stderr()"); log_error (e.message); } @@ -202,7 +187,7 @@ public abstract class AsyncTask : GLib.Object{ out_line = dis_out.read_line (null); while (out_line != null) { //log_msg("O: " + out_line); - if (!is_terminated && (out_line.length > 0)){ + if (out_line.length > 0) { parse_stdout_line(out_line); stdout_line_read(out_line); //signal } @@ -212,12 +197,13 @@ public abstract class AsyncTask : GLib.Object{ stdout_is_open = false; // dispose stdout - if ((dis_out != null) && !dis_out.is_closed()){ - dis_out.close(); + try { + if (dis_out != null) { + dis_out.close(); + } } - //dis_out.close(); + catch (GLib.Error ignored) {} dis_out = null; - GLib.FileUtils.close(output_fd); // check if complete if (!stdout_is_open && !stderr_is_open){ @@ -236,9 +222,7 @@ public abstract class AsyncTask : GLib.Object{ err_line = dis_err.read_line (null); while (err_line != null) { - if (!is_terminated && (err_line.length > 0)){ - error_msg += "%s\n".printf(err_line); - + if (err_line.length > 0){ parse_stderr_line(err_line); stderr_line_read(err_line); //signal } @@ -248,12 +232,13 @@ public abstract class AsyncTask : GLib.Object{ stderr_is_open = false; // dispose stderr - if ((dis_err != null) && !dis_err.is_closed()){ - dis_err.close(); + try { + if (dis_err != null) { + dis_err.close(); + } } - //dis_err.close(); + catch (GLib.Error ignored) {} dis_err = null; - GLib.FileUtils.close(error_fd); // check if complete if (!stdout_is_open && !stderr_is_open){ @@ -280,6 +265,12 @@ public abstract class AsyncTask : GLib.Object{ log_error (e.message); } } + + public virtual void execute() { + log_debug("AsyncTask:execute()"); + prepare(); + begin(); + } protected abstract void parse_stdout_line(string out_line); @@ -295,35 +286,18 @@ public abstract class AsyncTask : GLib.Object{ // dispose stdin try{ - if ((dos_in != null) && !dos_in.is_closed() && !dos_in.is_closing()){ + if (dos_in != null) { dos_in.close(); } } - catch(Error e){ + catch(GLib.IOError e) { // ignore - //log_error ("AsyncTask.finish(): dos_in.close()"); - //log_error (e.message); } - dos_in = null; - GLib.FileUtils.close(input_fd); // dispose child process Process.close_pid(child_pid); //required on Windows, doesn't do anything on Unix - try{ - // dispose log - if ((dos_log != null) && !dos_log.is_closed() && !dos_log.is_closing()){ - dos_log.close(); - } - dos_log = null; - } - catch (Error e) { - // error can be ignored - // dos_log is closed automatically when the last reference is set to null - // there may be pending operations which may throw an error - } - read_exit_code(); status_line = ""; @@ -343,7 +317,8 @@ public abstract class AsyncTask : GLib.Object{ task_complete(); //signal } - protected abstract void finish_task(); + // can be overloaded by subclasses, that wish to do special stuff during finish + protected virtual void finish_task() {} protected int read_exit_code(){ @@ -358,7 +333,6 @@ public abstract class AsyncTask : GLib.Object{ } public bool is_running(){ - return (status == AppStatus.RUNNING); } @@ -375,30 +349,6 @@ public abstract class AsyncTask : GLib.Object{ } } - public void set_priority() { - - if (background_mode){ - set_priority_value(5); - } - else{ - set_priority_value(0); - } - } - - public void set_priority_value(int prio) { - - Pid app_pid = Posix.getpid(); - process_set_priority (app_pid, prio); - - if (status == AppStatus.RUNNING) { - process_set_priority (child_pid, prio); - - foreach (Pid sub_child_pid in get_process_children (child_pid)) { - process_set_priority (sub_child_pid, prio); - } - } - } - public string stat_time_elapsed{ owned get{ long elapsed = (long) timer_elapsed(timer); @@ -471,15 +421,8 @@ public class RsyncTask : AsyncTask{ script_file = _script_file; log_file = _log_file; } - - public void prepare() { - string script_text = build_script(); - save_bash_script_temp(script_text, script_file); - } - - private string build_script() { - var script = new StringBuilder(); + protected override string build_script() { var cmd = "rsync -ai"; if (verbose){ @@ -513,36 +456,16 @@ public class RsyncTask : AsyncTask{ //cmd += " /. \"%s\"".printf(sync_path + "/localhost/"); - return script.str; + return cmd; } // execution ---------------------------- - public void execute() { - - prepare(); - - begin(); - - if (status == AppStatus.RUNNING){ - - - } - } - public override void parse_stdout_line(string out_line){ - if (is_terminated) { - return; - } - update_progress_parse_console_output(out_line); } public override void parse_stderr_line(string err_line){ - if (is_terminated) { - return; - } - update_progress_parse_console_output(err_line); } @@ -553,21 +476,5 @@ public class RsyncTask : AsyncTask{ return true; } - - protected override void finish_task(){ - if ((status != AppStatus.CANCELLED) && (status != AppStatus.PASSWORD_REQUIRED)) { - status = AppStatus.FINISHED; - } - } - - public int read_status(){ - var status_file = working_dir + "/status"; - var f = File.new_for_path(status_file); - if (f.query_exists()){ - var txt = file_read(status_file); - return int.parse(txt); - } - return -1; - } } */ diff --git a/src/Utility/DeleteFileTask.vala b/src/Utility/DeleteFileTask.vala index 0381ab96..4d925858 100644 --- a/src/Utility/DeleteFileTask.vala +++ b/src/Utility/DeleteFileTask.vala @@ -70,18 +70,14 @@ public class DeleteFileTask : AsyncTask{ } } - public void prepare() { - string script_text = build_script(); - log_debug(script_text); - save_bash_script_temp(script_text, script_file); - - log_debug("RsyncTask:prepare(): saved: %s".printf(script_file)); + public override void prepare() { + base.prepare(); status_line_count = 0; total_size = 0; } - private string build_script() { + protected override string build_script() { var cmd = ""; if (io_nice){ @@ -127,35 +123,11 @@ public class DeleteFileTask : AsyncTask{ // execution ---------------------------- - public void execute() { - - status = AppStatus.RUNNING; - - log_debug("RsyncTask:execute()"); - - prepare(); - - begin(); - - if (status == AppStatus.RUNNING){ - - - } - } - public override void parse_stdout_line(string out_line){ - if (is_terminated) { - return; - } - update_progress_parse_console_output(out_line); } public override void parse_stderr_line(string err_line){ - if (is_terminated) { - return; - } - update_progress_parse_console_output(err_line); } @@ -187,20 +159,4 @@ public class DeleteFileTask : AsyncTask{ return true; } - - protected override void finish_task(){ - if ((status != AppStatus.CANCELLED) && (status != AppStatus.PASSWORD_REQUIRED)) { - status = AppStatus.FINISHED; - } - } - - public int read_status(){ - var status_file = working_dir + "/status"; - var f = File.new_for_path(status_file); - if (f.query_exists()){ - var txt = file_read(status_file); - return int.parse(txt); - } - return -1; - } } diff --git a/src/Utility/RsyncSpaceCheckTask.vala b/src/Utility/RsyncSpaceCheckTask.vala index 13c4262b..241cce5e 100644 --- a/src/Utility/RsyncSpaceCheckTask.vala +++ b/src/Utility/RsyncSpaceCheckTask.vala @@ -68,19 +68,14 @@ public class RsyncSpaceCheckTask : AsyncTask{ } } - public void prepare() { - string script_text = build_script(); - - log_debug(script_text); - - save_bash_script_temp(script_text, script_file); - log_debug("RsyncSpaceCheckTask:prepare(): saved: %s".printf(script_file)); + public override void prepare() { + base.prepare(); total_size = 0; status_line_count = 0; } - private string build_script() { + protected override string build_script() { var cmd = "export LC_ALL=C.UTF-8\n"; cmd += "rsync -aii"; @@ -143,27 +138,11 @@ public class RsyncSpaceCheckTask : AsyncTask{ // execution ---------------------------- - public void execute() { - - log_debug("RsyncSpaceCheckTask:execute()"); - - prepare(); - begin(); - } - public override void parse_stdout_line(string out_line){ - if (is_terminated) { - return; - } - update_progress_parse_console_output(out_line); } public override void parse_stderr_line(string err_line){ - if (is_terminated) { - return; - } - update_progress_parse_console_output(err_line); } @@ -190,20 +169,4 @@ public class RsyncSpaceCheckTask : AsyncTask{ return true; } - - protected override void finish_task(){ - if ((status != AppStatus.CANCELLED) && (status != AppStatus.PASSWORD_REQUIRED)) { - status = AppStatus.FINISHED; - } - } - - public int read_status(){ - var status_file = working_dir + "/status"; - var f = File.new_for_path(status_file); - if (f.query_exists()){ - var txt = file_read(status_file); - return int.parse(txt); - } - return -1; - } } diff --git a/src/Utility/RsyncTask.vala b/src/Utility/RsyncTask.vala index da47bf74..57497b28 100644 --- a/src/Utility/RsyncTask.vala +++ b/src/Utility/RsyncTask.vala @@ -30,6 +30,21 @@ using TeeJee.Misc; public class RsyncTask : AsyncTask{ + private enum RegexType { + Status, + Created, + LogCreated, + Deleted, + LogDeleted, + Modified, + LogModified, + Unchanged, + LogUnchanged, + TotalSize, + + Count + } + // settings public bool delete_extra = true; public bool delete_after = false; @@ -46,7 +61,7 @@ public class RsyncTask : AsyncTask{ public bool dry_run = false; // regex - private Gee.HashMap regex_list; + private Regex[] regex_list = null; // status public GLib.Queue status_lines; @@ -76,7 +91,7 @@ public class RsyncTask : AsyncTask{ return; // already initialized } - regex_list = new Gee.HashMap(); + regex_list = new Regex[RegexType.Count]; /* 2018/02/03 16:53:46 [2419] >f..t...... boot/grub/grubenv @@ -110,34 +125,34 @@ public class RsyncTask : AsyncTask{ try { //Example: status=-1 - regex_list["status"] = new Regex( + regex_list[RegexType.Status] = new Regex( """([<>ch.*])([.fdLDS])(c|\+|\.| )(s|\+|\.| )(t|\+|\.| )(p|\+|\.| )(o|\+|\.| )(g|\+|\.| )(u|\+|\.| )(a|\+|\.| )(x|\+|\.| ) (.*)"""); - regex_list["created"] = new Regex( + regex_list[RegexType.Created] = new Regex( """([<>ch.*])([.fdLDS])[+]{9} (.*)"""); - regex_list["log-created"] = new Regex( + regex_list[RegexType.LogCreated] = new Regex( """[0-9\/]+ [0-9:.]+ \[[0-9]+\] ([<>ch.*])([.fdLDS])[+]{9} (.*)"""); - regex_list["deleted"] = new Regex( + regex_list[RegexType.Deleted] = new Regex( """\*deleting[ \t]+(.*)"""); - regex_list["log-deleted"] = new Regex( + regex_list[RegexType.LogDeleted] = new Regex( """[0-9\/]+ [0-9:.]+ \[[0-9]+\] \*deleting[ \t]+(.*)"""); - regex_list["modified"] = new Regex( + regex_list[RegexType.Modified] = new Regex( """([<>ch.])([.fdLDS])(c|\+|\.| )(s|\+|\.| )(t|\+|\.| )(p|\+|\.| )(o|\+|\.| )(g|\+|\.| )(u|\+|\.| )(a|\+|\.| )(x|\+|\.) (.*)"""); - regex_list["log-modified"] = new Regex( + regex_list[RegexType.LogModified] = new Regex( """[0-9\/]+ [0-9:.]+ \[[0-9]+\] ([<>ch.])([.fdLDS])(c|\+|\.| )(s|\+|\.| )(t|\+|\.| )(p|\+|\.| )(o|\+|\.| )(g|\+|\.| )(u|\+|\.| )(a|\+|\.| )(x|\+|\.) (.*)"""); - regex_list["unchanged"] = new Regex( + regex_list[RegexType.Unchanged] = new Regex( """([.h])([.fdLDS])[ ]{9} (.*)"""); - regex_list["log-unchanged"] = new Regex( + regex_list[RegexType.LogUnchanged] = new Regex( """[0-9\/]+ [0-9:.]+ \[[0-9]+\] ([.h])([.fdLDS])[ ]{9} (.*)"""); - - regex_list["total-size"] = new Regex( + + regex_list[RegexType.TotalSize] = new Regex( """total size is ([0-9,]+)[ \t]+speedup is [0-9.]+"""); } @@ -146,13 +161,8 @@ public class RsyncTask : AsyncTask{ } } - public void prepare() { - string script_text = build_script(); - - log_debug(script_text); - - save_bash_script_temp(script_text, script_file); - log_debug("RsyncTask:prepare(): saved: %s".printf(script_file)); + public override void prepare() { + base.prepare(); //status_lines = new GLib.Queue(); status_line_count = 0; @@ -172,7 +182,7 @@ public class RsyncTask : AsyncTask{ log = new StringBuilder(); } - private string build_script() { + protected override string build_script() { var cmd = "export LC_ALL=C.UTF-8\n"; @@ -246,7 +256,7 @@ public class RsyncTask : AsyncTask{ 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)); @@ -316,8 +326,8 @@ public class RsyncTask : AsyncTask{ item_path = ""; MatchInfo match; - if (regex_list["created"].match(line, 0, out match) - || regex_list["log-created"].match(line, 0, out match)) { + if (regex_list[RegexType.Created].match(line, 0, out match) + || regex_list[RegexType.LogCreated].match(line, 0, out match)) { if (dos_changes != null){ dos_changes.put_string("%s\n".printf(line)); @@ -335,8 +345,8 @@ public class RsyncTask : AsyncTask{ }*/ item_status = "created"; } - else if (regex_list["log-deleted"].match(line, 0, out match) - || regex_list["deleted"].match(line, 0, out match)) { + else if (regex_list[RegexType.LogDeleted].match(line, 0, out match) + || regex_list[RegexType.Deleted].match(line, 0, out match)) { //log_debug("matched: deleted:%s".printf(line)); @@ -348,8 +358,8 @@ public class RsyncTask : AsyncTask{ //item_type = item_path.has_suffix("/") ? FileType.DIRECTORY : FileType.REGULAR; item_status = "deleted"; } - else if (regex_list["modified"].match(line, 0, out match) - || regex_list["log-modified"].match(line, 0, out match)) { + else if (regex_list[RegexType.Modified].match(line, 0, out match) + || regex_list[RegexType.LogModified].match(line, 0, out match)) { //log_debug("matched: modified:%s".printf(line)); @@ -385,7 +395,7 @@ public class RsyncTask : AsyncTask{ item_status = "group"; } } - else if (regex_list["log-unchanged"].match(line, 0, out match)) { + else if (regex_list[RegexType.LogUnchanged].match(line, 0, out match)) { // ignore } else{ @@ -420,34 +430,11 @@ public class RsyncTask : AsyncTask{ // execution ---------------------------- - public void execute() { - - log_debug("RsyncTask:execute()"); - - prepare(); - - /*log_debug(string.nfill(70,'=')); - log_debug(script_file); - log_debug(string.nfill(70,'=')); - log_debug(file_read(script_file)); - log_debug(string.nfill(70,'='));*/ - - begin(); - } - public override void parse_stdout_line(string out_line){ - if (is_terminated) { - return; - } - update_progress_parse_console_output(out_line); } public override void parse_stderr_line(string err_line){ - if (is_terminated) { - return; - } - update_progress_parse_console_output(err_line); } @@ -464,7 +451,7 @@ public class RsyncTask : AsyncTask{ } //MatchInfo match; - //if (regex_list["status"].match(line, 0, out match)) { + //if (regex_list[RegexType.Status].match(line, 0, out match)) { // status_line = match.fetch(12); //status_lines.push_tail(status_line); @@ -473,7 +460,7 @@ public class RsyncTask : AsyncTask{ //} //} MatchInfo match; - if (regex_list["created"].match(line, 0, out match)) { + if (regex_list[RegexType.Created].match(line, 0, out match)) { //log_debug("matched: created:%s".printf(line)); @@ -481,7 +468,7 @@ public class RsyncTask : AsyncTask{ status_line = match.fetch(3).split(" -> ")[0].strip(); log.append(line + "\n"); } - else if (regex_list["deleted"].match(line, 0, out match)) { + else if (regex_list[RegexType.Deleted].match(line, 0, out match)) { //log_debug("matched: deleted:%s".printf(line)); @@ -489,14 +476,14 @@ public class RsyncTask : AsyncTask{ status_line = match.fetch(1).split(" -> ")[0].strip(); log.append(line + "\n"); } - else if (regex_list["unchanged"].match(line, 0, out match)) { + else if (regex_list[RegexType.Unchanged].match(line, 0, out match)) { //log_debug("matched: unchanged:%s".printf(line)); count_unchanged++; status_line = match.fetch(3).split(" -> ")[0].strip(); } - else if (regex_list["modified"].match(line, 0, out match)) { + else if (regex_list[RegexType.Modified].match(line, 0, out match)) { //log_debug("matched: modified:%s".printf(line)); @@ -526,7 +513,7 @@ public class RsyncTask : AsyncTask{ count_unchanged++; } } - else if (regex_list["total-size"].match(line, 0, out match)) { + else if (regex_list[RegexType.TotalSize].match(line, 0, out match)) { total_size = int64.parse(match.fetch(1).replace(",","")); } else{ @@ -536,22 +523,10 @@ public class RsyncTask : AsyncTask{ return true; } - protected override void finish_task(){ - - if ((status != AppStatus.CANCELLED) && (status != AppStatus.PASSWORD_REQUIRED)) { - status = AppStatus.FINISHED; + protected override void finish_task() { + if (0 < rsync_log_file.length) { + file_write(rsync_log_file + "-changes", log.str); } - - file_write(rsync_log_file + "-changes", log.str); } - public int read_status(){ - var status_file = working_dir + "/status"; - var f = File.new_for_path(status_file); - if (f.query_exists()){ - var txt = file_read(status_file); - return int.parse(txt); - } - return -1; - } } diff --git a/src/Utility/TeeJee.FileSystem.vala b/src/Utility/TeeJee.FileSystem.vala index 125c0c7b..3d26de46 100644 --- a/src/Utility/TeeJee.FileSystem.vala +++ b/src/Utility/TeeJee.FileSystem.vala @@ -140,10 +140,6 @@ namespace TeeJee.FileSystem{ dir_create(file_parent(file_path)); var file = File.new_for_path (file_path); - if (file.query_exists ()) { - file.delete (); - } - var file_stream = file.create (FileCreateFlags.REPLACE_DESTINATION); var data_stream = new DataOutputStream (file_stream); data_stream.put_string (contents);