You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by kw...@apache.org on 2021/06/07 16:08:44 UTC

[jackrabbit-filevault] branch master updated: JCRVLT-529 properly remove tmp files

This is an automated email from the ASF dual-hosted git repository.

kwin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jackrabbit-filevault.git


The following commit(s) were added to refs/heads/master by this push:
     new 293de22  JCRVLT-529 properly remove tmp files
293de22 is described below

commit 293de2239096c623d0a4d50211e92737ae7cdd89
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Mon Jun 7 18:08:06 2021 +0200

    JCRVLT-529 properly remove tmp files
---
 .../packaging/registry/impl/FSPackageRegistry.java | 88 +++++++++++-----------
 .../packaging/integration/IntegrationTestBase.java |  2 +-
 2 files changed, 47 insertions(+), 43 deletions(-)

diff --git a/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/registry/impl/FSPackageRegistry.java b/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/registry/impl/FSPackageRegistry.java
index aba2cb0..48a72b1 100644
--- a/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/registry/impl/FSPackageRegistry.java
+++ b/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/registry/impl/FSPackageRegistry.java
@@ -454,53 +454,57 @@ public class FSPackageRegistry extends AbstractPackageRegistry {
             throws IOException, PackageExistsException {
 
         Path tempFile = Files.createTempFile("upload", ".zip");
-        MemoryArchive archive = new MemoryArchive(false);
-
-        try (InputStreamPump pump = new InputStreamPump(in, archive)) {
-            // this will cause the input stream to be consumed and the memory
-            // archive being initialized.
-            try {
-                Files.copy(pump, tempFile, StandardCopyOption.REPLACE_EXISTING);
-            } catch (IOException e) {
-                String msg = "Stream could not be read successfully.";
-                throw new IOException(msg, e);
+        try {
+            MemoryArchive archive = new MemoryArchive(false);
+    
+            try (InputStreamPump pump = new InputStreamPump(in, archive)) {
+                // this will cause the input stream to be consumed and the memory
+                // archive being initialized.
+                try {
+                    Files.copy(pump, tempFile, StandardCopyOption.REPLACE_EXISTING);
+                } catch (IOException e) {
+                    String msg = "Stream could not be read successfully.";
+                    throw new IOException(msg, e);
+                }
             }
-        }
-        if (archive.getJcrRoot() == null) {
-            String msg = "Stream is not a content package. Missing 'jcr_root'.";
-            throw new IOException(msg);
-        }
-
-        final MetaInf inf = archive.getMetaInf();
-        PackageId pid = inf.getPackageProperties().getId();
-
-        // invalidate pid if path is unknown
-        if (pid == null) {
-            throw new IllegalArgumentException("Unable to create package. No package pid set.");
-        }
-        if (!pid.isValid()) {
-            throw new IllegalArgumentException("Unable to create package. Illegal package name.");
-        }
-
-        Path pkgFile = getPackageFile(pid);
-        FSInstallState state = getInstallState(pid);
-
-        if (Files.exists(pkgFile)) {
-            if (replace && !state.isExternal()) {
-                Files.delete(pkgFile);
+            if (archive.getJcrRoot() == null) {
+                String msg = "Stream is not a content package. Missing 'jcr_root'.";
+                throw new IOException(msg);
+            }
+    
+            final MetaInf inf = archive.getMetaInf();
+            PackageId pid = inf.getPackageProperties().getId();
+    
+            // invalidate pid if path is unknown
+            if (pid == null) {
+                throw new IllegalArgumentException("Unable to create package. No package pid set.");
+            }
+            if (!pid.isValid()) {
+                throw new IllegalArgumentException("Unable to create package. Illegal package name.");
+            }
+    
+            Path pkgFile = getPackageFile(pid);
+            FSInstallState state = getInstallState(pid);
+    
+            if (Files.exists(pkgFile)) {
+                if (replace && !state.isExternal()) {
+                    Files.delete(pkgFile);
+                } else {
+                    throw new PackageExistsException("Package already exists: " + pid).setId(pid);
+                }
             } else {
-                throw new PackageExistsException("Package already exists: " + pid).setId(pid);
+                Files.createDirectories(pkgFile.getParent());
             }
-        } else {
-            Files.createDirectories(pkgFile.getParent());
+    
+            ZipVaultPackage pkg = new ZipVaultPackage(archive, true);
+            registerSubPackages(pkg, replace);
+            Files.move(tempFile, pkgFile);
+            dispatch(Type.UPLOAD, pid, null);
+            return pkg;
+        } finally {
+            Files.deleteIfExists(tempFile);
         }
 
-        ZipVaultPackage pkg = new ZipVaultPackage(archive, true);
-        registerSubPackages(pkg, replace);
-        Files.move(tempFile, pkgFile);
-        dispatch(Type.UPLOAD, pid, null);
-        return pkg;
-
     }
 
     /**
diff --git a/vault-core/src/test/java/org/apache/jackrabbit/vault/packaging/integration/IntegrationTestBase.java b/vault-core/src/test/java/org/apache/jackrabbit/vault/packaging/integration/IntegrationTestBase.java
index 6cc4d81..64e4fac 100644
--- a/vault-core/src/test/java/org/apache/jackrabbit/vault/packaging/integration/IntegrationTestBase.java
+++ b/vault-core/src/test/java/org/apache/jackrabbit/vault/packaging/integration/IntegrationTestBase.java
@@ -342,7 +342,7 @@ public class IntegrationTestBase  {
     public File getTempFile(String name) throws IOException {
         InputStream in = getStream(name);
 
-        File tmpFile = File.createTempFile("vaultpack", ".zip");
+        File tmpFile = tempFolder.newFile();
         FileOutputStream out = FileUtils.openOutputStream(tmpFile);
         IOUtils.copy(in, out);
         in.close();