private void moveTempToTarget( File temp, File target )
throws ProxyException
{
// TODO file lock library
Lock lock = null;
try
{
lock = fileLockManager.writeFileLock( target );
if ( lock.getFile().exists() && !lock.getFile().delete() )
{
throw new ProxyException( "Unable to overwrite existing target file: " + target.getAbsolutePath() );
}
lock.getFile().getParentFile().mkdirs();
if ( !temp.renameTo( lock.getFile() ) )
{
log.warn( "Unable to rename tmp file to its final name... resorting to copy command." );
try
{
FileUtils.copyFile( temp, lock.getFile() );
}
catch ( IOException e )
{
if ( lock.getFile().exists() )
{
log.debug( "Tried to copy file {} to {} but file with this name already exists.",
temp.getName(), lock.getFile().getAbsolutePath() );
}
else
{
throw new ProxyException(
"Cannot copy tmp file " + temp.getAbsolutePath() + " to its final location", e );