Merge pull request #40 from overleaf/msw-delete-temp-files

Small code improvements around temporary files
This commit is contained in:
Michael Walker 2018-02-27 11:16:57 +00:00 committed by GitHub
commit e81931ef35
2 changed files with 26 additions and 1 deletions

View file

@ -38,4 +38,23 @@ public class DeletingFileInputStream extends FileInputStream {
} }
} }
} }
/**
* We shouldn't rely on this for correctness!
*/
@Override
protected void finalize() throws IOException {
try {
super.finalize();
} finally {
if(file != null) {
Log.warn("File open at finalization time: {}", file.getCanonicalPath());
try {
close();
} catch (IOException e) {
Log.error("Failed to delete file", e);
}
}
}
}
} }

View file

@ -42,6 +42,9 @@ public class Tar {
/* Closes target */ /* Closes target */
try (OutputStream bzip2 = new BZip2CompressorOutputStream(target)) { try (OutputStream bzip2 = new BZip2CompressorOutputStream(target)) {
tarTo(fileOrDir, bzip2); tarTo(fileOrDir, bzip2);
} catch (IOException e) {
tmp.delete();
throw e;
} }
if (sizePtr != null) { if (sizePtr != null) {
sizePtr[0] = tmp.length(); sizePtr[0] = tmp.length();
@ -68,7 +71,10 @@ public class Tar {
tmp.deleteOnExit(); tmp.deleteOnExit();
try (FileOutputStream target = new FileOutputStream(tmp)) { try (FileOutputStream target = new FileOutputStream(tmp)) {
tarTo(fileOrDir, target); tarTo(fileOrDir, target);
return new FileInputStream(tmp); return new DeletingFileInputStream(tmp);
} catch (IOException e) {
tmp.delete();
throw e;
} }
} }