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 2026/09/27 09:27:17 UTC

(jackrabbit-filevault) branch bugfix/remove-package-tmp-files-through-closewatcher created (now a16332a2)

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

kwin pushed a change to branch bugfix/remove-package-tmp-files-through-closewatcher
in repository https://gitbox.apache.org/repos/asf/jackrabbit-filevault.git


      at a16332a2 JCRVLT-851 Let CloseWatcher close Archive

This branch includes the following new commits:

     new 420b8dae Revert "JCRVLT-851 cleanup temp files properly (#432)"
     new a16332a2 JCRVLT-851 Let CloseWatcher close Archive

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(jackrabbit-filevault) 01/02: Revert "JCRVLT-851 cleanup temp files properly (#432)"

Posted by kw...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

kwin pushed a commit to branch bugfix/remove-package-tmp-files-through-closewatcher
in repository https://gitbox.apache.org/repos/asf/jackrabbit-filevault.git

commit 420b8daeafe55268f0b537dab97b7d6db17f0f63
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Sun Sep 27 11:19:29 2026 +0200

    Revert "JCRVLT-851 cleanup temp files properly (#432)"
    
    This reverts commit 67e4e79104215b49bf84aaf2acdc415d3c9a495a.
---
 .../vault/packaging/impl/JcrPackageImpl.java       |  22 ++---
 .../packaging/impl/JcrPackageImplTempFileTest.java | 110 ---------------------
 2 files changed, 7 insertions(+), 125 deletions(-)

diff --git a/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/impl/JcrPackageImpl.java b/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/impl/JcrPackageImpl.java
index 0758073e..293194f9 100644
--- a/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/impl/JcrPackageImpl.java
+++ b/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/impl/JcrPackageImpl.java
@@ -322,22 +322,14 @@ public class JcrPackageImpl implements JcrPackage {
                 }
             } else {
                 File tmpFile = File.createTempFile("vaultpack", ".zip");
-                // make sure the temp file is removed at the latest when the JVM terminates,
-                // in case it is never explicitly closed via ZipArchive.close()/ZipVaultPackage.close()
-                tmpFile.deleteOnExit();
-                try {
-                    Binary bin = getData().getBinary();
-                    try (FileOutputStream out = FileUtils.openOutputStream(tmpFile);
-                            InputStream in = bin.getStream()) {
-                        IOUtils.copy(in, out);
-                    } finally {
-                        bin.dispose();
-                    }
-                    pack = new ZipVaultPackage(tmpFile, true);
-                } catch (RepositoryException | IOException | RuntimeException e) {
-                    FileUtils.deleteQuietly(tmpFile);
-                    throw e;
+                Binary bin = getData().getBinary();
+                try (FileOutputStream out = FileUtils.openOutputStream(tmpFile);
+                        InputStream in = bin.getStream()) {
+                    IOUtils.copy(in, out);
+                } finally {
+                    bin.dispose();
                 }
+                pack = new ZipVaultPackage(tmpFile, true);
             }
         }
         return pack;
diff --git a/vault-core/src/test/java/org/apache/jackrabbit/vault/packaging/impl/JcrPackageImplTempFileTest.java b/vault-core/src/test/java/org/apache/jackrabbit/vault/packaging/impl/JcrPackageImplTempFileTest.java
deleted file mode 100644
index 39b24572..00000000
--- a/vault-core/src/test/java/org/apache/jackrabbit/vault/packaging/impl/JcrPackageImplTempFileTest.java
+++ /dev/null
@@ -1,110 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.jackrabbit.vault.packaging.impl;
-
-import javax.jcr.Binary;
-import javax.jcr.Node;
-import javax.jcr.Property;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Arrays;
-import java.util.HashSet;
-import java.util.Set;
-
-import org.apache.jackrabbit.vault.packaging.registry.impl.JcrPackageRegistry;
-import org.apache.jackrabbit.vault.util.JcrConstants;
-import org.junit.Test;
-import org.mockito.stubbing.Answer;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.fail;
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.anyInt;
-import static org.mockito.ArgumentMatchers.eq;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-/**
- * Regression test: {@link JcrPackageImpl#getPackage(boolean)} used to leak its
- * "vaultpack*.zip" temp file on disk if copying the JCR binary into it failed
- * before the {@code ZipVaultPackage} (and thus the file's owner) was constructed.
- */
-public class JcrPackageImplTempFileTest {
-
-    /**
-     * Lists the "vaultpack*.zip" files currently present in the system temp directory.
-     */
-    private Set<String> listVaultPackTempFiles() {
-        File tmpDir = new File(System.getProperty("java.io.tmpdir"));
-        String[] names = tmpDir.list((dir, name) -> name.startsWith("vaultpack") && name.endsWith(".zip"));
-        return new HashSet<>(Arrays.asList(names == null ? new String[0] : names));
-    }
-
-    /**
-     * Returns the single new file that appeared in the system temp dir since {@code before} was captured.
-     */
-    private File findNewTempFile(Set<String> before) {
-        Set<String> after = listVaultPackTempFiles();
-        after.removeAll(before);
-        assertEquals("expected exactly one new vaultpack temp file, found: " + after, 1, after.size());
-        return new File(System.getProperty("java.io.tmpdir"), after.iterator().next());
-    }
-
-    @Test
-    public void tempFileIsRemovedWhenPackageCreationFails() throws Exception {
-        JcrPackageRegistry mgr = mock(JcrPackageRegistry.class);
-        Node node = mock(Node.class);
-        Node contentNode = mock(Node.class);
-        Property dataProperty = mock(Property.class);
-        Binary binary = mock(Binary.class);
-        when(node.getNode(eq(JcrConstants.JCR_CONTENT))).thenReturn(contentNode);
-        when(contentNode.getProperty(eq(JcrConstants.JCR_DATA))).thenReturn(dataProperty);
-        // force the file-archive branch regardless of the reported length
-        when(dataProperty.getLength()).thenReturn(Long.MAX_VALUE);
-        when(dataProperty.getBinary()).thenReturn(binary);
-
-        // capture the temp file's name at the moment it is read (it is created on disk
-        // by then, but hasn't been deleted yet), then simulate a broken source stream.
-        Set<String> before = listVaultPackTempFiles();
-        File[] capturedTmpFile = new File[1];
-        InputStream failingStream = mock(InputStream.class);
-        Answer<Integer> failOnFirstRead = invocation -> {
-            capturedTmpFile[0] = findNewTempFile(before);
-            throw new IOException("boom");
-        };
-        when(failingStream.read(any(byte[].class))).thenAnswer(failOnFirstRead);
-        when(failingStream.read(any(byte[].class), anyInt(), anyInt())).thenAnswer(failOnFirstRead);
-        when(binary.getStream()).thenReturn(failingStream);
-
-        JcrPackageImpl jcrPackage = new JcrPackageImpl(mgr, node);
-        try {
-            jcrPackage.getPackage(true);
-            fail("expected IOException to be propagated");
-        } catch (IOException expected) {
-            // expected
-        }
-
-        assertNotNull("temp file was never created/read", capturedTmpFile[0]);
-        assertFalse("temp file should have been cleaned up after failed package creation", capturedTmpFile[0].exists());
-    }
-}


