Go: os: allow Chtimes with time.Time{} to avoid setting time

Created on 11 Jun 2019  ·  9Comments  ·  Source: golang/go

os.Chtimes(f, a, m) sets the access and modification times of the file f to a, m.
The underlying call can usually say "leave this one unset" to set just one of the two.
We could support that by defining that time.Time{} means "don't set this one".
We probably should do that.

Thoughts?

Related to but different from #31880.

NeedsFix Proposal Proposal-Accepted

Most helpful comment

I've mentioned a half dozen times to @ianlancetaylor and @griesemer that I thought we already did this and I've tried to find old CLs (that were never submitted?) or old threads on this, but all along it seems I was remembering https://golang.org/pkg/os/#Chown instead which says:

A uid or gid of -1 means to not change that value

The fact that Chown already does this pattern and I assumed we did the same for Chtimes seems like a vote in favor of this proposal.

All 9 comments

While this would be an improvement, it might have some issues with backwards compatibility.

Therefore I would propose, as I did in the related issue to have a new function os.Chfiletimes that takes os.FileTime in stead of time.Time for the last two arguments, where os.FileTime could be something like type FileTime struct { time.Time ; Now bool ; Ignore bool }.

Need to understand #31880 before we can reasonably decide something here - maybe more is needed and we can make one change instead of two. But #31880 is blocked on knowing more about the Windows-induced constraints.

Re @beoran's comment, we confirmed that Chtimes with the zero time right now ends up in 1754 on a Linux file system. So if we adopted the zero time convention then use on older versions would not recognize or reject the zero time and would end up in 1754 instead.

Given that it looks like #31880 may be declined, we'll probably want to decide this next. It seems like the main question is whether anyone needs this functionality. Does anyone need this?

(The backwards compatibility issue can be solved by just waiting a couple releases before starting to use a change.)

I looked at 150 uses of os.Chtimes in Google code. I found 20 cases where it appeared that it would be convenient to leave one of the times unchanged.

Coincidentally I found 21 cases that set one or both times to "now".

I've mentioned a half dozen times to @ianlancetaylor and @griesemer that I thought we already did this and I've tried to find old CLs (that were never submitted?) or old threads on this, but all along it seems I was remembering https://golang.org/pkg/os/#Chown instead which says:

A uid or gid of -1 means to not change that value

The fact that Chown already does this pattern and I assumed we did the same for Chtimes seems like a vote in favor of this proposal.

Matching os.Chown's approach for ignoring part of the operation seems like a decent argument for doing the same in os.Chtimes.

This seems like a likely accept in the absence of any objections or arguments against it.

Leaving open for a week for any final comments.

No change in consensus. Accepted.

Change https://golang.org/cl/219638 mentions this issue: os: skip change of the zero value in Chtimes for Posix

Was this page helpful?
0 / 5 - 0 ratings

Related issues

myitcv picture myitcv  ·  3Comments

mingrammer picture mingrammer  ·  3Comments

natefinch picture natefinch  ·  3Comments

gopherbot picture gopherbot  ·  3Comments

rakyll picture rakyll  ·  3Comments