Skip to content
API Documentation
BastionGuard™
CryptTabEntry.vala
Go to the documentation of this file.
1/*
2 * CryptTabEntry.vala
3 *
4 * Copyright 2016 Tony George <teejeetech@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
25using TeeJee.Logging;
30using TeeJee.System;
31using TeeJee.Misc;
32
33public class CryptTabEntry : GLib.Object{
34
35 public bool is_comment = false;
36 public bool is_empty_line = false;
37
38 // fields
39 public string mapped_name = "";
40 public string device_string = "";
41 public string keyfile = "none";
42 public string options = "luks,nofail";
43 public string line = "";
44
45 public string device_uuid {
46 owned get{
47 if (device_string.down().has_prefix("uuid=")){
48 return device_string.replace("\"","").replace("'","").split("=")[1];
49 }
50 else{
51 return "";
52 }
53 }
54 set {
55 device_string = "UUID=%s".printf(value);
56 }
57 }
58
59 public static Gee.ArrayList<CryptTabEntry> read_file(string file_path){
60
61 var list = new Gee.ArrayList<CryptTabEntry>();
62
63 if (!file_exists(file_path)){ return list; }
64
65 string text = file_read(file_path);
66 string[] lines = text.split("\n");
67 foreach(string line in lines){
68 var entry = new CryptTabEntry();
69 list.add(entry);
70
71 entry.is_comment = line.strip().has_prefix("#");
72 entry.is_empty_line = (line.strip().length == 0);
73
74 if (entry.is_comment){
75 entry.line = line;
76 }
77 else if (entry.is_empty_line){
78 entry.line = "";
79 }
80 else{
81 entry.line = line;
82
83 string[] parts = line.replace("\t"," ").split(" ");
84 int part_num = -1;
85 foreach(string part in parts){
86 if (part.strip().length == 0) { continue; }
87 switch (++part_num){
88 case 0:
89 entry.mapped_name = part.strip();
90 break;
91 case 1:
92 entry.device_string = part.strip();
93 break;
94 case 2:
95 entry.keyfile = part.strip();
96 break;
97 case 3:
98 entry.options = part.strip();
99 break;
100 }
101 }
102 }
103 }
104
105 return list;
106 }
107
108 public static string write_file(Gee.ArrayList<CryptTabEntry> entries, string file_path, bool keep_comments_and_empty_lines = true){
109
110 string text = "";
111 foreach(var entry in entries){
112 if (entry.is_comment || entry.is_empty_line){
113 if (keep_comments_and_empty_lines){
114 text += "%s\n".printf(entry.line);
115 }
116 }
117 else {
118 text += "%s\t%s\t%s\t%s\n".printf(
119 entry.mapped_name, entry.device_string,
120 entry.keyfile, entry.options);
121 }
122 }
123
124 if (file_exists(file_path)){
125 file_delete(file_path);
126 }
127
128 file_write(file_path, text);
129
130 return text;
131 }
132
133 public void append_option(string option){
134
135 if (!options.contains(option)){
136 options += ",%s".printf(option);
137 }
138
139 if(options.has_prefix(",")){
140 options = options[1:options.length];
141 }
142
143 options = options.strip();
144 }
145
146 public void remove_option(string option){
147
148 options = options.replace(option,"").strip();
149
150 if(options.has_prefix(",")){
151 options = options[1:options.length];
152 }
153
154 if (options.has_suffix(",")){
155 options = options[0:options.length - 1];
156 }
157
158 options = options.strip();
159 }
160
161 public static CryptTabEntry? find_entry_by_uuid(
162 Gee.ArrayList<CryptTabEntry> entries, string uuid){
163
164 foreach(var entry in entries){
165 if (entry.device_uuid == uuid){
166 return entry;
167 }
168 }
169
170 return null;
171 }
172}
bool file_exists(string file_path)
string? file_read(string file_path)
bool file_write(string file_path, string contents)
bool file_delete(string file_path)