diff options
author | Simon Rettberg | 2015-01-27 15:47:23 +0100 |
---|---|---|
committer | Simon Rettberg | 2015-01-27 15:47:23 +0100 |
commit | 47831fa1c668c5e45cd903fdd76ac068d541b88e (patch) | |
tree | 0aa88c97d0905edf96b393fd0212aa52337ed873 | |
parent | Stuff (diff) | |
download | tmlite-bwlp-47831fa1c668c5e45cd903fdd76ac068d541b88e.tar.gz tmlite-bwlp-47831fa1c668c5e45cd903fdd76ac068d541b88e.tar.xz tmlite-bwlp-47831fa1c668c5e45cd903fdd76ac068d541b88e.zip |
[RecompressArchive] Use temporary file for compression, only move to destination on success
-rw-r--r-- | src/main/java/org/openslx/taskmanager/tasks/RecompressArchive.java | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/src/main/java/org/openslx/taskmanager/tasks/RecompressArchive.java b/src/main/java/org/openslx/taskmanager/tasks/RecompressArchive.java index d428f03..1b5cf8a 100644 --- a/src/main/java/org/openslx/taskmanager/tasks/RecompressArchive.java +++ b/src/main/java/org/openslx/taskmanager/tasks/RecompressArchive.java @@ -3,6 +3,8 @@ package org.openslx.taskmanager.tasks; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -45,23 +47,32 @@ public class RecompressArchive extends AbstractTask @Override protected boolean execute() { - if ( execute2() ) { - return true; + String fn = new File( this.outputFile ).getName(); + String tmpFile = "/tmp/bwlp-" + System.nanoTime() + "-" + fn; + if ( execute2( tmpFile ) ) { + try { + FileUtils.deleteQuietly( new File( this.outputFile ) ); + Files.move( Paths.get( tmpFile ), Paths.get( this.outputFile ) ); + return true; + } catch ( Exception e ) { + status.error = e.toString(); + status.errorCode = Output.ErrorCode.WRITE_FAILED; + } } - FileUtils.deleteQuietly( new File( this.outputFile ) ); + FileUtils.deleteQuietly( new File( tmpFile ) ); return false; } - private boolean execute2() + private boolean execute2( String archiveFile ) { // Open output file archive TarArchiveOutputStream outArchive = null; try { try { FileUtils.forceMkdir( new File( new File( this.outputFile ).getParent() ) ); - outArchive = Archive.createTarArchive( this.outputFile ); + outArchive = Archive.createTarArchive( archiveFile ); } catch ( IOException e2 ) { - status.error = e2.getMessage(); + status.error = e2.toString(); status.errorCode = Output.ErrorCode.WRITE_FAILED; return false; } |