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

bamaer pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/hop.git


The following commit(s) were added to refs/heads/main by this push:
     new 94cc2e8abf File locked: resolve each file name once, fixes #9472 
(#9473)
94cc2e8abf is described below

commit 94cc2e8abf9b021dae1c3c9e28aaa06f8e50452a
Author: Hans Van Akelyen <[email protected]>
AuthorDate: Sat Nov 14 13:34:56 2026 +0100

    File locked: resolve each file name once, fixes #9472 (#9473)
---
 integration-tests/transforms/0127-file-locked.hpl  | 202 +++++++++++++++++++++
 .../transforms/datasets/golden-file-locked.csv     |   5 +
 .../transforms/main-0127-file-locked.hwf           |  79 ++++++++
 .../metadata/dataset/golden-file-locked.json       |  23 +++
 .../metadata/unit-test/0127-file-locked UNIT.json  |  32 ++++
 .../workflow/actions/checkfilelocked/LockFile.java |  27 +--
 .../actions/checkfilelocked/LockFileTest.java      | 131 +++++++++++++
 7 files changed, 476 insertions(+), 23 deletions(-)

diff --git a/integration-tests/transforms/0127-file-locked.hpl 
b/integration-tests/transforms/0127-file-locked.hpl
new file mode 100644
index 0000000000..facdda98e5
--- /dev/null
+++ b/integration-tests/transforms/0127-file-locked.hpl
@@ -0,0 +1,202 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+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.
+
+-->
+<pipeline>
+  <info>
+    <name>0127-file-locked</name>
+    <name_sync_with_filename>Y</name_sync_with_filename>
+    <description/>
+    <extended_description/>
+    <pipeline_version/>
+    <pipeline_type>Normal</pipeline_type>
+    <parameters>
+    </parameters>
+    <capture_transform_performance>N</capture_transform_performance>
+    
<transform_performance_capturing_delay>1000</transform_performance_capturing_delay>
+    
<transform_performance_capturing_size_limit>100</transform_performance_capturing_size_limit>
+    <created_user>-</created_user>
+    <created_date>2026/11/13 12:00:00.000</created_date>
+    <modified_user>-</modified_user>
+    <modified_date>2026/11/13 12:00:00.000</modified_date>
+  </info>
+  <notepads>
+  </notepads>
+  <order>
+    <hop>
+      <from>names</from>
+      <to>folder</to>
+      <enabled>Y</enabled>
+    </hop>
+    <hop>
+      <from>folder</from>
+      <to>filename</to>
+      <enabled>Y</enabled>
+    </hop>
+    <hop>
+      <from>filename</from>
+      <to>file locked</to>
+      <enabled>Y</enabled>
+    </hop>
+    <hop>
+      <from>file locked</from>
+      <to>verify</to>
+      <enabled>Y</enabled>
+    </hop>
+  </order>
+  <transform>
+    <name>names</name>
+    <type>DataGrid</type>
+    <description/>
+    <distribute>Y</distribute>
+    <custom_distribution/>
+    <copies>1</copies>
+    <partitioning>
+      <method>none</method>
+      <schema_name/>
+    </partitioning>
+    <data>
+      <line>
+        <item>sample-file1.txt</item>
+      </line>
+      <line>
+        <item>sample-file2.txt</item>
+      </line>
+      <line>
+        <item>getfilenames-tree</item>
+      </line>
+      <line>
+        <item>file-locked-does-not-exist.txt</item>
+      </line>
+    </data>
+    <fields>
+      <field>
+        <length>-1</length>
+        <precision>-1</precision>
+        <set_empty_string>N</set_empty_string>
+        <name>name</name>
+        <type>String</type>
+      </field>
+    </fields>
+    <attributes/>
+    <GUI>
+      <xloc>128</xloc>
+      <yloc>160</yloc>
+    </GUI>
+  </transform>
+  <transform>
+    <name>folder</name>
+    <type>GetVariable</type>
+    <description/>
+    <distribute>Y</distribute>
+    <custom_distribution/>
+    <copies>1</copies>
+    <partitioning>
+      <method>none</method>
+      <schema_name/>
+    </partitioning>
+    <fields>
+      <field>
+        <length>-1</length>
+        <name>folder</name>
+        <precision>-1</precision>
+        <trim_type>none</trim_type>
+        <type>String</type>
+        <variable>${PROJECT_HOME}/files/</variable>
+      </field>
+    </fields>
+    <attributes/>
+    <GUI>
+      <xloc>288</xloc>
+      <yloc>160</yloc>
+    </GUI>
+  </transform>
+  <transform>
+    <name>filename</name>
+    <type>Calculator</type>
+    <description/>
+    <distribute>Y</distribute>
+    <custom_distribution/>
+    <copies>1</copies>
+    <partitioning>
+      <method>none</method>
+      <schema_name/>
+    </partitioning>
+    <failIfNoFile>Y</failIfNoFile>
+    <calculation>
+      <calc_type>ADD</calc_type>
+      <conversion_mask/>
+      <currency_symbol/>
+      <decimal_symbol/>
+      <field_a>folder</field_a>
+      <field_b>name</field_b>
+      <field_c/>
+      <field_name>filename</field_name>
+      <grouping_symbol/>
+      <remove>N</remove>
+      <value_length>-1</value_length>
+      <value_precision>-1</value_precision>
+      <value_type>String</value_type>
+    </calculation>
+    <attributes/>
+    <GUI>
+      <xloc>448</xloc>
+      <yloc>160</yloc>
+    </GUI>
+  </transform>
+  <transform>
+    <name>file locked</name>
+    <type>FileLocked</type>
+    <description/>
+    <distribute>Y</distribute>
+    <custom_distribution/>
+    <copies>1</copies>
+    <partitioning>
+      <method>none</method>
+      <schema_name/>
+    </partitioning>
+    <addresultfilenames>Y</addresultfilenames>
+    <filenamefield>filename</filenamefield>
+    <resultfieldname>locked</resultfieldname>
+    <attributes/>
+    <GUI>
+      <xloc>608</xloc>
+      <yloc>160</yloc>
+    </GUI>
+  </transform>
+  <transform>
+    <name>verify</name>
+    <type>Dummy</type>
+    <description/>
+    <distribute>Y</distribute>
+    <custom_distribution/>
+    <copies>1</copies>
+    <partitioning>
+      <method>none</method>
+      <schema_name/>
+    </partitioning>
+    <attributes/>
+    <GUI>
+      <xloc>768</xloc>
+      <yloc>160</yloc>
+    </GUI>
+  </transform>
+  <transform_error_handling>
+  </transform_error_handling>
+  <attributes/>
+</pipeline>
diff --git a/integration-tests/transforms/datasets/golden-file-locked.csv 
b/integration-tests/transforms/datasets/golden-file-locked.csv
new file mode 100644
index 0000000000..62780f4d71
--- /dev/null
+++ b/integration-tests/transforms/datasets/golden-file-locked.csv
@@ -0,0 +1,5 @@
+name,locked
+file-locked-does-not-exist.txt,N
+getfilenames-tree,N
+sample-file1.txt,N
+sample-file2.txt,N
diff --git a/integration-tests/transforms/main-0127-file-locked.hwf 
b/integration-tests/transforms/main-0127-file-locked.hwf
new file mode 100644
index 0000000000..cbd11c9a10
--- /dev/null
+++ b/integration-tests/transforms/main-0127-file-locked.hwf
@@ -0,0 +1,79 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+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.
+
+-->
+<workflow>
+  <name>main-0127-file-locked</name>
+  <name_sync_with_filename>Y</name_sync_with_filename>
+  <description/>
+  <extended_description/>
+  <workflow_version/>
+  <created_user>-</created_user>
+  <created_date>2026/11/13 12:00:00.000</created_date>
+  <modified_user>-</modified_user>
+  <modified_date>2026/11/13 12:00:00.000</modified_date>
+  <parameters>
+    </parameters>
+  <actions>
+    <action>
+      <name>START</name>
+      <description/>
+      <type>SPECIAL</type>
+      <attributes/>
+      <repeat>N</repeat>
+      <schedulerType>0</schedulerType>
+      <intervalSeconds>0</intervalSeconds>
+      <intervalMinutes>60</intervalMinutes>
+      <hour>12</hour>
+      <minutes>0</minutes>
+      <weekDay>1</weekDay>
+      <DayOfMonth>1</DayOfMonth>
+      <parallel>N</parallel>
+      <xloc>96</xloc>
+      <yloc>80</yloc>
+      <attributes_hac/>
+    </action>
+    <action>
+      <name>0127-file-locked UNIT</name>
+      <description/>
+      <type>RunPipelineTests</type>
+      <attributes/>
+      <test_names>
+        <test_name>
+          <name>0127-file-locked UNIT</name>
+        </test_name>
+      </test_names>
+      <parallel>N</parallel>
+      <xloc>320</xloc>
+      <yloc>80</yloc>
+      <attributes_hac/>
+    </action>
+  </actions>
+  <hops>
+    <hop>
+      <from>START</from>
+      <to>0127-file-locked UNIT</to>
+      <enabled>Y</enabled>
+      <evaluation>Y</evaluation>
+      <unconditional>Y</unconditional>
+    </hop>
+  </hops>
+  <notepads>
+  </notepads>
+  <attributes/>
+</workflow>
diff --git 
a/integration-tests/transforms/metadata/dataset/golden-file-locked.json 
b/integration-tests/transforms/metadata/dataset/golden-file-locked.json
new file mode 100644
index 0000000000..b32de4dbf7
--- /dev/null
+++ b/integration-tests/transforms/metadata/dataset/golden-file-locked.json
@@ -0,0 +1,23 @@
+{
+  "base_filename": "golden-file-locked.csv",
+  "name": "golden-file-locked",
+  "description": "The lock indicator of files and a folder that nobody holds, 
and of a file that does not exist.",
+  "dataset_fields": [
+    {
+      "field_comment": "",
+      "field_length": -1,
+      "field_type": 2,
+      "field_precision": -1,
+      "field_name": "name",
+      "field_format": ""
+    },
+    {
+      "field_comment": "",
+      "field_length": -1,
+      "field_type": 4,
+      "field_precision": -1,
+      "field_name": "locked",
+      "field_format": ""
+    }
+  ]
+}
diff --git a/integration-tests/transforms/metadata/unit-test/0127-file-locked 
UNIT.json b/integration-tests/transforms/metadata/unit-test/0127-file-locked 
UNIT.json
new file mode 100644
index 0000000000..15bfcd820c
--- /dev/null
+++ b/integration-tests/transforms/metadata/unit-test/0127-file-locked 
UNIT.json        
@@ -0,0 +1,32 @@
+{
+  "variableValues": [],
+  "database_replacements": [],
+  "autoOpening": false,
+  "basePath": "",
+  "golden_data_sets": [
+    {
+      "field_mappings": [
+        {
+          "transform_field": "name",
+          "data_set_field": "name"
+        },
+        {
+          "transform_field": "locked",
+          "data_set_field": "locked"
+        }
+      ],
+      "field_order": [
+        "name"
+      ],
+      "transform_name": "verify",
+      "data_set_name": "golden-file-locked"
+    }
+  ],
+  "input_data_sets": [],
+  "name": "0127-file-locked UNIT",
+  "description": "Two files, a folder and a missing file that nobody holds are 
not locked",
+  "trans_test_tweaks": [],
+  "persist_filename": "",
+  "pipeline_filename": "./0127-file-locked.hpl",
+  "test_type": "UNIT_TEST"
+}
diff --git 
a/plugins/actions/checkfilelocked/src/main/java/org/apache/hop/workflow/actions/checkfilelocked/LockFile.java
 
b/plugins/actions/checkfilelocked/src/main/java/org/apache/hop/workflow/actions/checkfilelocked/LockFile.java
index 1752041715..61bba73930 100644
--- 
a/plugins/actions/checkfilelocked/src/main/java/org/apache/hop/workflow/actions/checkfilelocked/LockFile.java
+++ 
b/plugins/actions/checkfilelocked/src/main/java/org/apache/hop/workflow/actions/checkfilelocked/LockFile.java
@@ -43,38 +43,19 @@ public class LockFile {
 
     // In order to check is a file is locked
     // we will use a dummy renaming exercise
-    FileObject file = null;
-    FileObject dummyfile = null;
-
-    try {
-
-      file = HopVfs.getFileObject(filename, variables);
+    //
+    // The name is resolved once: resolving it again gives the same file 
object.
+    try (FileObject file = HopVfs.getFileObject(filename, variables)) {
       if (file.exists()) {
-        dummyfile = HopVfs.getFileObject(filename, variables);
         // move file to itself!
         // Not HopVfs.moveFile(): here the failing rename is the answer we 
came for, and its
         // fallback would copy this file onto itself and then delete it.
-        file.moveTo(dummyfile);
+        file.moveTo(file);
       }
     } catch (Exception e) {
       // We got an exception
       // The is locked by another process
       setLocked(true);
-    } finally {
-      if (file != null) {
-        try {
-          file.close();
-        } catch (Exception e) {
-          /* Ignore */
-        }
-      }
-      if (dummyfile != null) {
-        try {
-          file.close();
-        } catch (Exception e) {
-          /* Ignore */
-        }
-      }
     }
   }
 
diff --git 
a/plugins/actions/checkfilelocked/src/test/java/org/apache/hop/workflow/actions/checkfilelocked/LockFileTest.java
 
b/plugins/actions/checkfilelocked/src/test/java/org/apache/hop/workflow/actions/checkfilelocked/LockFileTest.java
new file mode 100644
index 0000000000..50cf1787f3
--- /dev/null
+++ 
b/plugins/actions/checkfilelocked/src/test/java/org/apache/hop/workflow/actions/checkfilelocked/LockFileTest.java
@@ -0,0 +1,131 @@
+/*
+ * 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.workflow.actions.checkfilelocked;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assumptions.assumeFalse;
+
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.attribute.BasicFileAttributes;
+import java.nio.file.attribute.PosixFilePermissions;
+import org.apache.commons.vfs2.FileObject;
+import org.apache.hop.core.variables.IVariables;
+import org.apache.hop.core.variables.Variables;
+import org.apache.hop.core.vfs.HopVfs;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.EnabledOnOs;
+import org.junit.jupiter.api.condition.OS;
+import org.junit.jupiter.api.io.TempDir;
+
+/** What the lock check answers for a file, and that the check leaves the file 
as it was. */
+class LockFileTest {
+
+  @TempDir Path folder;
+
+  private final IVariables variables = new Variables();
+
+  @Test
+  void fileThatNobodyHoldsIsNotLocked() throws Exception {
+    Path file = Files.writeString(folder.resolve("data.txt"), "data", 
StandardCharsets.UTF_8);
+
+    LockFile lockFile = new LockFile(file.toString(), variables);
+
+    assertFalse(lockFile.isLocked());
+    assertEquals(file.toString(), lockFile.getFilename());
+  }
+
+  @Test
+  void theCheckLeavesTheFileAsItWas() throws Exception {
+    Path file = Files.writeString(folder.resolve("data.txt"), "data", 
StandardCharsets.UTF_8);
+    BasicFileAttributes before = Files.readAttributes(file, 
BasicFileAttributes.class);
+
+    // more than once: the first check must not leave anything behind that 
changes the second
+    for (int i = 0; i < 3; i++) {
+      assertFalse(new LockFile(file.toString(), variables).isLocked());
+    }
+
+    BasicFileAttributes after = Files.readAttributes(file, 
BasicFileAttributes.class);
+    assertEquals("data", Files.readString(file, StandardCharsets.UTF_8));
+    assertEquals(before.lastModifiedTime(), after.lastModifiedTime());
+    assertEquals(before.fileKey(), after.fileKey());
+    try (FileObject fileObject = HopVfs.getFileObject(file.toString(), 
variables)) {
+      assertTrue(fileObject.exists(), "the file is still there for whoever 
resolves it next");
+      assertTrue(fileObject.isFile());
+    }
+  }
+
+  @Test
+  void fileNameAsUrlIsChecked() throws Exception {
+    Path file = Files.writeString(folder.resolve("data.txt"), "data", 
StandardCharsets.UTF_8);
+
+    assertFalse(new LockFile(file.toUri().toString(), variables).isLocked());
+    assertEquals("data", Files.readString(file, StandardCharsets.UTF_8));
+  }
+
+  @Test
+  void missingFileIsNotLocked() throws Exception {
+    Path missing = folder.resolve("missing.txt");
+
+    assertFalse(new LockFile(missing.toString(), variables).isLocked());
+    assertFalse(Files.exists(missing), "the check does not create the file");
+  }
+
+  @Test
+  void folderIsNotLockedAndKeepsItsFiles() throws Exception {
+    Path sub = Files.createDirectory(folder.resolve("sub"));
+    Path child = Files.writeString(sub.resolve("child.txt"), "child", 
StandardCharsets.UTF_8);
+
+    assertFalse(new LockFile(sub.toString(), variables).isLocked());
+    assertEquals("child", Files.readString(child, StandardCharsets.UTF_8));
+  }
+
+  @Test
+  @EnabledOnOs({OS.LINUX, OS.MAC})
+  void readOnlyFileInWritableFolderIsNotLocked() throws Exception {
+    Path file = Files.writeString(folder.resolve("data.txt"), "data", 
StandardCharsets.UTF_8);
+    Files.setPosixFilePermissions(file, 
PosixFilePermissions.fromString("r--r--r--"));
+
+    assertFalse(new LockFile(file.toString(), variables).isLocked());
+    assertEquals("data", Files.readString(file, StandardCharsets.UTF_8));
+  }
+
+  /** A file that can not be renamed counts as locked: in a read-only folder 
that is every file. */
+  @Test
+  @EnabledOnOs({OS.LINUX, OS.MAC})
+  void fileInReadOnlyFolderIsLocked() throws Exception {
+    Path readOnly = Files.createDirectory(folder.resolve("read-only"));
+    Path file = Files.writeString(readOnly.resolve("data.txt"), "data", 
StandardCharsets.UTF_8);
+    Files.setPosixFilePermissions(readOnly, 
PosixFilePermissions.fromString("r-xr-xr-x"));
+    try {
+      assumeFalse(
+          Files.isWritable(readOnly), "folder is still writable, cannot run as 
this user (root?)");
+
+      // more than once: a failed check must not change the answer of the next 
one
+      for (int i = 0; i < 2; i++) {
+        assertTrue(new LockFile(file.toString(), variables).isLocked());
+      }
+      assertEquals("data", Files.readString(file, StandardCharsets.UTF_8));
+    } finally {
+      Files.setPosixFilePermissions(readOnly, 
PosixFilePermissions.fromString("rwx------"));
+    }
+  }
+}

Reply via email to