summaryrefslogtreecommitdiffstats
path: root/libraries/systeminfo/src/distroutils.cpp
blob: cdba05d0a83d02ab602a0a65556aa617fbf67a20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*

Code has been taken from https://github.com/natefoo/lionshead and loosely
translated to C++ laced with Qt.

MIT License

Copyright (c) 2017 Nate Coraor

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

*/

#include "distroutils.h"

#include <QStringList>
#include <QMap>
#include <QSettings>
#include <QFile>
#include <QProcess>
#include <QDebug>
#include <QDir>

#include <functional>

Sys::DistributionInfo Sys::read_os_release()
{
	Sys::DistributionInfo out;
	QStringList files = { "/etc/os-release", "/usr/lib/os-release" };
	QString name;
	QString version;
	for (auto &file: files)
	{
		if(!QFile::exists(file))
		{
			continue;
		}
		QSettings settings(file, QSettings::IniFormat);
		if(settings.contains("ID"))
		{
			name = settings.value("ID").toString().toLower();
		}
		else if (settings.contains("NAME"))
		{
			name = settings.value("NAME").toString().toLower();
		}
		else
		{
			continue;
		}

		if(settings.contains("VERSION_ID"))
		{
			version = settings.value("VERSION_ID").toString().toLower();
		}
		else if(settings.contains("VERSION"))
		{
			version = settings.value("VERSION").toString().toLower();
		}
		break;
	}
	if(name.isEmpty())
	{
		return out;
	}
	out.distributionName = name;
	out.distributionVersion = version;
	return out;
}

bool Sys::main_lsb_info(Sys::LsbInfo & out)
{
	int status=0;
	QProcess lsbProcess;
	lsbProcess.start("lsb_release -a");
	lsbProcess.waitForFinished();
	status = lsbProcess.exitStatus();
	QString output = lsbProcess.readAllStandardOutput();
	qDebug() << output;
	lsbProcess.close();
	if(status == 0)
	{
		auto lines = output.split('\n');
		for(auto line:lines)
		{
			int index = line.indexOf(':');
			auto key = line.left(index).trimmed();
			auto value = line.mid(index + 1).toLower().trimmed();
			if(key == "Distributor ID")
				out.distributor = value;
			else if(key == "Release")
				out.version = value;
			else if(key == "Description")
				out.description = value;
			else if(key == "Codename")
				out.codename = value;
		}
		return !out.distributor.isEmpty();
	}
	return false;
}

bool Sys::fallback_lsb_info(Sys::LsbInfo & out)
{
	// running lsb_release failed, try to read the file instead
	// /etc/lsb-release format, if the file even exists, is non-standard.
	// Only the `lsb_release` command is specified by LSB. Nonetheless, some
	// distributions install an /etc/lsb-release as part of the base
	// distribution, but `lsb_release` remains optional.
	QString file = "/etc/lsb-release";
	if (QFile::exists(file))
	{
		QSettings settings(file, QSettings::IniFormat);
		if(settings.contains("DISTRIB_ID"))
		{
			out.distributor = settings.value("DISTRIB_ID").toString().toLower();
		}
		if(settings.contains("DISTRIB_RELEASE"))
		{
			out.version = settings.value("DISTRIB_RELEASE").toString().toLower();
		}
		return !out.distributor.isEmpty();
	}
	return false;
}

void Sys::lsb_postprocess(Sys::LsbInfo & lsb, Sys::DistributionInfo & out)
{
	QString dist = lsb.distributor;
	QString vers = lsb.version;
	if(dist.startsWith("redhatenterprise"))
	{
		dist = "rhel";
	}
	else if(dist == "archlinux")
	{
		dist = "arch";
	}
	else if (dist.startsWith("suse"))
	{
		if(lsb.description.startsWith("opensuse"))
		{
			dist = "opensuse";
		}
		else if (lsb.description.startsWith("suse linux enterprise"))
		{
			dist = "sles";
		}
	}
	else if (dist == "debian" and vers == "testing")
	{
		vers = lsb.codename;
	}
	else
	{
		// ubuntu, debian, gentoo, scientific, slackware, ... ?
		auto parts = dist.split(QRegExp("\\s+"), QString::SkipEmptyParts);
		if(parts.size())
		{
			dist = parts[0];
		}
	}
	if(!dist.isEmpty())
	{
		out.distributionName = dist;
		out.distributionVersion = vers;
	}
}

Sys::DistributionInfo Sys::read_lsb_release()
{
	LsbInfo lsb;
	if(!main_lsb_info(lsb))
	{
		if(!fallback_lsb_info(lsb))
		{
			return Sys::DistributionInfo();
		}
	}
	Sys::DistributionInfo out;
	lsb_postprocess(lsb, out);
	return out;
}

QString Sys::_extract_distribution(const QString & x)
{
	QString release = x.toLower();
	if (release.startsWith("red hat enterprise"))
	{
		return "rhel";
	}
	if (release.startsWith("suse linux enterprise"))
	{
		return "sles";
	}
	QStringList list = release.split(QRegExp("\\s+"), QString::SkipEmptyParts);
	if(list.size())
	{
		return list[0];
	}
	return QString();
}

QString Sys::_extract_version(const QString & x)
{
	QRegExp versionish_string("\\d+(?:\\.\\d+)*$");
	QStringList list = x.split(QRegExp("\\s+"), QString::SkipEmptyParts);
	for(int i = list.size() - 1; i >= 0; --i)
	{
		QString chunk = list[i];
		if(versionish_string.exactMatch(chunk))
		{
			return chunk;
		}
	}
	return QString();
}

Sys::DistributionInfo Sys::read_legacy_release()
{
	struct checkEntry
	{
		QString file;
		std::function<QString(const QString &)> extract_distro;
		std::function<QString(const QString &)> extract_version;
	};
	QList<checkEntry> checks =
	{
		{"/etc/arch-release", [](const QString &){ return "arch";}, [](const QString &){ return "rolling";}},
		{"/etc/slackware-version", &Sys::_extract_distribution, &Sys::_extract_version},
		{QString(), &Sys::_extract_distribution, &Sys::_extract_version},
		{"/etc/debian_version", [](const QString &){ return "debian";}, [](const QString & x){ return x;}},
	};
	for(auto & check: checks)
	{
		QStringList files;
		if(check.file.isNull())
		{
			QDir etcDir("/etc");
			etcDir.setNameFilters({"*-release"});
			etcDir.setFilter(QDir::Files | QDir::NoDot | QDir::NoDotDot | QDir::Readable | QDir::Hidden);
			files = etcDir.entryList();
		}
		else
		{
			files.append(check.file);
		}
		for (auto file : files)
		{
			QFile relfile(file);
			if(!relfile.open(QIODevice::ReadOnly | QIODevice::Text))
				continue;
			QString contents = QString::fromUtf8(relfile.readLine()).trimmed();
			QString dist = check.extract_distro(contents);
			QString vers = check.extract_version(contents);
			if(!dist.isEmpty())
			{
				Sys::DistributionInfo out;
				out.distributionName = dist;
				out.distributionVersion = vers;
				return out;
			}
		}
	}
	return Sys::DistributionInfo();
}