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 04ab528 Add and use PathUtils.setLastModifiedTime(Path) for better
precision.
04ab528 is described below
commit 04ab5286843bf3b8194aabd9371e43277bc23189
Author: Gary Gregory <[email protected]>
AuthorDate: Tue Sep 14 11:37:00 2021 -0400
Add and use PathUtils.setLastModifiedTime(Path) for better precision.
Add and use PathUtils.setLastModifiedTime(Path, Path) for better
precision.
---
src/changes/changes.xml | 4 ++++
src/main/java/org/apache/commons/io/FileUtils.java | 11 ++++++---
.../java/org/apache/commons/io/file/PathUtils.java | 26 ++++++++++++++++++++++
3 files changed, 38 insertions(+), 3 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 2f285a1..010208d 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -175,6 +175,10 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add FileUtils.current().
</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">
+ Add and use PathUtils.setLastModifiedTime(Path) for better precision.
+ Add and use PathUtils.setLastModifiedTime(Path, Path) for better
precision.
+ </action>
<!-- UPDATE -->
<action dev="ggregory" type="add" due-to="Gary Gregory">
Update FileEntry to use FileTime instead of long for file time stamps.
diff --git a/src/main/java/org/apache/commons/io/FileUtils.java
b/src/main/java/org/apache/commons/io/FileUtils.java
index e0a2b58..2a97da3 100644
--- a/src/main/java/org/apache/commons/io/FileUtils.java
+++ b/src/main/java/org/apache/commons/io/FileUtils.java
@@ -2821,14 +2821,19 @@ public class FileUtils {
* Sets the given {@code targetFile}'s last modified date to the value
from {@code sourceFile}.
*
* @param sourceFile The source file to query.
- * @param targetFile The target file to set.
+ * @param targetFile The target file or directory to set.
* @throws NullPointerException if sourceFile is {@code null}.
* @throws NullPointerException if targetFile is {@code null}.
* @throws IOException if setting the last-modified time failed.
*/
private static void setLastModified(final File sourceFile, final File
targetFile) throws IOException {
Objects.requireNonNull(sourceFile, "sourceFile");
- setLastModified(targetFile, lastModified(sourceFile));
+ Objects.requireNonNull(targetFile, "targetFile");
+ if (targetFile.isFile()) {
+ PathUtils.setLastModifiedTime(targetFile.toPath(),
sourceFile.toPath());
+ } else {
+ setLastModified(targetFile, lastModified(sourceFile));
+ }
}
/**
@@ -3133,7 +3138,7 @@ public class FileUtils {
if (!file.exists()) {
openOutputStream(file).close();
}
- setLastModified(file, System.currentTimeMillis());
+ PathUtils.setLastModifiedTime(file.toPath());
}
/**
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 2136ac7..fc77ed0 100644
--- a/src/main/java/org/apache/commons/io/file/PathUtils.java
+++ b/src/main/java/org/apache/commons/io/file/PathUtils.java
@@ -943,6 +943,32 @@ public final class PathUtils {
}
/**
+ * Sets the given {@code targetFile}'s last modified time to the value
from {@code sourceFile}.
+ *
+ * @param sourceFile The source path to query.
+ * @param targetFile The target path to set.
+ * @throws NullPointerException if sourceFile is {@code null}.
+ * @throws NullPointerException if targetFile is {@code null}.
+ * @throws IOException if setting the last-modified time failed.
+ * @since 2.12.0
+ */
+ public static void setLastModifiedTime(final Path sourceFile, final Path
targetFile) throws IOException {
+ Objects.requireNonNull(sourceFile, "sourceFile");
+ Files.setLastModifiedTime(targetFile,
Files.getLastModifiedTime(sourceFile));
+ }
+
+ /**
+ * Sets the last modified time of the given file path to now.
+ *
+ * @param path The file path to set.
+ * @throws IOException if an I/O error occurs.
+ * @since 2.12.0
+ */
+ public static void setLastModifiedTime(final Path path) throws IOException
{
+ Files.setLastModifiedTime(path, FileTime.from(Instant.now()));
+ }
+
+ /**
* Sets the given Path to the {@code readOnly} value.
* <p>
* This behavior is OS dependent.