(jackrabbit-filevault) 02/02: JCRVLT-851 Let CloseWatcher close Archive

Posted by kw...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

kwin pushed a commit to branch bugfix/remove-package-tmp-files-through-closewatcher
in repository https://gitbox.apache.org/repos/asf/jackrabbit-filevault.git

commit a16332a27a3e0dc401dd9e4f11cfd36947890e56
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Sun Sep 27 11:27:08 2026 +0200

    JCRVLT-851 Let CloseWatcher close Archive
    
    This comprises the closing JarFile an potentially deleting the temp
    file. Previously just the JarFile has been closed leading to orphan temp
    files.
---
 .../org/apache/jackrabbit/vault/fs/io/ZipArchive.java     | 15 ++++++++++-----
 .../org/apache/jackrabbit/vault/fs/io/ZipNioArchive.java  | 15 ++++++++++-----
 2 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/io/ZipArchive.java b/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/io/ZipArchive.java
index 1543a2cd..63a7b1dd 100644
--- a/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/io/ZipArchive.java
+++ b/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/io/ZipArchive.java
@@ -98,6 +98,10 @@ public class ZipArchive extends AbstractArchive {
     public ZipArchive(@NotNull File zipFile, boolean isTempFile) {
         this.file = zipFile;
         this.isTempFile = isTempFile;
+        if (isTempFile) {
+            watcher = CloseWatcher.register(this, this, SHOULD_CREATE_STACK_TRACE);
+        }
+        dumpUnclosedArchives();
     }
 
     @Override
@@ -153,8 +157,9 @@ public class ZipArchive extends AbstractArchive {
         if (inf.getNodeTypes().isEmpty()) {
             log.debug("Zip {} does not contain nodetypes.", file.getPath());
         }
-        dumpUnclosedArchives();
-        watcher = CloseWatcher.register(this, jar, SHOULD_CREATE_STACK_TRACE);
+        if (watcher == null) {
+            watcher = CloseWatcher.register(this, this, SHOULD_CREATE_STACK_TRACE);
+        }
     }
 
     @Override
@@ -219,13 +224,13 @@ public class ZipArchive extends AbstractArchive {
 
     @Override
     public void close() {
+        if (watcher != null) {
+            CloseWatcher.unregister(watcher);
+        }
         try {
             if (jar != null) {
                 jar.close();
                 jar = null;
-                if (watcher != null) {
-                    CloseWatcher.unregister(watcher);
-                }
             }
             if (file != null && isTempFile) {
                 FileUtils.deleteQuietly(file);
diff --git a/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/io/ZipNioArchive.java b/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/io/ZipNioArchive.java
index 42f6b2d3..b3dc41d8 100644
--- a/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/io/ZipNioArchive.java
+++ b/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/io/ZipNioArchive.java
@@ -90,6 +90,10 @@ public class ZipNioArchive extends AbstractArchive {
         this.path = path;
         zipFileSystem = null;
         this.deleteAtClose = deleteAtClose;
+        if (deleteAtClose) {
+            watcher = CloseWatcher.register(this, this, SHOULD_CREATE_STACK_TRACE);
+        }
+        dumpUnclosedArchives();
     }
 
     @Override
@@ -103,8 +107,9 @@ public class ZipNioArchive extends AbstractArchive {
         } catch (ProviderNotFoundException e) {
             throw new IOException("Can not open zip file '" + path + "'", e);
         }
-        dumpUnclosedArchives();
-        watcher = CloseWatcher.register(this, zipFileSystem, SHOULD_CREATE_STACK_TRACE);
+        if (watcher == null) {
+            watcher = CloseWatcher.register(this, this, SHOULD_CREATE_STACK_TRACE);
+        }
     }
 
     @Override
@@ -199,10 +204,10 @@ public class ZipNioArchive extends AbstractArchive {
 
     @Override
     public void close() {
+        if (watcher != null) {
+            CloseWatcher.unregister(watcher);
+        }
         if (zipFileSystem != null) {
-            if (watcher != null) {
-                CloseWatcher.unregister(watcher);
-            }
             try {
                 zipFileSystem.close();
             } catch (IOException e) {