~teejee2008/timeshift/trunk

« back to all changes in this revision

Viewing changes to src/Core/CryptTabEntry.vala

  • Committer: Tony George
  • Date: 2017-01-26 06:10:54 UTC
  • Revision ID: tony.george.kol@gmail.com-20170126061054-n4ny40gxcktrzqe4
Code cleanup

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
using TeeJee.Logging;
2
 
using TeeJee.FileSystem;
3
 
using TeeJee.JsonHelper;
4
 
using TeeJee.ProcessHelper;
5
 
using TeeJee.GtkHelper;
6
 
using TeeJee.System;
7
 
using TeeJee.Misc;
8
 
 
9
 
public class CryptTabEntry : GLib.Object{
10
 
        public bool is_comment = false;
11
 
        public bool is_empty_line = false;
12
 
 
13
 
        // fields
14
 
        public string mapped_name = "";
15
 
        public string device_string = "";
16
 
        public string keyfile = "none";
17
 
        public string options = "luks,nofail";
18
 
        public string line = "";
19
 
 
20
 
        public string device_uuid {
21
 
                owned get{
22
 
                        if (device_string.down().has_prefix("uuid=")){
23
 
                                return device_string.replace("\"","").replace("'","").split("=")[1];
24
 
                        }
25
 
                        else{
26
 
                                return "";
27
 
                        }
28
 
                }
29
 
                set {
30
 
                        device_string = "UUID=%s".printf(value);
31
 
                }
32
 
        }
33
 
 
34
 
        public static Gee.ArrayList<CryptTabEntry> read_file(string file_path){
35
 
                var list = new Gee.ArrayList<CryptTabEntry>();
36
 
 
37
 
                if (!file_exists(file_path)){ return list; }
38
 
 
39
 
                string text = file_read(file_path);
40
 
                string[] lines = text.split("\n");
41
 
                foreach(string line in lines){
42
 
                        var entry = new CryptTabEntry();
43
 
                        list.add(entry);
44
 
 
45
 
                        entry.is_comment = line.strip().has_prefix("#");
46
 
                        entry.is_empty_line = (line.strip().length == 0);
47
 
 
48
 
                        if (entry.is_comment){
49
 
                                entry.line = line;
50
 
                        }
51
 
                        else if (entry.is_empty_line){
52
 
                                entry.line = "";
53
 
                        }
54
 
                        else{
55
 
                                entry.line = line;
56
 
 
57
 
                                string[] parts = line.replace("\t"," ").split(" ");
58
 
                                int part_num = -1;
59
 
                                foreach(string part in parts){
60
 
                                        if (part.strip().length == 0) { continue; }
61
 
                                        switch (++part_num){
62
 
                                                case 0:
63
 
                                                        entry.mapped_name = part.strip();
64
 
                                                        break;
65
 
                                                case 1:
66
 
                                                        entry.device_string = part.strip();
67
 
                                                        break;
68
 
                                                case 2:
69
 
                                                        entry.keyfile = part.strip();
70
 
                                                        break;
71
 
                                                case 3:
72
 
                                                        entry.options = part.strip();
73
 
                                                        break;
74
 
                                        }
75
 
                                }
76
 
                        }
77
 
                }
78
 
 
79
 
                return list;
80
 
        }
81
 
 
82
 
        public static string write_file(
83
 
                Gee.ArrayList<CryptTabEntry> entries, string file_path,
84
 
                bool keep_comments_and_empty_lines = true){
85
 
                        
86
 
                string text = "";
87
 
                foreach(var entry in entries){
88
 
                        if (entry.is_comment || entry.is_empty_line){
89
 
                                if (keep_comments_and_empty_lines){
90
 
                                        text += "%s\n".printf(entry.line);
91
 
                                }
92
 
                        }
93
 
                        else {
94
 
                                text += "%s\t%s\t%s\t%s\n".printf(
95
 
                                        entry.mapped_name, entry.device_string,
96
 
                                        entry.keyfile, entry.options);
97
 
                        }
98
 
                }
99
 
 
100
 
                if (file_exists(file_path)){
101
 
                        file_delete(file_path);
102
 
                }
103
 
                
104
 
                file_write(file_path, text);
105
 
                
106
 
                return text;
107
 
        }
108
 
 
109
 
        public void append_option(string option){
110
 
                
111
 
                if (!options.contains(option)){
112
 
                        options += ",%s".printf(option);
113
 
                }
114
 
                
115
 
                if(options.has_prefix(",")){
116
 
                        options = options[1:options.length];
117
 
                }
118
 
                
119
 
                options = options.strip();
120
 
        }
121
 
 
122
 
        public void remove_option(string option){
123
 
                
124
 
                options = options.replace(option,"").strip();
125
 
                                        
126
 
                if(options.has_prefix(",")){
127
 
                        options = options[1:options.length];
128
 
                }
129
 
 
130
 
                if (options.has_suffix(",")){
131
 
                        options = options[0:options.length - 1];
132
 
                }
133
 
 
134
 
                options = options.strip();
135
 
        }
136
 
 
137
 
        public static CryptTabEntry? find_entry_by_uuid(
138
 
                Gee.ArrayList<CryptTabEntry> entries, string uuid){
139
 
                        
140
 
                foreach(var entry in entries){
141
 
                        if (entry.device_uuid == uuid){
142
 
                                return entry;
143
 
                        }
144
 
                }
145
 
                
146
 
                return null;
147
 
        }
148
 
}