D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 20370 - On POSIX, std.file.copy only copies the file times at second precision
Summary: On POSIX, std.file.copy only copies the file times at second precision
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86_64 All
: P1 normal
Assignee: No Owner
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2019-11-08 14:27 UTC by Jonathan M Davis
Modified: 2020-09-18 12:59 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Jonathan M Davis 2019-11-08 14:27:05 UTC
Currently, on POSIX systems, after std.file.copy has copied the file, it sets the access time and modification times of the copy to be the same as the original using utime:

        utimbuf utim = void;
        utim.actime = cast(time_t) statbufr.st_atime;
        utim.modtime = cast(time_t) statbufr.st_mtime;

        cenforce(utime(toz, &utim) != -1, f, fromz);

utime only has second precision (as shown by the cast to time_t). The result is that if you copy a file on a file system that supports sub-second precision (which should be most file systems), the copy will actually be considered newer than the original. E.G. if the modification time of the original was "2019-Nov-07 19:41:49.2518144", the copy would be "2019-Nov-07 19:41:49".

So, std.file.copy should changed to use a function with sub-second precision. From the little bit of research I've done thus far, it looks like utimensat would be the correct replacement (it having nanosecond precision and being from "IEEE Std 1003.1-2008 ('POSIX.1')"). It looks like FreeBSD and Linux both have this function, and druntime has it for both. I don't know if Mac OS X has it or not, but druntime does not have it for Darwin. Assuming that it's not just missing from druntime, we could use utimes on Mac OS X, which isn't as good as utimensat (utimes only has microsecond precision), but it's still better than utime.
Comment 1 Dlang Bot 2020-09-12 03:44:56 UTC
@n8sh created dlang/phobos pull request #7629 "On POSIX std.file.copy was copying file times at second precision" fixing this issue:

- Fix Issue 20370 - On POSIX, std.file.copy only copies the file times at second precision

https://github.com/dlang/phobos/pull/7629
Comment 2 Dlang Bot 2020-09-16 07:31:51 UTC
dlang/phobos pull request #7629 "On POSIX std.file.copy was copying file times at second precision" was merged into stable:

- 99e7366b99c240014eaabd0ec496584efe2c0bde by Nathan Sashihara:
  Fix Issue 20370 - On POSIX, std.file.copy only copies the file times at second precision

https://github.com/dlang/phobos/pull/7629
Comment 3 Dlang Bot 2020-09-18 12:59:22 UTC
dlang/phobos pull request #7639 "merge stable" was merged into master:

- b8e9e70e9046aafe723e2184fcdf2c6f069bc9f6 by Nathan Sashihara:
  Fix Issue 20370 - On POSIX, std.file.copy only copies the file times at second precision

https://github.com/dlang/phobos/pull/7639