pax_global_header00006660000000000000000000000064140421174000014503gustar00rootroot0000000000000052 comment=a1edc5dc082a710e4a808e99c78989b8d04e3025 times-1.5.0/000077500000000000000000000000001404211740000126275ustar00rootroot00000000000000times-1.5.0/.travis.sh000066400000000000000000000011471404211740000145540ustar00rootroot00000000000000#!/bin/bash set -e script() { if [ "${TRAVIS_PULL_REQUEST}" == "false" ]; then COVERALLS_PARALLEL=true if [ ! -z "$JS" ]; then bash js.cover.sh else go test -covermode=count -coverprofile=profile.cov fi go get github.com/axw/gocov/gocov github.com/mattn/goveralls golang.org/x/tools/cmd/cover $HOME/gopath/bin/goveralls --coverprofile=profile.cov -service=travis-ci fi if [ -z "$JS" ]; then go get golang.org/x/lint/golint && golint ./... go vet go test -bench=.* -v ./... fi } "$@"times-1.5.0/.travis.yml000066400000000000000000000010311404211740000147330ustar00rootroot00000000000000language: go matrix: include: - os: linux go: tip - os: linux go: tip env: - JS=1 - os: osx go: tip - os: windows go: 1.x #Added power jobs - os: linux go: tip arch: ppc64le - os: linux go: tip arch: ppc64le env: - JS=1 script: bash .travis.sh script notifications: webhooks: https://coveralls.io/webhook email: on_success: never on_failure: change times-1.5.0/LICENSE000066400000000000000000000020641404211740000136360ustar00rootroot00000000000000The 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. times-1.5.0/README.md000066400000000000000000000044541404211740000141150ustar00rootroot00000000000000times ========== [![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) [![Build Status](https://travis-ci.org/djherbis/times.svg?branch=master)](https://travis-ci.org/djherbis/times) [![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 | ✓ | | | | | ✓ | ✓| ✓ | | | * 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 ``` times-1.5.0/bench_test.go000066400000000000000000000017221404211740000152760ustar00rootroot00000000000000package 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() } times-1.5.0/ctime_windows.go000066400000000000000000000065241404211740000160400ustar00rootroot00000000000000package 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 } times-1.5.0/example/000077500000000000000000000000001404211740000142625ustar00rootroot00000000000000times-1.5.0/example/main.go000066400000000000000000000034211404211740000155350ustar00rootroot00000000000000package 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()) } } times-1.5.0/go.mod000066400000000000000000000000521404211740000137320ustar00rootroot00000000000000module github.com/djherbis/times go 1.16 times-1.5.0/js.cover.dockerfile000066400000000000000000000004601404211740000164110ustar00rootroot00000000000000FROM golang:1.16 RUN curl -sL https://deb.nodesource.com/setup_8.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" times-1.5.0/js.cover.sh000066400000000000000000000004201404211740000147100ustar00rootroot00000000000000#!/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.timestimes-1.5.0/times.go000066400000000000000000000027661404211740000143120ustar00rootroot00000000000000// 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") } times-1.5.0/times_aix.go000066400000000000000000000014121404211740000151360ustar00rootroot00000000000000// 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 } times-1.5.0/times_darwin.go000066400000000000000000000015041404211740000156430ustar00rootroot00000000000000// 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 } times-1.5.0/times_dragonfly.go000066400000000000000000000014131404211740000163430ustar00rootroot00000000000000// 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 } times-1.5.0/times_freebsd.go000066400000000000000000000015051404211740000157720ustar00rootroot00000000000000// 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 } times-1.5.0/times_js.go000066400000000000000000000014671404211740000150030ustar00rootroot00000000000000// 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 } times-1.5.0/times_linux.go000066400000000000000000000014071404211740000155200ustar00rootroot00000000000000// 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 ( "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 } times-1.5.0/times_nacl.go000066400000000000000000000014441404211740000152770ustar00rootroot00000000000000// 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 } times-1.5.0/times_netbsd.go000066400000000000000000000015041404211740000156360ustar00rootroot00000000000000// 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 } times-1.5.0/times_openbsd.go000066400000000000000000000014111404211740000160060ustar00rootroot00000000000000// 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 } times-1.5.0/times_plan9.go000066400000000000000000000012031404211740000153760ustar00rootroot00000000000000// 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 } times-1.5.0/times_solaris.go000066400000000000000000000014111404211740000160300ustar00rootroot00000000000000// 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 } times-1.5.0/times_test.go000066400000000000000000000053231404211740000153410ustar00rootroot00000000000000package 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.Error(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.Error(err.Error()) } ts, err := sf(symname) if err != nil { t.Error(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") } } times-1.5.0/times_windows.go000066400000000000000000000013731404211740000160550ustar00rootroot00000000000000// 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 } times-1.5.0/times_windows_test.go000066400000000000000000000020611404211740000171070ustar00rootroot00000000000000package 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) }) } times-1.5.0/use_generic_stat.go000066400000000000000000000010261404211740000165000ustar00rootroot00000000000000// +build !windows 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 } times-1.5.0/util_test.go000066400000000000000000000034161404211740000151760ustar00rootroot00000000000000package 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() }