~teejee2008/timeshift/trunk

« back to all changes in this revision

Viewing changes to src/Core/Snapshot.vala

  • Committer: Tony George
  • Date: 2016-08-24 17:02:17 UTC
  • Revision ID: tony.george.kol@gmail.com-20160824170217-evbrjfbzrkjbt4x5
Moved classes to separate files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using TeeJee.Logging;
 
2
using TeeJee.FileSystem;
 
3
using TeeJee.Devices;
 
4
using TeeJee.JsonHelper;
 
5
using TeeJee.ProcessHelper;
 
6
using TeeJee.GtkHelper;
 
7
using TeeJee.System;
 
8
using TeeJee.Misc;
 
9
using Json;
 
10
 
 
11
public class Snapshot : GLib.Object{
 
12
        public string path = "";
 
13
        public string name = "";
 
14
        public DateTime date;
 
15
        public string sys_uuid = "";
 
16
        public string sys_distro = "";
 
17
        public string app_version = "";
 
18
        public string description = "";
 
19
        public Gee.ArrayList<string> tags;
 
20
        public Gee.ArrayList<string> exclude_list;
 
21
        public Gee.ArrayList<FsTabEntry> fstab_list;
 
22
        public bool is_valid = true;
 
23
 
 
24
        // private
 
25
        private bool thr_success = false;
 
26
        private bool thr_running = false;
 
27
        //private int thr_retval = -1;
 
28
 
 
29
        public Snapshot(string dir_path){
 
30
 
 
31
                try{
 
32
                        var f = File.new_for_path(dir_path);
 
33
                        var info = f.query_info("*", FileQueryInfoFlags.NONE);
 
34
 
 
35
                        path = dir_path;
 
36
                        name = info.get_name();
 
37
                        description = "";
 
38
 
 
39
                        date = new DateTime.from_unix_utc(0);
 
40
                        tags = new Gee.ArrayList<string>();
 
41
                        exclude_list = new Gee.ArrayList<string>();
 
42
                        fstab_list = new Gee.ArrayList<FsTabEntry>();
 
43
 
 
44
                        read_control_file();
 
45
                        read_exclude_list();
 
46
                        read_fstab_file();
 
47
                }
 
48
                catch(Error e){
 
49
                        log_error (e.message);
 
50
                }
 
51
        }
 
52
 
 
53
        // manage tags
 
54
        
 
55
        public string taglist{
 
56
                owned get{
 
57
                        string str = "";
 
58
                        foreach(string tag in tags){
 
59
                                str += " " + tag;
 
60
                        }
 
61
                        return str.strip();
 
62
                }
 
63
                set{
 
64
                        tags.clear();
 
65
                        foreach(string tag in value.split(" ")){
 
66
                                if (!tags.contains(tag.strip())){
 
67
                                        tags.add(tag.strip());
 
68
                                }
 
69
                        }
 
70
                }
 
71
        }
 
72
 
 
73
        public string taglist_short{
 
74
                owned get{
 
75
                        string str = "";
 
76
                        foreach(string tag in tags){
 
77
                                str += " " + tag.replace("ondemand","O").replace("boot","B").replace("hourly","H").replace("daily","D").replace("weekly","W").replace("monthly","M");
 
78
                        }
 
79
                        return str.strip();
 
80
                }
 
81
        }
 
82
 
 
83
        public void add_tag(string tag){
 
84
                if (!tags.contains(tag.strip())){
 
85
                        tags.add(tag.strip());
 
86
                        update_control_file();
 
87
                }
 
88
        }
 
89
 
 
90
        public void remove_tag(string tag){
 
91
                if (tags.contains(tag.strip())){
 
92
                        tags.remove(tag.strip());
 
93
                        update_control_file();
 
94
                }
 
95
        }
 
96
 
 
97
        public bool has_tag(string tag){
 
98
                return tags.contains(tag.strip());
 
99
        }
 
100
 
 
101
        // control files
 
102
        
 
103
        public void read_control_file(){
 
104
                string ctl_file = path + "/info.json";
 
105
 
 
106
                var f = File.new_for_path(ctl_file);
 
107
                if (f.query_exists()) {
 
108
                        var parser = new Json.Parser();
 
109
                        try{
 
110
                                parser.load_from_file(ctl_file);
 
111
                        } catch (Error e) {
 
112
                                log_error (e.message);
 
113
                        }
 
114
                        var node = parser.get_root();
 
115
                        var config = node.get_object();
 
116
 
 
117
 
 
118
                        if ((node == null)||(config == null)){
 
119
                                is_valid = false;
 
120
                                return;
 
121
                        }
 
122
 
 
123
                        string val = json_get_string(config,"created","");
 
124
                        if (val.length > 0) {
 
125
                                DateTime date_utc = new DateTime.from_unix_utc(int64.parse(val));
 
126
                                date = date_utc.to_local();
 
127
                        }
 
128
 
 
129
                        sys_uuid = json_get_string(config,"sys-uuid","");
 
130
                        sys_distro = json_get_string(config,"sys-distro","");
 
131
                        taglist = json_get_string(config,"tags","");
 
132
                        description = json_get_string(config,"comments","");
 
133
                        app_version = json_get_string(config,"app-version","");
 
134
                }
 
135
                else{
 
136
                        is_valid = false;
 
137
                }
 
138
        }
 
139
 
 
140
        public void read_exclude_list(){
 
141
                string list_file = path + "/exclude.list";
 
142
 
 
143
                exclude_list.clear();
 
144
 
 
145
                var f = File.new_for_path(list_file);
 
146
                if (f.query_exists()) {
 
147
                        foreach(string path in file_read(list_file).split("\n")){
 
148
                                path = path.strip();
 
149
                                if (!exclude_list.contains(path) && path.length > 0){
 
150
                                        exclude_list.add(path);
 
151
                                }
 
152
                        }
 
153
                }
 
154
                else{
 
155
                        is_valid = false;
 
156
                }
 
157
        }
 
158
 
 
159
        public void read_fstab_file(){
 
160
                string fstab_path = path + "/localhost/etc/fstab";
 
161
                fstab_list = FsTabEntry.read_fstab_file(fstab_path);
 
162
        }
 
163
 
 
164
        public void update_control_file(){
 
165
                /* Updates tag and comments */
 
166
                
 
167
                try{
 
168
                        string ctl_file = path + "/info.json";
 
169
                        var f = File.new_for_path(ctl_file);
 
170
 
 
171
                        if (f.query_exists()) {
 
172
 
 
173
                                var parser = new Json.Parser();
 
174
                                try{
 
175
                                        parser.load_from_file(ctl_file);
 
176
                                } catch (Error e) {
 
177
                                        log_error (e.message);
 
178
                                }
 
179
                                var node = parser.get_root();
 
180
                                var config = node.get_object();
 
181
 
 
182
                                config.set_string_member("tags", taglist);
 
183
                                config.set_string_member("comments", description);
 
184
 
 
185
                                var json = new Json.Generator();
 
186
                                json.pretty = true;
 
187
                                json.indent = 2;
 
188
                                node.set_object(config);
 
189
                                json.set_root(node);
 
190
                                f.delete();
 
191
                                json.to_file(ctl_file);
 
192
                        }
 
193
                } catch (Error e) {
 
194
                        log_error (e.message);
 
195
                }
 
196
        }
 
197
 
 
198
        public void remove_control_file(){
 
199
                string ctl_file = path + "/info.json";
 
200
                file_delete(ctl_file);
 
201
        }
 
202
        
 
203
        public static Snapshot write_control_file(
 
204
                string snapshot_path, DateTime dt_created,
 
205
                string tag, string root_uuid, string distro_full_name){
 
206
                        
 
207
                var ctl_path = snapshot_path + "/info.json";
 
208
                var config = new Json.Object();
 
209
 
 
210
                config.set_string_member("created", dt_created.to_utc().to_unix().to_string());
 
211
                config.set_string_member("sys-uuid", root_uuid);
 
212
                config.set_string_member("sys-distro", distro_full_name);
 
213
                config.set_string_member("app-version", AppVersion);
 
214
                config.set_string_member("tags", tag);
 
215
                config.set_string_member("comments", "");
 
216
 
 
217
                var json = new Json.Generator();
 
218
                json.pretty = true;
 
219
                json.indent = 2;
 
220
                var node = new Json.Node(NodeType.OBJECT);
 
221
                node.set_object(config);
 
222
                json.set_root(node);
 
223
 
 
224
                try{
 
225
                        var f = File.new_for_path(ctl_path);
 
226
                        if (f.query_exists()){
 
227
                                f.delete();
 
228
                        }
 
229
 
 
230
                        json.to_file(ctl_path);
 
231
                } catch (Error e) {
 
232
                log_error (e.message);
 
233
            }
 
234
 
 
235
            return (new Snapshot(snapshot_path));
 
236
        }
 
237
 
 
238
        // check
 
239
        
 
240
        public bool has_subvolumes(){
 
241
                foreach(FsTabEntry en in fstab_list){
 
242
                        if (en.options.contains("subvol=@")){
 
243
                                return true;
 
244
                        }
 
245
                }
 
246
                return false;
 
247
        }
 
248
 
 
249
        // actions
 
250
 
 
251
        public bool remove(){
 
252
                try {
 
253
                        thr_running = true;
 
254
                        thr_success = false;
 
255
                        Thread.create<void> (remove_snapshot_thread, true);
 
256
                } catch (ThreadError e) {
 
257
                        thr_running = false;
 
258
                        thr_success = false;
 
259
                        log_error (e.message);
 
260
                }
 
261
 
 
262
                while (thr_running){
 
263
                        gtk_do_events ();
 
264
                        Thread.usleep((ulong) GLib.TimeSpan.MILLISECOND * 500);
 
265
                }
 
266
 
 
267
                return thr_success;
 
268
        }
 
269
 
 
270
        private void remove_snapshot_thread(){
 
271
                string cmd = "";
 
272
                string std_out;
 
273
                string std_err;
 
274
                int ret_val;
 
275
 
 
276
                log_msg(_("Removing snapshot") + " '%s'...".printf(name));
 
277
 
 
278
                try{
 
279
                        var f = File.new_for_path(path);
 
280
                        if(f.query_exists()){
 
281
                                cmd = "rm -rf \"%s\"".printf(path);
 
282
 
 
283
                                if (LOG_COMMANDS) { log_debug(cmd); }
 
284
 
 
285
                                Process.spawn_command_line_sync(cmd, out std_out, out std_err, out ret_val);
 
286
 
 
287
                                if (ret_val != 0){
 
288
                                        log_error(_("Failed to remove") + ": '%s'".printf(path));
 
289
                                        thr_success = false;
 
290
                                        thr_running = false;
 
291
                                        return;
 
292
                                }
 
293
                                else{
 
294
                                        log_msg(_("Removed") + ": '%s'".printf(path));
 
295
                                        thr_success = true;
 
296
                                        thr_running = false;
 
297
                                        return;
 
298
                                }
 
299
                        }
 
300
                        else{
 
301
                                log_error(_("Directory not found") + ": '%s'".printf(path));
 
302
                                thr_success = true;
 
303
                                thr_running = false;
 
304
                        }
 
305
                }
 
306
                catch(Error e){
 
307
                        log_error (e.message);
 
308
                        thr_success = false;
 
309
                        thr_running = false;
 
310
                        return;
 
311
                }
 
312
        }
 
313
 
 
314
}