~teejee2008/timeshift/trunk

« back to all changes in this revision

Viewing changes to src/Utility/Bash.vala

  • Committer: Tony George
  • Date: 2016-08-13 04:16:47 UTC
  • Revision ID: tony.george.kol@gmail.com-20160813041647-ivf2g6rszt00xco5
Updated project structure

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Bash.vala
 
3
 *
 
4
 * Copyright 2015 Tony George <teejee2008@gmail.com>
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 
19
 * MA 02110-1301, USA.
 
20
 *
 
21
 *
 
22
 */
 
23
 
 
24
using GLib;
 
25
using Gtk;
 
26
using Gee;
 
27
using Json;
 
28
 
 
29
using TeeJee.Logging;
 
30
using TeeJee.FileSystem;
 
31
using TeeJee.JsonHelper;
 
32
using TeeJee.ProcessHelper;
 
33
using TeeJee.GtkHelper;
 
34
using TeeJee.System;
 
35
using TeeJee.Misc;
 
36
 
 
37
public class Bash : AsyncTask {
 
38
 
 
39
        public Pid bash_pid = -1;
 
40
        public int status_code = -1;
 
41
        private static Gee.HashMap<string, Regex> regex_list;
 
42
        
 
43
        public Bash() {
 
44
                var temp_dir = TEMP_DIR + "/" + timestamp_for_path();
 
45
                log_file = temp_dir + "/log.txt";
 
46
                script_file = temp_dir + "/bash.sh";
 
47
                working_dir = temp_dir;
 
48
                
 
49
                init_regular_expressions();
 
50
        }
 
51
 
 
52
        private static void init_regular_expressions(){
 
53
                if (regex_list != null){
 
54
                        return; // already initialized
 
55
                }
 
56
                
 
57
                regex_list = new Gee.HashMap<string,Regex>();
 
58
                
 
59
                try {
 
60
                        //Example: status=-1
 
61
                        regex_list["status"] = new Regex("""status=([0-9\-]+)""");
 
62
                }
 
63
                catch (Error e) {
 
64
                        log_error (e.message);
 
65
                }
 
66
        }
 
67
 
 
68
        // execution ----------------------------
 
69
 
 
70
        public void start_shell() {
 
71
                dir_create(working_dir);
 
72
 
 
73
                var sh = "bash -c 'pkexec bash'";
 
74
                save_bash_script_temp(sh, script_file);
 
75
                begin();
 
76
                log_debug("Started bash shell");
 
77
 
 
78
                if (status == AppStatus.RUNNING){
 
79
                        bash_pid = -1;
 
80
                        while ((status == AppStatus.RUNNING) && (bash_pid == -1)) {
 
81
                                sleep(200);
 
82
                                var children = get_process_children(child_pid);
 
83
                                if (children.length > 0){
 
84
                                        bash_pid = children[0];
 
85
                                }
 
86
                        }
 
87
                        
 
88
                        log_debug("script pid: %d".printf(child_pid));  
 
89
                        log_debug("bash shell pid: %d".printf(bash_pid));       
 
90
                }
 
91
        }
 
92
 
 
93
        public override void parse_stdout_line(string out_line){
 
94
                if ((out_line == null) || (out_line.length == 0)) {
 
95
                        return;
 
96
                }
 
97
                MatchInfo match;
 
98
                if (regex_list["status"].match(out_line, 0, out match)) {
 
99
                        status_code = int.parse(match.fetch(1));
 
100
                }
 
101
                stdout.printf(out_line + "\n");
 
102
                stdout.flush();
 
103
        }
 
104
        
 
105
        public override void parse_stderr_line(string err_line){
 
106
                stdout.printf(err_line + "\n");
 
107
                stdout.flush();
 
108
        }
 
109
 
 
110
        public int execute(string line){
 
111
                status_code = -1;
 
112
                write_stdin(line);
 
113
                write_stdin("echo status=$?");
 
114
 
 
115
                while (status_code == -1){
 
116
                        sleep(200);
 
117
                        gtk_do_events();
 
118
                }
 
119
 
 
120
                return status_code;
 
121
        }
 
122
        
 
123
        protected override void finish_task(){
 
124
                log_debug("Bash: finish_task()");
 
125
        }
 
126
 
 
127
        public int read_status(){
 
128
                var status_file = working_dir + "/status";
 
129
                var f = File.new_for_path(status_file);
 
130
                if (f.query_exists()){
 
131
                        var txt = file_read(status_file);
 
132
                        return int.parse(txt);
 
133
                }
 
134
                return -1;
 
135
        }
 
136
 
 
137
}
 
138