forked from I2P_Developers/i2p.i2p
* FileUtil: Add a rename method and a new copy method
This commit is contained in:
@ -359,7 +359,7 @@ public class NewsFetcher implements Runnable, EepGet.StatusListener {
|
|||||||
|
|
||||||
long now = _context.clock().now();
|
long now = _context.clock().now();
|
||||||
if (_tempFile.exists()) {
|
if (_tempFile.exists()) {
|
||||||
boolean copied = FileUtil.copy(_tempFile.getAbsolutePath(), _newsFile.getAbsolutePath(), true);
|
boolean copied = FileUtil.copy(_tempFile, _newsFile, true, false);
|
||||||
if (copied) {
|
if (copied) {
|
||||||
_lastUpdated = now;
|
_lastUpdated = now;
|
||||||
_tempFile.delete();
|
_tempFile.delete();
|
||||||
|
@ -93,8 +93,8 @@ public class UnsignedUpdateHandler extends UpdateHandler {
|
|||||||
_log.log(Log.CRIT, "Corrupt zip file from " + url);
|
_log.log(Log.CRIT, "Corrupt zip file from " + url);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String to = (new File(_context.getRouterDir(), Router.UPDATE_FILE)).getAbsolutePath();
|
File to = new File(_context.getRouterDir(), Router.UPDATE_FILE);
|
||||||
boolean copied = FileUtil.copy(_updateFile, to, true);
|
boolean copied = FileUtil.copy(updFile, to, true, false);
|
||||||
if (copied) {
|
if (copied) {
|
||||||
updFile.delete();
|
updFile.delete();
|
||||||
String policy = _context.getProperty(ConfigUpdateHandler.PROP_UPDATE_POLICY);
|
String policy = _context.getProperty(ConfigUpdateHandler.PROP_UPDATE_POLICY);
|
||||||
@ -124,7 +124,7 @@ public class UnsignedUpdateHandler extends UpdateHandler {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
_log.log(Log.CRIT, "Failed copy to " + to);
|
_log.log(Log.CRIT, "Failed copy to " + to);
|
||||||
updateStatus("<b>" + _("Failed copy to {0}", to) + "</b>");
|
updateStatus("<b>" + _("Failed copy to {0}", to.getAbsolutePath()) + "</b>");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -402,7 +402,7 @@ public class CPUID {
|
|||||||
}
|
}
|
||||||
// copy to install dir, ignore failure
|
// copy to install dir, ignore failure
|
||||||
File newFile = new File(I2PAppContext.getGlobalContext().getBaseDir(), filename);
|
File newFile = new File(I2PAppContext.getGlobalContext().getBaseDir(), filename);
|
||||||
FileUtil.copy(outFile.getAbsolutePath(), newFile.getAbsolutePath(), false, true);
|
FileUtil.copy(outFile, newFile, false, true);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -193,7 +193,7 @@ public class SingleFileNamingService extends NamingService {
|
|||||||
out.write(d.toBase64());
|
out.write(d.toBase64());
|
||||||
out.newLine();
|
out.newLine();
|
||||||
out.close();
|
out.close();
|
||||||
boolean success = rename(tmp, _file);
|
boolean success = FileUtil.rename(tmp, _file);
|
||||||
if (success) {
|
if (success) {
|
||||||
for (NamingServiceListener nsl : _listeners) {
|
for (NamingServiceListener nsl : _listeners) {
|
||||||
nsl.entryChanged(this, hostname, d, options);
|
nsl.entryChanged(this, hostname, d, options);
|
||||||
@ -284,7 +284,7 @@ public class SingleFileNamingService extends NamingService {
|
|||||||
tmp.delete();
|
tmp.delete();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
success = rename(tmp, _file);
|
success = FileUtil.rename(tmp, _file);
|
||||||
if (success) {
|
if (success) {
|
||||||
for (NamingServiceListener nsl : _listeners) {
|
for (NamingServiceListener nsl : _listeners) {
|
||||||
nsl.entryRemoved(this, hostname);
|
nsl.entryRemoved(this, hostname);
|
||||||
@ -442,24 +442,6 @@ public class SingleFileNamingService extends NamingService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean rename(File from, File to) {
|
|
||||||
boolean success = false;
|
|
||||||
boolean isWindows = System.getProperty("os.name").startsWith("Win");
|
|
||||||
// overwrite fails on windows
|
|
||||||
if (!isWindows)
|
|
||||||
success = from.renameTo(to);
|
|
||||||
if (!success) {
|
|
||||||
to.delete();
|
|
||||||
success = from.renameTo(to);
|
|
||||||
if (!success) {
|
|
||||||
// hard way
|
|
||||||
success = FileUtil.copy(from.getAbsolutePath(), to.getAbsolutePath(), true, true);
|
|
||||||
from.delete();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return success;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void getReadLock() {
|
private void getReadLock() {
|
||||||
_fileLock.readLock().lock();
|
_fileLock.readLock().lock();
|
||||||
}
|
}
|
||||||
|
@ -400,7 +400,15 @@ public class FileUtil {
|
|||||||
public static boolean copy(String source, String dest, boolean overwriteExisting, boolean quiet) {
|
public static boolean copy(String source, String dest, boolean overwriteExisting, boolean quiet) {
|
||||||
File src = new File(source);
|
File src = new File(source);
|
||||||
File dst = new File(dest);
|
File dst = new File(dest);
|
||||||
|
return copy(src, dst, overwriteExisting, quiet);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param quiet don't log fails to wrapper log if true
|
||||||
|
* @return true if it was copied successfully
|
||||||
|
* @since 0.8.8
|
||||||
|
*/
|
||||||
|
public static boolean copy(File src, File dst, boolean overwriteExisting, boolean quiet) {
|
||||||
if (dst.exists() && dst.isDirectory())
|
if (dst.exists() && dst.isDirectory())
|
||||||
dst = new File(dst, src.getName());
|
dst = new File(dst, src.getName());
|
||||||
|
|
||||||
@ -429,6 +437,35 @@ public class FileUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Try to rename, if it doesn't work then copy and delete the old.
|
||||||
|
* Always overwrites any existing "to" file.
|
||||||
|
* Method moved from SingleFileNamingService.
|
||||||
|
*
|
||||||
|
* @return true if it was renamed / copied successfully
|
||||||
|
* @since 0.8.8
|
||||||
|
*/
|
||||||
|
public static boolean rename(File from, File to) {
|
||||||
|
if (!from.exists())
|
||||||
|
return false;
|
||||||
|
boolean success = false;
|
||||||
|
boolean isWindows = System.getProperty("os.name").startsWith("Win");
|
||||||
|
// overwrite fails on windows
|
||||||
|
if (!isWindows)
|
||||||
|
success = from.renameTo(to);
|
||||||
|
if (!success) {
|
||||||
|
to.delete();
|
||||||
|
success = from.renameTo(to);
|
||||||
|
if (!success) {
|
||||||
|
// hard way
|
||||||
|
success = copy(from, to, true, true);
|
||||||
|
if (success)
|
||||||
|
from.delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Usage: FileUtil (delete path | copy source dest | unzip path.zip)
|
* Usage: FileUtil (delete path | copy source dest | unzip path.zip)
|
||||||
*
|
*
|
||||||
|
@ -606,7 +606,7 @@ public class NativeBigInteger extends BigInteger {
|
|||||||
}
|
}
|
||||||
// copy to install dir, ignore failure
|
// copy to install dir, ignore failure
|
||||||
File newFile = new File(I2PAppContext.getGlobalContext().getBaseDir(), filename);
|
File newFile = new File(I2PAppContext.getGlobalContext().getBaseDir(), filename);
|
||||||
FileUtil.copy(outFile.getAbsolutePath(), newFile.getAbsolutePath(), false, true);
|
FileUtil.copy(outFile, newFile, false, true);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user