| | 518 | static int |
|---|
| | 519 | noit_console_write_xml(void *vncct, const char *buffer, int len) { |
|---|
| | 520 | noit_console_closure_t ncct = vncct; |
|---|
| | 521 | return nc_write(ncct, buffer, len); |
|---|
| | 522 | } |
|---|
| | 523 | static int |
|---|
| | 524 | noit_console_close_xml(void *vncct) { |
|---|
| | 525 | return 0; |
|---|
| | 526 | } |
|---|
| | 527 | static int |
|---|
| | 528 | noit_conf_write_terminal(noit_console_closure_t ncct, |
|---|
| | 529 | int argc, char **argv, |
|---|
| | 530 | noit_console_state_t *state, void *closure) { |
|---|
| | 531 | xmlOutputBufferPtr out; |
|---|
| | 532 | xmlCharEncodingHandlerPtr enc; |
|---|
| | 533 | enc = xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF8); |
|---|
| | 534 | out = xmlOutputBufferCreateIO(noit_console_write_xml, |
|---|
| | 535 | noit_console_close_xml, |
|---|
| | 536 | ncct, enc); |
|---|
| | 537 | xmlSaveFileTo(out, master_config, "utf8"); |
|---|
| | 538 | return 0; |
|---|
| | 539 | } |
|---|
| | 540 | static int |
|---|
| | 541 | noit_conf_write_file(noit_console_closure_t ncct, |
|---|
| | 542 | int argc, char **argv, |
|---|
| | 543 | noit_console_state_t *state, void *closure) { |
|---|
| | 544 | int fd, len; |
|---|
| | 545 | char master_file_tmp[PATH_MAX]; |
|---|
| | 546 | xmlOutputBufferPtr out; |
|---|
| | 547 | xmlCharEncodingHandlerPtr enc; |
|---|
| | 548 | |
|---|
| | 549 | snprintf(master_file_tmp, sizeof(master_file_tmp), |
|---|
| | 550 | "%s.tmp", master_config_file); |
|---|
| | 551 | unlink(master_file_tmp); |
|---|
| | 552 | fd = open(master_file_tmp, O_CREAT|O_EXCL|O_WRONLY, 0640); |
|---|
| | 553 | if(fd < 0) { |
|---|
| | 554 | nc_printf(ncct, "Failed to open tmp file: %s\n", strerror(errno)); |
|---|
| | 555 | return -1; |
|---|
| | 556 | } |
|---|
| | 557 | enc = xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF8); |
|---|
| | 558 | out = xmlOutputBufferCreateFd(fd, enc); |
|---|
| | 559 | if(!out) { |
|---|
| | 560 | close(fd); |
|---|
| | 561 | unlink(master_file_tmp); |
|---|
| | 562 | nc_printf(ncct, "internal error: OutputBufferCreate failed\n"); |
|---|
| | 563 | return -1; |
|---|
| | 564 | } |
|---|
| | 565 | len = xmlSaveFileTo(out, master_config, "utf8"); |
|---|
| | 566 | close(fd); |
|---|
| | 567 | if(len <= 0) { |
|---|
| | 568 | nc_printf(ncct, "internal error: writing to tmp file failed.\n"); |
|---|
| | 569 | return -1; |
|---|
| | 570 | } |
|---|
| | 571 | if(rename(master_file_tmp, master_config_file) != 0) { |
|---|
| | 572 | nc_printf(ncct, "Failed to replace file: %s\n", strerror(errno)); |
|---|
| | 573 | return -1; |
|---|
| | 574 | } |
|---|
| | 575 | nc_printf(ncct, "%d bytes written.\n", len); |
|---|
| | 576 | return 0; |
|---|
| | 577 | } |
|---|
| | 578 | |
|---|
| | 585 | |
|---|
| | 586 | _write_state = calloc(1, sizeof(*_write_state)); |
|---|
| | 587 | noit_console_state_add_cmd(_write_state, |
|---|
| | 588 | NCSCMD("terminal", noit_conf_write_terminal, NULL, NULL)); |
|---|
| | 589 | noit_console_state_add_cmd(_write_state, |
|---|
| | 590 | NCSCMD("file", noit_conf_write_file, NULL, NULL)); |
|---|
| | 591 | /* write mememory? It's to a file, but I like router syntax */ |
|---|
| | 592 | noit_console_state_add_cmd(_write_state, |
|---|
| | 593 | NCSCMD("memory", noit_conf_write_file, NULL, NULL)); |
|---|