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

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git


The following commit(s) were added to refs/heads/master by this push:
     new 5d3da80  Add PathUtils.waitFor(Path, Duration, LinkOption...).
5d3da80 is described below

commit 5d3da80947db25ae8e7311e40f567c542fa31e0a
Author: Gary Gregory <gardgreg...@gmail.com>
AuthorDate: Tue Sep 7 11:29:17 2021 -0400

    Add PathUtils.waitFor(Path, Duration, LinkOption...).
---
 src/changes/changes.xml                            |  5 ++-
 src/main/java/org/apache/commons/io/FileUtils.java | 24 ++-----------
 .../java/org/apache/commons/io/file/PathUtils.java | 41 ++++++++++++++++++++++
 3 files changed, 47 insertions(+), 23 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 4a4e5d5..aa11f28 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -164,9 +164,12 @@ The <action> type attribute can be add,update,fix,remove.
         Add FileTimes.
       </action>
       <action dev="ggregory" type="add" due-to="Gary Gregory">
-        Update FileEntry to use FileTime instead of long for file time stamps.
+        Add PathUtils.waitFor(Path, Duration, LinkOption...).
       </action>
       <!-- UPDATE -->
+      <action dev="ggregory" type="add" due-to="Gary Gregory">
+        Update FileEntry to use FileTime instead of long for file time stamps.
+      </action>
       <action dev="ggregory" type="update" due-to="Dependabot">
         Bump Maven Javadoc plugin from 3.2.0 to 3.3.0.
       </action>
diff --git a/src/main/java/org/apache/commons/io/FileUtils.java 
b/src/main/java/org/apache/commons/io/FileUtils.java
index e760bba..fd5438d 100644
--- a/src/main/java/org/apache/commons/io/FileUtils.java
+++ b/src/main/java/org/apache/commons/io/FileUtils.java
@@ -43,6 +43,7 @@ import java.nio.file.NotDirectoryException;
 import java.nio.file.Path;
 import java.nio.file.StandardCopyOption;
 import java.nio.file.attribute.FileTime;
+import java.time.Duration;
 import java.time.Instant;
 import java.time.LocalTime;
 import java.time.ZoneId;
@@ -3175,28 +3176,7 @@ public class FileUtils {
      */
     public static boolean waitFor(final File file, final int seconds) {
         Objects.requireNonNull(file, "file");
-        final long finishAtMillis = System.currentTimeMillis() + (seconds * 
1000L);
-        boolean wasInterrupted = false;
-        try {
-            while (!file.exists()) {
-                final long remainingMillis = finishAtMillis -  
System.currentTimeMillis();
-                if (remainingMillis < 0){
-                    return false;
-                }
-                try {
-                    Thread.sleep(Math.min(100, remainingMillis));
-                } catch (final InterruptedException ignore) {
-                    wasInterrupted = true;
-                } catch (final Exception ex) {
-                    break;
-                }
-            }
-        } finally {
-            if (wasInterrupted) {
-                Thread.currentThread().interrupt();
-            }
-        }
-        return true;
+        return PathUtils.waitFor(file.toPath(), Duration.ofSeconds(seconds), 
PathUtils.EMPTY_LINK_OPTION_ARRAY);
     }
 
     /**
diff --git a/src/main/java/org/apache/commons/io/file/PathUtils.java 
b/src/main/java/org/apache/commons/io/file/PathUtils.java
index acfa053..29827b5 100644
--- a/src/main/java/org/apache/commons/io/file/PathUtils.java
+++ b/src/main/java/org/apache/commons/io/file/PathUtils.java
@@ -47,6 +47,7 @@ import java.nio.file.attribute.FileTime;
 import java.nio.file.attribute.PosixFileAttributeView;
 import java.nio.file.attribute.PosixFileAttributes;
 import java.nio.file.attribute.PosixFilePermission;
+import java.time.Duration;
 import java.time.Instant;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -1069,6 +1070,46 @@ public final class PathUtils {
     }
 
     /**
+     * Waits for the file system to propagate a file creation, with a timeout.
+     * <p>
+     * This method repeatedly tests {@link File#exists()} until it returns 
true up to the maximum time specified in seconds.
+     * </p>
+     *
+     * @param file the file to check, must not be {@code null}.
+     * @param duration the maximum time in seconds to wait.
+     * @param options options indicating how symbolic links are handled.
+     * @return true if file exists.
+     * @throws NullPointerException if the file is {@code null}.
+     * @since 2.12.0
+     */
+    public static boolean waitFor(final Path file, final Duration duration, 
LinkOption... options) {
+        Objects.requireNonNull(file, "file");
+        final Instant finishInstant = Instant.now().plus(duration);
+        boolean wasInterrupted = false;
+        final long minSleepMillis = 100;
+        try {
+            while (!Files.exists(file, options)) {
+                final long remainingMillis = 
finishInstant.minusMillis(System.currentTimeMillis()).toEpochMilli();
+                if (remainingMillis < 0) {
+                    return false;
+                }
+                try {
+                    Thread.sleep(Math.min(minSleepMillis, remainingMillis));
+                } catch (final InterruptedException ignore) {
+                    wasInterrupted = true;
+                } catch (final Exception ex) {
+                    break;
+                }
+            }
+        } finally {
+            if (wasInterrupted) {
+                Thread.currentThread().interrupt();
+            }
+        }
+        return true;
+    }
+
+    /**
      * Returns a stream of filtered paths.
      *
      * @param start the start path

Reply via email to