github.com/olljanat/moby@v1.13.1/pkg/system/chtimes.go (about)

     1  package system
     2  
     3  import (
     4  	"os"
     5  	"syscall"
     6  	"time"
     7  	"unsafe"
     8  )
     9  
    10  var (
    11  	maxTime time.Time
    12  )
    13  
    14  func init() {
    15  	if unsafe.Sizeof(syscall.Timespec{}.Nsec) == 8 {
    16  		// This is a 64 bit timespec
    17  		// os.Chtimes limits time to the following
    18  		maxTime = time.Unix(0, 1<<63-1)
    19  	} else {
    20  		// This is a 32 bit timespec
    21  		maxTime = time.Unix(1<<31-1, 0)
    22  	}
    23  }
    24  
    25  // Chtimes changes the access time and modified time of a file at the given path
    26  func Chtimes(name string, atime time.Time, mtime time.Time) error {
    27  	unixMinTime := time.Unix(0, 0)
    28  	unixMaxTime := maxTime
    29  
    30  	// If the modified time is prior to the Unix Epoch, or after the
    31  	// end of Unix Time, os.Chtimes has undefined behavior
    32  	// default to Unix Epoch in this case, just in case
    33  
    34  	if atime.Before(unixMinTime) || atime.After(unixMaxTime) {
    35  		atime = unixMinTime
    36  	}
    37  
    38  	if mtime.Before(unixMinTime) || mtime.After(unixMaxTime) {
    39  		mtime = unixMinTime
    40  	}
    41  
    42  	if err := os.Chtimes(name, atime, mtime); err != nil {
    43  		return err
    44  	}
    45  
    46  	// Take platform specific action for setting create time.
    47  	if err := setCTime(name, mtime); err != nil {
    48  		return err
    49  	}
    50  
    51  	return nil
    52  }