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.

463 lines
15KB

  1. /*
  2. * alpm_config
  3. *
  4. * Copyright (C) 2014-2016 Guillaume Benoit <guillaume@manjaro.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a get of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. [Compact]
  20. class AlpmRepo {
  21. public string name;
  22. public Alpm.Signature.Level siglevel;
  23. public Alpm.Signature.Level siglevel_mask;
  24. public Alpm.DB.Usage usage;
  25. public GLib.List<string> urls;
  26. public AlpmRepo (string name) {
  27. this.name = name;
  28. siglevel = Alpm.Signature.Level.USE_DEFAULT;
  29. usage = 0;
  30. urls = new GLib.List<string> ();
  31. }
  32. public static int compare_name (AlpmRepo a, AlpmRepo b) {
  33. return strcmp (a.name, b.name);
  34. }
  35. public static int search_name (AlpmRepo a, string name) {
  36. return strcmp (a.name, name);
  37. }
  38. }
  39. class AlpmConfig {
  40. string conf_path;
  41. string? rootdir;
  42. string? dbpath;
  43. string? logfile;
  44. string? gpgdir;
  45. string? arch;
  46. double deltaratio;
  47. int usesyslog;
  48. int checkspace;
  49. GLib.List<string> cachedirs;
  50. GLib.List<string> hookdirs;
  51. GLib.List<string> ignoregroups;
  52. GLib.List<string> ignorepkgs;
  53. GLib.List<string> noextracts;
  54. GLib.List<string> noupgrades;
  55. GLib.List<string> holdpkgs;
  56. GLib.List<string> syncfirsts;
  57. Alpm.Signature.Level siglevel;
  58. Alpm.Signature.Level localfilesiglevel;
  59. Alpm.Signature.Level remotefilesiglevel;
  60. Alpm.Signature.Level siglevel_mask;
  61. Alpm.Signature.Level localfilesiglevel_mask;
  62. Alpm.Signature.Level remotefilesiglevel_mask;
  63. GLib.List<AlpmRepo> repo_order;
  64. public AlpmConfig (string path) {
  65. conf_path = path;
  66. reload ();
  67. }
  68. public unowned GLib.List<string> get_holdpkgs () {
  69. return holdpkgs;
  70. }
  71. public unowned GLib.List<string> get_syncfirsts () {
  72. return syncfirsts;
  73. }
  74. public void reload () {
  75. // set default options
  76. cachedirs = new GLib.List<string> ();
  77. hookdirs = new GLib.List<string> ();
  78. ignoregroups = new GLib.List<string> ();
  79. ignorepkgs = new GLib.List<string> ();
  80. noextracts = new GLib.List<string> ();
  81. noupgrades = new GLib.List<string> ();
  82. holdpkgs = new GLib.List<string> ();
  83. syncfirsts = new GLib.List<string> ();
  84. usesyslog = 0;
  85. checkspace = 0;
  86. deltaratio = 0.7;
  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 () {
  127. Alpm.Errno error;
  128. Alpm.Handle? handle = new Alpm.Handle (rootdir, dbpath, out error);
  129. if (handle == null) {
  130. stderr.printf ("Failed to initialize alpm library" + " (%s)\n".printf (Alpm.strerror (error)));
  131. return null;
  132. }
  133. // define options
  134. handle.logfile = logfile;
  135. handle.gpgdir = gpgdir;
  136. handle.arch = arch;
  137. handle.deltaratio = deltaratio;
  138. handle.usesyslog = usesyslog;
  139. handle.checkspace = checkspace;
  140. handle.defaultsiglevel = siglevel;
  141. localfilesiglevel = merge_siglevel (siglevel, localfilesiglevel, localfilesiglevel_mask);
  142. remotefilesiglevel = merge_siglevel (siglevel, remotefilesiglevel, remotefilesiglevel_mask);
  143. handle.localfilesiglevel = localfilesiglevel;
  144. handle.remotefilesiglevel = remotefilesiglevel;
  145. foreach (unowned string cachedir in cachedirs) {
  146. handle.add_cachedir (cachedir);
  147. }
  148. foreach (unowned string hookdir in hookdirs) {
  149. handle.add_hookdir (hookdir);
  150. }
  151. foreach (unowned string ignoregroup in ignoregroups) {
  152. handle.add_ignoregroup (ignoregroup);
  153. }
  154. foreach (unowned string ignorepkg in ignorepkgs) {
  155. handle.add_ignorepkg (ignorepkg);
  156. }
  157. foreach (unowned string noextract in noextracts) {
  158. handle.add_noextract (noextract);
  159. }
  160. foreach (unowned string noupgrade in noupgrades) {
  161. handle.add_noupgrade (noupgrade);
  162. }
  163. // register dbs
  164. foreach (unowned AlpmRepo repo in repo_order) {
  165. repo.siglevel = merge_siglevel (siglevel, repo.siglevel, repo.siglevel_mask);
  166. unowned Alpm.DB db = handle.register_syncdb (repo.name, repo.siglevel);
  167. foreach (unowned string url in repo.urls) {
  168. db.add_server (url.replace ("$repo", repo.name).replace ("$arch", handle.arch));
  169. }
  170. if (repo.usage == 0) {
  171. db.usage = Alpm.DB.Usage.ALL;
  172. } else {
  173. db.usage = repo.usage;
  174. }
  175. }
  176. return handle;
  177. }
  178. void parse_file (string path, string? section = null) {
  179. string? current_section = section;
  180. var file = GLib.File.new_for_path (path);
  181. if (file.query_exists ()) {
  182. try {
  183. // Open file for reading and wrap returned FileInputStream into a
  184. // DataInputStream, so we can read line by line
  185. var dis = new DataInputStream (file.read ());
  186. string? line;
  187. // Read lines until end of file (null) is reached
  188. while ((line = dis.read_line ()) != null) {
  189. if (line.length == 0) {
  190. continue;
  191. }
  192. // ignore whole line and end of line comments
  193. string[] splitted = line.split ("#", 2);
  194. line = splitted[0].strip ();
  195. if (line.length == 0) {
  196. continue;
  197. }
  198. if (line[0] == '[' && line[line.length-1] == ']') {
  199. current_section = line[1:-1];
  200. if (current_section != "options") {
  201. var repo = new AlpmRepo (current_section);
  202. if (repo_order.find_custom (repo, AlpmRepo.compare_name) == null) {
  203. repo_order.append ((owned) repo);
  204. }
  205. }
  206. continue;
  207. }
  208. splitted = line.split ("=", 2);
  209. unowned string key = splitted[0]._strip ();
  210. unowned string? val = null;
  211. if (splitted.length == 2) {
  212. val = splitted[1]._strip ();
  213. }
  214. if (key == "Include") {
  215. parse_file (val, current_section);
  216. }
  217. if (current_section == "options") {
  218. if (key == "RootDir") {
  219. rootdir = val;
  220. } else if (key == "DBPath") {
  221. dbpath = val;
  222. } else if (key == "CacheDir") {
  223. foreach (unowned string dir in val.split (" ")) {
  224. cachedirs.append (dir);
  225. }
  226. } else if (key == "HookDir") {
  227. foreach (unowned string dir in val.split (" ")) {
  228. hookdirs.append (dir);
  229. }
  230. } else if (key == "LogFile") {
  231. logfile = val;
  232. } else if (key == "GPGDir") {
  233. gpgdir = val;
  234. } else if (key == "LogFile") {
  235. logfile = val;
  236. } else if (key == "Architecture") {
  237. if (val == "auto") {
  238. arch = Posix.utsname ().machine;
  239. } else {
  240. arch = val;
  241. }
  242. } else if (key == "UseDelta") {
  243. deltaratio = double.parse (val);
  244. } else if (key == "UseSysLog") {
  245. usesyslog = 1;
  246. } else if (key == "CheckSpace") {
  247. checkspace = 1;
  248. } else if (key == "SigLevel") {
  249. process_siglevel (val, ref siglevel, ref siglevel_mask);
  250. } else if (key == "LocalFileSigLevel") {
  251. process_siglevel (val, ref localfilesiglevel, ref localfilesiglevel_mask);
  252. } else if (key == "RemoteFileSigLevel") {
  253. process_siglevel (val, ref remotefilesiglevel, ref remotefilesiglevel_mask);
  254. } else if (key == "HoldPkg") {
  255. foreach (unowned string name in val.split (" ")) {
  256. holdpkgs.append (name);
  257. }
  258. } else if (key == "SyncFirst") {
  259. foreach (unowned string name in val.split (" ")) {
  260. syncfirsts.append (name);
  261. }
  262. } else if (key == "IgnoreGroup") {
  263. foreach (unowned string name in val.split (" ")) {
  264. ignoregroups.append (name);
  265. }
  266. } else if (key == "IgnorePkg") {
  267. foreach (unowned string name in val.split (" ")) {
  268. ignorepkgs.append (name);
  269. }
  270. } else if (key == "Noextract") {
  271. foreach (unowned string name in val.split (" ")) {
  272. noextracts.append (name);
  273. }
  274. } else if (key == "NoUpgrade") {
  275. foreach (unowned string name in val.split (" ")) {
  276. noupgrades.append (name);
  277. }
  278. }
  279. } else {
  280. unowned GLib.List<AlpmRepo>? found = repo_order.search (current_section, (SearchFunc) AlpmRepo.search_name);
  281. if (found != null) {
  282. unowned AlpmRepo repo = found.data;
  283. if (key == "Server") {
  284. repo.urls.append (val);
  285. } else if (key == "SigLevel") {
  286. process_siglevel (val, ref repo.siglevel, ref repo.siglevel_mask);
  287. } else if (key == "Usage") {
  288. repo.usage = define_usage (val);
  289. }
  290. }
  291. }
  292. }
  293. } catch (GLib.Error e) {
  294. GLib.stderr.printf("%s\n", e.message);
  295. }
  296. } else {
  297. GLib.stderr.printf ("File '%s' doesn't exist.\n", path);
  298. }
  299. }
  300. public void write (HashTable<string,Variant> new_conf) {
  301. var file = GLib.File.new_for_path (conf_path);
  302. if (file.query_exists ()) {
  303. try {
  304. // Open file for reading and wrap returned FileInputStream into a
  305. // DataInputStream, so we can read line by line
  306. var dis = new DataInputStream (file.read ());
  307. string? line;
  308. string[] data = {};
  309. // Read lines until end of file (null) is reached
  310. while ((line = dis.read_line ()) != null) {
  311. if (line.length == 0) {
  312. data += "\n";
  313. continue;
  314. }
  315. if (line.contains ("IgnorePkg")) {
  316. if (new_conf.contains ("IgnorePkg")) {
  317. string val = new_conf.get ("IgnorePkg").get_string ();
  318. if (val == "") {
  319. data += "#IgnorePkg =\n";
  320. } else {
  321. data += "IgnorePkg = %s\n".printf (val);
  322. }
  323. new_conf.remove ("IgnorePkg");
  324. } else {
  325. data += line + "\n";
  326. }
  327. } else if (line.contains ("CheckSpace")) {
  328. if (new_conf.contains ("CheckSpace")) {
  329. bool val = new_conf.get ("CheckSpace").get_boolean ();
  330. if (val) {
  331. data += "CheckSpace\n";
  332. } else {
  333. data += "#CheckSpace\n";
  334. }
  335. new_conf.remove ("CheckSpace");
  336. } else {
  337. data += line + "\n";
  338. }
  339. } else {
  340. data += line + "\n";
  341. }
  342. }
  343. // delete the file before rewrite it
  344. file.delete ();
  345. // creating a DataOutputStream to the file
  346. var dos = new DataOutputStream (file.create (FileCreateFlags.REPLACE_DESTINATION));
  347. foreach (unowned string new_line in data) {
  348. // writing a short string to the stream
  349. dos.put_string (new_line);
  350. }
  351. reload ();
  352. } catch (GLib.Error e) {
  353. GLib.stderr.printf("%s\n", e.message);
  354. }
  355. } else {
  356. GLib.stderr.printf ("File '%s' doesn't exist.\n", conf_path);
  357. }
  358. }
  359. Alpm.DB.Usage define_usage (string conf_string) {
  360. Alpm.DB.Usage usage = 0;
  361. foreach (unowned string directive in conf_string.split(" ")) {
  362. if (directive == "Sync") {
  363. usage |= Alpm.DB.Usage.SYNC;
  364. } else if (directive == "Search") {
  365. usage |= Alpm.DB.Usage.SEARCH;
  366. } else if (directive == "Install") {
  367. usage |= Alpm.DB.Usage.INSTALL;
  368. } else if (directive == "Upgrade") {
  369. usage |= Alpm.DB.Usage.UPGRADE;
  370. } else if (directive == "All") {
  371. usage |= Alpm.DB.Usage.ALL;
  372. }
  373. }
  374. return usage;
  375. }
  376. void process_siglevel (string conf_string, ref Alpm.Signature.Level siglevel, ref Alpm.Signature.Level siglevel_mask) {
  377. foreach (unowned string directive in conf_string.split(" ")) {
  378. bool affect_package = false;
  379. bool affect_database = false;
  380. if ("Package" in directive) {
  381. affect_package = true;
  382. } else if ("Database" in directive) {
  383. affect_database = true;
  384. } else {
  385. affect_package = true;
  386. affect_database = true;
  387. }
  388. if ("Never" in directive) {
  389. if (affect_package) {
  390. siglevel &= ~Alpm.Signature.Level.PACKAGE;
  391. siglevel_mask |= Alpm.Signature.Level.PACKAGE;
  392. }
  393. if (affect_database) {
  394. siglevel &= ~Alpm.Signature.Level.DATABASE;
  395. siglevel_mask |= Alpm.Signature.Level.DATABASE;
  396. }
  397. } else if ("Optional" in directive) {
  398. if (affect_package) {
  399. siglevel |= (Alpm.Signature.Level.PACKAGE | Alpm.Signature.Level.PACKAGE_OPTIONAL);
  400. siglevel_mask |= (Alpm.Signature.Level.PACKAGE | Alpm.Signature.Level.PACKAGE_OPTIONAL);
  401. }
  402. if (affect_database) {
  403. siglevel |= (Alpm.Signature.Level.DATABASE | Alpm.Signature.Level.DATABASE_OPTIONAL);
  404. siglevel_mask |= (Alpm.Signature.Level.DATABASE | Alpm.Signature.Level.DATABASE_OPTIONAL);
  405. }
  406. } else if ("Required" in directive) {
  407. if (affect_package) {
  408. siglevel |= Alpm.Signature.Level.PACKAGE;
  409. siglevel_mask |= Alpm.Signature.Level.PACKAGE;
  410. siglevel &= ~Alpm.Signature.Level.PACKAGE_OPTIONAL;
  411. siglevel_mask |= Alpm.Signature.Level.PACKAGE_OPTIONAL;
  412. }
  413. if (affect_database) {
  414. siglevel |= Alpm.Signature.Level.DATABASE;
  415. siglevel_mask |= Alpm.Signature.Level.DATABASE;
  416. siglevel &= ~Alpm.Signature.Level.DATABASE_OPTIONAL;
  417. siglevel_mask |= Alpm.Signature.Level.DATABASE_OPTIONAL;
  418. }
  419. } else if ("TrustedOnly" in directive) {
  420. if (affect_package) {
  421. siglevel &= ~(Alpm.Signature.Level.PACKAGE_MARGINAL_OK | Alpm.Signature.Level.PACKAGE_UNKNOWN_OK);
  422. siglevel_mask |= (Alpm.Signature.Level.PACKAGE_MARGINAL_OK | Alpm.Signature.Level.PACKAGE_UNKNOWN_OK);
  423. }
  424. if (affect_database) {
  425. siglevel &= ~(Alpm.Signature.Level.DATABASE_MARGINAL_OK | Alpm.Signature.Level.DATABASE_UNKNOWN_OK);
  426. siglevel_mask |= (Alpm.Signature.Level.DATABASE_MARGINAL_OK | Alpm.Signature.Level.DATABASE_UNKNOWN_OK);
  427. }
  428. } else if ("TrustAll" in directive) {
  429. if (affect_package) {
  430. siglevel |= (Alpm.Signature.Level.PACKAGE_MARGINAL_OK | Alpm.Signature.Level.PACKAGE_UNKNOWN_OK);
  431. siglevel_mask |= (Alpm.Signature.Level.PACKAGE_MARGINAL_OK | Alpm.Signature.Level.PACKAGE_UNKNOWN_OK);
  432. }
  433. if (affect_database) {
  434. siglevel |= (Alpm.Signature.Level.DATABASE_MARGINAL_OK | Alpm.Signature.Level.DATABASE_UNKNOWN_OK);
  435. siglevel_mask |= (Alpm.Signature.Level.DATABASE_MARGINAL_OK | Alpm.Signature.Level.DATABASE_UNKNOWN_OK);
  436. }
  437. } else {
  438. GLib.stderr.printf("unrecognized siglevel: %s\n", conf_string);
  439. }
  440. }
  441. siglevel &= ~Alpm.Signature.Level.USE_DEFAULT;
  442. }
  443. Alpm.Signature.Level merge_siglevel(Alpm.Signature.Level sigbase, Alpm.Signature.Level sigover, Alpm.Signature.Level sigmask) {
  444. return (sigmask != 0) ? (sigover & sigmask) | (sigbase & ~sigmask) : sigover;
  445. }
  446. }