Skip to content

Commit

Permalink
loopback: Replace buggy syscall.NsecToTimeval()
Browse files Browse the repository at this point in the history
syscall.NsecToTimespec does not work properly for times
before 1970, see golang/go#12777

This caused xfstests generic/258 to fail.

Fixes issue hanwen#58.
  • Loading branch information
rfjakob committed Sep 29, 2015
1 parent 8783b8e commit 01a124c
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions fuse/nodefs/files_linux.go
Expand Up @@ -20,20 +20,30 @@ func (f *loopbackFile) Allocate(off uint64, sz uint64, mode uint32) fuse.Status
const _UTIME_NOW = ((1 << 30) - 1)
const _UTIME_OMIT = ((1 << 30) - 2)

// timeToTimeval - Convert time.Time to syscall.Timeval
//
// Note: This does not use syscall.NsecToTimespec because
// that does not work properly for times before 1970,
// see https://github.com/golang/go/issues/12777
func timeToTimeval(t *time.Time) syscall.Timeval {
var tv syscall.Timeval
tv.Usec = int64(t.Nanosecond() / 1000)
tv.Sec = t.Unix()
return tv
}

func (f *loopbackFile) Utimens(a *time.Time, m *time.Time) fuse.Status {
tv := make([]syscall.Timeval, 2)
if a == nil {
tv[0].Usec = _UTIME_OMIT
} else {
n := a.UnixNano()
tv[0] = syscall.NsecToTimeval(n)
tv[0] = timeToTimeval(a)
}

if m == nil {
tv[1].Usec = _UTIME_OMIT
} else {
n := a.UnixNano()
tv[1] = syscall.NsecToTimeval(n)
tv[1] = timeToTimeval(m)
}

f.lock.Lock()
Expand Down

1 comment on commit 01a124c

@hanwen
Copy link

@hanwen hanwen commented on 01a124c Sep 29, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs a test.

Please sign in to comment.