[
https://issues.apache.org/jira/browse/IO-769?focusedWorklogId=805670&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-805670
]
ASF GitHub Bot logged work on IO-769:
-------------------------------------
Author: ASF GitHub Bot
Created on: 02/Sep/22 03:28
Start Date: 02/Sep/22 03:28
Worklog Time Spent: 10m
Work Description: kinow commented on code in PR #377:
URL: https://github.com/apache/commons-io/pull/377#discussion_r961252278
##########
src/main/java/org/apache/commons/io/FileUtils.java:
##########
@@ -2840,37 +2836,30 @@ private static File requireFileIfExists(final File
file, final String name) {
}
/**
- * Sets the given {@code targetFile}'s last modified date to the value
from {@code sourceFile}.
+ * Set file lastModifiedTime, lastAccessTime and creationTime to match
source file
*
* @param sourceFile The source file to query.
* @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 {
+ private static void setTimes(final File sourceFile, final File targetFile)
throws IOException {
Objects.requireNonNull(sourceFile, "sourceFile");
Objects.requireNonNull(targetFile, "targetFile");
- if (targetFile.isFile()) {
- PathUtils.setLastModifiedTime(targetFile.toPath(),
sourceFile.toPath());
- } else {
- setLastModified(targetFile, lastModified(sourceFile));
- }
- }
-
- /**
- * Sets the given {@code targetFile}'s last modified date to the given
value.
- *
- * @param file The source file to query.
- * @param timeMillis The new last-modified time, measured in milliseconds
since the epoch 01-01-1970 GMT.
- * @throws NullPointerException if file is {@code null}.
- * @throws IOException if setting the last-modified time failed.
- */
- private static void setLastModified(final File file, final long
timeMillis) throws IOException {
- Objects.requireNonNull(file, "file");
- if (!file.setLastModified(timeMillis)) {
- throw new IOException(String.format("Failed setLastModified(%s) on
'%s'", timeMillis, file));
- }
+ try {
+ // Set creation, modified, last accessed to match source file
+ final BasicFileAttributes srcAttr =
Files.readAttributes(sourceFile.toPath(), BasicFileAttributes.class);
+ final BasicFileAttributeView destAttrView =
Files.getFileAttributeView(targetFile.toPath(), BasicFileAttributeView.class);
+ // null guards are not needed; BasicFileAttributes.setTimes(...)
is null safe
+ destAttrView.setTimes(srcAttr.lastModifiedTime(),
srcAttr.lastAccessTime(), srcAttr.creationTime());
+ } catch(IOException unused) {
+ // Fallback: Only set modified time to match source file
+ targetFile.setLastModified(sourceFile.lastModified());
+ }
+
+ // TODO: (Help!) Determine historically why setLastModified(File,
File) needed PathUtils.setLastModifiedTime() if
+ // sourceFile.isFile() was true, but needed setLastModifiedTime(File,
long) if sourceFile.isFile() was false
Review Comment:
Thanks for adding the note. No idea why that changed, didn't have time to
dig into `git`'s history, sorry.
Issue Time Tracking
-------------------
Worklog Id: (was: 805670)
Time Spent: 20m (was: 10m)
> FileUtils.copyFileToDirectory can lead to not accessible file when preserving
> the file date
> -------------------------------------------------------------------------------------------
>
> Key: IO-769
> URL: https://issues.apache.org/jira/browse/IO-769
> Project: Commons IO
> Issue Type: Bug
> Components: Utilities
> Affects Versions: 2.11.0
> Reporter: Jérémy Carnus
> Priority: Major
> Time Spent: 20m
> Remaining Estimate: 0h
>
> Hi,
> The current implementation for copyFileToDirectory by default preserve the
> file.
> There 2 issues regarding this:
> * the javadoc mentions this is done by File.setLastModified by in fact this
> is done by the COPY_ATTRIBUTES options
> * Under Windows, the COPY_ATTRIBUTES also copies the security attributes
> (SID and permissions) and can lead to a file not beeing readable after copy
> (if for example, you copie from a mount under docker or a shared folder)
>
--
This message was sent by Atlassian Jira
(v8.20.10#820010)