time_unix.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. //go:build !windows
  2. // +build !windows
  3. /*
  4. Copyright The containerd Authors.
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. */
  15. package archive
  16. import (
  17. "fmt"
  18. "time"
  19. "golang.org/x/sys/unix"
  20. )
  21. func chtimes(path string, atime, mtime time.Time) error {
  22. var utimes [2]unix.Timespec
  23. utimes[0] = unix.NsecToTimespec(atime.UnixNano())
  24. utimes[1] = unix.NsecToTimespec(mtime.UnixNano())
  25. if err := unix.UtimesNanoAt(unix.AT_FDCWD, path, utimes[0:], unix.AT_SYMLINK_NOFOLLOW); err != nil {
  26. return fmt.Errorf("failed call to UtimesNanoAt for %s: %w", path, err)
  27. }
  28. return nil
  29. }