Roxen.git / server / base_server / read_config.pike

version» Context lines:

Roxen.git/server/base_server/read_config.pike:1:   // This file is part of Roxen WebServer.   // Copyright © 1996 - 2009, Roxen IS. - // $Id: read_config.pike,v 1.72 2009/11/16 14:18:20 grubba Exp $ + // $Id$      #include <module.h>      #ifndef IN_INSTALL   inherit "newdecode";   #else   import spider;   # include "newdecode.pike"   #endif   
Roxen.git/server/base_server/read_config.pike:28:    mkdirhier(configuration_dir+"test"); // removes the last element..    fii=get_dir(configuration_dir);    if(!fii)    {    report_fatal("I cannot read from the configurations directory ("+    combine_path(getcwd(), configuration_dir)+")\n");    roxenloader.real_exit(-1); // Restart.    }    return ({});    } -  return map(filter(fii, lambda(string s){ -  if(s=="CVS" || s=="Global_Variables" || s=="Global Variables" -  || s=="global_variables" || s=="global variables" || s=="server_version" -  || s[0] == '_') +  return Array.uniq(map(filter(fii, lambda(string s){ +  if(s == "CVS" || s == "Global_Variables" || s == "Global Variables" || +  s == "global_variables" || s == "global variables" || +  s == "server_version" || +  s[0] == '_' || s[0] == '.' || s[0] == '#' || s[-1] == '~')    return 0; -  return (s[-1]!='~' && s[0]!='#' && s[0]!='.'); -  }), lambda(string s) { return replace(utf8_to_string(s), "_", " "); }); +  return 1; +  }), lambda(string s) { +  if (has_suffix(s, "~")) { +  if (has_suffix(s, "~2~")) +  s = s[..<3]; +  else if (has_suffix(s, ".new~")) +  s = s[..<5]; +  else +  s = s[..<1];    } -  +  return replace(utf8_to_string(s), "_", " "); +  })); + }         private mapping call_outs = ([]);   private Thread.Mutex call_outs_mutex = Thread.Mutex();   private int counter = 0;   void save_it(string cl, mapping data)   {    Thread.MutexKey lock = call_outs_mutex->lock();    if( call_outs[ cl ] ) {   #ifdef DEBUG_CONFIG
Roxen.git/server/base_server/read_config.pike:101:    }   #endif    string data = encode_regions( data, config );    int num = fd->write( data );       if(num != strlen(data))    error("Failed to write all data to new config file ("+new+")"    " ("+strerror(fd->errno())+")"    "\n");    +  if (fd->sync) { +  // Make sure that the data is synced to the filesystem, +  // some filesystems (eg ext4) otherwise may lose data +  // on reboot due to inodes being updated before data. +  fd->sync(); +  }    fd->close();       fd = open( new, "r" );       if(!fd)    error("Failed to open new config file (" + new + ") for reading"    " (" + strerror (errno()) + ")\n" );    config_stat_cache[cl] = fd->stat();       string read_data = fd->read();
Roxen.git/server/base_server/read_config.pike:197:    return call_outs[cl]->data;    }    lock = 0;      #ifdef DEBUG_CONFIG    report_debug("CONFIG: Read configuration file for cl "+cl+"\n");   #endif       string base = configuration_dir + replace(cl, " ", "_");    Stdio.File fd; +  +  foreach(({ "", ".new~", "~", "~2~" }), string suffix) {    mixed err = catch { -  fd = open(base, "r"); -  if( fd ) -  { + #ifdef DEBUG_CONFIG +  report_debug("CONFIG: Trying " + base + suffix + "\n"); + #endif +  fd = open(base + suffix, "r"); +  if (!fd) { +  if (suffix != ".new~") { +  report_warning("Failed to open configuration %sfile %O for %O.\n", +  sizeof(suffix)?"backup ":"", +  base + suffix, cl); +  } +  continue; +  } +     string data = fd->read(); -  if( data && strlen( data ) ) -  { +  if (!sizeof(data || "")) { +  if (suffix != ".new~") { +  report_error("Configuration %sfile %O for %O is truncated.\n", +  sizeof(suffix)?"backup ":"", +  base + suffix, cl); +  } +  continue; +  } +     config_stat_cache[cl] = fd->stat();    fd->close(); -  return decode_config_file( data ); +  mapping res = decode_config_file( data ); +  if (sizeof(suffix)) { + #ifdef DEBUG_CONFIG +  report_debug("CONFIG: Restoring " + base + " from " + +  base + suffix + "\n"); + #endif +  mv(base + suffix, base);    } -  } +  return res;    };       catch (fd->close());       if (err) { -  string backup_file; -  if (file_stat (base + "~2~")) backup_file = base + "~2~"; -  if (file_stat (base + "~")) backup_file = base + "~"; -  report_error("Failed to read configuration file (%s) for %O.%s\n" +  report_error("Failed to read configuration %sfile %O for %O.\n"    "%s\n", -  base, cl, -  backup_file ? " There is a backup file " + backup_file + ". " -  "You can try it instead by moving it to the original name. " : "", +  sizeof(suffix)?"backup ":"", +  base + suffix, cl,    describe_backtrace(err));    } -  +  }       return ([]);   }         void remove( string reg , Configuration current_configuration )   {    string cl;   #ifndef IN_INSTALL    if(!current_configuration)
Roxen.git/server/base_server/read_config.pike:276:    if (file_stat (f) && !rm (f))    error ("Failed to remove config file (" + f + ") "    "(" + strerror (errno()) + ")\n");    }       last_read = 0; last_data = 0;   }      void store( string reg, mapping(string:mixed) vars, int q,    Configuration current_configuration ) + //! Store the settings for a configuration region. + //! + //! @param reg + //! Region name. + //! + //! @param vars + //! Variables. Either + //! @mixed + //! @type mapping(string:Variable) + //! A mapping of configuration variables. This is used + //! when @[q] is @expr{0@}. + //! + //! @type mapping(string:mixed) + //! A mapping from string to raw values. This is used + //! when @[q] is @expr{1@}. + //! @endmixed + //! + //! @param q + //! @[vars] mode, see above. + //! + //! @param current_configuration + //! Configuration that the settings belong to.   {    string cl;    mapping m;      #ifndef IN_INSTALL    if(!current_configuration)   #endif    cl="Global Variables";   #ifndef IN_INSTALL    else
Roxen.git/server/base_server/read_config.pike:312:    data[ reg ] = m = vars;    else    {    m = ([ ]);    foreach ([mapping(string:Variable.Variable)] vars;    string name; Variable.Variable var) {    if (var->save) {    // Support for special save callbacks.    savers[var->save] = 1;    } else { -  m[ name ] = var->query(); +  m[ name ] = var->encode();    }    }    data[ reg ] = m;    if(!sizeof( m ))    m_delete( data, reg );    }       // Call any potential special save callbacks.    indices(savers)();