use of net.i2p.util.SecureFileOutputStream in project i2p.i2p by i2p.
the class WorkingDir method migrateJettyXml.
/**
* Copy over the jetty.xml file with modifications
* It was already copied over once in migrate(), throw that out and
* do it again with modifications.
*/
static boolean migrateJettyXml(File olddir, File todir, String filename, String oldString, String newString) {
File oldFile = new File(olddir, filename);
File newFile = new File(todir, filename);
FileInputStream in = null;
PrintWriter out = null;
try {
in = new FileInputStream(oldFile);
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new SecureFileOutputStream(newFile), "UTF-8")));
String s = null;
while ((s = DataHelper.readLine(in)) != null) {
// readLine() doesn't strip \r
if (s.endsWith("\r"))
s = s.substring(0, s.length() - 1);
if (s.indexOf(oldString) >= 0) {
s = s.replace(oldString, newString);
}
out.println(s);
}
out.println("<!-- Modified by I2P User dir migration script -->");
System.err.println("Copied " + oldFile + " with modifications");
return true;
} catch (IOException ioe) {
if (in != null) {
System.err.println("FAILED copy " + oldFile + ": " + ioe);
}
return false;
} finally {
if (in != null)
try {
in.close();
} catch (IOException ioe) {
}
if (out != null)
out.close();
}
}
use of net.i2p.util.SecureFileOutputStream in project i2p.i2p by i2p.
the class WorkingDir method setupSystemOut.
/**
* Redirect stdout and stderr to a wrapper.log file if there is no wrapper,
* unless system property I2P_DISABLE_OUTPUT_OVERRIDE is set.
*
* If there is no -Dwrapper.log=/path/to/wrapper.log on the java command line
* to specify a log file, check for existence of wrapper.log in CWD,
* for backward compatibility in old installations (don't move it).
* Otherwise, use (system temp dir)/wrapper.log.
* Create if it doesn't exist, and append to it if it does.
* Put the location in the environment as an absolute path, so logs.jsp can find it.
*
* @param dir if null, use Java temp dir; System property wrapper.logfile overrides
* @since 0.8.13
*/
private static void setupSystemOut(String dir) {
if (SystemVersion.hasWrapper())
return;
if (System.getProperty("I2P_DISABLE_OUTPUT_OVERRIDE") != null)
return;
String path = System.getProperty(PROP_WRAPPER_LOG);
File logfile;
if (path != null) {
logfile = new File(path);
} else {
logfile = new File(DEFAULT_WRAPPER_LOG);
if (!logfile.exists()) {
if (dir == null)
dir = System.getProperty("java.io.tmpdir");
logfile = new File(dir, DEFAULT_WRAPPER_LOG);
}
}
System.setProperty(PROP_WRAPPER_LOG, logfile.getAbsolutePath());
try {
PrintStream ps = new PrintStream(new SecureFileOutputStream(logfile, true), true, "UTF-8");
System.setOut(ps);
System.setErr(ps);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
use of net.i2p.util.SecureFileOutputStream in project i2p.i2p by i2p.
the class WorkingDir method copyFile.
/**
* @param src not a directory, must exist
* @param dst not a directory, will be overwritten if existing, will be mode 600
* @return true if it was copied successfully
*/
static boolean copyFile(File src, File dst) {
if (!src.exists())
return false;
boolean rv = true;
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream(src);
out = new SecureFileOutputStream(dst);
DataHelper.copy(in, out);
System.err.println("Copied " + src.getPath());
} catch (IOException ioe) {
System.err.println("FAILED copy " + src.getPath() + ": " + ioe);
rv = false;
} finally {
if (in != null)
try {
in.close();
} catch (IOException ioe) {
}
if (out != null)
try {
out.close();
} catch (IOException ioe) {
}
}
if (rv)
dst.setLastModified(src.lastModified());
return rv;
}
use of net.i2p.util.SecureFileOutputStream in project i2p.i2p by i2p.
the class MarkLiveliness method ping.
private void ping() {
FileOutputStream fos = null;
try {
fos = new SecureFileOutputStream(_pingFile);
fos.write(DataHelper.getASCII(Long.toString(System.currentTimeMillis())));
} catch (IOException ioe) {
if (!_errorLogged) {
Log log = _router.getContext().logManager().getLog(MarkLiveliness.class);
log.logAlways(Log.WARN, "Error writing to ping file " + _pingFile + ": " + ioe);
_errorLogged = true;
}
} finally {
if (fos != null)
try {
fos.close();
} catch (IOException ioe) {
}
}
}
use of net.i2p.util.SecureFileOutputStream in project i2p.i2p by i2p.
the class HostTxtParser method write.
/**
* Write contents of Map map to the File file. Output is written
* with one key, value pair on each line, in the format: key=value.
* Write to a temp file in the same directory and then rename, to not corrupt
* simultaneous accesses by the router. Except on Windows where renameTo()
* will fail if the target exists.
*
* @param map
* A Map to write to file.
* @param file
* A File to write the Map to.
* @throws IOException
* if file cannot be written to.
*/
public static void write(Map<String, HostTxtEntry> map, File file) throws IOException {
boolean success = false;
if (!isWindows) {
File tmp = SecureFile.createTempFile("temp-", ".tmp", file.getAbsoluteFile().getParentFile());
write(map, new BufferedWriter(new OutputStreamWriter(new SecureFileOutputStream(tmp), "UTF-8")));
success = tmp.renameTo(file);
if (!success) {
tmp.delete();
// System.out.println("Warning: addressbook rename fail from " + tmp + " to " + file);
}
}
if (!success) {
// hmm, that didn't work, try it the old way
write(map, new BufferedWriter(new OutputStreamWriter(new SecureFileOutputStream(file), "UTF-8")));
}
}
Aggregations