* DataHelper: Sanity checks in storeProps(), use

storeProps() for router config again
This commit is contained in:
zzz
2012-10-05 13:00:52 +00:00
parent 1d174d6797
commit 090d59fcb7
2 changed files with 29 additions and 30 deletions

View File

@@ -49,6 +49,7 @@ import net.i2p.util.FortunaRandomSource;
import net.i2p.util.I2PAppThread;
import net.i2p.util.I2PThread;
import net.i2p.util.Log;
import net.i2p.util.OrderedProperties;
import net.i2p.util.SecureFileOutputStream;
import net.i2p.util.SimpleByteCache;
import net.i2p.util.SimpleScheduler;
@@ -976,44 +977,24 @@ public class Router implements RouterClock.ClockShiftListener {
* Save the current config options (returning true if save was
* successful, false otherwise)
*
* Note that unlike DataHelper.storeProps(),
* this does escape the \r or \n that are unescaped in DataHelper.loadProps().
* Note that the escaping of \r or \n was probably a mistake and should be taken out.
*
* Synchronized with file read in getConfig()
*/
public boolean saveConfig() {
synchronized(_configFileLock) {
FileOutputStream fos = null;
try {
fos = new SecureFileOutputStream(_configFilename);
StringBuilder buf = new StringBuilder(8*1024);
buf.append("# NOTE: This I2P config file must use UTF-8 encoding\n");
TreeSet ordered = new TreeSet(_config.keySet());
for (Iterator iter = ordered.iterator() ; iter.hasNext(); ) {
String key = (String)iter.next();
String val = _config.get(key);
// Escape line breaks before saving.
// Remember: "\" needs escaping both for regex and string.
// NOOO - see comments in DataHelper
//val = val.replaceAll("\\r","\\\\r");
//val = val.replaceAll("\\n","\\\\n");
buf.append(key).append('=').append(val).append('\n');
}
fos.write(buf.toString().getBytes("UTF-8"));
} catch (IOException ioe) {
try {
Properties ordered = new OrderedProperties();
synchronized(_configFileLock) {
ordered.putAll(_config);
DataHelper.storeProps(ordered, new File(_configFilename));
}
} catch (Exception ioe) {
// warning, _log will be null when called from constructor
if (_log != null)
_log.error("Error saving the config to " + _configFilename, ioe);
else
System.err.println("Error saving the config to " + _configFilename + ": " + ioe);
return false;
} finally {
if (fos != null) try { fos.close(); } catch (IOException ioe) {}
}
return true;
}
return true;
}
/**