pamac-classic/src/alpm_config.vala

490 lines
16 KiB
Vala
Raw Normal View History

2014-10-22 13:44:02 -03:00
/*
2016-04-14 13:19:20 -03:00
* alpm_config
2014-10-22 13:44:02 -03:00
*
2017-01-03 05:43:19 -03:00
* Copyright (C) 2014-2017 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/>.
*/
[Compact]
2016-04-14 13:19:20 -03:00
class AlpmRepo {
public string name;
public Alpm.Signature.Level siglevel;
public Alpm.Signature.Level siglevel_mask;
public Alpm.DB.Usage usage;
public GLib.List<string> urls;
2014-10-22 13:44:02 -03:00
public AlpmRepo (string name) {
this.name = name;
siglevel = Alpm.Signature.Level.USE_DEFAULT;
usage = 0;
urls = new GLib.List<string> ();
}
2016-02-02 05:28:07 -03:00
public static int compare_name (AlpmRepo a, AlpmRepo b) {
return strcmp (a.name, b.name);
}
2016-02-02 05:28:07 -03:00
public static int search_name (AlpmRepo a, string name) {
return strcmp (a.name, name);
2014-10-22 13:44:02 -03:00
}
}
2014-10-22 13:44:02 -03:00
2016-04-14 13:19:20 -03:00
class AlpmConfig {
string conf_path;
string? rootdir;
string? dbpath;
string? logfile;
string? gpgdir;
string? arch;
double deltaratio;
int usesyslog;
2017-07-29 10:35:04 -04:00
public int checkspace;
2016-04-14 13:19:20 -03:00
GLib.List<string> cachedirs;
GLib.List<string> hookdirs;
GLib.List<string> ignoregroups;
GLib.List<string> ignorepkgs;
GLib.List<string> noextracts;
GLib.List<string> noupgrades;
GLib.List<string> holdpkgs;
GLib.List<string> syncfirsts;
Alpm.Signature.Level siglevel;
Alpm.Signature.Level localfilesiglevel;
Alpm.Signature.Level remotefilesiglevel;
Alpm.Signature.Level siglevel_mask;
Alpm.Signature.Level localfilesiglevel_mask;
Alpm.Signature.Level remotefilesiglevel_mask;
GLib.List<AlpmRepo> repo_order;
2014-12-03 12:02:14 -03:00
public AlpmConfig (string path) {
conf_path = path;
reload ();
}
2016-04-14 13:19:20 -03:00
public unowned GLib.List<string> get_holdpkgs () {
return holdpkgs;
}
public unowned GLib.List<string> get_syncfirsts () {
return syncfirsts;
}
public void reload () {
// set default options
2016-04-14 13:19:20 -03:00
cachedirs = new GLib.List<string> ();
hookdirs = new GLib.List<string> ();
ignoregroups = new GLib.List<string> ();
ignorepkgs = new GLib.List<string> ();
noextracts = new GLib.List<string> ();
noupgrades = new GLib.List<string> ();
holdpkgs = new GLib.List<string> ();
syncfirsts = new GLib.List<string> ();
usesyslog = 0;
checkspace = 0;
deltaratio = 0.7;
siglevel = Alpm.Signature.Level.PACKAGE | Alpm.Signature.Level.PACKAGE_OPTIONAL | Alpm.Signature.Level.DATABASE | Alpm.Signature.Level.DATABASE_OPTIONAL;
localfilesiglevel = Alpm.Signature.Level.USE_DEFAULT;
remotefilesiglevel = Alpm.Signature.Level.USE_DEFAULT;
repo_order = new GLib.List<AlpmRepo> ();
// parse conf file
parse_file (conf_path);
// 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/");
2016-02-02 05:28:07 -03:00
}
if (logfile == null) {
logfile = Path.build_path ("/", rootdir, "var/log/pacman.log");
2016-02-02 05:28:07 -03:00
}
} else {
rootdir = "/";
if (dbpath == null) {
dbpath = "/var/lib/pacman/";
2016-02-02 05:28:07 -03:00
}
if (logfile == null) {
logfile = "/var/log/pacman.log";
2016-02-02 05:28:07 -03:00
}
2014-10-30 10:44:09 -03:00
}
2016-04-14 13:19:20 -03:00
if (cachedirs.length () == 0) {
cachedirs.append ("/var/cache/pacman/pkg/");
}
2016-04-14 13:19:20 -03:00
if (hookdirs.length () == 0) {
hookdirs.append ("/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
2017-07-29 10:35:04 -04:00
public Alpm.Handle? get_handle (bool files_db = false, bool tmp_db = false) {
Alpm.Errno error = 0;
Alpm.Handle? handle = null;
if (tmp_db) {
2017-08-22 06:11:31 -03:00
string tmp_dbpath = "/tmp/pamac-checkdbs";
2017-07-29 10:35:04 -04:00
try {
Process.spawn_command_line_sync ("mkdir -p %s".printf (tmp_dbpath));
Process.spawn_command_line_sync ("ln -sf %s/local %s".printf (dbpath, tmp_dbpath));
2017-10-02 10:23:53 -03:00
Process.spawn_command_line_sync ("cp -au %s/sync %s".printf (dbpath, tmp_dbpath));
2017-08-22 06:11:31 -03:00
Process.spawn_command_line_sync ("chmod -R 777 %s/sync".printf (tmp_dbpath));
2017-07-29 10:35:04 -04:00
handle = new Alpm.Handle (rootdir, tmp_dbpath, out error);
} catch (SpawnError e) {
stderr.printf ("SpawnError: %s\n", e.message);
}
} else {
handle = new Alpm.Handle (rootdir, dbpath, out error);
}
2017-04-16 07:40:08 -03:00
if (error == Alpm.Errno.DB_VERSION) {
try {
Process.spawn_command_line_sync ("pacman-db-upgrade", null, null, null);
} catch (SpawnError e) {
stdout.printf ("Error: %s\n", e.message);
}
handle = new Alpm.Handle (rootdir, dbpath, out error);
}
if (handle == null) {
2016-04-14 13:19:20 -03:00
stderr.printf ("Failed to initialize alpm library" + " (%s)\n".printf (Alpm.strerror (error)));
return null;
}
// define options
2017-06-03 08:46:08 -04:00
if (files_db) {
handle.dbext = ".files";
}
2017-07-29 10:35:04 -04:00
if (!tmp_db) {
handle.logfile = logfile;
}
handle.gpgdir = gpgdir;
handle.arch = arch;
handle.deltaratio = deltaratio;
handle.usesyslog = usesyslog;
handle.checkspace = checkspace;
handle.defaultsiglevel = siglevel;
localfilesiglevel = merge_siglevel (siglevel, localfilesiglevel, localfilesiglevel_mask);
remotefilesiglevel = merge_siglevel (siglevel, remotefilesiglevel, remotefilesiglevel_mask);
handle.localfilesiglevel = localfilesiglevel;
handle.remotefilesiglevel = remotefilesiglevel;
2016-04-14 13:19:20 -03:00
foreach (unowned string cachedir in cachedirs) {
handle.add_cachedir (cachedir);
}
foreach (unowned string hookdir in hookdirs) {
handle.add_hookdir (hookdir);
}
2016-04-14 13:19:20 -03:00
foreach (unowned string ignoregroup in ignoregroups) {
handle.add_ignoregroup (ignoregroup);
}
foreach (unowned string ignorepkg in ignorepkgs) {
handle.add_ignorepkg (ignorepkg);
}
foreach (unowned string noextract in noextracts) {
handle.add_noextract (noextract);
}
foreach (unowned string noupgrade in noupgrades) {
handle.add_noupgrade (noupgrade);
}
// register dbs
foreach (unowned AlpmRepo repo in repo_order) {
repo.siglevel = merge_siglevel (siglevel, repo.siglevel, repo.siglevel_mask);
unowned Alpm.DB db = handle.register_syncdb (repo.name, repo.siglevel);
foreach (unowned string url in repo.urls) {
db.add_server (url.replace ("$repo", repo.name).replace ("$arch", handle.arch));
2016-02-02 05:28:07 -03:00
}
if (repo.usage == 0) {
db.usage = Alpm.DB.Usage.ALL;
} else {
db.usage = repo.usage;
2014-10-22 13:44:02 -03:00
}
}
2016-04-14 13:19:20 -03:00
return handle;
}
2014-10-22 13:44:02 -03:00
2016-04-14 13:19:20 -03:00
void parse_file (string path, string? section = null) {
string? current_section = section;
var file = GLib.File.new_for_path (path);
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 ());
string? line;
// Read lines until end of file (null) is reached
while ((line = dis.read_line ()) != 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;
}
if (line[0] == '[' && line[line.length-1] == ']') {
current_section = line[1:-1];
if (current_section != "options") {
var repo = new AlpmRepo (current_section);
if (repo_order.find_custom (repo, AlpmRepo.compare_name) == null) {
repo_order.append ((owned) repo);
2014-10-22 13:44:02 -03:00
}
2015-03-04 11:55:36 -03:00
}
continue;
}
splitted = line.split ("=", 2);
unowned string key = splitted[0]._strip ();
unowned string? val = null;
if (splitted.length == 2) {
val = splitted[1]._strip ();
}
if (key == "Include") {
parse_file (val, current_section);
}
if (current_section == "options") {
if (key == "RootDir") {
rootdir = val;
} else if (key == "DBPath") {
dbpath = val;
} else if (key == "CacheDir") {
foreach (unowned string dir in val.split (" ")) {
2016-04-14 13:19:20 -03:00
cachedirs.append (dir);
}
} else if (key == "HookDir") {
foreach (unowned string dir in val.split (" ")) {
2016-04-14 13:19:20 -03:00
hookdirs.append (dir);
}
} else if (key == "LogFile") {
logfile = val;
} else if (key == "GPGDir") {
gpgdir = val;
} else if (key == "LogFile") {
logfile = val;
} else if (key == "Architecture") {
if (val == "auto") {
arch = Posix.utsname ().machine;
} else {
arch = val;
}
} else if (key == "UseDelta") {
deltaratio = double.parse (val);
} else if (key == "UseSysLog") {
usesyslog = 1;
} else if (key == "CheckSpace") {
checkspace = 1;
} else if (key == "SigLevel") {
process_siglevel (val, ref siglevel, ref siglevel_mask);
} else if (key == "LocalFileSigLevel") {
process_siglevel (val, ref localfilesiglevel, ref localfilesiglevel_mask);
} else if (key == "RemoteFileSigLevel") {
process_siglevel (val, ref remotefilesiglevel, ref remotefilesiglevel_mask);
} else if (key == "HoldPkg") {
foreach (unowned string name in val.split (" ")) {
holdpkgs.append (name);
}
} else if (key == "SyncFirst") {
foreach (unowned string name in val.split (" ")) {
syncfirsts.append (name);
}
} else if (key == "IgnoreGroup") {
foreach (unowned string name in val.split (" ")) {
2016-04-14 13:19:20 -03:00
ignoregroups.append (name);
}
} else if (key == "IgnorePkg") {
foreach (unowned string name in val.split (" ")) {
2016-04-14 13:19:20 -03:00
ignorepkgs.append (name);
}
} else if (key == "Noextract") {
foreach (unowned string name in val.split (" ")) {
2016-04-14 13:19:20 -03:00
noextracts.append (name);
}
} else if (key == "NoUpgrade") {
foreach (unowned string name in val.split (" ")) {
2016-04-14 13:19:20 -03:00
noupgrades.append (name);
}
2015-03-04 11:55:36 -03:00
}
} else {
unowned GLib.List<AlpmRepo>? found = repo_order.search (current_section, (SearchFunc) AlpmRepo.search_name);
if (found != null) {
unowned AlpmRepo repo = found.data;
if (key == "Server") {
repo.urls.append (val);
2015-08-20 10:11:18 -03:00
} 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);
}
} 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);
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 ());
string? line;
string[] data = {};
// Read lines until end of file (null) is reached
while ((line = dis.read_line ()) != null) {
if (line.length == 0) {
data += "\n";
continue;
}
if (line.contains ("IgnorePkg")) {
if (new_conf.contains ("IgnorePkg")) {
string val = new_conf.get ("IgnorePkg").get_string ();
if (val == "") {
data += "#IgnorePkg =\n";
2015-03-04 11:55:36 -03:00
} else {
data += "IgnorePkg = %s\n".printf (val);
2015-03-04 11:55:36 -03:00
}
new_conf.remove ("IgnorePkg");
} else {
data += line + "\n";
}
} else if (line.contains ("CheckSpace")) {
if (new_conf.contains ("CheckSpace")) {
bool val = new_conf.get ("CheckSpace").get_boolean ();
if (val) {
data += "CheckSpace\n";
2015-03-04 11:55:36 -03:00
} else {
data += "#CheckSpace\n";
2015-03-04 11:55:36 -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";
}
}
// delete the file before rewrite it
file.delete ();
// creating a DataOutputStream to the file
var dos = new DataOutputStream (file.create (FileCreateFlags.REPLACE_DESTINATION));
foreach (unowned string new_line in data) {
// writing a short string to the stream
dos.put_string (new_line);
}
2016-04-14 13:19:20 -03:00
reload ();
} catch (GLib.Error e) {
GLib.stderr.printf("%s\n", e.message);
2014-12-03 12:02:14 -03:00
}
} else {
GLib.stderr.printf ("File '%s' doesn't exist.\n", conf_path);
2014-12-03 12:02:14 -03:00
}
}
2014-12-03 12:02:14 -03:00
2016-04-14 13:19:20 -03:00
Alpm.DB.Usage define_usage (string conf_string) {
Alpm.DB.Usage usage = 0;
foreach (unowned string directive in conf_string.split(" ")) {
if (directive == "Sync") {
usage |= Alpm.DB.Usage.SYNC;
} else if (directive == "Search") {
usage |= Alpm.DB.Usage.SEARCH;
} else if (directive == "Install") {
usage |= Alpm.DB.Usage.INSTALL;
} else if (directive == "Upgrade") {
usage |= Alpm.DB.Usage.UPGRADE;
} else if (directive == "All") {
usage |= Alpm.DB.Usage.ALL;
2014-10-22 13:44:02 -03:00
}
}
return usage;
}
2014-10-22 13:44:02 -03:00
2016-04-14 13:19:20 -03:00
void process_siglevel (string conf_string, ref Alpm.Signature.Level siglevel, ref Alpm.Signature.Level siglevel_mask) {
foreach (unowned string directive in conf_string.split(" ")) {
bool affect_package = false;
bool affect_database = false;
if ("Package" in directive) {
affect_package = true;
} else if ("Database" in directive) {
affect_database = true;
} else {
affect_package = true;
affect_database = true;
}
if ("Never" in directive) {
if (affect_package) {
siglevel &= ~Alpm.Signature.Level.PACKAGE;
siglevel_mask |= Alpm.Signature.Level.PACKAGE;
2014-10-22 13:44:02 -03:00
}
if (affect_database) {
siglevel &= ~Alpm.Signature.Level.DATABASE;
siglevel_mask |= Alpm.Signature.Level.DATABASE;
}
} else if ("Optional" in directive) {
if (affect_package) {
siglevel |= (Alpm.Signature.Level.PACKAGE | Alpm.Signature.Level.PACKAGE_OPTIONAL);
siglevel_mask |= (Alpm.Signature.Level.PACKAGE | Alpm.Signature.Level.PACKAGE_OPTIONAL);
}
if (affect_database) {
siglevel |= (Alpm.Signature.Level.DATABASE | Alpm.Signature.Level.DATABASE_OPTIONAL);
siglevel_mask |= (Alpm.Signature.Level.DATABASE | Alpm.Signature.Level.DATABASE_OPTIONAL);
}
} else if ("Required" in directive) {
if (affect_package) {
siglevel |= Alpm.Signature.Level.PACKAGE;
siglevel_mask |= Alpm.Signature.Level.PACKAGE;
siglevel &= ~Alpm.Signature.Level.PACKAGE_OPTIONAL;
siglevel_mask |= Alpm.Signature.Level.PACKAGE_OPTIONAL;
}
if (affect_database) {
siglevel |= Alpm.Signature.Level.DATABASE;
siglevel_mask |= Alpm.Signature.Level.DATABASE;
siglevel &= ~Alpm.Signature.Level.DATABASE_OPTIONAL;
siglevel_mask |= Alpm.Signature.Level.DATABASE_OPTIONAL;
}
} else if ("TrustedOnly" in directive) {
if (affect_package) {
siglevel &= ~(Alpm.Signature.Level.PACKAGE_MARGINAL_OK | Alpm.Signature.Level.PACKAGE_UNKNOWN_OK);
siglevel_mask |= (Alpm.Signature.Level.PACKAGE_MARGINAL_OK | Alpm.Signature.Level.PACKAGE_UNKNOWN_OK);
2015-03-04 11:55:36 -03:00
}
if (affect_database) {
siglevel &= ~(Alpm.Signature.Level.DATABASE_MARGINAL_OK | Alpm.Signature.Level.DATABASE_UNKNOWN_OK);
siglevel_mask |= (Alpm.Signature.Level.DATABASE_MARGINAL_OK | Alpm.Signature.Level.DATABASE_UNKNOWN_OK);
}
} else if ("TrustAll" in directive) {
if (affect_package) {
siglevel |= (Alpm.Signature.Level.PACKAGE_MARGINAL_OK | Alpm.Signature.Level.PACKAGE_UNKNOWN_OK);
siglevel_mask |= (Alpm.Signature.Level.PACKAGE_MARGINAL_OK | Alpm.Signature.Level.PACKAGE_UNKNOWN_OK);
}
if (affect_database) {
siglevel |= (Alpm.Signature.Level.DATABASE_MARGINAL_OK | Alpm.Signature.Level.DATABASE_UNKNOWN_OK);
siglevel_mask |= (Alpm.Signature.Level.DATABASE_MARGINAL_OK | Alpm.Signature.Level.DATABASE_UNKNOWN_OK);
}
} else {
GLib.stderr.printf("unrecognized siglevel: %s\n", conf_string);
2014-10-22 13:44:02 -03:00
}
}
siglevel &= ~Alpm.Signature.Level.USE_DEFAULT;
}
2014-10-22 13:44:02 -03:00
2016-04-14 13:19:20 -03:00
Alpm.Signature.Level merge_siglevel(Alpm.Signature.Level sigbase, Alpm.Signature.Level sigover, Alpm.Signature.Level sigmask) {
return (sigmask != 0) ? (sigover & sigmask) | (sigbase & ~sigmask) : sigover;
2014-10-22 13:44:02 -03:00
}
}