pamac-classic/src/alpm_config.vala

400 lines
13 KiB
Vala
Raw Normal View History

2014-10-22 13:44:02 -03:00
/*
* pamac-vala
*
2015-01-03 17:59:25 -03:00
* Copyright (C) 2014, 2015 Guillaume Benoit <guillaume@manjaro.org>
2014-10-22 13:44:02 -03:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a get of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Alpm {
class Repo {
public string name;
2014-12-03 12:02:14 -03:00
public Signature.Level siglevel;
public DB.Usage usage;
2014-10-22 13:44:02 -03:00
public string[] urls;
public Repo (string name) {
this.name = name;
2015-01-02 17:39:32 -03:00
siglevel = Signature.Level.USE_DEFAULT;
2014-12-03 12:02:14 -03:00
usage = 0;
2014-10-22 13:44:02 -03:00
urls = {};
}
}
public class Config {
2014-12-03 12:02:14 -03:00
string conf_path;
2014-10-30 10:44:09 -03:00
string rootdir;
string dbpath;
string gpgdir;
string logfile;
string arch;
double deltaratio;
int usesyslog;
public int checkspace;
2015-01-03 15:32:10 -03:00
Alpm.List<string> *cachedirs;
Alpm.List<string> *ignoregroups;
public string ignorepkg;
2015-01-03 15:32:10 -03:00
Alpm.List<string> *ignorepkgs;
Alpm.List<string> *noextracts;
Alpm.List<string> *noupgrades;
public GLib.List<string> holdpkgs;
public GLib.List<string> syncfirsts;
public string syncfirst;
2014-12-03 12:02:14 -03:00
Signature.Level defaultsiglevel;
Signature.Level localfilesiglevel;
Signature.Level remotefilesiglevel;
2014-10-22 13:44:02 -03:00
Repo[] repo_order;
2014-12-03 12:02:14 -03:00
public unowned Handle? handle;
2014-10-22 13:44:02 -03:00
public Config (string path) {
2014-12-03 12:02:14 -03:00
conf_path = path;
handle = null;
reload ();
}
public void reload () {
// set default options
2014-10-30 10:44:09 -03:00
rootdir = "/";
dbpath = "/var/lib/pacman";
gpgdir = "/etc/pacman.d/gnupg/";
logfile = "/var/log/pacman.log";
2015-01-03 15:32:10 -03:00
string default_cachedir = "/var/cache/pacman/pkg/";
2014-10-30 10:44:09 -03:00
arch = Posix.utsname().machine;
holdpkgs = new GLib.List<string> ();
syncfirsts = new GLib.List<string> ();
syncfirst = "";
2015-01-03 15:32:10 -03:00
Alpm.List.free_all (cachedirs);
Alpm.List.free_all (ignoregroups);
Alpm.List.free_all (ignorepkgs);
Alpm.List.free_all (noextracts);
Alpm.List.free_all (noupgrades);
cachedirs = new Alpm.List<string> ();
2015-01-03 15:32:10 -03:00
cachedirs->add_str (default_cachedir);
2015-01-03 13:26:47 -03:00
ignoregroups = new Alpm.List<string> ();
ignorepkgs = new Alpm.List<string> ();
ignorepkg = "";
noextracts = new Alpm.List<string> ();
noupgrades = new Alpm.List<string> ();
2014-10-30 10:44:09 -03:00
usesyslog = 0;
checkspace = 0;
deltaratio = 0.7;
2014-12-03 12:02:14 -03:00
defaultsiglevel = Signature.Level.PACKAGE | Signature.Level.PACKAGE_OPTIONAL | Signature.Level.DATABASE | Signature.Level.DATABASE_OPTIONAL;
localfilesiglevel = Signature.Level.USE_DEFAULT;
remotefilesiglevel = Signature.Level.USE_DEFAULT;
2014-10-22 13:44:02 -03:00
repo_order = {};
// parse conf file
2014-12-03 12:02:14 -03:00
parse_file (conf_path);
get_handle ();
2014-10-30 10:44:09 -03:00
}
2014-12-03 12:02:14 -03:00
public void get_handle () {
2014-10-30 10:44:09 -03:00
Alpm.Errno error;
2014-12-03 12:02:14 -03:00
if (handle != null)
Handle.release (handle);
handle = Handle.new (rootdir, dbpath, out error);
2014-10-30 10:44:09 -03:00
if (handle == null) {
stderr.printf ("Failed to initialize alpm library" + " (%s)\n".printf(Alpm.strerror (error)));
2014-12-03 12:02:14 -03:00
return;
2014-10-30 10:44:09 -03:00
}
// define options
handle.gpgdir = gpgdir;
handle.logfile = logfile;
handle.arch = arch;
handle.deltaratio = deltaratio;
handle.usesyslog = usesyslog;
handle.checkspace = checkspace;
handle.defaultsiglevel = defaultsiglevel;
2015-01-02 17:39:32 -03:00
localfilesiglevel = merge_siglevel (defaultsiglevel, localfilesiglevel);
remotefilesiglevel = merge_siglevel (defaultsiglevel, remotefilesiglevel);
2014-10-30 10:44:09 -03:00
handle.localfilesiglevel = localfilesiglevel;
handle.remotefilesiglevel = remotefilesiglevel;
handle.cachedirs = cachedirs;
2015-01-03 13:26:47 -03:00
handle.ignoregroups = ignoregroups;
handle.ignorepkgs = ignorepkgs;
handle.noextracts = noextracts;
handle.noupgrades = noupgrades;
2014-10-22 13:44:02 -03:00
// register dbs
foreach (var repo in repo_order) {
2014-10-22 13:44:02 -03:00
unowned DB db = handle.register_syncdb (repo.name, repo.siglevel);
foreach (var url in repo.urls)
2014-10-22 13:44:02 -03:00
db.add_server (url.replace ("$repo", repo.name).replace ("$arch", handle.arch));
2014-12-03 12:02:14 -03:00
if (repo.usage == 0)
db.usage = DB.Usage.ALL;
else
db.usage = repo.usage;
2014-10-22 13:44:02 -03:00
}
}
public void parse_file (string path, string? section = null) {
string? current_section = section;
2014-10-22 13:44:02 -03:00
var file = GLib.File.new_for_path (path);
if (file.query_exists () == false) {
GLib.stderr.printf ("File '%s' doesn't exist.\n", path);
} else {
2014-10-22 13:44:02 -03:00
try {
// Open file for reading and wrap returned FileInputStream into a
// DataInputStream, so we can read line by line
var dis = new DataInputStream (file.read ());
string line;
// Read lines until end of file (null) is reached
while ((line = dis.read_line (null)) != null) {
if (line.length == 0) continue;
// ignore whole line and end of line comments
string[] splitted = line.split ("#", 2);
line = splitted[0].strip ();
if (line.length == 0) continue;
2014-10-22 13:44:02 -03:00
if (line[0] == '[' && line[line.length-1] == ']') {
current_section = line[1:-1];
if (current_section != "options") {
var repo = new Repo (current_section);
2014-10-22 13:44:02 -03:00
repo_order += repo;
}
continue;
}
splitted = line.split ("=", 2);
2014-10-22 13:44:02 -03:00
string _key = splitted[0].strip ();
string? _value = null;
2014-10-22 13:44:02 -03:00
if (splitted[1] != null)
_value = splitted[1].strip ();
if (_key == "Include")
parse_file (_value, current_section);
if (current_section == "options") {
if (_key == "GPGDir")
2014-10-30 10:44:09 -03:00
gpgdir = _value;
2014-10-22 13:44:02 -03:00
else if (_key == "LogFile")
2014-10-30 10:44:09 -03:00
logfile = _value;
2014-10-22 13:44:02 -03:00
else if (_key == "Architecture") {
if (_value == "auto")
2014-10-30 10:44:09 -03:00
arch = Posix.utsname ().machine;
2014-10-22 13:44:02 -03:00
else
2014-10-30 10:44:09 -03:00
arch = _value;
2014-10-22 13:44:02 -03:00
} else if (_key == "UseDelta")
2014-10-30 10:44:09 -03:00
deltaratio = double.parse (_value);
2014-10-22 13:44:02 -03:00
else if (_key == "UseSysLog")
2014-10-30 10:44:09 -03:00
usesyslog = 1;
2014-10-22 13:44:02 -03:00
else if (_key == "CheckSpace")
2014-10-30 10:44:09 -03:00
checkspace = 1;
2014-10-22 13:44:02 -03:00
else if (_key == "SigLevel")
2014-10-30 10:44:09 -03:00
defaultsiglevel = define_siglevel (defaultsiglevel, _value);
2015-01-02 17:39:32 -03:00
else if (_key == "LocalFileSigLevel")
localfilesiglevel = define_siglevel (localfilesiglevel, _value);
else if (_key == "RemoteFileSigLevel")
remotefilesiglevel = define_siglevel (remotefilesiglevel, _value);
2014-10-22 13:44:02 -03:00
else if (_key == "HoldPkg") {
foreach (string name in _value.split (" "))
holdpkgs.append (name);
2014-10-22 13:44:02 -03:00
} else if (_key == "SyncFirst") {
syncfirst = _value;
foreach (string name in _value.split (" "))
syncfirsts.append (name);
2014-10-22 13:44:02 -03:00
} else if (_key == "CacheDir") {
foreach (string dir in _value.split (" "))
2015-01-03 15:32:10 -03:00
cachedirs->add_str (dir);
2014-10-22 13:44:02 -03:00
} else if (_key == "IgnoreGroup") {
foreach (string name in _value.split (" "))
2015-01-03 15:32:10 -03:00
ignoregroups->add_str (name);
2014-10-22 13:44:02 -03:00
} else if (_key == "IgnorePkg") {
ignorepkg = _value;
foreach (string name in _value.split (" "))
2015-01-03 15:32:10 -03:00
ignorepkgs->add_str (name);
2014-10-22 13:44:02 -03:00
} else if (_key == "Noextract") {
foreach (string name in _value.split (" "))
2015-01-03 15:32:10 -03:00
noextracts->add_str (name);
2014-10-22 13:44:02 -03:00
} else if (_key == "NoUpgrade") {
foreach (string name in _value.split (" "))
2015-01-03 15:32:10 -03:00
noupgrades->add_str (name);
2014-10-22 13:44:02 -03:00
}
} else {
foreach (var repo in repo_order) {
if (repo.name == current_section) {
2014-10-22 13:44:02 -03:00
if (_key == "Server")
repo.urls += _value;
2015-01-02 17:39:32 -03:00
else if (_key == "SigLevel") {
if (repo.siglevel == Signature.Level.USE_DEFAULT)
repo.siglevel = defaultsiglevel;
repo.siglevel = define_siglevel (repo.siglevel, _value);
} else if (_key == "Usage")
repo.usage = define_usage (_value);
2014-10-22 13:44:02 -03:00
}
}
}
}
} catch (GLib.Error e) {
GLib.stderr.printf("%s\n", e.message);
}
}
}
public void write (HashTable<string,Variant> new_conf) {
var file = GLib.File.new_for_path (conf_path);
if (file.query_exists () == false)
GLib.stderr.printf ("File '%s' doesn't exist.\n", conf_path);
else {
try {
// Open file for reading and wrap returned FileInputStream into a
// DataInputStream, so we can read line by line
var dis = new DataInputStream (file.read ());
string line;
string[] data = {};
// Read lines until end of file (null) is reached
while ((line = dis.read_line (null)) != null) {
if (line.length == 0) {
data += "\n";
continue;
}
if (line.contains ("IgnorePkg")) {
if (new_conf.contains ("IgnorePkg")) {
string _value = new_conf.get ("IgnorePkg").get_string ();
if (_value == "")
data += "#IgnorePkg =\n";
else
data += "IgnorePkg = %s\n".printf (_value);
} else
data += line + "\n";
} else if (line.contains ("SyncFirst")) {
if (new_conf.contains ("SyncFirst")) {
string _value = new_conf.get ("SyncFirst").get_string ();
if (_value == "")
data += "#SyncFirst =\n";
else
data += "SyncFirst = %s\n".printf (_value);
} else
data += line + "\n";
} else if (line.contains ("CheckSpace")) {
if (new_conf.contains ("CheckSpace")) {
int _value = new_conf.get ("CheckSpace").get_int32 ();
if (_value == 1)
data += "CheckSpace\n";
else
data += "#CheckSpace\n";
} else
data += line + "\n";
} else
data += line + "\n";
}
// delete the file before rewrite it
file.delete ();
// creating a DataOutputStream to the file
var dos = new DataOutputStream (file.create (FileCreateFlags.REPLACE_DESTINATION));
foreach (string new_line in data) {
// writing a short string to the stream
dos.put_string (new_line);
}
} catch (GLib.Error e) {
GLib.stderr.printf("%s\n", e.message);
}
2014-12-03 12:02:14 -03:00
}
}
public DB.Usage define_usage (string conf_string) {
DB.Usage usage = 0;
foreach (string directive in conf_string.split(" ")) {
if (directive == "Sync") {
usage |= DB.Usage.SYNC;
} else if (directive == "Search") {
usage |= DB.Usage.SEARCH;
} else if (directive == "Install") {
usage |= DB.Usage.INSTALL;
} else if (directive == "Upgrade") {
usage |= DB.Usage.UPGRADE;
} else if (directive == "All") {
usage |= DB.Usage.ALL;
}
2014-10-22 13:44:02 -03:00
}
2014-12-03 12:02:14 -03:00
return usage;
2014-10-22 13:44:02 -03:00
}
2014-12-03 12:02:14 -03:00
public Signature.Level define_siglevel (Signature.Level default_level, string conf_string) {
2014-10-22 13:44:02 -03:00
foreach (string directive in conf_string.split(" ")) {
bool affect_package = false;
bool affect_database = false;
2015-01-02 17:39:32 -03:00
if ("Package" in directive)
affect_package = true;
else if ("Database" in directive)
affect_database = true;
2014-10-22 13:44:02 -03:00
else {
affect_package = true;
affect_database = true;
}
if ("Never" in directive) {
if (affect_package) {
2014-12-03 12:02:14 -03:00
default_level &= ~Signature.Level.PACKAGE;
default_level |= Signature.Level.PACKAGE_SET;
2014-10-22 13:44:02 -03:00
}
2015-01-02 17:39:32 -03:00
if (affect_database)
default_level &= ~Signature.Level.DATABASE;
} else if ("Optional" in directive) {
2014-10-22 13:44:02 -03:00
if (affect_package) {
2014-12-03 12:02:14 -03:00
default_level |= Signature.Level.PACKAGE;
default_level |= Signature.Level.PACKAGE_OPTIONAL;
default_level |= Signature.Level.PACKAGE_SET;
2014-10-22 13:44:02 -03:00
}
if (affect_database) {
2014-12-03 12:02:14 -03:00
default_level |= Signature.Level.DATABASE;
default_level |= Signature.Level.DATABASE_OPTIONAL;
2014-10-22 13:44:02 -03:00
}
2015-01-02 17:39:32 -03:00
} else if ("Required" in directive) {
2014-10-22 13:44:02 -03:00
if (affect_package) {
2014-12-03 12:02:14 -03:00
default_level |= Signature.Level.PACKAGE;
default_level &= ~Signature.Level.PACKAGE_OPTIONAL;
default_level |= Signature.Level.PACKAGE_SET;
2014-10-22 13:44:02 -03:00
}
if (affect_database) {
2014-12-03 12:02:14 -03:00
default_level |= Signature.Level.DATABASE;
default_level &= ~Signature.Level.DATABASE_OPTIONAL;
2014-10-22 13:44:02 -03:00
}
2015-01-02 17:39:32 -03:00
} else if ("TrustedOnly" in directive) {
2014-10-22 13:44:02 -03:00
if (affect_package) {
2014-12-03 12:02:14 -03:00
default_level &= ~Signature.Level.PACKAGE_MARGINAL_OK;
default_level &= ~Signature.Level.PACKAGE_UNKNOWN_OK;
default_level |= Signature.Level.PACKAGE_TRUST_SET;
2014-10-22 13:44:02 -03:00
}
if (affect_database) {
2014-12-03 12:02:14 -03:00
default_level &= ~Signature.Level.DATABASE_MARGINAL_OK;
default_level &= ~Signature.Level.DATABASE_UNKNOWN_OK;
2014-10-22 13:44:02 -03:00
}
2015-01-02 17:39:32 -03:00
} else if ("TrustAll" in directive) {
2014-10-22 13:44:02 -03:00
if (affect_package) {
2014-12-03 12:02:14 -03:00
default_level |= Signature.Level.PACKAGE_MARGINAL_OK;
default_level |= Signature.Level.PACKAGE_UNKNOWN_OK;
default_level |= Signature.Level.PACKAGE_TRUST_SET;
2014-10-22 13:44:02 -03:00
}
if (affect_database) {
2014-12-03 12:02:14 -03:00
default_level |= Signature.Level.DATABASE_MARGINAL_OK;
default_level |= Signature.Level.DATABASE_UNKNOWN_OK;
2014-10-22 13:44:02 -03:00
}
}
2015-01-02 17:39:32 -03:00
else
GLib.stderr.printf("unrecognized siglevel: %s\n", conf_string);
2014-10-22 13:44:02 -03:00
}
2014-12-03 12:02:14 -03:00
default_level &= ~Signature.Level.USE_DEFAULT;
2014-10-22 13:44:02 -03:00
return default_level;
}
2014-12-03 12:02:14 -03:00
public Signature.Level merge_siglevel (Signature.Level base_level, Signature.Level over_level) {
2015-01-02 17:39:32 -03:00
if ((over_level & Signature.Level.USE_DEFAULT) == 0) {
2014-12-03 12:02:14 -03:00
if ((over_level & Signature.Level.PACKAGE_SET) == 0) {
over_level |= base_level & Signature.Level.PACKAGE;
over_level |= base_level & Signature.Level.PACKAGE_OPTIONAL;
2014-10-22 13:44:02 -03:00
}
2014-12-03 12:02:14 -03:00
if ((over_level & Signature.Level.PACKAGE_TRUST_SET) == 0) {
over_level |= base_level & Signature.Level.PACKAGE_MARGINAL_OK;
over_level |= base_level & Signature.Level.PACKAGE_UNKNOWN_OK;
2014-10-22 13:44:02 -03:00
}
}
return over_level;
}
}
}