Skip to content

Commit

Permalink
use platform specific stat if available, fallback to generic stat
Browse files Browse the repository at this point in the history
  • Loading branch information
djherbis committed Jan 12, 2016
1 parent b894535 commit f32f202
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
14 changes: 10 additions & 4 deletions ctime_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ type timespecEx struct {

// 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(syscall.Handle(file.Fd()), &fileInfo); err != nil {
if err := getFileInformationByHandleEx(h, &fileInfo); err != nil {
return nil, err
}

Expand All @@ -29,9 +33,10 @@ func StatFile(file *os.File) (Timespec, error) {
return t, nil
}

func Stat(name string) (Timespec, error) {
const hasPlatformSpecificStat = true

func platformSpecficStat(name string) (Timespec, error) {
if findProcErr != nil {
// fail fast, don't bother opening the file
return nil, findProcErr
}

Expand All @@ -40,7 +45,8 @@ func Stat(name string) (Timespec, error) {
return nil, err
}
defer f.Close()
return StatFile(f)

return statFile(syscall.Handle(f.Fd()))
}

var (
Expand Down
13 changes: 0 additions & 13 deletions stat_generic.go

This file was deleted.

15 changes: 15 additions & 0 deletions times.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ func Get(fi os.FileInfo) Timespec {
return getTimespec(fi)
}

// Stat returns the Timespec for the given filename.
func Stat(name string) (Timespec, error) {
if hasPlatformSpecificStat {
if ts, err := platformSpecficStat(name); err == nil {
return ts, nil
}
}

fi, err := os.Stat(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.
Expand Down
5 changes: 5 additions & 0 deletions use_generic_stat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// +build !windows

package times

const hasPlatformSpecificStat = false

0 comments on commit f32f202

Please sign in to comment.