pax_global_header00006660000000000000000000000064146614625310014522gustar00rootroot0000000000000052 comment=fb0f3ca39cb7f62f974258c7d5d09efb113a9f4c golang-github-djherbis-times-1.6.0/000077500000000000000000000000001466146253100171645ustar00rootroot00000000000000golang-github-djherbis-times-1.6.0/.github/000077500000000000000000000000001466146253100205245ustar00rootroot00000000000000golang-github-djherbis-times-1.6.0/.github/workflows/000077500000000000000000000000001466146253100225615ustar00rootroot00000000000000golang-github-djherbis-times-1.6.0/.github/workflows/go-test.yml000066400000000000000000000016631466146253100246740ustar00rootroot00000000000000name: go test on: workflow_dispatch: push: branches: - master schedule: - cron: '0 17 * * 1' # https://crontab.guru/#0_17_*_*_1 jobs: build: runs-on: ${{ matrix.os }} strategy: matrix: os: [ubuntu-latest, windows-latest, macos-latest] js: [false] include: - os: ubuntu-latest js: true steps: - id: go-test uses: djherbis/actions/go-test@main with: github_token: ${{ secrets.GITHUB_TOKEN }} coveralls_parallel: true if: ${{ !matrix.js }} - id: go-test-js uses: djherbis/actions/go-test-js@main with: github_token: ${{ secrets.GITHUB_TOKEN }} coveralls_parallel: true if: ${{ matrix.js }} finish: needs: build runs-on: ubuntu-latest steps: - uses: shogo82148/actions-goveralls@v1 with: parallel-finished: true golang-github-djherbis-times-1.6.0/LICENSE000066400000000000000000000020641466146253100201730ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2015 Dustin H Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. golang-github-djherbis-times-1.6.0/README.md000066400000000000000000000050151466146253100204440ustar00rootroot00000000000000times ========== [![GoDoc](https://godoc.org/github.com/djherbis/times?status.svg)](https://godoc.org/github.com/djherbis/times) [![Release](https://img.shields.io/github/release/djherbis/times.svg)](https://github.com/djherbis/times/releases/latest) [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](LICENSE.txt) [![go test](https://github.com/djherbis/times/actions/workflows/go-test.yml/badge.svg)](https://github.com/djherbis/times/actions/workflows/go-test.yml) [![Coverage Status](https://coveralls.io/repos/djherbis/times/badge.svg?branch=master)](https://coveralls.io/r/djherbis/times?branch=master) [![Go Report Card](https://goreportcard.com/badge/github.com/djherbis/times)](https://goreportcard.com/report/github.com/djherbis/times) [![Sourcegraph](https://sourcegraph.com/github.com/djherbis/times/-/badge.svg)](https://sourcegraph.com/github.com/djherbis/times?badge) Usage ------------ File Times for #golang Go has a hidden time functions for most platforms, this repo makes them accessible. ```go package main import ( "log" "github.com/djherbis/times" ) func main() { t, err := times.Stat("myfile") if err != nil { log.Fatal(err.Error()) } log.Println(t.AccessTime()) log.Println(t.ModTime()) if t.HasChangeTime() { log.Println(t.ChangeTime()) } if t.HasBirthTime() { log.Println(t.BirthTime()) } } ``` Supported Times ------------ | | windows | linux | solaris | dragonfly | nacl | freebsd | darwin | netbsd | openbsd | plan9 | js | aix | |:-----:|:-------:|:-----:|:-------:|:---------:|:------:|:-------:|:----:|:------:|:-------:|:-----:|:-----:|:-----:| | atime | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | mtime | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | ctime | ✓* | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | ✓ | ✓ | | btime | ✓ | ✓* | | | | ✓ | ✓| ✓ | | | * Linux btime requires kernel 4.11 and filesystem support, so HasBirthTime = false. Use Timespec.HasBirthTime() to check if file has birth time. Get(FileInfo) never returns btime. * Windows XP does not have ChangeTime so HasChangeTime = false, however Vista onward does have ChangeTime so Timespec.HasChangeTime() will only return false on those platforms when the syscall used to obtain them fails. * Also note, Get(FileInfo) will now only return values available in FileInfo.Sys(), this means Stat() is required to get ChangeTime on Windows Installation ------------ ```sh go get -u github.com/djherbis/times ``` golang-github-djherbis-times-1.6.0/bench_test.go000066400000000000000000000017221466146253100216330ustar00rootroot00000000000000package times import ( "os" "testing" ) func BenchmarkGet(t *testing.B) { fileTest(t, func(f *os.File) { fi, err := os.Stat(f.Name()) if err != nil { t.Error(err) } for i := 0; i < t.N; i++ { Get(fi) } }) t.ReportAllocs() } func BenchmarkStatFile(t *testing.B) { fileTest(t, func(f *os.File) { for i := 0; i < t.N; i++ { StatFile(f) } }) t.ReportAllocs() } func BenchmarkStat(t *testing.B) { fileTest(t, func(f *os.File) { for i := 0; i < t.N; i++ { Stat(f.Name()) } }) t.ReportAllocs() } func BenchmarkLstat(t *testing.B) { fileTest(t, func(f *os.File) { for i := 0; i < t.N; i++ { Lstat(f.Name()) } }) t.ReportAllocs() } func BenchmarkOsStat(t *testing.B) { fileTest(t, func(f *os.File) { for i := 0; i < t.N; i++ { os.Stat(f.Name()) } }) t.ReportAllocs() } func BenchmarkOsLstat(t *testing.B) { fileTest(t, func(f *os.File) { for i := 0; i < t.N; i++ { os.Lstat(f.Name()) } }) t.ReportAllocs() } golang-github-djherbis-times-1.6.0/ctime_windows.go000066400000000000000000000065241466146253100223750ustar00rootroot00000000000000package times import ( "os" "syscall" "time" "unsafe" ) // Stat returns the Timespec for the given filename. func Stat(name string) (Timespec, error) { ts, err := platformSpecficStat(name) if err == nil { return ts, err } return stat(name, os.Stat) } // Lstat returns the Timespec for the given filename, and does not follow Symlinks. func Lstat(name string) (Timespec, error) { ts, err := platformSpecficLstat(name) if err == nil { return ts, err } return stat(name, os.Lstat) } type timespecEx struct { atime mtime ctime btime } // StatFile finds a Windows Timespec with ChangeTime. func StatFile(file *os.File) (Timespec, error) { return statFile(syscall.Handle(file.Fd())) } func statFile(h syscall.Handle) (Timespec, error) { var fileInfo fileBasicInfo if err := getFileInformationByHandleEx(h, &fileInfo); err != nil { return nil, err } var t timespecEx t.atime.v = time.Unix(0, fileInfo.LastAccessTime.Nanoseconds()) t.mtime.v = time.Unix(0, fileInfo.LastWriteTime.Nanoseconds()) t.ctime.v = time.Unix(0, fileInfo.ChangeTime.Nanoseconds()) t.btime.v = time.Unix(0, fileInfo.CreationTime.Nanoseconds()) return t, nil } func platformSpecficLstat(name string) (Timespec, error) { if findProcErr != nil { return nil, findProcErr } isSym, err := isSymlink(name) if err != nil { return nil, err } var attrs = uint32(syscall.FILE_FLAG_BACKUP_SEMANTICS) if isSym { attrs |= syscall.FILE_FLAG_OPEN_REPARSE_POINT } return openHandleAndStat(name, attrs) } func isSymlink(name string) (bool, error) { fi, err := os.Lstat(name) if err != nil { return false, err } return fi.Mode()&os.ModeSymlink != 0, nil } func platformSpecficStat(name string) (Timespec, error) { if findProcErr != nil { return nil, findProcErr } return openHandleAndStat(name, syscall.FILE_FLAG_BACKUP_SEMANTICS) } func openHandleAndStat(name string, attrs uint32) (Timespec, error) { pathp, e := syscall.UTF16PtrFromString(name) if e != nil { return nil, e } h, e := syscall.CreateFile(pathp, syscall.FILE_WRITE_ATTRIBUTES, syscall.FILE_SHARE_WRITE, nil, syscall.OPEN_EXISTING, attrs, 0) if e != nil { return nil, e } defer syscall.Close(h) return statFile(h) } var ( findProcErr error procGetFileInformationByHandleEx *syscall.Proc ) func init() { var modkernel32 *syscall.DLL if modkernel32, findProcErr = syscall.LoadDLL("kernel32.dll"); findProcErr == nil { procGetFileInformationByHandleEx, findProcErr = modkernel32.FindProc("GetFileInformationByHandleEx") } } // fileBasicInfo holds the C++ data for FileTimes. // // https://msdn.microsoft.com/en-us/library/windows/desktop/aa364217(v=vs.85).aspx type fileBasicInfo struct { CreationTime syscall.Filetime LastAccessTime syscall.Filetime LastWriteTime syscall.Filetime ChangeTime syscall.Filetime FileAttributes uint32 _ uint32 // padding } type fileInformationClass int const ( fileBasicInfoClass fileInformationClass = iota ) func getFileInformationByHandleEx(handle syscall.Handle, data *fileBasicInfo) (err error) { if findProcErr != nil { return findProcErr } r1, _, e1 := syscall.Syscall6(procGetFileInformationByHandleEx.Addr(), 4, uintptr(handle), uintptr(fileBasicInfoClass), uintptr(unsafe.Pointer(data)), unsafe.Sizeof(*data), 0, 0) if r1 == 0 { err = syscall.EINVAL if e1 != 0 { err = error(e1) } } return } golang-github-djherbis-times-1.6.0/example/000077500000000000000000000000001466146253100206175ustar00rootroot00000000000000golang-github-djherbis-times-1.6.0/example/main.go000066400000000000000000000034211466146253100220720ustar00rootroot00000000000000package main import ( "fmt" "io/ioutil" "log" "os" "path/filepath" "time" "github.com/djherbis/times" ) func main() { switch len(os.Args) { case 1: tempFile() fmt.Println() tempDir() default: printTimes(os.Args[1]) } } func tempDir() { name, err := ioutil.TempDir("", "") if err != nil { log.Fatal(err) } defer os.Remove(name) fmt.Println("# DIR: " + name) symname := filepath.Join(filepath.Dir(name), "sym-"+filepath.Base(name)) if err := os.Symlink(name, symname); err != nil { log.Fatal(err) } defer os.Remove(symname) newAtime := time.Now().Add(-10 * time.Second) newMtime := time.Now().Add(10 * time.Second) if err := os.Chtimes(name, newAtime, newMtime); err != nil { log.Fatal(err) } printTimes(symname) } func tempFile() { f, err := ioutil.TempFile("", "") if err != nil { log.Fatal(err) } defer os.Remove(f.Name()) defer f.Close() fmt.Println("# FILE: " + f.Name()) symname := filepath.Join(filepath.Dir(f.Name()), "sym-"+filepath.Base(f.Name())) if err := os.Symlink(f.Name(), symname); err != nil { log.Fatal(err) } defer os.Remove(symname) newAtime := time.Now().Add(-10 * time.Second) newMtime := time.Now().Add(10 * time.Second) if err := os.Chtimes(f.Name(), newAtime, newMtime); err != nil { log.Fatal(err) } printTimes(symname) } func printTimes(name string) { fmt.Println("## Stat:", name) printTimespec(times.Stat(name)) fmt.Println("\n## Lstat:", name) printTimespec(times.Lstat(name)) } func printTimespec(ts times.Timespec, err error) { if err != nil { log.Fatal(err) } fmt.Println("AccessTime:", ts.AccessTime()) fmt.Println("ModTime:", ts.ModTime()) if ts.HasChangeTime() { fmt.Println("ChangeTime:", ts.ChangeTime()) } if ts.HasBirthTime() { fmt.Println("BirthTime:", ts.BirthTime()) } } golang-github-djherbis-times-1.6.0/go.mod000066400000000000000000000001471466146253100202740ustar00rootroot00000000000000module github.com/djherbis/times go 1.16 require golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c golang-github-djherbis-times-1.6.0/go.sum000066400000000000000000000003171466146253100203200ustar00rootroot00000000000000golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c h1:aFV+BgZ4svzjfabn8ERpuB4JI4N6/rdy1iusx77G3oU= golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang-github-djherbis-times-1.6.0/js.cover.dockerfile000066400000000000000000000004611466146253100227470ustar00rootroot00000000000000FROM golang:1.17 RUN curl -sL https://deb.nodesource.com/setup_17.x | bash RUN apt-get install --yes nodejs WORKDIR /go/src/github.com/djherbis/times COPY . . RUN GO111MODULE=auto GOOS=js GOARCH=wasm go test -covermode=count -coverprofile=profile.cov -exec="$(go env GOROOT)/misc/wasm/go_js_wasm_exec" golang-github-djherbis-times-1.6.0/js.cover.sh000066400000000000000000000004201466146253100212450ustar00rootroot00000000000000#!/bin/bash set -e docker build -f js.cover.dockerfile -t js.cover.djherbis.times . docker create --name js.cover.djherbis.times js.cover.djherbis.times docker cp js.cover.djherbis.times:/go/src/github.com/djherbis/times/profile.cov . docker rm -v js.cover.djherbis.timesgolang-github-djherbis-times-1.6.0/linux.cover.dockerfile000066400000000000000000000002161466146253100234700ustar00rootroot00000000000000FROM golang:1.17 WORKDIR /go/src/github.com/djherbis/times COPY . . RUN GO111MODULE=auto go test -covermode=count -coverprofile=profile.cov golang-github-djherbis-times-1.6.0/linux.cover.sh000066400000000000000000000004421466146253100217740ustar00rootroot00000000000000#!/bin/bash set -e docker build -f linux.cover.dockerfile -t linux.cover.djherbis.times . docker create --name linux.cover.djherbis.times linux.cover.djherbis.times docker cp linux.cover.djherbis.times:/go/src/github.com/djherbis/times/profile.cov . docker rm -v linux.cover.djherbis.timesgolang-github-djherbis-times-1.6.0/times.go000066400000000000000000000027661466146253100206470ustar00rootroot00000000000000// Package times provides a platform-independent way to get atime, mtime, ctime and btime for files. package times import ( "os" "time" ) // Get returns the Timespec for the given FileInfo func Get(fi os.FileInfo) Timespec { return getTimespec(fi) } type statFunc func(string) (os.FileInfo, error) func stat(name string, sf statFunc) (Timespec, error) { fi, err := sf(name) if err != nil { return nil, err } return getTimespec(fi), nil } // Timespec provides access to file times. // ChangeTime() panics unless HasChangeTime() is true and // BirthTime() panics unless HasBirthTime() is true. type Timespec interface { ModTime() time.Time AccessTime() time.Time ChangeTime() time.Time BirthTime() time.Time HasChangeTime() bool HasBirthTime() bool } type atime struct { v time.Time } func (a atime) AccessTime() time.Time { return a.v } type ctime struct { v time.Time } func (ctime) HasChangeTime() bool { return true } func (c ctime) ChangeTime() time.Time { return c.v } type mtime struct { v time.Time } func (m mtime) ModTime() time.Time { return m.v } type btime struct { v time.Time } func (btime) HasBirthTime() bool { return true } func (b btime) BirthTime() time.Time { return b.v } type noctime struct{} func (noctime) HasChangeTime() bool { return false } func (noctime) ChangeTime() time.Time { panic("ctime not available") } type nobtime struct{} func (nobtime) HasBirthTime() bool { return false } func (nobtime) BirthTime() time.Time { panic("birthtime not available") } golang-github-djherbis-times-1.6.0/times_aix.go000066400000000000000000000014121466146253100214730ustar00rootroot00000000000000// Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // https://golang.org/src/os/stat_aix.go package times import ( "os" "syscall" "time" ) // HasChangeTime and HasBirthTime are true if and only if // the target OS supports them. const ( HasChangeTime = true HasBirthTime = false ) type timespec struct { atime mtime ctime nobtime } func timespecToTime(ts syscall.StTimespec_t) time.Time { return time.Unix(int64(ts.Sec), int64(ts.Nsec)) } func getTimespec(fi os.FileInfo) (t timespec) { stat := fi.Sys().(*syscall.Stat_t) t.atime.v = timespecToTime(stat.Atim) t.mtime.v = timespecToTime(stat.Mtim) t.ctime.v = timespecToTime(stat.Ctim) return t } golang-github-djherbis-times-1.6.0/times_darwin.go000066400000000000000000000015041466146253100222000ustar00rootroot00000000000000// Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // http://golang.org/src/os/stat_darwin.go package times import ( "os" "syscall" "time" ) // HasChangeTime and HasBirthTime are true if and only if // the target OS supports them. const ( HasChangeTime = true HasBirthTime = true ) type timespec struct { atime mtime ctime btime } func timespecToTime(ts syscall.Timespec) time.Time { return time.Unix(int64(ts.Sec), int64(ts.Nsec)) } func getTimespec(fi os.FileInfo) (t timespec) { stat := fi.Sys().(*syscall.Stat_t) t.atime.v = timespecToTime(stat.Atimespec) t.mtime.v = timespecToTime(stat.Mtimespec) t.ctime.v = timespecToTime(stat.Ctimespec) t.btime.v = timespecToTime(stat.Birthtimespec) return t } golang-github-djherbis-times-1.6.0/times_dragonfly.go000066400000000000000000000014131466146253100227000ustar00rootroot00000000000000// Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // http://golang.org/src/os/stat_dragonfly.go package times import ( "os" "syscall" "time" ) // HasChangeTime and HasBirthTime are true if and only if // the target OS supports them. const ( HasChangeTime = true HasBirthTime = false ) type timespec struct { atime mtime ctime nobtime } func timespecToTime(ts syscall.Timespec) time.Time { return time.Unix(int64(ts.Sec), int64(ts.Nsec)) } func getTimespec(fi os.FileInfo) (t timespec) { stat := fi.Sys().(*syscall.Stat_t) t.atime.v = timespecToTime(stat.Atim) t.mtime.v = timespecToTime(stat.Mtim) t.ctime.v = timespecToTime(stat.Ctim) return t } golang-github-djherbis-times-1.6.0/times_freebsd.go000066400000000000000000000015051466146253100223270ustar00rootroot00000000000000// Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // http://golang.org/src/os/stat_freebsd.go package times import ( "os" "syscall" "time" ) // HasChangeTime and HasBirthTime are true if and only if // the target OS supports them. const ( HasChangeTime = true HasBirthTime = true ) type timespec struct { atime mtime ctime btime } func timespecToTime(ts syscall.Timespec) time.Time { return time.Unix(int64(ts.Sec), int64(ts.Nsec)) } func getTimespec(fi os.FileInfo) (t timespec) { stat := fi.Sys().(*syscall.Stat_t) t.atime.v = timespecToTime(stat.Atimespec) t.mtime.v = timespecToTime(stat.Mtimespec) t.ctime.v = timespecToTime(stat.Ctimespec) t.btime.v = timespecToTime(stat.Birthtimespec) return t } golang-github-djherbis-times-1.6.0/times_js.go000066400000000000000000000014671466146253100213400ustar00rootroot00000000000000// Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // https://golang.org/src/os/stat_nacljs.go // +build js,wasm package times import ( "os" "syscall" "time" ) // HasChangeTime and HasBirthTime are true if and only if // the target OS supports them. const ( HasChangeTime = true HasBirthTime = false ) type timespec struct { atime mtime ctime nobtime } func timespecToTime(sec, nsec int64) time.Time { return time.Unix(sec, nsec) } func getTimespec(fi os.FileInfo) (t timespec) { stat := fi.Sys().(*syscall.Stat_t) t.atime.v = timespecToTime(stat.Atime, stat.AtimeNsec) t.mtime.v = timespecToTime(stat.Mtime, stat.MtimeNsec) t.ctime.v = timespecToTime(stat.Ctime, stat.CtimeNsec) return t } golang-github-djherbis-times-1.6.0/times_linux.go000066400000000000000000000101201466146253100220450ustar00rootroot00000000000000// Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // http://golang.org/src/os/stat_linux.go package times import ( "errors" "os" "sync/atomic" "syscall" "time" "golang.org/x/sys/unix" ) // HasChangeTime and HasBirthTime are true if and only if // the target OS supports them. const ( HasChangeTime = true HasBirthTime = false ) type timespec struct { atime mtime ctime nobtime } type timespecBtime struct { atime mtime ctime btime } var ( supportsStatx int32 = 1 statxFunc = unix.Statx ) func isStatXSupported() bool { return atomic.LoadInt32(&supportsStatx) == 1 } func isStatXUnsupported(err error) bool { // linux 4.10 and earlier does not support Statx syscall if err != nil && errors.Is(err, unix.ENOSYS) { atomic.StoreInt32(&supportsStatx, 0) return true } return false } // Stat returns the Timespec for the given filename. func Stat(name string) (Timespec, error) { if isStatXSupported() { ts, err := statX(name) if err == nil { return ts, nil } if !isStatXUnsupported(err) { return nil, err } // Fallback. } return stat(name, os.Stat) } func statX(name string) (Timespec, error) { // https://man7.org/linux/man-pages/man2/statx.2.html var statx unix.Statx_t err := statxFunc(unix.AT_FDCWD, name, unix.AT_STATX_SYNC_AS_STAT, unix.STATX_ATIME|unix.STATX_MTIME|unix.STATX_CTIME|unix.STATX_BTIME, &statx) if err != nil { return nil, err } return extractTimes(&statx), nil } // Lstat returns the Timespec for the given filename, and does not follow Symlinks. func Lstat(name string) (Timespec, error) { if isStatXSupported() { ts, err := lstatx(name) if err == nil { return ts, nil } if !isStatXUnsupported(err) { return nil, err } // Fallback. } return stat(name, os.Lstat) } func lstatx(name string) (Timespec, error) { // https://man7.org/linux/man-pages/man2/statx.2.html var statX unix.Statx_t err := statxFunc(unix.AT_FDCWD, name, unix.AT_STATX_SYNC_AS_STAT|unix.AT_SYMLINK_NOFOLLOW, unix.STATX_ATIME|unix.STATX_MTIME|unix.STATX_CTIME|unix.STATX_BTIME, &statX) if err != nil { return nil, err } return extractTimes(&statX), nil } func statXFile(file *os.File) (Timespec, error) { sc, err := file.SyscallConn() if err != nil { return nil, err } var statx unix.Statx_t var statxErr error err = sc.Control(func(fd uintptr) { // https://man7.org/linux/man-pages/man2/statx.2.html statxErr = statxFunc(int(fd), "", unix.AT_EMPTY_PATH|unix.AT_STATX_SYNC_AS_STAT, unix.STATX_ATIME|unix.STATX_MTIME|unix.STATX_CTIME|unix.STATX_BTIME, &statx) }) if err != nil { return nil, err } if statxErr != nil { return nil, statxErr } return extractTimes(&statx), nil } // StatFile returns the Timespec for the given *os.File. func StatFile(file *os.File) (Timespec, error) { if isStatXSupported() { ts, err := statXFile(file) if err == nil { return ts, nil } if !isStatXUnsupported(err) { return nil, err } // Fallback. } return statFile(file) } func statFile(file *os.File) (Timespec, error) { fi, err := file.Stat() if err != nil { return nil, err } return getTimespec(fi), nil } func statxTimestampToTime(ts unix.StatxTimestamp) time.Time { return time.Unix(ts.Sec, int64(ts.Nsec)) } func extractTimes(statx *unix.Statx_t) Timespec { if statx.Mask&unix.STATX_BTIME == unix.STATX_BTIME { var t timespecBtime t.atime.v = statxTimestampToTime(statx.Atime) t.mtime.v = statxTimestampToTime(statx.Mtime) t.ctime.v = statxTimestampToTime(statx.Ctime) t.btime.v = statxTimestampToTime(statx.Btime) return t } var t timespec t.atime.v = statxTimestampToTime(statx.Atime) t.mtime.v = statxTimestampToTime(statx.Mtime) t.ctime.v = statxTimestampToTime(statx.Ctime) return t } func timespecToTime(ts syscall.Timespec) time.Time { return time.Unix(int64(ts.Sec), int64(ts.Nsec)) } func getTimespec(fi os.FileInfo) (t timespec) { stat := fi.Sys().(*syscall.Stat_t) t.atime.v = timespecToTime(stat.Atim) t.mtime.v = timespecToTime(stat.Mtim) t.ctime.v = timespecToTime(stat.Ctim) return t } golang-github-djherbis-times-1.6.0/times_linux_test.go000066400000000000000000000056361466146253100231240ustar00rootroot00000000000000package times import ( "errors" "os" "sync/atomic" "testing" "time" "golang.org/x/sys/unix" ) func timeToStatx(t time.Time) unix.StatxTimestamp { nsec := time.Duration(t.UnixNano()) * time.Nanosecond nsec -= time.Duration(t.Unix()) * time.Second return unix.StatxTimestamp{Sec: t.Unix(), Nsec: uint32(nsec)} } func statxT(t time.Time, hasBtime bool) *unix.Statx_t { var statx unix.Statx_t statxt := timeToStatx(t) statx.Atime = statxt statx.Mtime = statxt statx.Ctime = statxt if hasBtime { statx.Mask = unix.STATX_BTIME statx.Btime = statxt } return &statx } type statxFuncTyp func(dirfd int, path string, flags int, mask int, stat *unix.Statx_t) (err error) func unsupportedStatx(dirfd int, path string, flags int, mask int, stat *unix.Statx_t) (err error) { return unix.ENOSYS } var errBadStatx = errors.New("bad") func badStatx(dirfd int, path string, flags int, mask int, stat *unix.Statx_t) (err error) { return errBadStatx } func fakeSupportedStatx(ts *unix.Statx_t) statxFuncTyp { return func(dirfd int, path string, flags int, mask int, stat *unix.Statx_t) (err error) { *stat = *ts return nil } } func setStatx(fn statxFuncTyp) func() { atomic.StoreInt32(&supportsStatx, 1) restoreStatx := statxFunc statxFunc = fn return func() { statxFunc = restoreStatx } } func TestStatx(t *testing.T) { tests := []struct { name string statx statxFuncTyp wantErr error }{ {name: "unsupported", statx: unsupportedStatx}, {name: "fake supported with btime", statx: fakeSupportedStatx(statxT(time.Now(), true))}, {name: "fake supported without btime", statx: fakeSupportedStatx(statxT(time.Now(), false))}, {name: "bad stat", statx: badStatx, wantErr: errBadStatx}, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { t.Run("stat", func(t *testing.T) { restore := setStatx(test.statx) defer restore() fileAndDirTest(t, func(name string) { ts, err := Stat(name) if err != nil { if err == test.wantErr { return } t.Fatal(err.Error()) } timespecTest(ts, newInterval(time.Now(), time.Second), t) }) }) t.Run("statFile", func(t *testing.T) { restore := setStatx(test.statx) defer restore() fileAndDirTest(t, func(name string) { fi, err := os.Open(name) if err != nil { t.Fatal(err.Error()) } defer fi.Close() ts, err := StatFile(fi) if err != nil { if err == test.wantErr { return } t.Fatal(err.Error()) } timespecTest(ts, newInterval(time.Now(), time.Second), t) }) }) t.Run("lstat", func(t *testing.T) { restore := setStatx(test.statx) defer restore() fileAndDirTest(t, func(name string) { ts, err := Lstat(name) if err != nil { if err == test.wantErr { return } t.Fatal(err.Error()) } timespecTest(ts, newInterval(time.Now(), time.Second), t) }) }) }) } } golang-github-djherbis-times-1.6.0/times_nacl.go000066400000000000000000000014441466146253100216340ustar00rootroot00000000000000// Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // https://golang.org/src/os/stat_nacljs.go package times import ( "os" "syscall" "time" ) // HasChangeTime and HasBirthTime are true if and only if // the target OS supports them. const ( HasChangeTime = true HasBirthTime = false ) type timespec struct { atime mtime ctime nobtime } func timespecToTime(sec, nsec int64) time.Time { return time.Unix(sec, nsec) } func getTimespec(fi os.FileInfo) (t timespec) { stat := fi.Sys().(*syscall.Stat_t) t.atime.v = timespecToTime(stat.Atime, stat.AtimeNsec) t.mtime.v = timespecToTime(stat.Mtime, stat.MtimeNsec) t.ctime.v = timespecToTime(stat.Ctime, stat.CtimeNsec) return t } golang-github-djherbis-times-1.6.0/times_netbsd.go000066400000000000000000000015041466146253100221730ustar00rootroot00000000000000// Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // http://golang.org/src/os/stat_netbsd.go package times import ( "os" "syscall" "time" ) // HasChangeTime and HasBirthTime are true if and only if // the target OS supports them. const ( HasChangeTime = true HasBirthTime = true ) type timespec struct { atime mtime ctime btime } func timespecToTime(ts syscall.Timespec) time.Time { return time.Unix(int64(ts.Sec), int64(ts.Nsec)) } func getTimespec(fi os.FileInfo) (t timespec) { stat := fi.Sys().(*syscall.Stat_t) t.atime.v = timespecToTime(stat.Atimespec) t.mtime.v = timespecToTime(stat.Mtimespec) t.ctime.v = timespecToTime(stat.Ctimespec) t.btime.v = timespecToTime(stat.Birthtimespec) return t } golang-github-djherbis-times-1.6.0/times_openbsd.go000066400000000000000000000014111466146253100223430ustar00rootroot00000000000000// Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // http://golang.org/src/os/stat_openbsd.go package times import ( "os" "syscall" "time" ) // HasChangeTime and HasBirthTime are true if and only if // the target OS supports them. const ( HasChangeTime = true HasBirthTime = false ) type timespec struct { atime mtime ctime nobtime } func timespecToTime(ts syscall.Timespec) time.Time { return time.Unix(int64(ts.Sec), int64(ts.Nsec)) } func getTimespec(fi os.FileInfo) (t timespec) { stat := fi.Sys().(*syscall.Stat_t) t.atime.v = timespecToTime(stat.Atim) t.mtime.v = timespecToTime(stat.Mtim) t.ctime.v = timespecToTime(stat.Ctim) return t } golang-github-djherbis-times-1.6.0/times_plan9.go000066400000000000000000000012031466146253100217330ustar00rootroot00000000000000// Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // http://golang.org/src/os/stat_plan9.go package times import ( "os" "syscall" "time" ) // HasChangeTime and HasBirthTime are true if and only if // the target OS supports them. const ( HasChangeTime = false HasBirthTime = false ) type timespec struct { atime mtime noctime nobtime } func getTimespec(fi os.FileInfo) (t timespec) { stat := fi.Sys().(*syscall.Dir) t.atime.v = time.Unix(int64(stat.Atime), 0) t.mtime.v = time.Unix(int64(stat.Mtime), 0) return t } golang-github-djherbis-times-1.6.0/times_solaris.go000066400000000000000000000014111466146253100223650ustar00rootroot00000000000000// Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // http://golang.org/src/os/stat_solaris.go package times import ( "os" "syscall" "time" ) // HasChangeTime and HasBirthTime are true if and only if // the target OS supports them. const ( HasChangeTime = true HasBirthTime = false ) type timespec struct { atime mtime ctime nobtime } func timespecToTime(ts syscall.Timespec) time.Time { return time.Unix(int64(ts.Sec), int64(ts.Nsec)) } func getTimespec(fi os.FileInfo) (t timespec) { stat := fi.Sys().(*syscall.Stat_t) t.atime.v = timespecToTime(stat.Atim) t.mtime.v = timespecToTime(stat.Mtim) t.ctime.v = timespecToTime(stat.Ctim) return t } golang-github-djherbis-times-1.6.0/times_test.go000066400000000000000000000053231466146253100216760ustar00rootroot00000000000000package times import ( "os" "path/filepath" "testing" "time" ) func TestStat(t *testing.T) { fileAndDirTest(t, func(name string) { ts, err := Stat(name) if err != nil { t.Error(err.Error()) } timespecTest(ts, newInterval(time.Now(), time.Second), t) }) } func TestGet(t *testing.T) { fileAndDirTest(t, func(name string) { fi, err := os.Stat(name) if err != nil { t.Error(err.Error()) } timespecTest(Get(fi), newInterval(time.Now(), time.Second), t) }) } func TestStatFile(t *testing.T) { fileTest(t, func(f *os.File) { ts, err := StatFile(f) if err != nil { t.Error(err.Error()) } timespecTest(ts, newInterval(time.Now(), time.Second), t) }) } func TestStatFileErr(t *testing.T) { fileTest(t, func(f *os.File) { f.Close() _, err := StatFile(f) if err == nil { t.Error("got nil err, but err was expected!") } }) } type tsFunc func(string) (Timespec, error) var offsetTime = -10 * time.Second func TestStatSymlink(t *testing.T) { testStatSymlink(Stat, time.Now().Add(offsetTime), t) } func TestLstatSymlink(t *testing.T) { testStatSymlink(Lstat, time.Now(), t) } func testStatSymlink(sf tsFunc, expectTime time.Time, t *testing.T) { fileAndDirTest(t, func(name string) { start := time.Now() symname := filepath.Join(filepath.Dir(name), "sym-"+filepath.Base(name)) if err := os.Symlink(name, symname); err != nil { t.Fatal(err.Error()) } defer os.Remove(symname) // modify the realFileTime so symlink and real file see diff values. realFileTime := start.Add(offsetTime) if err := os.Chtimes(name, realFileTime, realFileTime); err != nil { t.Fatal(err.Error()) } ts, err := sf(symname) if err != nil { t.Fatal(err.Error()) } timespecTest(ts, newInterval(expectTime, time.Second), t, Timespec.AccessTime, Timespec.ModTime) }) } func TestStatErr(t *testing.T) { _, err := Stat("badfile?") if err == nil { t.Error("expected an error") } } func TestLstatErr(t *testing.T) { _, err := Lstat("badfile?") if err == nil { t.Error("expected an error") } } func TestCheat(t *testing.T) { // not all times are available for all platforms // this allows us to get 100% test coverage for platforms which do not have // ChangeTime/BirthTime var c ctime if c.HasChangeTime() { c.ChangeTime() } var b btime if b.HasBirthTime() { b.BirthTime() } var paniced = false var nc noctime func() { if !nc.HasChangeTime() { defer func() { recover() paniced = true }() } nc.ChangeTime() }() if !paniced { t.Error("expected panic") } paniced = false var nb nobtime func() { if !nb.HasBirthTime() { defer func() { recover() paniced = true }() } nb.BirthTime() }() if !paniced { t.Error("expected panic") } } golang-github-djherbis-times-1.6.0/times_wasip1.go000066400000000000000000000015141466146253100221210ustar00rootroot00000000000000// Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // https://github.com/golang/go/blob/master/src/os/stat_wasip1.go //go:build wasip1 // +build wasip1 package times import ( "os" "syscall" "time" ) // HasChangeTime and HasBirthTime are true if and only if // the target OS supports them. const ( HasChangeTime = true HasBirthTime = false ) type timespec struct { atime mtime ctime nobtime } func timespecToTime(sec, nsec int64) time.Time { return time.Unix(sec, nsec) } func getTimespec(fi os.FileInfo) (t timespec) { stat := fi.Sys().(*syscall.Stat_t) t.atime.v = timespecToTime(int64(stat.Atime), 0) t.mtime.v = timespecToTime(int64(stat.Mtime), 0) t.ctime.v = timespecToTime(int64(stat.Ctime), 0) return t } golang-github-djherbis-times-1.6.0/times_windows.go000066400000000000000000000013731466146253100224120ustar00rootroot00000000000000// Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // http://golang.org/src/os/stat_windows.go package times import ( "os" "syscall" "time" ) // HasChangeTime and HasBirthTime are true if and only if // the target OS supports them. const ( HasChangeTime = false HasBirthTime = true ) type timespec struct { atime mtime noctime btime } func getTimespec(fi os.FileInfo) Timespec { var t timespec stat := fi.Sys().(*syscall.Win32FileAttributeData) t.atime.v = time.Unix(0, stat.LastAccessTime.Nanoseconds()) t.mtime.v = time.Unix(0, stat.LastWriteTime.Nanoseconds()) t.btime.v = time.Unix(0, stat.CreationTime.Nanoseconds()) return t } golang-github-djherbis-times-1.6.0/times_windows_test.go000066400000000000000000000020611466146253100234440ustar00rootroot00000000000000package times import ( "errors" "os" "syscall" "testing" "time" ) func TestStatFileProcErr(t *testing.T) { fileTest(t, func(f *os.File) { findProcErr = errors.New("fake error") defer func() { findProcErr = nil }() _, err := StatFile(f) if err == nil { t.Error("got nil err, but err was expected!") } }) } func TestStatBadNameErr(t *testing.T) { _, err := platformSpecficStat(string([]byte{0})) if err != syscall.EINVAL { t.Error(err) } } func TestStatProcErrFallback(t *testing.T) { fileAndDirTest(t, func(name string) { findProcErr = errors.New("fake error") defer func() { findProcErr = nil }() ts, err := Stat(name) if err != nil { t.Error(err.Error()) } timespecTest(ts, newInterval(time.Now(), time.Second), t) }) } func TestLstatProcErrFallback(t *testing.T) { fileAndDirTest(t, func(name string) { findProcErr = errors.New("fake error") defer func() { findProcErr = nil }() ts, err := Lstat(name) if err != nil { t.Error(err.Error()) } timespecTest(ts, newInterval(time.Now(), time.Second), t) }) } golang-github-djherbis-times-1.6.0/use_generic_stat.go000066400000000000000000000010351466146253100230350ustar00rootroot00000000000000// +build !windows,!linux package times import "os" // Stat returns the Timespec for the given filename. func Stat(name string) (Timespec, error) { return stat(name, os.Stat) } // Lstat returns the Timespec for the given filename, and does not follow Symlinks. func Lstat(name string) (Timespec, error) { return stat(name, os.Lstat) } // StatFile returns the Timespec for the given *os.File. func StatFile(file *os.File) (Timespec, error) { fi, err := file.Stat() if err != nil { return nil, err } return getTimespec(fi), nil } golang-github-djherbis-times-1.6.0/util_test.go000066400000000000000000000034161466146253100215330ustar00rootroot00000000000000package times import ( "io/ioutil" "os" "reflect" "runtime" "testing" "time" ) type timeRange struct { start time.Time end time.Time } func newInterval(t time.Time, dur time.Duration) timeRange { return timeRange{start: t.Add(-dur), end: t.Add(dur)} } func (t timeRange) Contains(findTime time.Time) bool { return !findTime.Before(t.start) && !findTime.After(t.end) } func fileAndDirTest(t testing.TB, testFunc func(name string)) { filenameTest(t, testFunc) dirTest(t, testFunc) } // creates a file and cleans it up after the test is run. func fileTest(t testing.TB, testFunc func(f *os.File)) { f, err := ioutil.TempFile("", "") if err != nil { t.Fatal(err) } defer os.Remove(f.Name()) defer f.Close() testFunc(f) } func filenameTest(t testing.TB, testFunc func(filename string)) { fileTest(t, func(f *os.File) { testFunc(f.Name()) }) } // creates a dir and cleans it up after the test is run. func dirTest(t testing.TB, testFunc func(dirname string)) { dirname, err := ioutil.TempDir("", "") if err != nil { t.Fatal(err) } defer os.Remove(dirname) testFunc(dirname) } type timeFetcher func(Timespec) time.Time func timespecTest(ts Timespec, r timeRange, t testing.TB, getTimes ...timeFetcher) { if len(getTimes) == 0 { getTimes = append(getTimes, Timespec.AccessTime, Timespec.ModTime) if ts.HasChangeTime() { getTimes = append(getTimes, Timespec.ChangeTime) } if ts.HasBirthTime() { getTimes = append(getTimes, Timespec.BirthTime) } } for _, getTime := range getTimes { if !r.Contains(getTime(ts)) { t.Errorf("expected %s=%s to be in range: \n[%s, %s]\n", GetFunctionName(getTime), getTime(ts), r.start, r.end) } } } func GetFunctionName(i interface{}) string { return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name() }