msk.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /* vim: ts=4 sw=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2018 K. Lange
  5. *
  6. * msk - Package Management Utility for ToaruOS
  7. *
  8. * This is a not-quite-faithful reconstruction of the original
  9. * Python msk. The supported package format is a bit different,
  10. * to avoid the need to implement a full JSON parser.
  11. *
  12. * Packages can optionally be uncompressed, which is also
  13. * important for bootstrapping at the moment.
  14. */
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <unistd.h>
  18. #include <fcntl.h>
  19. #include <sys/stat.h>
  20. #include <toaru/confreader.h>
  21. #include <toaru/list.h>
  22. #include <toaru/hashmap.h>
  23. #define MSK_VERSION "1.0.0"
  24. #define VAR_PATH "/var/msk"
  25. #define LOCK_PATH "/var/run/msk.lock"
  26. static confreader_t * msk_config = NULL;
  27. static confreader_t * msk_manifest = NULL;
  28. static hashmap_t * msk_installed = NULL;
  29. static int lock_fd = -1;
  30. static int verbose = 0;
  31. static void release_lock(void) {
  32. if (lock_fd != -1) {
  33. unlink(LOCK_PATH);
  34. }
  35. }
  36. static void needs_lock(void) {
  37. if (lock_fd == -1) {
  38. lock_fd = open(LOCK_PATH, O_RDWR|O_CREAT|O_EXCL);
  39. if (lock_fd < 0) {
  40. fprintf(stderr, "msk: failed to obtain exclusive lock\n");
  41. exit(1);
  42. }
  43. atexit(release_lock);
  44. }
  45. }
  46. /**
  47. * checks whether 'candidate' is newer than 'current'.
  48. *
  49. * Requires version strings to be of the form x.y.z
  50. *
  51. * > 0 candidate is newer
  52. * = 0 candidate is the same
  53. * < 0 candidate is older
  54. */
  55. static int compare_version_strings(char * current, char * candidate) {
  56. int current_x, current_y, current_z;
  57. int candidate_x, candidate_y, candidate_z;
  58. sscanf(current, "%d.%d.%d", &current_x, &current_y, &current_z);
  59. sscanf(candidate, "%d.%d.%d", &candidate_x, &candidate_y, &candidate_z);
  60. if (candidate_x > current_x) {
  61. return 1;
  62. } else if (candidate_x == current_x) {
  63. if (candidate_y >current_y) {
  64. return 1;
  65. } else if (candidate_y == current_y) {
  66. if (candidate_z > current_z) {
  67. return 1;
  68. } else if (candidate_z == current_z) {
  69. return 0;
  70. }
  71. }
  72. }
  73. return -1;
  74. }
  75. static void read_config(void) {
  76. confreader_t * conf = confreader_load("/etc/msk.conf");
  77. if (!conf) {
  78. fprintf(stderr, "failed to read configuration file\n");
  79. exit(1);
  80. }
  81. if (!strcmp(confreader_getd(conf, "", "verbose",""), "y")) {
  82. verbose = 1;
  83. }
  84. msk_config = conf;
  85. }
  86. static void read_manifest(int required) {
  87. confreader_t * conf = confreader_load(VAR_PATH "/manifest");
  88. if (!conf) {
  89. if (required) {
  90. fprintf(stderr, "no manifest; try `msk update` first\n");
  91. exit(1);
  92. } else {
  93. conf = confreader_create_empty();
  94. }
  95. }
  96. msk_manifest = conf;
  97. }
  98. static void read_installed(void) {
  99. msk_installed = hashmap_create(10);
  100. FILE * installed = fopen(VAR_PATH "/installed", "r");
  101. if (!installed) return;
  102. while (!feof(installed)) {
  103. char tmp[128] = {0};
  104. if (!fgets(tmp, 128, installed)) break;
  105. char * nl = strstr(tmp, "\n");
  106. if (nl) *nl = '\0';
  107. char * eqeq = strstr(tmp, "==");
  108. if (!eqeq) {
  109. fprintf(stderr, "Installation cache is malformed\n");
  110. fprintf(stderr, "line was: [%s]\n", tmp);
  111. exit(1);
  112. }
  113. *eqeq = '\0';
  114. char * version = eqeq+2;
  115. hashmap_set(msk_installed, tmp, strdup(version));
  116. }
  117. }
  118. static void make_var(void) {
  119. struct stat buf;
  120. if (stat(VAR_PATH, &buf)) {
  121. mkdir(VAR_PATH, 0755);
  122. }
  123. }
  124. static void needs_root(void) {
  125. if (geteuid() != 0) {
  126. fprintf(stderr, "only root can install packages; try `sudo`\n");
  127. exit(1);
  128. }
  129. }
  130. static int usage(int argc, char * argv[]) {
  131. #define _IT "\033[3m"
  132. #define _END "\033[0m\n"
  133. fprintf(stderr,
  134. "%s - package manager " MSK_VERSION "\n"
  135. "\n"
  136. "usage: %s update\n"
  137. " %s install [PACKAGE...]\n"
  138. "\n"
  139. " update " _IT "update local manifest from remote" _END
  140. " install " _IT "install packages" _END
  141. "\n", argv[0], argv[0], argv[0]);
  142. return 1;
  143. }
  144. static int update_stores(int argc, char * argv[]) {
  145. needs_root();
  146. if (argc > 2) {
  147. fprintf(stderr,"%s: %s: unexpected arguments in command\n", argv[0], argv[1]);
  148. return usage(argc,argv);
  149. }
  150. needs_lock();
  151. read_config();
  152. make_var();
  153. confreader_t * manifest_out = confreader_create_empty();
  154. hashmap_t * remotes = hashmap_get(msk_config->sections, "remotes");
  155. int one_success = 0;
  156. char * order = strdup(confreader_getd(msk_config, "", "remote_order", ""));
  157. char * save;
  158. char * tok = strtok_r(order, ",", &save);
  159. do {
  160. char * remote_name = strdup(tok);
  161. char * remote_path = hashmap_get(remotes, remote_name);
  162. if (!remote_path) {
  163. fprintf(stderr, "Undefined remote specified in remote_order: %s\n", remote_name);
  164. goto _next;
  165. }
  166. confreader_t * manifest;
  167. if (remote_path[0] == '/') {
  168. char source[512];
  169. sprintf(source, "%s/manifest", remote_path);
  170. manifest = confreader_load(source);
  171. if (!manifest) {
  172. fprintf(stderr, "Skipping unavailable local manifest '%s'.\n", remote_name);
  173. goto _next;
  174. }
  175. } else {
  176. char cmd[512];
  177. sprintf(cmd, "fetch -vo /tmp/.msk_remote_%s %s/manifest", remote_name, remote_path);
  178. fprintf(stderr, "Downloading remote manifest '%s'...\n", remote_name);
  179. if (system(cmd)) {
  180. fprintf(stderr, "Error loading remote '%s' from '%s'.\n", remote_name, remote_path);
  181. goto _next;
  182. }
  183. sprintf(cmd, "/tmp/.msk_remote_%s", remote_name);
  184. manifest = confreader_load(cmd);
  185. }
  186. list_t * packages = hashmap_keys(manifest->sections);
  187. foreach(nnode, packages) {
  188. char * package_name = (char*)nnode->value;
  189. hashmap_t * package_data = (hashmap_t*)hashmap_get(manifest->sections, package_name);
  190. if (!strcmp(package_name,"")) continue; /* skip intro section - remote repo information */
  191. hashmap_set(package_data, "remote_path", remote_path);
  192. hashmap_set(package_data, "remote_name", remote_name);
  193. if (!hashmap_has(manifest_out->sections, package_name)) {
  194. /* Package not yet known */
  195. hashmap_set(manifest_out->sections, package_name, package_data);
  196. } else {
  197. /* Package is known, keep the newer version */
  198. char * old_version = confreader_get(manifest_out, package_name, "version");
  199. char * new_version = confreader_get(manifest, package_name, "version");
  200. if (compare_version_strings(old_version, new_version) > 0) {
  201. hashmap_set(manifest_out->sections, package_name, package_data);
  202. }
  203. }
  204. }
  205. one_success = 1;
  206. _next:
  207. tok = strtok_r(NULL, " ", &save);
  208. } while (tok);
  209. free(order);
  210. if (!one_success) {
  211. fprintf(stderr, "\033[1;31merror\033[0m: no remote succeeded, no packages are available\n");
  212. return 1;
  213. }
  214. return confreader_write(manifest_out, VAR_PATH "/manifest");
  215. }
  216. static int list_contains(list_t * list, char * key) {
  217. foreach(node, list) {
  218. char * v = node->value;
  219. if (!strcmp(v,key)) return 1;
  220. }
  221. return 0;
  222. }
  223. static int process_package(list_t * pkgs, char * name) {
  224. if (hashmap_has(msk_installed, name)) return 0;
  225. if (list_contains(pkgs, name)) return 0;
  226. if (!hashmap_has(msk_manifest->sections, name)) {
  227. fprintf(stderr, "don't know how to install '%s'\n", name);
  228. return 1;
  229. }
  230. /* Gather dependencies */
  231. char * tmp = confreader_get(msk_manifest, name, "dependencies");
  232. if (strlen(tmp)) {
  233. char * deps = strdup(tmp);
  234. char * save;
  235. char * tok = strtok_r(deps, " ", &save);
  236. do {
  237. process_package(pkgs, tok);
  238. tok = strtok_r(NULL, " ", &save);
  239. } while (tok);
  240. free(deps);
  241. }
  242. /* Insert */
  243. list_insert(pkgs, strdup(name));
  244. return 0;
  245. }
  246. static int install_package(char * pkg) {
  247. char * type = confreader_getd(msk_manifest, pkg, "type", "");
  248. char * msk_remote = confreader_get(msk_manifest, pkg, "remote_path");
  249. if (strstr(msk_remote, "http:") == msk_remote) {
  250. char * source = confreader_get(msk_manifest, pkg, "source");
  251. if (source) {
  252. fprintf(stderr, "Download %s...\n", pkg);
  253. char cmd[1024];
  254. sprintf(cmd, "fetch -o /tmp/msk.file -v %s/%s", msk_remote,
  255. source);
  256. system(cmd);
  257. hashmap_set(hashmap_get(msk_manifest->sections, pkg), "source", "/tmp/msk.file");
  258. }
  259. }
  260. fprintf(stderr, "Install '%s'...\n", pkg);
  261. if (!strcmp(type, "file")) {
  262. /* Legacy single-file package, has a source and a destination */
  263. if (verbose) {
  264. fprintf(stderr, " - Copy file '%s' to '%s' and set its mask to '%s'\n",
  265. confreader_get(msk_manifest, pkg, "source"),
  266. confreader_get(msk_manifest, pkg, "destination"),
  267. confreader_get(msk_manifest, pkg, "mask"));
  268. }
  269. char cmd[1024];
  270. sprintf(cmd, "cp %s %s; chmod 0%s %s",
  271. confreader_get(msk_manifest, pkg, "source"),
  272. confreader_get(msk_manifest, pkg, "destination"),
  273. confreader_get(msk_manifest, pkg, "mask"),
  274. confreader_get(msk_manifest, pkg, "destination"));
  275. int status;
  276. if ((status = system(cmd))) {
  277. fprintf(stderr, "installation command returned %d\n", status);
  278. return status;
  279. }
  280. } else if (!strcmp(type, "tar")) {
  281. /* Uncompressed archive */
  282. if (verbose) {
  283. fprintf(stderr, " - Extract '%s' to '%s'\n",
  284. confreader_get(msk_manifest, pkg, "source"),
  285. confreader_get(msk_manifest, pkg, "destination"));
  286. }
  287. char cmd[1024];
  288. sprintf(cmd, "cd %s; tar -xf %s",
  289. confreader_get(msk_manifest, pkg, "destination"),
  290. confreader_get(msk_manifest, pkg, "source"));
  291. int status;
  292. if ((status = system(cmd))) {
  293. fprintf(stderr, "installation command returned %d\n", status);
  294. return status;
  295. }
  296. } else if (!strcmp(type, "tgz")) {
  297. /* Compressed archive */
  298. if (verbose) {
  299. fprintf(stderr, " - Extract (compressed) '%s' to '%s'\n",
  300. confreader_get(msk_manifest, pkg, "source"),
  301. confreader_get(msk_manifest, pkg, "destination"));
  302. }
  303. char cmd[1024];
  304. sprintf(cmd, "cd %s; tar -xzf %s",
  305. confreader_get(msk_manifest, pkg, "destination"),
  306. confreader_get(msk_manifest, pkg, "source"));
  307. int status;
  308. if ((status = system(cmd))) {
  309. fprintf(stderr, "installation command returned %d\n", status);
  310. return status;
  311. }
  312. } else if (!strcmp(type, "meta")) {
  313. /* Do nothing */
  314. } else {
  315. fprintf(stderr, "Unknown package type: %s\n", type);
  316. return 1;
  317. }
  318. char * post = confreader_getd(msk_manifest, pkg, "post", "");
  319. if (strlen(post)) {
  320. int status;
  321. if ((status = system(post))) {
  322. fprintf(stderr, "post-installation command returned %d\n", status);
  323. return status;
  324. }
  325. }
  326. /* Mark as installed */
  327. FILE * installed = fopen(VAR_PATH "/installed", "a");
  328. fprintf(installed, "%s==%s\n", pkg, confreader_get(msk_manifest, pkg, "version"));
  329. fclose(installed);
  330. return 0;
  331. }
  332. static int install_packages(int argc, char * argv[]) {
  333. needs_root();
  334. needs_lock();
  335. read_config();
  336. read_manifest(1);
  337. read_installed();
  338. /* Go through each package and find its dependencies */
  339. list_t * ordered = list_create();
  340. for (int i = 2; i < argc; ++i) {
  341. if (process_package(ordered, argv[i])) {
  342. return 1;
  343. }
  344. }
  345. /* Additional packages must be installed, let's ask. */
  346. if (ordered->length != (unsigned int)(argc - 2) && !getenv("MSK_YES")) {
  347. fprintf(stderr, "The following packages will be installed:\n");
  348. fprintf(stderr, " ");
  349. int notfirst = 0;
  350. foreach(node, ordered) {
  351. fprintf(stderr, "%s%s", notfirst ? " " : "", (char*)node->value);
  352. notfirst = 1;
  353. }
  354. fprintf(stderr, "\nContinue? [Y/n] ");
  355. fflush(stdout);
  356. char resp[4];
  357. fgets(resp, 4, stdin);
  358. if (!(!strcmp(resp,"\n") || !strcmp(resp,"y\n") || !strcmp(resp,"Y\n") || !strcmp(resp,"yes\n"))) {
  359. fprintf(stderr, "Aborting.\n");
  360. return 1;
  361. }
  362. }
  363. foreach(node, ordered) {
  364. if (install_package(node->value)) {
  365. return 1;
  366. }
  367. }
  368. return 0;
  369. }
  370. static int list_packages(int argc, char * argv[]) {
  371. read_config();
  372. read_manifest(0);
  373. read_installed();
  374. /* Go through sections */
  375. list_t * packages = hashmap_keys(msk_manifest->sections);
  376. foreach(node, packages) {
  377. char * name = node->value;
  378. if (!strlen(name)) continue; /* skip empty section */
  379. char * desc = confreader_get(msk_manifest, name, "description");
  380. fprintf(stderr, " %c %20s %s\n", hashmap_has(msk_installed, name) ? 'I' : ' ', name, desc);
  381. /* TODO: Installation status */
  382. }
  383. return 0;
  384. }
  385. static int count_packages(int argc, char * argv[]) {
  386. read_config();
  387. read_manifest(0);
  388. read_installed();
  389. int installed = 0;
  390. int available = 0;
  391. /* Go through sections */
  392. list_t * packages = hashmap_keys(msk_manifest->sections);
  393. foreach(node, packages) {
  394. char * name = node->value;
  395. if (!strlen(name)) continue; /* skip empty section */
  396. available++;
  397. if (hashmap_has(msk_installed, name)) {
  398. installed++;
  399. }
  400. }
  401. fprintf(stdout, "%d installed; %d available\n", installed, available);
  402. return 0;
  403. }
  404. static int version(void) {
  405. fprintf(stderr, "msk " MSK_VERSION "\n");
  406. return 0;
  407. }
  408. int main(int argc, char * argv[]) {
  409. if (argc < 2) {
  410. return usage(argc,argv);
  411. } else if (!strcmp(argv[1],"--version")) {
  412. return version();
  413. } else if (!strcmp(argv[1],"update")) {
  414. return update_stores(argc,argv);
  415. } else if (!strcmp(argv[1], "install")) {
  416. return install_packages(argc,argv);
  417. } else if (!strcmp(argv[1], "list")) {
  418. return list_packages(argc, argv);
  419. } else if (!strcmp(argv[1], "count")) {
  420. return count_packages(argc, argv);
  421. } else {
  422. fprintf(stderr, "%s: unknown command '%s'\n", argv[0], argv[1]);
  423. return usage(argc,argv);
  424. }
  425. }