Graphical package manager for pacman based on pamac 5.x.x
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

487 lines
16KB

  1. /*
  2. * alpm_config
  3. *
  4. * Copyright (C) 2017-2019 Chris Cromer <cromer@cromnix.org>
  5. * Copyright (C) 2014-2017 Guillaume Benoit <guillaume@manjaro.org>
  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 3 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 get of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. //using Alpm
  21. [Compact]
  22. class AlpmRepo {
  23. public string name;
  24. public Alpm.Signature.Level siglevel;
  25. public Alpm.Signature.Level siglevel_mask;
  26. public Alpm.DB.Usage usage;
  27. public GLib.List<string> urls;
  28. public AlpmRepo (string name) {
  29. this.name = name;
  30. siglevel = Alpm.Signature.Level.USE_DEFAULT;
  31. usage = 0;
  32. urls = new GLib.List<string> ();
  33. }
  34. public static int compare_name (AlpmRepo a, AlpmRepo b) {
  35. return strcmp (a.name, b.name);
  36. }
  37. public static int search_name (AlpmRepo a, string name) {
  38. return strcmp (a.name, name);
  39. }
  40. }
  41. public class AlpmConfig {
  42. string conf_path;
  43. string? rootdir;
  44. string? dbpath;
  45. string? logfile;
  46. string? gpgdir;
  47. string? arch;
  48. int usesyslog;
  49. public int checkspace;
  50. GLib.List<string> cachedirs;
  51. GLib.List<string> hookdirs;
  52. GLib.List<string> ignoregroups;
  53. GLib.List<string> ignorepkgs;
  54. GLib.List<string> noextracts;
  55. GLib.List<string> noupgrades;
  56. GLib.List<string> holdpkgs;
  57. GLib.List<string> syncfirsts;
  58. Alpm.Signature.Level siglevel;
  59. Alpm.Signature.Level localfilesiglevel;
  60. Alpm.Signature.Level remotefilesiglevel;
  61. Alpm.Signature.Level siglevel_mask;
  62. Alpm.Signature.Level localfilesiglevel_mask;
  63. Alpm.Signature.Level remotefilesiglevel_mask;
  64. GLib.List<AlpmRepo> repo_order;
  65. public AlpmConfig (string path) {
  66. conf_path = path;
  67. reload ();
  68. }
  69. public unowned GLib.List<string> get_holdpkgs () {
  70. return holdpkgs;
  71. }
  72. public unowned GLib.List<string> get_syncfirsts () {
  73. return syncfirsts;
  74. }
  75. public void reload () {
  76. // set default options
  77. cachedirs = new GLib.List<string> ();
  78. hookdirs = new GLib.List<string> ();
  79. ignoregroups = new GLib.List<string> ();
  80. ignorepkgs = new GLib.List<string> ();
  81. noextracts = new GLib.List<string> ();
  82. noupgrades = new GLib.List<string> ();
  83. holdpkgs = new GLib.List<string> ();
  84. syncfirsts = new GLib.List<string> ();
  85. usesyslog = 0;
  86. checkspace = 0;
  87. siglevel = Alpm.Signature.Level.PACKAGE | Alpm.Signature.Level.PACKAGE_OPTIONAL | Alpm.Signature.Level.DATABASE | Alpm.Signature.Level.DATABASE_OPTIONAL;
  88. localfilesiglevel = Alpm.Signature.Level.USE_DEFAULT;
  89. remotefilesiglevel = Alpm.Signature.Level.USE_DEFAULT;
  90. repo_order = new GLib.List<AlpmRepo> ();
  91. // parse conf file
  92. parse_file (conf_path);
  93. // if rootdir is set and dbpath/logfile are not
  94. // set, then set those as well to reside under the root.
  95. if (rootdir != null) {
  96. if (dbpath == null) {
  97. dbpath = Path.build_path ("/", rootdir, "var/lib/pacman/");
  98. }
  99. if (logfile == null) {
  100. logfile = Path.build_path ("/", rootdir, "var/log/pacman.log");
  101. }
  102. } else {
  103. rootdir = "/";
  104. if (dbpath == null) {
  105. dbpath = "/var/lib/pacman/";
  106. }
  107. if (logfile == null) {
  108. logfile = "/var/log/pacman.log";
  109. }
  110. }
  111. if (cachedirs.length () == 0) {
  112. cachedirs.append ("/var/cache/pacman/pkg/");
  113. }
  114. if (hookdirs.length () == 0) {
  115. hookdirs.append ("/etc/pacman.d/hooks/");
  116. }
  117. if (gpgdir == null) {
  118. // gpgdir it is not relative to rootdir, even if
  119. // rootdir is defined because it contains configuration data.
  120. gpgdir = "/etc/pacman.d/gnupg/";
  121. }
  122. if (arch == null) {
  123. arch = Posix.utsname().machine;
  124. }
  125. }
  126. public Alpm.Handle? get_handle (bool files_db = false, bool tmp_db = false) {
  127. Alpm.Errno error = 0;
  128. Alpm.Handle? handle = null;
  129. if (tmp_db) {
  130. string tmp_dbpath = "/tmp/pamac-checkdbs";
  131. try {
  132. Process.spawn_command_line_sync ("mkdir -p %s/sync".printf (tmp_dbpath));
  133. Process.spawn_command_line_sync ("ln -sf %s/local %s".printf (dbpath, tmp_dbpath));
  134. Process.spawn_command_line_sync ("chmod -R 777 %s/sync".printf (tmp_dbpath));
  135. handle = new Alpm.Handle (rootdir, tmp_dbpath, out error);
  136. } catch (SpawnError e) {
  137. stderr.printf ("SpawnError: %s\n", e.message);
  138. }
  139. } else {
  140. handle = new Alpm.Handle (rootdir, dbpath, out error);
  141. }
  142. if (error == Alpm.Errno.DB_VERSION) {
  143. try {
  144. Process.spawn_command_line_sync ("pacman-db-upgrade", null, null, null);
  145. } catch (SpawnError e) {
  146. stdout.printf ("Error: %s\n", e.message);
  147. }
  148. handle = new Alpm.Handle (rootdir, dbpath, out error);
  149. }
  150. if (handle == null) {
  151. stderr.printf ("Failed to initialize alpm library" + " (%s)\n".printf (Alpm.strerror (error)));
  152. return null;
  153. }
  154. // define options
  155. if (files_db) {
  156. handle.dbext = ".files";
  157. }
  158. if (!tmp_db) {
  159. handle.logfile = logfile;
  160. }
  161. handle.gpgdir = gpgdir;
  162. handle.arch = arch;
  163. handle.usesyslog = usesyslog;
  164. handle.checkspace = checkspace;
  165. handle.defaultsiglevel = siglevel;
  166. localfilesiglevel = merge_siglevel (siglevel, localfilesiglevel, localfilesiglevel_mask);
  167. remotefilesiglevel = merge_siglevel (siglevel, remotefilesiglevel, remotefilesiglevel_mask);
  168. handle.localfilesiglevel = localfilesiglevel;
  169. handle.remotefilesiglevel = remotefilesiglevel;
  170. foreach (unowned string cachedir in cachedirs) {
  171. handle.add_cachedir (cachedir);
  172. }
  173. foreach (unowned string hookdir in hookdirs) {
  174. handle.add_hookdir (hookdir);
  175. }
  176. foreach (unowned string ignoregroup in ignoregroups) {
  177. handle.add_ignoregroup (ignoregroup);
  178. }
  179. foreach (unowned string ignorepkg in ignorepkgs) {
  180. handle.add_ignorepkg (ignorepkg);
  181. }
  182. foreach (unowned string noextract in noextracts) {
  183. handle.add_noextract (noextract);
  184. }
  185. foreach (unowned string noupgrade in noupgrades) {
  186. handle.add_noupgrade (noupgrade);
  187. }
  188. // register dbs
  189. foreach (unowned AlpmRepo repo in repo_order) {
  190. repo.siglevel = merge_siglevel (siglevel, repo.siglevel, repo.siglevel_mask);
  191. unowned Alpm.DB db = handle.register_syncdb (repo.name, repo.siglevel);
  192. foreach (unowned string url in repo.urls) {
  193. db.add_server (url.replace ("$repo", repo.name).replace ("$arch", handle.arch));
  194. }
  195. if (repo.usage == 0) {
  196. db.usage = Alpm.DB.Usage.ALL;
  197. } else {
  198. db.usage = repo.usage;
  199. }
  200. }
  201. return handle;
  202. }
  203. void parse_file (string path, string? section = null) {
  204. string? current_section = section;
  205. var file = GLib.File.new_for_path (path);
  206. if (file.query_exists ()) {
  207. try {
  208. // Open file for reading and wrap returned FileInputStream into a
  209. // DataInputStream, so we can read line by line
  210. var dis = new DataInputStream (file.read ());
  211. string? line;
  212. // Read lines until end of file (null) is reached
  213. while ((line = dis.read_line ()) != null) {
  214. if (line.length == 0) {
  215. continue;
  216. }
  217. // ignore whole line and end of line comments
  218. string[] splitted = line.split ("#", 2);
  219. line = splitted[0].strip ();
  220. if (line.length == 0) {
  221. continue;
  222. }
  223. if (line[0] == '[' && line[line.length-1] == ']') {
  224. current_section = line[1:-1];
  225. if (current_section != "options") {
  226. var repo = new AlpmRepo (current_section);
  227. if (repo_order.find_custom (repo, AlpmRepo.compare_name) == null) {
  228. repo_order.append ((owned) repo);
  229. }
  230. }
  231. continue;
  232. }
  233. splitted = line.split ("=", 2);
  234. unowned string key = splitted[0]._strip ();
  235. unowned string? val = null;
  236. if (splitted.length == 2) {
  237. val = splitted[1]._strip ();
  238. }
  239. if (key == "Include") {
  240. parse_file (val, current_section);
  241. }
  242. if (current_section == "options") {
  243. if (key == "RootDir") {
  244. rootdir = val;
  245. } else if (key == "DBPath") {
  246. dbpath = val;
  247. } else if (key == "CacheDir") {
  248. foreach (unowned string dir in val.split (" ")) {
  249. cachedirs.append (dir);
  250. }
  251. } else if (key == "HookDir") {
  252. foreach (unowned string dir in val.split (" ")) {
  253. hookdirs.append (dir);
  254. }
  255. } else if (key == "LogFile") {
  256. logfile = val;
  257. } else if (key == "GPGDir") {
  258. gpgdir = val;
  259. } else if (key == "LogFile") {
  260. logfile = val;
  261. } else if (key == "Architecture") {
  262. if (val == "auto") {
  263. arch = Posix.utsname ().machine;
  264. } else {
  265. arch = val;
  266. }
  267. } else if (key == "UseSysLog") {
  268. usesyslog = 1;
  269. } else if (key == "CheckSpace") {
  270. checkspace = 1;
  271. } else if (key == "SigLevel") {
  272. process_siglevel (val, ref siglevel, ref siglevel_mask);
  273. } else if (key == "LocalFileSigLevel") {
  274. process_siglevel (val, ref localfilesiglevel, ref localfilesiglevel_mask);
  275. } else if (key == "RemoteFileSigLevel") {
  276. process_siglevel (val, ref remotefilesiglevel, ref remotefilesiglevel_mask);
  277. } else if (key == "HoldPkg") {
  278. foreach (unowned string name in val.split (" ")) {
  279. holdpkgs.append (name);
  280. }
  281. } else if (key == "SyncFirst") {
  282. foreach (unowned string name in val.split (" ")) {
  283. syncfirsts.append (name);
  284. }
  285. } else if (key == "IgnoreGroup") {
  286. foreach (unowned string name in val.split (" ")) {
  287. ignoregroups.append (name);
  288. }
  289. } else if (key == "IgnorePkg") {
  290. foreach (unowned string name in val.split (" ")) {
  291. ignorepkgs.append (name);
  292. }
  293. } else if (key == "Noextract") {
  294. foreach (unowned string name in val.split (" ")) {
  295. noextracts.append (name);
  296. }
  297. } else if (key == "NoUpgrade") {
  298. foreach (unowned string name in val.split (" ")) {
  299. noupgrades.append (name);
  300. }
  301. }
  302. } else {
  303. unowned GLib.List<AlpmRepo>? found = repo_order.search (current_section, (SearchFunc) AlpmRepo.search_name);
  304. if (found != null) {
  305. unowned AlpmRepo repo = found.data;
  306. if (key == "Server") {
  307. repo.urls.append (val);
  308. } else if (key == "SigLevel") {
  309. process_siglevel (val, ref repo.siglevel, ref repo.siglevel_mask);
  310. } else if (key == "Usage") {
  311. repo.usage = define_usage (val);
  312. }
  313. }
  314. }
  315. }
  316. } catch (GLib.Error e) {
  317. stderr.printf("%s\n", e.message);
  318. }
  319. } else {
  320. stderr.printf ("File '%s' doesn't exist.\n", path);
  321. }
  322. }
  323. public void write (HashTable<string,Variant> new_conf) {
  324. var file = GLib.File.new_for_path (conf_path);
  325. if (file.query_exists ()) {
  326. try {
  327. // Open file for reading and wrap returned FileInputStream into a
  328. // DataInputStream, so we can read line by line
  329. var dis = new DataInputStream (file.read ());
  330. string? line;
  331. string[] data = {};
  332. // Read lines until end of file (null) is reached
  333. while ((line = dis.read_line ()) != null) {
  334. if (line.length == 0) {
  335. data += "\n";
  336. continue;
  337. }
  338. if (line.contains ("IgnorePkg")) {
  339. if (new_conf.contains ("IgnorePkg")) {
  340. string val = new_conf.get ("IgnorePkg").get_string ();
  341. if (val == "") {
  342. data += "#IgnorePkg =\n";
  343. } else {
  344. data += "IgnorePkg = %s\n".printf (val);
  345. }
  346. new_conf.remove ("IgnorePkg");
  347. } else {
  348. data += line + "\n";
  349. }
  350. } else if (line.contains ("CheckSpace")) {
  351. if (new_conf.contains ("CheckSpace")) {
  352. bool val = new_conf.get ("CheckSpace").get_boolean ();
  353. if (val) {
  354. data += "CheckSpace\n";
  355. } else {
  356. data += "#CheckSpace\n";
  357. }
  358. new_conf.remove ("CheckSpace");
  359. } else {
  360. data += line + "\n";
  361. }
  362. } else {
  363. data += line + "\n";
  364. }
  365. }
  366. // delete the file before rewrite it
  367. file.delete ();
  368. // creating a DataOutputStream to the file
  369. var dos = new DataOutputStream (file.create (FileCreateFlags.REPLACE_DESTINATION));
  370. foreach (unowned string new_line in data) {
  371. // writing a short string to the stream
  372. dos.put_string (new_line);
  373. }
  374. reload ();
  375. } catch (GLib.Error e) {
  376. stderr.printf("%s\n", e.message);
  377. }
  378. } else {
  379. stderr.printf ("File '%s' doesn't exist.\n", conf_path);
  380. }
  381. }
  382. Alpm.DB.Usage define_usage (string conf_string) {
  383. Alpm.DB.Usage usage = 0;
  384. foreach (unowned string directive in conf_string.split(" ")) {
  385. if (directive == "Sync") {
  386. usage |= Alpm.DB.Usage.SYNC;
  387. } else if (directive == "Search") {
  388. usage |= Alpm.DB.Usage.SEARCH;
  389. } else if (directive == "Install") {
  390. usage |= Alpm.DB.Usage.INSTALL;
  391. } else if (directive == "Upgrade") {
  392. usage |= Alpm.DB.Usage.UPGRADE;
  393. } else if (directive == "All") {
  394. usage |= Alpm.DB.Usage.ALL;
  395. }
  396. }
  397. return usage;
  398. }
  399. void process_siglevel (string conf_string, ref Alpm.Signature.Level siglevel, ref Alpm.Signature.Level siglevel_mask) {
  400. foreach (unowned string directive in conf_string.split(" ")) {
  401. bool affect_package = false;
  402. bool affect_database = false;
  403. if ("Package" in directive) {
  404. affect_package = true;
  405. } else if ("Database" in directive) {
  406. affect_database = true;
  407. } else {
  408. affect_package = true;
  409. affect_database = true;
  410. }
  411. if ("Never" in directive) {
  412. if (affect_package) {
  413. siglevel &= ~Alpm.Signature.Level.PACKAGE;
  414. siglevel_mask |= Alpm.Signature.Level.PACKAGE;
  415. }
  416. if (affect_database) {
  417. siglevel &= ~Alpm.Signature.Level.DATABASE;
  418. siglevel_mask |= Alpm.Signature.Level.DATABASE;
  419. }
  420. } else if ("Optional" in directive) {
  421. if (affect_package) {
  422. siglevel |= (Alpm.Signature.Level.PACKAGE | Alpm.Signature.Level.PACKAGE_OPTIONAL);
  423. siglevel_mask |= (Alpm.Signature.Level.PACKAGE | Alpm.Signature.Level.PACKAGE_OPTIONAL);
  424. }
  425. if (affect_database) {
  426. siglevel |= (Alpm.Signature.Level.DATABASE | Alpm.Signature.Level.DATABASE_OPTIONAL);
  427. siglevel_mask |= (Alpm.Signature.Level.DATABASE | Alpm.Signature.Level.DATABASE_OPTIONAL);
  428. }
  429. } else if ("Required" in directive) {
  430. if (affect_package) {
  431. siglevel |= Alpm.Signature.Level.PACKAGE;
  432. siglevel_mask |= Alpm.Signature.Level.PACKAGE;
  433. siglevel &= ~Alpm.Signature.Level.PACKAGE_OPTIONAL;
  434. siglevel_mask |= Alpm.Signature.Level.PACKAGE_OPTIONAL;
  435. }
  436. if (affect_database) {
  437. siglevel |= Alpm.Signature.Level.DATABASE;
  438. siglevel_mask |= Alpm.Signature.Level.DATABASE;
  439. siglevel &= ~Alpm.Signature.Level.DATABASE_OPTIONAL;
  440. siglevel_mask |= Alpm.Signature.Level.DATABASE_OPTIONAL;
  441. }
  442. } else if ("TrustedOnly" in directive) {
  443. if (affect_package) {
  444. siglevel &= ~(Alpm.Signature.Level.PACKAGE_MARGINAL_OK | Alpm.Signature.Level.PACKAGE_UNKNOWN_OK);
  445. siglevel_mask |= (Alpm.Signature.Level.PACKAGE_MARGINAL_OK | Alpm.Signature.Level.PACKAGE_UNKNOWN_OK);
  446. }
  447. if (affect_database) {
  448. siglevel &= ~(Alpm.Signature.Level.DATABASE_MARGINAL_OK | Alpm.Signature.Level.DATABASE_UNKNOWN_OK);
  449. siglevel_mask |= (Alpm.Signature.Level.DATABASE_MARGINAL_OK | Alpm.Signature.Level.DATABASE_UNKNOWN_OK);
  450. }
  451. } else if ("TrustAll" in directive) {
  452. if (affect_package) {
  453. siglevel |= (Alpm.Signature.Level.PACKAGE_MARGINAL_OK | Alpm.Signature.Level.PACKAGE_UNKNOWN_OK);
  454. siglevel_mask |= (Alpm.Signature.Level.PACKAGE_MARGINAL_OK | Alpm.Signature.Level.PACKAGE_UNKNOWN_OK);
  455. }
  456. if (affect_database) {
  457. siglevel |= (Alpm.Signature.Level.DATABASE_MARGINAL_OK | Alpm.Signature.Level.DATABASE_UNKNOWN_OK);
  458. siglevel_mask |= (Alpm.Signature.Level.DATABASE_MARGINAL_OK | Alpm.Signature.Level.DATABASE_UNKNOWN_OK);
  459. }
  460. } else {
  461. stderr.printf("unrecognized siglevel: %s\n", conf_string);
  462. }
  463. }
  464. siglevel &= ~Alpm.Signature.Level.USE_DEFAULT;
  465. }
  466. Alpm.Signature.Level merge_siglevel(Alpm.Signature.Level sigbase, Alpm.Signature.Level sigover, Alpm.Signature.Level sigmask) {
  467. return (sigmask != 0) ? (sigover & sigmask) | (sigbase & ~sigmask) : sigover;
  468. }
  469. }