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.

alpm_config.vala 16KB

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