diff --git a/core/src/main/java/org/apache/hop/core/vfs/HopLocalFileProvider.java b/core/src/main/java/org/apache/hop/core/vfs/HopLocalFileProvider.java new file mode 100644 index 00000000000..ad1d68315ca --- /dev/null +++ b/core/src/main/java/org/apache/hop/core/vfs/HopLocalFileProvider.java @@ -0,0 +1,77 @@ +/* + * 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.hop.core.vfs; + +import java.io.File; +import org.apache.commons.vfs2.FileName; +import org.apache.commons.vfs2.FileObject; +import org.apache.commons.vfs2.FileSystem; +import org.apache.commons.vfs2.FileSystemException; +import org.apache.commons.vfs2.FileSystemOptions; +import org.apache.commons.vfs2.provider.AbstractFileName; +import org.apache.commons.vfs2.provider.local.DefaultLocalFileProvider; +import org.apache.commons.vfs2.provider.local.LocalFile; +import org.apache.commons.vfs2.provider.local.LocalFileName; +import org.apache.commons.vfs2.provider.local.LocalFileSystem; + +/** + * The local file provider of Hop VFS: Commons VFS's own, except that creating a folder that + * appeared in the meantime is not an error. + * + *
Commons VFS checks that a folder is missing and then calls {@code mkdirs()}, which fails when + * another thread or process created it between the two: several copies of a transform writing files + * into the same new folder. The folder is there, which is what the caller asked for, so only a + * folder that is still missing is reported. + */ +public class HopLocalFileProvider extends DefaultLocalFileProvider { + + @Override + protected FileSystem doCreateFileSystem(FileName name, FileSystemOptions fileSystemOptions) + throws FileSystemException { + LocalFileName rootName = (LocalFileName) name; + return new HopLocalFileSystem(rootName, rootName.getRootFile(), fileSystemOptions); + } + + static class HopLocalFileSystem extends LocalFileSystem { + private final String rootFile; + + HopLocalFileSystem(FileName rootName, String rootFile, FileSystemOptions options) { + super(rootName, rootFile, options); + this.rootFile = rootFile; + } + + @Override + protected FileObject createFile(AbstractFileName name) throws FileSystemException { + return new HopLocalFile(this, rootFile, name); + } + } + + static class HopLocalFile extends LocalFile { + HopLocalFile(LocalFileSystem fileSystem, String rootFile, AbstractFileName name) { + super(fileSystem, rootFile, name); + } + + @Override + protected void doCreateFolder() throws Exception { + File file = getLocalFile(); + if (!file.mkdirs() && !file.isDirectory()) { + throw new FileSystemException("vfs.provider.local/create-folder.error", file); + } + } + } +} diff --git a/core/src/main/java/org/apache/hop/core/vfs/HopVfs.java b/core/src/main/java/org/apache/hop/core/vfs/HopVfs.java index b46e7c74180..cd5e2fb19d2 100644 --- a/core/src/main/java/org/apache/hop/core/vfs/HopVfs.java +++ b/core/src/main/java/org/apache/hop/core/vfs/HopVfs.java @@ -355,8 +355,7 @@ static DefaultFileSystemManager createFileSystemManager() throws HopException { try { DefaultFileSystemManager fsm = new DefaultFileSystemManager(); fsm.addProvider("ram", new org.apache.commons.vfs2.provider.ram.RamFileProvider()); - fsm.addProvider( - "file", new org.apache.commons.vfs2.provider.local.DefaultLocalFileProvider()); + fsm.addProvider("file", new HopLocalFileProvider()); fsm.addProvider("res", new org.apache.commons.vfs2.provider.res.ResourceFileProvider()); fsm.addProvider("zip", new org.apache.commons.vfs2.provider.zip.ZipFileProvider()); fsm.addProvider("gz", new org.apache.commons.vfs2.provider.gzip.GzipFileProvider()); diff --git a/core/src/test/java/org/apache/hop/core/vfs/HopLocalFileProviderTest.java b/core/src/test/java/org/apache/hop/core/vfs/HopLocalFileProviderTest.java new file mode 100644 index 00000000000..1bfe6b99e92 --- /dev/null +++ b/core/src/test/java/org/apache/hop/core/vfs/HopLocalFileProviderTest.java @@ -0,0 +1,64 @@ +/* + * 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.hop.core.vfs; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.nio.file.Files; +import java.nio.file.Path; +import org.apache.commons.vfs2.FileObject; +import org.apache.commons.vfs2.impl.DefaultFileSystemManager; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +class HopLocalFileProviderTest { + + @TempDir Path tempDir; + + @Test + void aFolderCreatedByAnotherWriterInTheMeantimeIsNotAnError() throws Exception { + DefaultFileSystemManager manager = new DefaultFileSystemManager(); + manager.addProvider("file", new HopLocalFileProvider()); + manager.init(); + try { + Path folder = tempDir.resolve("out").resolve("copy"); + FileObject target = manager.resolveFile(folder.toUri().toString()); + // Seen as missing ... + assertTrue(!target.exists()); + // ... then another copy of the transform, or another process, creates it + Files.createDirectories(folder); + target.createFolder(); + assertTrue(Files.isDirectory(folder)); + } finally { + manager.close(); + } + } + + /** Hop VFS reads and writes local files through this provider. */ + @Test + void hopVfsCreatesAFolderThatAppearedInTheMeantime() throws Exception { + Path folder = tempDir.resolve("vfs").resolve("copy"); + FileObject target = HopVfs.getFileObject(folder.toString()); + assertTrue(!target.exists()); + Files.createDirectories(folder); + + target.createFolder(); + + assertTrue(Files.isDirectory(folder)); + } +}