Skip to content
API Documentation
BastionGuard™
LinuxDistro.vala
Go to the documentation of this file.
1
2/*
3 * LinuxDistro.vala
4 *
5 * Copyright 2012-2018 Tony George <teejeetech@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 * MA 02110-1301, USA.
21 *
22 *
23 */
24
25using TeeJee.Logging;
28
29public class LinuxDistro : GLib.Object{
30
31 /* Class for storing information about Linux distribution */
32
33 public string dist_id = "";
34 public string description = "";
35 public string release = "";
36 public string codename = "";
37
38 public LinuxDistro(){
39 dist_id = "";
40 description = "";
41 release = "";
42 codename = "";
43 }
44
45 public string full_name(){
46
47 if (dist_id == ""){
48 return "";
49 }
50 else{
51 string val = "";
52 val += dist_id;
53 val += (release.length > 0) ? " " + release : "";
54 val += (codename.length > 0) ? " (" + codename + ")" : "";
55 return val;
56 }
57 }
58
59 public static LinuxDistro get_dist_info(string root_path){
60
61 /* Returns information about the Linux distribution
62 * installed at the given root path */
63
64
65 /*
66 try to read from /etc/lsb-release
67 example content:
68
69 DISTRIB_ID=Ubuntu
70 DISTRIB_RELEASE=13.04
71 DISTRIB_CODENAME=raring
72 DISTRIB_DESCRIPTION="Ubuntu 13.04"
73 */
74 LinuxDistro? info = read_info_file(root_path + "/etc/lsb-release");
75 if(info != null) {
76 return info;
77 }
78
79 /*
80 fallback to /etc/os-release
81 example content:
82
83 NAME="Ubuntu"
84 VERSION="13.04, Raring Ringtail"
85 ID=ubuntu
86 ID_LIKE=debian
87 PRETTY_NAME="Ubuntu 13.04"
88 VERSION_ID="13.04"
89 HOME_URL="http://www.ubuntu.com/"
90 SUPPORT_URL="http://help.ubuntu.com/"
91 BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
92 */
93 return read_info_file(root_path + "/etc/os-release") ?? new LinuxDistro();
94 }
95
96 // read a generic info file like /etc/os-release or /etc/lsb-release
97 private static LinuxDistro? read_info_file(string file_path) {
98 string dist_file_cont;
99 try {
100 GLib.FileUtils.get_contents(file_path, out dist_file_cont);
101 } catch (Error e) {
102 return null;
103 }
104
105 if(dist_file_cont.length == 0) {
106 return null;
107 }
108
109 LinuxDistro info = new LinuxDistro();
110 string[] lines = dist_file_cont.split("\n");
111
112 foreach(string line in lines){
113 // split for 3 to detect if there are to many
114 string[] linesplit = line.split("=", 3);
115
116 if (linesplit.length != 2){ continue; }
117
118 string key = linesplit[0].strip();
119 string val = linesplit[1].strip();
120
121 // removing leading "
122 if (val.has_prefix("\"")) {
123 val = val[1:val.length];
124 }
125
126 // remove trailing "
127 if (val.has_suffix("\"")) {
128 val = val[0:val.length-1];
129 }
130
131 switch (key) {
132 case "ID":
133 case "DISTRIB_ID":
134 info.dist_id = val;
135 break;
136 case "VERSION_ID":
137 case "DISTRIB_RELEASE":
138 info.release = val;
139 break;
140 case "VERSION_CODENAME":
141 case "DISTRIB_CODENAME":
142 info.codename = val;
143 break;
144 case "PRETTY_NAME":
145 case "DISTRIB_DESCRIPTION":
146 info.description = val;
147 break;
148 }
149 }
150 return info;
151 }
152
153 public string dist_type {
154
155 owned get{
156
157 if (dist_id.down() in "fedora rhel rocky centos almalinux"){
158 return "redhat";
159 }
160 else if (dist_id.down().contains("manjaro") || dist_id.down().contains("arch")){
161 return "arch";
162 }
163 else if (dist_id.down().contains("ubuntu") || dist_id.down().contains("debian")){
164 return "debian";
165 }
166 else{
167 return "";
168 }
169
170 }
171 }
172}
173
174
string full_name()
string codename
string description
static LinuxDistro get_dist_info(string root_path)
static ? LinuxDistro read_info_file(string file_path)