pamac-classic/src/alpm_config.vala

461 lines
15 KiB
Vala
Raw Normal View History

2014-10-22 13:44:02 -03:00
/*
* pamac-vala
*
2016-02-02 05:28:07 -03:00
* Copyright (C) 2014-2016 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 {
2016-02-02 05:28:07 -03:00
[Compact]
public class Repo {
2014-10-22 13:44:02 -03:00
public string name;
2014-12-03 12:02:14 -03:00
public Signature.Level siglevel;
2016-02-02 05:28:07 -03:00
public Signature.Level siglevel_mask;
2014-12-03 12:02:14 -03:00
public DB.Usage usage;
2016-02-02 05:28:07 -03:00
public GLib.List<string> urls;
2014-10-22 13:44:02 -03:00
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;
2016-02-02 05:28:07 -03:00
urls = new GLib.List<string> ();
}
public static int compare_name (Repo a, Repo b) {
return strcmp (a.name, b.name);
}
public static int search_name (Repo a, string name) {
return strcmp (a.name, name);
}
2014-10-22 13:44:02 -03:00
}
2016-02-02 05:28:07 -03:00
[Compact]
2014-10-22 13:44:02 -03:00
public class Config {
2016-02-02 05:28:07 -03:00
public string conf_path;
public string? rootdir;
public string? dbpath;
public string? logfile;
public string? gpgdir;
public string? arch;
public double deltaratio;
public int usesyslog;
public int checkspace;
2016-02-02 05:28:07 -03:00
public Alpm.List<string>? cachedirs;
public Alpm.List<string?>? hookdirs;
public Alpm.List<string>? ignoregroups;
public Alpm.List<string>? ignorepkgs;
public Alpm.List<string>? noextracts;
public Alpm.List<string>? noupgrades;
public GLib.List<string>? holdpkgs;
public GLib.List<string>? syncfirsts;
public Signature.Level siglevel;
public Signature.Level localfilesiglevel;
public Signature.Level remotefilesiglevel;
public Signature.Level siglevel_mask;
public Signature.Level localfilesiglevel_mask;
public Signature.Level remotefilesiglevel_mask;
public GLib.List<Repo> repo_order;
public 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;
reload ();
}
public void reload () {
// set default options
holdpkgs = new GLib.List<string> ();
syncfirsts = new GLib.List<string> ();
2016-02-02 05:28:07 -03:00
// free internal data of alpm lists
if (cachedirs != null) {
cachedirs.free_data ();
}
if (hookdirs != null) {
hookdirs.free_data ();
}
if (ignoregroups != null) {
ignoregroups.free_data ();
}
if (ignorepkgs != null) {
ignorepkgs.free_data ();
}
if (noextracts != null) {
noextracts.free_data ();
}
if (noupgrades != null) {
noupgrades.free_data ();
}
2014-10-30 10:44:09 -03:00
usesyslog = 0;
checkspace = 0;
deltaratio = 0.7;
2016-02-02 05:28:07 -03:00
siglevel = Signature.Level.PACKAGE | Signature.Level.PACKAGE_OPTIONAL | Signature.Level.DATABASE | Signature.Level.DATABASE_OPTIONAL;
2014-12-03 12:02:14 -03:00
localfilesiglevel = Signature.Level.USE_DEFAULT;
remotefilesiglevel = Signature.Level.USE_DEFAULT;
2016-02-02 05:28:07 -03:00
repo_order = new GLib.List<Repo> ();
2014-10-22 13:44:02 -03:00
// parse conf file
2014-12-03 12:02:14 -03:00
parse_file (conf_path);
2016-02-02 05:28:07 -03:00
// if rootdir is set and dbpath/logfile are not
// set, then set those as well to reside under the root.
if (rootdir != null) {
if (dbpath == null) {
dbpath = Path.build_path ("/", rootdir, "var/lib/pacman/");
}
if (logfile == null) {
logfile = Path.build_path ("/", rootdir, "var/log/pacman.log");
}
} else {
rootdir = "/";
if (dbpath == null) {
dbpath = "/var/lib/pacman/";
}
if (logfile == null) {
logfile = "/var/log/pacman.log";
}
}
if (cachedirs.length == 0) {
cachedirs.add ("/var/cache/pacman/pkg/");
}
if (hookdirs.length == 0) {
hookdirs.add ("/etc/pacman.d/hooks/");
}
if (gpgdir == null) {
// gpgdir it is not relative to rootdir, even if
// rootdir is defined because it contains configuration data.
gpgdir = "/etc/pacman.d/gnupg/";
}
if (arch == null) {
arch = Posix.utsname().machine;
}
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
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.logfile = logfile;
2016-02-02 05:28:07 -03:00
handle.gpgdir = gpgdir;
2014-10-30 10:44:09 -03:00
handle.arch = arch;
handle.deltaratio = deltaratio;
handle.usesyslog = usesyslog;
handle.checkspace = checkspace;
2016-02-02 05:28:07 -03:00
handle.defaultsiglevel = siglevel;
localfilesiglevel = merge_siglevel (siglevel, localfilesiglevel, localfilesiglevel_mask);
remotefilesiglevel = merge_siglevel (siglevel, remotefilesiglevel, remotefilesiglevel_mask);
2014-10-30 10:44:09 -03:00
handle.localfilesiglevel = localfilesiglevel;
handle.remotefilesiglevel = remotefilesiglevel;
handle.cachedirs = cachedirs;
2016-02-02 05:28:07 -03:00
// add hook directories 1-by-1 to avoid overwriting the system directory
foreach (unowned string hookdir in hookdirs) {
handle.add_hookdir (hookdir);
}
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
2016-02-02 05:28:07 -03:00
foreach (unowned Repo repo in repo_order) {
repo.siglevel = merge_siglevel (siglevel, repo.siglevel, repo.siglevel_mask);
2014-10-22 13:44:02 -03:00
unowned DB db = handle.register_syncdb (repo.name, repo.siglevel);
2016-02-02 05:28:07 -03:00
foreach (unowned string url in repo.urls) {
2014-10-22 13:44:02 -03:00
db.add_server (url.replace ("$repo", repo.name).replace ("$arch", handle.arch));
2015-03-04 11:55:36 -03:00
}
if (repo.usage == 0) {
2014-12-03 12:02:14 -03:00
db.usage = DB.Usage.ALL;
2015-03-04 11:55:36 -03:00
} else {
2014-12-03 12:02:14 -03:00
db.usage = repo.usage;
2015-03-04 11:55:36 -03:00
}
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);
2015-08-20 10:11:18 -03:00
if (file.query_exists ()) {
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 ());
2015-08-20 10:11:18 -03:00
string? line;
2014-10-22 13:44:02 -03:00
// Read lines until end of file (null) is reached
2015-08-20 10:11:18 -03:00
while ((line = dis.read_line ()) != null) {
2015-03-04 11:55:36 -03:00
if (line.length == 0) {
continue;
}
// ignore whole line and end of line comments
string[] splitted = line.split ("#", 2);
line = splitted[0].strip ();
2015-03-04 11:55:36 -03:00
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);
2016-02-02 05:28:07 -03:00
if (repo_order.find_custom (repo, Repo.compare_name) == null) {
repo_order.append ((owned) repo);
}
2014-10-22 13:44:02 -03:00
}
continue;
}
splitted = line.split ("=", 2);
2016-02-02 05:28:07 -03:00
unowned string key = splitted[0]._strip ();
unowned string? val = null;
if (splitted.length == 2) {
val = splitted[1]._strip ();
2015-03-04 11:55:36 -03:00
}
2015-08-20 10:11:18 -03:00
if (key == "Include") {
parse_file (val, current_section);
2015-03-04 11:55:36 -03:00
}
2014-10-22 13:44:02 -03:00
if (current_section == "options") {
2016-02-02 05:28:07 -03:00
if (key == "RootDir") {
rootdir = val;
} else if (key == "DBPath") {
dbpath = val;
} else if (key == "CacheDir") {
foreach (unowned string dir in val.split (" ")) {
cachedirs.add (dir);
}
} else if (key == "HookDir") {
foreach (unowned string dir in val.split (" ")) {
hookdirs.add (dir);
}
} else if (key == "LogFile") {
logfile = val;
} else if (key == "GPGDir") {
2015-08-20 10:11:18 -03:00
gpgdir = val;
} else if (key == "LogFile") {
logfile = val;
} else if (key == "Architecture") {
if (val == "auto") {
2014-10-30 10:44:09 -03:00
arch = Posix.utsname ().machine;
2015-03-04 11:55:36 -03:00
} else {
2015-08-20 10:11:18 -03:00
arch = val;
2015-03-04 11:55:36 -03:00
}
2015-08-20 10:11:18 -03:00
} else if (key == "UseDelta") {
deltaratio = double.parse (val);
} else if (key == "UseSysLog") {
2014-10-30 10:44:09 -03:00
usesyslog = 1;
2015-08-20 10:11:18 -03:00
} else if (key == "CheckSpace") {
2014-10-30 10:44:09 -03:00
checkspace = 1;
2015-08-20 10:11:18 -03:00
} else if (key == "SigLevel") {
2016-02-02 05:28:07 -03:00
process_siglevel (val, ref siglevel, ref siglevel_mask);
2015-08-20 10:11:18 -03:00
} else if (key == "LocalFileSigLevel") {
2016-02-02 05:28:07 -03:00
process_siglevel (val, ref localfilesiglevel, ref localfilesiglevel_mask);
2015-08-20 10:11:18 -03:00
} else if (key == "RemoteFileSigLevel") {
2016-02-02 05:28:07 -03:00
process_siglevel (val, ref remotefilesiglevel, ref remotefilesiglevel_mask);
2015-08-20 10:11:18 -03:00
} else if (key == "HoldPkg") {
2016-02-02 05:28:07 -03:00
foreach (unowned string name in val.split (" ")) {
holdpkgs.append (name);
2015-03-04 11:55:36 -03:00
}
2015-08-20 10:11:18 -03:00
} else if (key == "SyncFirst") {
2016-02-02 05:28:07 -03:00
foreach (unowned string name in val.split (" ")) {
syncfirsts.append (name);
2015-03-04 11:55:36 -03:00
}
2015-08-20 10:11:18 -03:00
} else if (key == "IgnoreGroup") {
2016-02-02 05:28:07 -03:00
foreach (unowned string name in val.split (" ")) {
ignoregroups.add (name);
2015-03-04 11:55:36 -03:00
}
2015-08-20 10:11:18 -03:00
} else if (key == "IgnorePkg") {
2016-02-02 05:28:07 -03:00
foreach (unowned string name in val.split (" ")) {
ignorepkgs.add (name);
2015-03-04 11:55:36 -03:00
}
2015-08-20 10:11:18 -03:00
} else if (key == "Noextract") {
2016-02-02 05:28:07 -03:00
foreach (unowned string name in val.split (" ")) {
noextracts.add (name);
2015-03-04 11:55:36 -03:00
}
2015-08-20 10:11:18 -03:00
} else if (key == "NoUpgrade") {
2016-02-02 05:28:07 -03:00
foreach (unowned string name in val.split (" ")) {
noupgrades.add (name);
2015-03-04 11:55:36 -03:00
}
2014-10-22 13:44:02 -03:00
}
} else {
2016-02-02 05:28:07 -03:00
unowned GLib.List<Repo>? found = repo_order.search (current_section, (SearchFunc) Repo.search_name);
if (found != null) {
unowned Repo repo = found.data;
if (key == "Server") {
repo.urls.append (val);
} else if (key == "SigLevel") {
process_siglevel (val, ref repo.siglevel, ref repo.siglevel_mask);
} else if (key == "Usage") {
repo.usage = define_usage (val);
2014-10-22 13:44:02 -03:00
}
}
}
}
} catch (GLib.Error e) {
GLib.stderr.printf("%s\n", e.message);
}
2015-08-20 10:11:18 -03:00
} else {
GLib.stderr.printf ("File '%s' doesn't exist.\n", path);
}
}
public void write (HashTable<string,Variant> new_conf) {
var file = GLib.File.new_for_path (conf_path);
2015-08-20 10:11:18 -03:00
if (file.query_exists ()) {
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 ());
2015-08-20 10:11:18 -03:00
string? line;
string[] data = {};
// Read lines until end of file (null) is reached
2015-08-20 10:11:18 -03:00
while ((line = dis.read_line ()) != null) {
if (line.length == 0) {
data += "\n";
continue;
}
if (line.contains ("IgnorePkg")) {
if (new_conf.contains ("IgnorePkg")) {
2015-08-20 10:11:18 -03:00
string val = new_conf.get ("IgnorePkg").get_string ();
if (val == "") {
data += "#IgnorePkg =\n";
2015-03-04 11:55:36 -03:00
} else {
2015-08-20 10:11:18 -03:00
data += "IgnorePkg = %s\n".printf (val);
2015-03-04 11:55:36 -03:00
}
2015-08-20 10:11:18 -03:00
new_conf.remove ("IgnorePkg");
2015-03-04 11:55:36 -03:00
} else {
data += line + "\n";
2015-03-04 11:55:36 -03:00
}
} else if (line.contains ("CheckSpace")) {
if (new_conf.contains ("CheckSpace")) {
2015-08-20 10:11:18 -03:00
bool val = new_conf.get ("CheckSpace").get_boolean ();
if (val == true) {
data += "CheckSpace\n";
2015-03-04 11:55:36 -03:00
} else {
data += "#CheckSpace\n";
2015-03-04 11:55:36 -03:00
}
2015-08-20 10:11:18 -03:00
new_conf.remove ("CheckSpace");
2015-03-04 11:55:36 -03:00
} else {
data += line + "\n";
2015-03-04 11:55:36 -03:00
}
} else {
data += line + "\n";
2015-03-04 11:55:36 -03:00
}
}
// delete the file before rewrite it
file.delete ();
// creating a DataOutputStream to the file
var dos = new DataOutputStream (file.create (FileCreateFlags.REPLACE_DESTINATION));
2016-02-02 05:28:07 -03:00
foreach (unowned 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);
}
2015-08-20 10:11:18 -03:00
} else {
GLib.stderr.printf ("File '%s' doesn't exist.\n", conf_path);
2014-12-03 12:02:14 -03:00
}
}
public DB.Usage define_usage (string conf_string) {
DB.Usage usage = 0;
2016-02-02 05:28:07 -03:00
foreach (unowned string directive in conf_string.split(" ")) {
2014-12-03 12:02:14 -03:00
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
}
2016-02-02 05:28:07 -03:00
public void process_siglevel (string conf_string, ref Signature.Level siglevel, ref Signature.Level siglevel_mask) {
foreach (unowned string directive in conf_string.split(" ")) {
2014-10-22 13:44:02 -03:00
bool affect_package = false;
bool affect_database = false;
2015-03-04 11:55:36 -03:00
if ("Package" in directive) {
2015-01-02 17:39:32 -03:00
affect_package = true;
2015-03-04 11:55:36 -03:00
} else if ("Database" in directive) {
2015-01-02 17:39:32 -03:00
affect_database = true;
2015-03-04 11:55:36 -03:00
} else {
2014-10-22 13:44:02 -03:00
affect_package = true;
affect_database = true;
}
if ("Never" in directive) {
if (affect_package) {
2016-02-02 05:28:07 -03:00
siglevel &= ~Signature.Level.PACKAGE;
siglevel_mask |= Signature.Level.PACKAGE;
2014-10-22 13:44:02 -03:00
}
2015-03-04 11:55:36 -03:00
if (affect_database) {
2016-02-02 05:28:07 -03:00
siglevel &= ~Signature.Level.DATABASE;
siglevel_mask |= Signature.Level.DATABASE;
2015-03-04 11:55:36 -03:00
}
2015-01-02 17:39:32 -03:00
} else if ("Optional" in directive) {
2014-10-22 13:44:02 -03:00
if (affect_package) {
2016-02-02 05:28:07 -03:00
siglevel |= (Signature.Level.PACKAGE | Signature.Level.PACKAGE_OPTIONAL);
siglevel_mask |= (Signature.Level.PACKAGE | Signature.Level.PACKAGE_OPTIONAL);
2014-10-22 13:44:02 -03:00
}
if (affect_database) {
2016-02-02 05:28:07 -03:00
siglevel |= (Signature.Level.DATABASE | Signature.Level.DATABASE_OPTIONAL);
siglevel_mask |= (Signature.Level.DATABASE | 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) {
2016-02-02 05:28:07 -03:00
siglevel |= Signature.Level.PACKAGE;
siglevel_mask |= Signature.Level.PACKAGE;
siglevel &= ~Signature.Level.PACKAGE_OPTIONAL;
siglevel_mask |= Signature.Level.PACKAGE_OPTIONAL;
2014-10-22 13:44:02 -03:00
}
if (affect_database) {
2016-02-02 05:28:07 -03:00
siglevel |= Signature.Level.DATABASE;
siglevel_mask |= Signature.Level.DATABASE;
siglevel &= ~Signature.Level.DATABASE_OPTIONAL;
siglevel_mask |= 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) {
2016-02-02 05:28:07 -03:00
siglevel &= ~(Signature.Level.PACKAGE_MARGINAL_OK | Signature.Level.PACKAGE_UNKNOWN_OK);
siglevel_mask |= (Signature.Level.PACKAGE_MARGINAL_OK | Signature.Level.PACKAGE_UNKNOWN_OK);
2014-10-22 13:44:02 -03:00
}
if (affect_database) {
2016-02-02 05:28:07 -03:00
siglevel &= ~(Signature.Level.DATABASE_MARGINAL_OK | Signature.Level.DATABASE_UNKNOWN_OK);
siglevel_mask |= (Signature.Level.DATABASE_MARGINAL_OK | 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) {
2016-02-02 05:28:07 -03:00
siglevel |= (Signature.Level.PACKAGE_MARGINAL_OK | Signature.Level.PACKAGE_UNKNOWN_OK);
siglevel_mask |= (Signature.Level.PACKAGE_MARGINAL_OK | Signature.Level.PACKAGE_UNKNOWN_OK);
2014-10-22 13:44:02 -03:00
}
if (affect_database) {
2016-02-02 05:28:07 -03:00
siglevel |= (Signature.Level.DATABASE_MARGINAL_OK | Signature.Level.DATABASE_UNKNOWN_OK);
siglevel_mask |= (Signature.Level.DATABASE_MARGINAL_OK | Signature.Level.DATABASE_UNKNOWN_OK);
2014-10-22 13:44:02 -03:00
}
2015-03-04 11:55:36 -03:00
} else {
2015-01-02 17:39:32 -03:00
GLib.stderr.printf("unrecognized siglevel: %s\n", conf_string);
2015-03-04 11:55:36 -03:00
}
2014-10-22 13:44:02 -03:00
}
2016-02-02 05:28:07 -03:00
siglevel &= ~Signature.Level.USE_DEFAULT;
2014-10-22 13:44:02 -03:00
}
2016-02-02 05:28:07 -03:00
public Signature.Level merge_siglevel(Signature.Level sigbase, Signature.Level sigover, Signature.Level sigmask) {
return (sigmask != 0) ? (sigover & sigmask) | (sigbase & ~sigmask) : sigover;
2014-10-22 13:44:02 -03:00
}
}
}