4

I don't know if this affects MacOS versions newer than High Sierra, but it seems that cp -p won't preserve timestamps with nanoseconds resolution.

Because of this, the shell can wrongly tell that a file is older than other even if it was copied by cp -p. This breaks build system dependencies when some files were installed by cp -p: they keep reinstalling and reinstalling again because the nanoseconds don't match.

Can you suggest some workaround for this? I thought compiling my own cp binary, but however cp is usually a shell builtin, and moreover I'm not aware of any nanosecond-preserving cp version out there.

Note: I know of a workaround, but it's not reasonable: Execute touch on the source file before executing cp -p. Because touch doesn't preserve nanoseconds either, it makes them zero on the source file, so the result after cp -p guarantees exactly the same timestamp. However, as I said, it's not reasonable, because I don't like to touch files just for this: you lose their real last saved date.

0
3

Good to know that I'm not the only one who thinks this is a problem. I ran into it while writing a Python script for monitoring parts of my macOS (10.15.7) filesystem, which also involved coping files. The script sometimes behaved erratic and produced unpredictable output. After some poking around I found out that the incapability of cp -p to preserve nanoseconds is the core of the problem.

One workaround for your problem (but not for mine) could be coreutils: gcp -p behaves as intended.

2

I thought this was fixed in 10.15, but I just didn't repro the problem correctly. To save others the same embarrassment, here's a repro that shows the problem:

% python3 -c 'open("file1", "w")'
% stat -f %Fm file1
1609301645.481666557
% cp -p file1 file2
% stat -f %Fm file2
1609301645.481666000
3
  • aren't the listed numbers short of a nanosecond accuracy. Nanosecond = 10^-9. listed decimals are 10^-6.
    – Natsfan
    Dec 30 '20 at 4:09
  • You're right! I thought it copying sub-second timestamp info was good news, but it isn't. I edited my "answer".
    – thakis
    Dec 30 '20 at 4:17
  • I also filed a feedback report: openradar.appspot.com/radar?id=4946596567449600 I'd recommend everyone who wants this fixed to also file a report at feedbackassistant.apple.com
    – thakis
    Dec 30 '20 at 17:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .