utilize.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. #include <dirent.h>
  2. #include <rid.h>
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #define STYLE_9
  7. #include <fn85.h>
  8. #include <rid_fn85.h>
  9. #include <unfy/unfy.h>
  10. #include <rls/rls.h>
  11. #include <rls/rlsp.h>
  12. #define NIT_SHORT_NAMES
  13. #include <nit/list.h>
  14. #include <nit/set.h>
  15. #include <nit/map.h>
  16. #include <nit/err.h>
  17. #include <nit/file.h>
  18. #include <nit/buf.h>
  19. #include <nit/gap.h>
  20. #include "utilize.h"
  21. #include "examine.h"
  22. #include "front.h"
  23. #include "config.h"
  24. /* add_________________ */
  25. const Rid ilk_add_id = { 0x1f, 0x98, 0x8a, 0xb8, 0xfb, 0xe0, 0x0e, 0x10,
  26. 0xfb, 0xe0, 0x0e, 0x10, 0xfb, 0xe0, 0x0e, 0x10 };
  27. /* id__________________ */
  28. const Rid ilk_id_id = { 0x38, 0x84, 0x13, 0x2b, 0xfb, 0xe0, 0x0e, 0x10,
  29. 0xfb, 0xe0, 0x0e, 0x10, 0xfb, 0xe0, 0x0e, 0x10 };
  30. /* head________________ */
  31. const Rid ilk_head_id = { 0x35, 0x69, 0x1f, 0x92, 0xfb, 0xe0, 0x0e, 0x10,
  32. 0xfb, 0xe0, 0x0e, 0x10, 0xfb, 0xe0, 0x0e, 0x10 };
  33. /* Core code */
  34. static Ilk_grp *
  35. grp_get(Ilk_sys *sys, const Rid id, Rls_rule *rules)
  36. {
  37. Ilk_grp *grp = sys->lib->old_grps;
  38. if (grp) {
  39. sys->lib->old_grps = (Ilk_grp *) grp->rules;
  40. } else if (!(grp = malloc(sizeof(*grp)))) {
  41. Rid_fn85 str;
  42. err_std(&sys->lib->err);
  43. rid_fn85(str, id);
  44. err_where(&sys->lib->err, str);
  45. return NULL;
  46. }
  47. rid_set(grp->id, id);
  48. grp->rules = rules;
  49. grp->changed = 0;
  50. grp->accessed = 1;
  51. return grp;
  52. }
  53. static void
  54. grp_recycle(Ilk_sys *sys, Ilk_grp *grp)
  55. {
  56. grp->rules = (Rls_rule *) sys->lib->old_grps;
  57. sys->lib->old_grps = grp;
  58. }
  59. Ilk_grp *
  60. ilk_grp_load(Ilk_sys *sys, const Rid id, Unfy_stat *stat)
  61. {
  62. Ilk_rule_path path;
  63. Ilk_grp *grp = grp_get(sys, id, NULL);
  64. if (!grp)
  65. goto err_no_mem;
  66. ilk_rule_path(path, id);
  67. switch (file_exists(&sys->dir, path, &sys->lib->err)) {
  68. case 0:
  69. if (stat) {
  70. *stat = UNFY_NO;
  71. grp_recycle(sys, grp);
  72. return NULL;
  73. }
  74. break;
  75. case 1:
  76. if (ilk_path_grp(grp, path, 1, sys) >= 0)
  77. break;
  78. /* fallthru */
  79. default:
  80. goto err_grp;
  81. }
  82. if (map_add(&sys->grps, id, sizeof(Rid), grp, &sys->lib->stack) < 0) {
  83. err_cust(&sys->lib->err, "Failed to add grp");
  84. goto err_grp;
  85. }
  86. return grp;
  87. err_grp:
  88. grp_recycle(sys, grp);
  89. err_no_mem:
  90. if (stat)
  91. *stat = UNFY_ERR;
  92. return NULL;
  93. }
  94. int
  95. ilk_grp_unload(Ilk_grp *grp, Ilk_sys *sys)
  96. {
  97. if (ilk_grp_save(grp, sys) < 0)
  98. return -1;
  99. ilk_grp_discard(grp, sys);
  100. return 0;
  101. }
  102. void
  103. ilk_grp_discard(Ilk_grp *grp, Ilk_sys *sys)
  104. {
  105. ilk_grp_dispose(grp, sys);
  106. map_remove(&sys->grps, grp->id, sizeof(grp->id), &sys->lib->stack);
  107. grp_recycle(sys, grp);
  108. }
  109. Ilk_grp *
  110. ilk_grp_get(Ilk_sys *sys, const Rid id, Unfy_stat *stat)
  111. {
  112. Ilk_grp *grp = map_get(&sys->grps, id, sizeof(Rid));
  113. if (!grp)
  114. grp = ilk_grp_load(sys, id, stat);
  115. if (grp)
  116. grp->accessed = 1;
  117. return grp;
  118. }
  119. static int
  120. rule_name(const Unfy_term *term, Rid id)
  121. {
  122. if (!term)
  123. return -1;
  124. switch (term->type) {
  125. case UNFY_CONST:
  126. rid_set(id, term->u.id);
  127. return 0;
  128. case UNFY_LIST:
  129. if (!term->u.list || term->u.list->term->type != UNFY_CONST)
  130. return -1;
  131. rid_set(id, term->u.list->term->u.id);
  132. return 0;
  133. default:
  134. break;
  135. }
  136. return -1;
  137. }
  138. Ilk_grp *
  139. ilk_grp_get_term(Ilk_sys *sys, const Unfy_term *term, Unfy_stat *stat)
  140. {
  141. Rid id;
  142. if (rule_name(term, id) < 0) {
  143. if (stat)
  144. *stat = UNFY_ERR;
  145. err_cust(&sys->lib->err, "Invalid rule name");
  146. return NULL;
  147. }
  148. return ilk_grp_get(sys, id, stat);
  149. }
  150. int
  151. ilk_grp_save(Ilk_grp *grp, Ilk_sys *sys)
  152. {
  153. size_t pos = 0;
  154. Ilk_rule_path path;
  155. Nit_file file;
  156. Nit_err tmp;
  157. if (!grp->changed)
  158. return 0;
  159. ilk_rule_path(path, grp->id);
  160. if (rlsp_rule_ser(grp->rules, &sys->lib->buf.bytes, &sys->lib->buf.size,
  161. &pos, &sys->lib->srlz, sys) < 0) {
  162. err_cust(&sys->lib->err, "Failed to serialize rules");
  163. goto err_no_file;
  164. }
  165. if (dir_sub(&sys->dir, path, &sys->lib->err) < 0 ||
  166. file_open(&file, &sys->dir, path, &sys->lib->err) < 0)
  167. goto err_no_file;
  168. if (file_write(&file, grp->id, sizeof(Rid), &sys->lib->err) < 0 ||
  169. file_write(&file, sys->lib->buf.bytes, pos, &sys->lib->err) < 0)
  170. goto err_file;
  171. file_cutoff(&file);
  172. if(file_close(&file, &sys->lib->err) < 0)
  173. goto err_file;
  174. grp->changed = 0;
  175. return 0;
  176. err_file:
  177. file_close(&file, &tmp);
  178. err_no_file:
  179. err_where(&sys->lib->err, path);
  180. return -1;
  181. }
  182. int
  183. ilk_rule_origin(Rls_rule *rule, Rls_srch **srch_ref,
  184. uint64_t srch_cnt, Ilk_sys *sys)
  185. {
  186. Ilk_srch_path path;
  187. Rid_fn85 id_str;
  188. if (rule) {
  189. Ilk_more *more = rule->dat;
  190. /* a priori*/
  191. if (!more->srch_cnt) {
  192. *srch_ref = NULL;
  193. return 0;
  194. }
  195. if (!*srch_ref &&
  196. ilk_srch_get(srch_ref, more->srch_cnt, sys) < 0)
  197. return -1;
  198. }
  199. for (; *srch_ref; rls_srch_next(srch_ref)) {
  200. Unfy_term *id;
  201. if (!rid_cmp((*srch_ref)->rule->id, ilk_add_id) &&
  202. (!rule ||
  203. ((id = unfy_bind_get((*srch_ref)->rbind, ilk_id_id)) &&
  204. !rid_cmp(id->u.id, rule->id))))
  205. return 0;
  206. }
  207. if (!rule)
  208. return 0;
  209. err_cust(&sys->lib->err, "Srch file does not contain rule");
  210. ilk_srch_path(path, srch_cnt);
  211. err_where(&sys->lib->err, path);
  212. rid_fn85(id_str, rule->id);
  213. err_what(&sys->lib->err, id_str);
  214. return -1;
  215. }
  216. int
  217. ilk_meta_init(Ilk_meta *meta, uint64_t srch_cnt, uint64_t deps_len)
  218. {
  219. meta->srch_cnt = srch_cnt;
  220. return gap_init(&meta->deps, deps_len);
  221. }
  222. void
  223. ilk_meta_dispose(Ilk_meta *meta)
  224. {
  225. gap_dispose(&meta->deps);
  226. }
  227. void
  228. ilk_meta_get(Ilk_meta *meta, Rls_srch **srch_ref, Ilk_sys *sys)
  229. {
  230. Unfy_term *id;
  231. Unfy_term *head;
  232. ilk_rule_origin(NULL, srch_ref, meta->srch_cnt, sys);
  233. if (!*srch_ref)
  234. return;
  235. id = unfy_bind_get((*srch_ref)->rbind, ilk_id_id);
  236. rid_set(meta->id, id->u.id);
  237. head = unfy_bind_get((*srch_ref)->rbind, ilk_head_id);
  238. rule_name(head, meta->grp_id);
  239. meta->changed = 1;
  240. }
  241. int
  242. ilk_meta_save(Ilk_sys *sys, Ilk_meta *meta)
  243. {
  244. size_t pos = 0;
  245. size_t size = 0;
  246. unsigned char *buf = NULL;
  247. Ilk_meta_path path;
  248. Nit_file file;
  249. Nit_err tmp;
  250. if (!meta->changed)
  251. return 0;
  252. ilk_meta_path(path, meta->id);
  253. if (ilk_meta_ser(meta, &buf, &size, &pos) < 0) {
  254. err_cust(&sys->lib->err, "Failed to serialize rules");
  255. goto err_no_file;
  256. }
  257. if (dir_sub(&sys->dir, path, &sys->lib->err) < 0 ||
  258. file_open(&file, &sys->dir, path, &sys->lib->err) < 0)
  259. goto err_no_file;
  260. if (file_set(&file, buf, pos, &sys->lib->err) < 0 ||
  261. file_close(&file, &sys->lib->err) < 0)
  262. goto err_file;
  263. free(buf);
  264. meta->changed = 0;
  265. return 0;
  266. err_file:
  267. file_close(&file, &tmp);
  268. err_no_file:
  269. free(buf);
  270. err_where(&sys->lib->err, path);
  271. return -1;
  272. }
  273. int
  274. ilk_meta_load(Ilk_meta *meta, const Rid id, Ilk_sys *sys)
  275. {
  276. Ilk_rule_path path;
  277. ilk_meta_path(path, id);
  278. rid_set(meta->id, id);
  279. return ilk_path_meta(meta, path, 1, sys);
  280. }
  281. int
  282. ilk_meta_dep(Ilk_meta *meta, uint64_t srch_cnt)
  283. {
  284. gap_end(&meta->deps);
  285. return gap_write(&meta->deps, &srch_cnt, sizeof(srch_cnt));
  286. }
  287. static void
  288. clean_grps(Ilk_sys *sys)
  289. {
  290. Nit_iter iter;
  291. int more = 1;
  292. if (!sys->grps.entry_num)
  293. return;
  294. iter_init(&iter, &sys->grps);
  295. while (more) {
  296. Ilk_grp *grp = iter_val(&iter);
  297. more = iter_next(&iter);
  298. if (grp->accessed) {
  299. grp->accessed = 0;
  300. continue;
  301. }
  302. /* If we can't unload, it's no big deal. We'll do it later. */
  303. ilk_grp_unload(grp, sys);
  304. }
  305. }
  306. static void
  307. clean_srchs(Ilk_sys *sys)
  308. {
  309. Nit_iter iter;
  310. int more = 1;
  311. if (!sys->srchs.entry_num)
  312. return;
  313. iter_init(&iter, &sys->srchs);
  314. while (more) {
  315. Ilk_grp *grp = iter_val(&iter);
  316. more = iter_next(&iter);
  317. if (grp->accessed) {
  318. grp->accessed = 0;
  319. continue;
  320. }
  321. /* If we can't unload, it's no big deal. We'll do it later. */
  322. ilk_grp_unload(grp, sys);
  323. }
  324. }
  325. Unfy_stat
  326. ilk_srch(Ilk_sys *sys, Rls_srch **srch_ref, Unfy_term *term,
  327. int *limit, Rls_info *info)
  328. {
  329. Unfy_stat stat;
  330. ++sys->srch_cnt;
  331. stat = rls_srch_limit(srch_ref, term, NULL, limit,
  332. sys->lib->bend, info, sys, &sys->lib->rec);
  333. switch (stat) {
  334. case UNFY_YES:
  335. if (!(sys->srch_cnt % GRP_CLEAN_CYCLE) && !sys->lib->testing)
  336. clean_grps(sys);
  337. if (!(sys->srch_cnt % SRCH_CLEAN_CYCLE) && !sys->lib->testing)
  338. clean_srchs(sys);
  339. break;
  340. case UNFY_ERR:
  341. /* Since rls doesn't use nit err */
  342. if (!sys->lib->err.occured)
  343. err_cust(&sys->lib->err, "Failed searching");
  344. /* fallthru */
  345. case UNFY_NO:
  346. --sys->srch_cnt;
  347. break;
  348. }
  349. return stat;
  350. }
  351. static int
  352. srch_extract_meta(Rls_srch *srch, Ilk_sys *sys, uint64_t srch_cnt)
  353. {
  354. Rls_srch **srch_ref = &srch;
  355. Ilk_meta meta;
  356. ilk_meta_init(&meta, srch_cnt, 0);
  357. while (*srch_ref) {
  358. ilk_meta_get(&meta, srch_ref, sys);
  359. if (!*srch_ref)
  360. break;
  361. if (ilk_meta_save(sys, &meta) < 0) {
  362. ilk_meta_dispose(&meta);
  363. return -1;
  364. }
  365. rls_srch_next(srch_ref);
  366. }
  367. ilk_meta_dispose(&meta);
  368. return 0;
  369. }
  370. static int
  371. srch_update_meta(Rls_srch *srch, Ilk_sys *sys, uint64_t srch_cnt)
  372. {
  373. Ilk_more *more;
  374. uint64_t tmp;
  375. if (!srch)
  376. return 0;
  377. more = srch->rule->dat;
  378. tmp = more->srch_cnt;
  379. /* tmp == 0 => a priori, it has no meta file */
  380. if (tmp != srch_cnt && tmp) {
  381. Ilk_meta_path path;
  382. ilk_meta_path(path, srch->rule->id);
  383. if (ilk_path_meta_dep(sys, path, srch_cnt) < 0)
  384. return -1;
  385. }
  386. more->srch_cnt = srch_cnt;
  387. rls_srch_next(&srch);
  388. if (srch_update_meta(srch, sys, srch_cnt) < 0) {
  389. more->srch_cnt = tmp;
  390. return -1;
  391. }
  392. more->srch_cnt = tmp;
  393. return 0;
  394. }
  395. int
  396. ilk_srch_save(Ilk_sys *sys, Rls_srch *srch, uint64_t srch_cnt)
  397. {
  398. size_t pos = 0;
  399. Ilk_srch_path path;
  400. Nit_file file;
  401. Nit_err tmp;
  402. if (srch_extract_meta(srch, sys, srch_cnt) < 0)
  403. return -1;
  404. if (srch_update_meta(srch, sys, srch_cnt) < 0)
  405. return -1;
  406. if (rlsp_srch_ser(srch, &sys->lib->buf.bytes, &sys->lib->buf.size, &pos)) {
  407. err_cust(&sys->lib->err, "Failed to serialize srch");
  408. return -1;
  409. }
  410. ilk_srch_path(path, srch_cnt);
  411. switch (file_exists(&sys->dir, path, &sys->lib->err)) {
  412. case 0:
  413. break;
  414. case 1:
  415. err_cust(&sys->lib->err, "Srch file already exists");
  416. /* fallthru */
  417. default:
  418. goto err_no_file;
  419. }
  420. if (dir_sub(&sys->dir, path, &sys->lib->err) < 0 ||
  421. file_open(&file, &sys->dir, path, &sys->lib->err) < 0)
  422. goto err_no_file;
  423. if (file_set(&file, sys->lib->buf.bytes, pos, &sys->lib->err) < 0 ||
  424. file_close(&file, &sys->lib->err) < 0)
  425. goto err_file;
  426. return 0;
  427. err_file:
  428. file_close(&file, &tmp);
  429. err_no_file:
  430. err_where(&sys->lib->err, path);
  431. return -1;
  432. }
  433. int
  434. ilk_srch_get(Rls_srch **srch_ref, uint64_t srch_cnt, Ilk_sys *sys)
  435. {
  436. if ((*srch_ref = map_get(&sys->srchs, &srch_cnt, sizeof(srch_cnt))))
  437. return 0;
  438. return ilk_srch_load(srch_ref, srch_cnt, sys);
  439. }
  440. int
  441. ilk_srch_load(Rls_srch **srch_ref, uint64_t srch_cnt, Ilk_sys *sys)
  442. {
  443. Ilk_rule_path path;
  444. ilk_srch_path(path, srch_cnt);
  445. if (ilk_path_srch(srch_ref, path, sys) < 0)
  446. return -1;
  447. if (map_add(&sys->srchs, &srch_cnt, sizeof(srch_cnt),
  448. *srch_ref, &sys->lib->stack) < 0) {
  449. err_cust(&sys->lib->err, "Failed to add srch");
  450. rls_recycle_srch(&sys->lib->rec, *srch_ref);
  451. *srch_ref = NULL;
  452. return -1;
  453. }
  454. return 0;
  455. }
  456. int
  457. ilk_srch_unload(uint64_t srch_cnt, Ilk_sys *sys)
  458. {
  459. Rls_srch *srch = map_get(&sys->srchs, &srch_cnt, sizeof(srch_cnt));
  460. if (!srch) {
  461. err_cust(&sys->lib->err, "Srch not found in unloading");
  462. return -1;
  463. }
  464. if (ilk_srch_save(sys, srch, srch_cnt) < 0)
  465. return -1;
  466. map_remove(&sys->srchs, &srch_cnt, sizeof(srch_cnt), &sys->lib->stack);
  467. rls_recycle_srch(&sys->lib->rec, srch);
  468. return 0;
  469. }