Changeset 07ad2653e890ccb2dabfb826b6396a54666afe20

Show
Ignore:
Timestamp:
02/11/08 23:00:25 (6 years ago)
Author:
Theo Schlossnagle <jesus@omniti.com>
git-committer:
Theo Schlossnagle <jesus@omniti.com> 1202770825 +0000
git-parent:

[a9f9cf219e5f72b7ab7572aa93337ea47935eb23]

git-author:
Theo Schlossnagle <jesus@omniti.com> 1202770825 +0000
Message:

support writing out the config

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • src/noit_conf.c

    r67a2095 r07ad265  
    77 
    88#include <stdio.h> 
     9#include <unistd.h> 
     10#include <fcntl.h> 
     11#include <errno.h> 
    912#include <assert.h> 
    1013#include <libxml/parser.h> 
     
    2124static noit_hash_table _tmp_config = NOIT_HASH_EMPTY; 
    2225static xmlDocPtr master_config = NULL; 
     26static char master_config_file[PATH_MAX] = ""; 
    2327static xmlXPathContextPtr xpath_ctxt = NULL; 
    2428 
     
    6367    master_config = new_config; 
    6468    xpath_ctxt = xmlXPathNewContext(master_config); 
     69    realpath(path, master_config_file); 
    6570    return 0; 
    6671  } 
     
    511516} 
    512517 
     518static int 
     519noit_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} 
     523static int 
     524noit_console_close_xml(void *vncct) { 
     525  return 0; 
     526} 
     527static int 
     528noit_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} 
     540static int 
     541noit_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 
    513579static 
    514580void register_console_config_commands() { 
    515   noit_console_state_t *tl, *_conf_state, *_conf_t_state; 
     581  noit_console_state_t *tl, *_conf_state, *_conf_t_state, 
     582                       *_write_state; 
    516583 
    517584  tl = noit_console_state_initial(); 
     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)); 
    518594 
    519595  _conf_t_state = calloc(1, sizeof(*_conf_t_state)); 
     
    524600  noit_console_state_add_cmd(_conf_t_state, 
    525601    NCSCMD("cd", noit_console_config_cd, NULL, NULL)); 
     602  noit_console_state_add_cmd(_conf_t_state, 
     603    NCSCMD("write", noit_console_state_delegate, _write_state, NULL)); 
    526604 
    527605  _conf_state = calloc(1, sizeof(*_conf_state)); 
     
    531609  noit_console_state_add_cmd(tl, 
    532610    NCSCMD("configure", noit_console_state_delegate, _conf_state, NULL)); 
    533 
     611  noit_console_state_add_cmd(tl, 
     612    NCSCMD("write", noit_console_state_delegate, _write_state, NULL)); 
     613