This is an automated email from the ASF dual-hosted git repository.
garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-vfs.git
The following commit(s) were added to refs/heads/master by this push:
new 624aa73aa Replace the shared vfs_cache temp dir in
DefaultFileReplicator (#775)
624aa73aa is described below
commit 624aa73aa014bc6a839aac4b762204feda662f5c
Author: Naveed Khan <[email protected]>
AuthorDate: Tue Aug 4 12:14:42 2026 +0000
Replace the shared vfs_cache temp dir in DefaultFileReplicator (#775)
* Replace the shared vfs_cache temp dir in DefaultFileReplicator
The replica directory was pinned to <java.io.tmpdir>/vfs_cache and left to
mkdirs under the default umask, so replicated files landed world-readable in a
directory any local user can predict and pre-create. Allocate it with
Files.createTempDirectory on first use, which picks an unguessable name and
creates it rwx------ on POSIX file systems in one step.
* Honor the parent parameter in createAndAddFile
createAndAddFile ignored its parent argument and always used the temp
directory, making the protected API misleading. Use parent as passed in;
allocateFile already supplies the temp directory, so behavior is unchanged.
---
.../commons/vfs2/impl/DefaultFileReplicator.java | 42 ++++++++++----
.../vfs2/impl/DefaultFileReplicatorTest.java | 67 ++++++++++++++++++++++
2 files changed, 98 insertions(+), 11 deletions(-)
diff --git
a/commons-vfs2/src/main/java/org/apache/commons/vfs2/impl/DefaultFileReplicator.java
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/impl/DefaultFileReplicator.java
index 37f461340..916f9674e 100644
---
a/commons-vfs2/src/main/java/org/apache/commons/vfs2/impl/DefaultFileReplicator.java
+++
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/impl/DefaultFileReplicator.java
@@ -17,10 +17,11 @@
package org.apache.commons.vfs2.impl;
import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Random;
-import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.vfs2.FileObject;
@@ -91,7 +92,7 @@ public class DefaultFileReplicator extends
AbstractVfsComponent implements FileR
fileCount++;
}
- return createAndAddFile(tempDir, actualBaseName);
+ return createAndAddFile(getTempDir(), actualBaseName);
}
/**
@@ -119,13 +120,13 @@ public class DefaultFileReplicator extends
AbstractVfsComponent implements FileR
/**
* Adds a file.
*
- * @param parent ignored.
+ * @param parent The parent directory of the new file.
* @param baseName The base file name.
* @return A File.
* @throws FileSystemException if a file system error occurs.
*/
protected File createAndAddFile(final File parent, final String baseName)
throws FileSystemException {
- final File file = createFile(tempDir, baseName);
+ final File file = createFile(parent, baseName);
// Keep track to delete later
addFile(file);
return file;
@@ -184,24 +185,43 @@ public class DefaultFileReplicator extends
AbstractVfsComponent implements FileR
}
/**
- * Initializes this component.
+ * Gets the directory that holds the replicated files, creating it on
first use.
+ * <p>
+ * Unless a directory was supplied to {@link
#DefaultFileReplicator(File)}, the directory is created with
+ * {@link Files#createTempDirectory(String,
java.nio.file.attribute.FileAttribute...)}, which picks an
+ * unpredictable name and, on POSIX file systems, restricts it to the
owner.
+ * </p>
*
- * @throws FileSystemException if an error occurs.
+ * @return the directory that holds the replicated files.
+ * @throws FileSystemException if the directory cannot be created.
*/
- @Override
- public void init() throws FileSystemException {
+ private synchronized File getTempDir() throws FileSystemException {
if (tempDir == null) {
- tempDir = new File(FileUtils.getTempDirectoryPath(),
"vfs_cache").getAbsoluteFile();
+ try {
+ tempDir =
Files.createTempDirectory("vfs_cache").toFile().getAbsoluteFile();
+ } catch (final IOException e) {
+ throw new
FileSystemException("vfs.impl/init-replicator.error", e);
+ }
}
- fileCount = RANDOM.nextInt() & MASK;
-
if (!tempDirMessageLogged) {
final String message =
Messages.getString("vfs.impl/temp-dir.debug", tempDir);
VfsLog.debug(getLogger(), log, message);
tempDirMessageLogged = true;
}
+
+ return tempDir;
+ }
+
+ /**
+ * Initializes this component.
+ *
+ * @throws FileSystemException if an error occurs.
+ */
+ @Override
+ public void init() throws FileSystemException {
+ fileCount = RANDOM.nextInt() & MASK;
}
/**
diff --git
a/commons-vfs2/src/test/java/org/apache/commons/vfs2/impl/DefaultFileReplicatorTest.java
b/commons-vfs2/src/test/java/org/apache/commons/vfs2/impl/DefaultFileReplicatorTest.java
new file mode 100644
index 000000000..c1e0a566f
--- /dev/null
+++
b/commons-vfs2/src/test/java/org/apache/commons/vfs2/impl/DefaultFileReplicatorTest.java
@@ -0,0 +1,67 @@
+/*
+ * 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
+ *
+ * https://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.commons.vfs2.impl;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assumptions.assumeTrue;
+
+import java.io.File;
+import java.nio.file.FileSystems;
+import java.nio.file.Files;
+import java.nio.file.attribute.PosixFilePermissions;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.vfs2.FileSystemException;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link DefaultFileReplicator}.
+ */
+public class DefaultFileReplicatorTest {
+
+ private File allocateTempDir() throws FileSystemException {
+ final DefaultFileReplicator replicator = new DefaultFileReplicator();
+ replicator.init();
+ return replicator.allocateFile("test.txt").getParentFile();
+ }
+
+ @Test
+ public void testTempDirIsNotSharedByName() throws Exception {
+ final File tempDir = allocateTempDir();
+ try {
+ assertNotEquals(new File(FileUtils.getTempDirectoryPath(),
"vfs_cache").getAbsoluteFile(), tempDir,
+ "the replica directory must not use a name another user
can guess and pre-create");
+ assertTrue(tempDir.isDirectory(), tempDir + " was not created");
+ } finally {
+ FileUtils.deleteQuietly(tempDir);
+ }
+ }
+
+ @Test
+ public void testTempDirIsOwnerOnly() throws Exception {
+
assumeTrue(FileSystems.getDefault().supportedFileAttributeViews().contains("posix"));
+ final File tempDir = allocateTempDir();
+ try {
+ assertEquals(PosixFilePermissions.fromString("rwx------"),
+ Files.getPosixFilePermissions(tempDir.toPath()));
+ } finally {
+ FileUtils.deleteQuietly(tempDir);
+ }
+ }
+}