~teejee2008/timeshift/trunk

« back to all changes in this revision

Viewing changes to src/Utility/CronTab.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
using TeeJee.Logging;
 
3
using TeeJee.FileSystem;
 
4
//using TeeJee.JSON;
 
5
using TeeJee.ProcessHelper;
 
6
//using TeeJee.Multimedia;
 
7
//using TeeJee.System;
 
8
using TeeJee.Misc;
 
9
 
 
10
public class CronTab : GLib.Object {
 
11
        
 
12
        public static string crontab_read_all(string user_name = ""){
 
13
                string std_out, std_err;
 
14
 
 
15
                var cmd = "crontab -l";
 
16
                if (user_name.length > 0){
 
17
                        cmd += " -u %s".printf(user_name);
 
18
                }
 
19
 
 
20
                log_debug(cmd);
 
21
                
 
22
                int ret_val = exec_sync(cmd, out std_out, out std_err);
 
23
                
 
24
                if (ret_val != 0){
 
25
                        log_debug(_("Failed to read cron tab"));
 
26
                        return "";
 
27
                }
 
28
                else{
 
29
                        return std_out;
 
30
                }
 
31
        }
 
32
 
 
33
        public static bool add_job(string entry){
 
34
                // read crontab file
 
35
                string tab = crontab_read_all();
 
36
                var lines = new Gee.ArrayList<string>();
 
37
                foreach(string line in tab.split("\n")){
 
38
                        lines.add(line);
 
39
                }
 
40
 
 
41
                // check if entry exists
 
42
                foreach(string line in lines){
 
43
                        if (line == entry){
 
44
                                return true; // return
 
45
                        }
 
46
                }
 
47
 
 
48
                // append entry
 
49
                lines.add(entry);
 
50
 
 
51
                // create new tab
 
52
                string tab_new = "";
 
53
                foreach(string line in lines){
 
54
                        if (line.length > 0){
 
55
                                tab_new += "%s\n".printf(line);
 
56
                        }
 
57
                }
 
58
 
 
59
                // write temp crontab file
 
60
                string temp_file = get_temp_file_path();
 
61
                file_write(temp_file, tab_new);
 
62
 
 
63
                // install crontab file
 
64
                var cmd = "crontab \"%s\"".printf(temp_file);
 
65
                int status = exec_sync(cmd);
 
66
 
 
67
                if (status != 0){
 
68
                        log_error(_("Failed to add cron job") + ": %s".printf(entry));
 
69
                        return false;
 
70
                }
 
71
                else{
 
72
                        log_msg(_("Cron job added") + ": %s".printf(entry));
 
73
                        return true;
 
74
                }
 
75
        }
 
76
 
 
77
        public static bool remove_job(string entry){
 
78
                // read crontab file
 
79
                string tab = crontab_read_all();
 
80
                var lines = new Gee.ArrayList<string>();
 
81
                foreach(string line in tab.split("\n")){
 
82
                        lines.add(line);
 
83
                }
 
84
 
 
85
                // check if entry exists
 
86
                bool found = false;
 
87
                for(int i=0; i < lines.size; i++){
 
88
                        string line = lines[i];
 
89
                        if (line != null){
 
90
                                line = line.strip();
 
91
                        }
 
92
                        if (line == entry){
 
93
                                lines.remove(line);
 
94
                                found = true;
 
95
                        }
 
96
                }
 
97
                if (!found){
 
98
                        return true;
 
99
                }
 
100
 
 
101
                // create new tab
 
102
                string tab_new = "";
 
103
                foreach(string line in lines){
 
104
                        if (line.length > 0){
 
105
                                tab_new += "%s\n".printf(line);
 
106
                        }
 
107
                }
 
108
 
 
109
                // write temp crontab file
 
110
                string temp_file = get_temp_file_path();
 
111
                file_write(temp_file, tab_new);
 
112
 
 
113
                // install crontab file
 
114
                var cmd = "crontab \"%s\"".printf(temp_file);
 
115
                int status = exec_sync(cmd);
 
116
 
 
117
                if (status != 0){
 
118
                        log_error(_("Failed to remove cron job") + ": %s".printf(entry));
 
119
                        return false;
 
120
                }
 
121
                else{
 
122
                        log_msg(_("Cron job removed") + ": %s".printf(entry));
 
123
                        return true;
 
124
                }
 
125
        }
 
126
 
 
127
        public static bool install(string file_path, string user_name = ""){
 
128
 
 
129
                if (!file_exists(file_path)){
 
130
                        log_error(_("File not found") + ": %s".printf(file_path));
 
131
                        return false;
 
132
                }
 
133
                
 
134
                var cmd = "crontab";
 
135
                if (user_name.length > 0){
 
136
                        cmd += " -u %s".printf(user_name);
 
137
                }
 
138
                cmd += " \"%s\"".printf(file_path);
 
139
 
 
140
                log_debug(cmd);
 
141
 
 
142
                int status = exec_sync(cmd);
 
143
 
 
144
                if (status != 0){
 
145
                        log_error(_("Failed to install crontab file") + ": %s".printf(file_path));
 
146
                        return false;
 
147
                }
 
148
                else{
 
149
                        log_msg(_("crontab file installed") + ": %s".printf(file_path));
 
150
                        return true;
 
151
                }
 
152
        }
 
153
        
 
154
        public static bool export(string file_path, string user_name = ""){
 
155
                if (file_exists(file_path)){
 
156
                        file_delete(file_path);
 
157
                }
 
158
                
 
159
                bool ok = file_write(file_path, crontab_read_all(user_name));
 
160
 
 
161
                if (!ok){
 
162
                        log_error(_("Failed to export crontab file") + ": %s".printf(file_path));
 
163
                        return false;
 
164
                }
 
165
                else{
 
166
                        log_msg(_("crontab file exported") + ": %s".printf(file_path));
 
167
                        return true;
 
168
                }
 
169
        }
 
170
 
 
171
        public static bool import(string file_path, string user_name = ""){
 
172
                return install(file_path, user_name);
 
173
        }
 
174
}