| 581 | | static |
|---|
| 582 | | const char *template_description(const char *tbasedir, const char *name) |
|---|
| 583 | | { |
|---|
| 584 | | char *tconf = NULL; |
|---|
| 585 | | asprintf(&tconf, "%s/%s.conf", tbasedir, name); |
|---|
| 586 | | |
|---|
| 587 | | if (str_isempty(tconf) || !isfile(tconf)) |
|---|
| 588 | | return "(none)"; |
|---|
| 589 | | |
|---|
| 590 | | cfg_t *tcfg = cfg_init(BUILD_OPTS, CFGF_NOCASE); |
|---|
| 591 | | |
|---|
| 592 | | switch (cfg_parse(tcfg, tconf)) { |
|---|
| 593 | | case CFG_FILE_ERROR: |
|---|
| 594 | | case CFG_PARSE_ERROR: |
|---|
| 595 | | mem_free(tconf); |
|---|
| 596 | | return "(parse error in configuration)"; |
|---|
| 597 | | |
|---|
| 598 | | default: |
|---|
| 599 | | mem_free(tconf); |
|---|
| 600 | | break; |
|---|
| 601 | | } |
|---|
| 602 | | |
|---|
| 603 | | const char *description = cfg_getstr(tcfg, "description"); |
|---|
| 604 | | |
|---|
| 605 | | return str_isempty(description) ? "(none)" : description; |
|---|
| 606 | | } |
|---|
| 607 | | |
|---|
| 608 | | /* vx.templates([string name]) */ |
|---|
| 609 | | xmlrpc_value *m_vx_templates(xmlrpc_env *env, xmlrpc_value *p, void *c) |
|---|
| 610 | | { |
|---|
| 611 | | LOG_TRACEME |
|---|
| 612 | | |
|---|
| 613 | | xmlrpc_value *params; |
|---|
| 614 | | |
|---|
| 615 | | params = method_init(env, p, c, VCD_CAP_CREATE, 0); |
|---|
| 616 | | method_return_if_fault(env); |
|---|
| 617 | | |
|---|
| 618 | | xmlrpc_decompose_value(env, params, |
|---|
| 619 | | "{s:s,*}", |
|---|
| 620 | | "name", &name); |
|---|
| 621 | | method_return_if_fault(env); |
|---|
| 622 | | |
|---|
| 623 | | int curfd = open_read("."); |
|---|
| 624 | | |
|---|
| 625 | | /* get template dir */ |
|---|
| 626 | | const char *tbasedir = cfg_getstr(cfg, "templatedir"); |
|---|
| 627 | | |
|---|
| 628 | | if (chdir(tbasedir) == -1) |
|---|
| 629 | | method_return_sys_faultf(env, "chdir(%s)", tbasedir); |
|---|
| 630 | | |
|---|
| 631 | | DIR *tfd = opendir("."); |
|---|
| 632 | | |
|---|
| 633 | | if (tfd == NULL) |
|---|
| 634 | | method_return_sys_faultf(env, "opendir(%s)", tbasedir); |
|---|
| 635 | | |
|---|
| 636 | | struct dirent *dent; |
|---|
| 637 | | xmlrpc_value *response = xmlrpc_array_new(env); |
|---|
| 638 | | |
|---|
| 639 | | /* list all templates */ |
|---|
| 640 | | while ((dent = readdir(tfd))) { |
|---|
| 641 | | if (str_equal(dent->d_name, ".") || str_equal(dent->d_name, "..") || |
|---|
| 642 | | (!str_isempty(name) && !str_equal(name, dent->d_name))) |
|---|
| 643 | | continue; |
|---|
| 644 | | |
|---|
| 645 | | if (isdir(dent->d_name)) { |
|---|
| 646 | | const char *description = template_description(tbasedir, |
|---|
| 647 | | dent->d_name); |
|---|
| 648 | | |
|---|
| 649 | | xmlrpc_array_append_item(env, response, xmlrpc_build_value(env, |
|---|
| 650 | | "{s:s,s:s}", |
|---|
| 651 | | "name", dent->d_name, |
|---|
| 652 | | "description", description)); |
|---|
| 653 | | } |
|---|
| 654 | | } |
|---|
| 655 | | |
|---|
| 656 | | closedir(tfd); |
|---|
| 657 | | fchdir(curfd); |
|---|
| 658 | | close(curfd); |
|---|
| 659 | | |
|---|
| 660 | | return response; |
|---|
| 661 | | } |
|---|
| 662 | | |
|---|