archive: windows: chtimes(): remove redundant conversion

It looks like this function was converting the time (`windows.NsecToTimespec()`),
only to convert it back (`windows.TimespecToNsec()`). This became clear when
moving the lines together:

```go
ctimespec := windows.NsecToTimespec(ctime.UnixNano())
c := windows.NsecToFiletime(windows.TimespecToNsec(ctimespec))
```

And looking at the Golang code, it looks like they're indeed the exact reverse:

```go
func TimespecToNsec(ts Timespec) int64 { return int64(ts.Sec)*1e9 + int64(ts.Nsec) }

func NsecToTimespec(nsec int64) (ts Timespec) {
    ts.Sec = nsec / 1e9
    ts.Nsec = nsec % 1e9
    return
}
```

While modifying this code, also renaming the `e` variable to a more common `err`.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-10-07 19:38:56 +02:00
parent 6fcfcf3a89
commit 0aefb52807
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C

View File

@ -25,18 +25,17 @@ import (
// chtimes will set the create time on a file using the given modtime. // chtimes will set the create time on a file using the given modtime.
// This requires calling SetFileTime and explicitly including the create time. // This requires calling SetFileTime and explicitly including the create time.
func chtimes(path string, atime, mtime time.Time) error { func chtimes(path string, atime, mtime time.Time) error {
ctimespec := windows.NsecToTimespec(mtime.UnixNano()) pathp, err := windows.UTF16PtrFromString(path)
pathp, e := windows.UTF16PtrFromString(path) if err != nil {
if e != nil { return err
return e
} }
h, e := windows.CreateFile(pathp, h, err := windows.CreateFile(pathp,
windows.FILE_WRITE_ATTRIBUTES, windows.FILE_SHARE_WRITE, nil, windows.FILE_WRITE_ATTRIBUTES, windows.FILE_SHARE_WRITE, nil,
windows.OPEN_EXISTING, windows.FILE_FLAG_BACKUP_SEMANTICS, 0) windows.OPEN_EXISTING, windows.FILE_FLAG_BACKUP_SEMANTICS, 0)
if e != nil { if err != nil {
return e return err
} }
defer windows.Close(h) defer windows.Close(h)
c := windows.NsecToFiletime(windows.TimespecToNsec(ctimespec)) c := windows.NsecToFiletime(mtime.UnixNano())
return windows.SetFileTime(h, &c, nil, nil) return windows.SetFileTime(h, &c, nil, nil)
} }