aboutsummaryrefslogtreecommitdiff
path: root/internal/robustio/robustio_windows.go
blob: 616c32883d6026d7de9cb2929b30ff519e5101b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright 2019 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.

package robustio

import (
	"errors"
	"syscall"
	"time"
)

const errFileNotFound = syscall.ERROR_FILE_NOT_FOUND

// isEphemeralError returns true if err may be resolved by waiting.
func isEphemeralError(err error) bool {
	var errno syscall.Errno
	if errors.As(err, &errno) {
		switch errno {
		case syscall.ERROR_ACCESS_DENIED,
			syscall.ERROR_FILE_NOT_FOUND,
			ERROR_SHARING_VIOLATION:
			return true
		}
	}
	return false
}

// Note: it may be convenient to have this helper return fs.FileInfo, but
// implementing this is actually quite involved on Windows. Since we only
// currently use mtime, keep it simple.
func getFileID(filename string) (FileID, time.Time, error) {
	filename16, err := syscall.UTF16PtrFromString(filename)
	if err != nil {
		return FileID{}, time.Time{}, err
	}
	h, err := syscall.CreateFile(filename16, 0, 0, nil, syscall.OPEN_EXISTING, uint32(syscall.FILE_FLAG_BACKUP_SEMANTICS), 0)
	if err != nil {
		return FileID{}, time.Time{}, err
	}
	defer syscall.CloseHandle(h)
	var i syscall.ByHandleFileInformation
	if err := syscall.GetFileInformationByHandle(h, &i); err != nil {
		return FileID{}, time.Time{}, err
	}
	mtime := time.Unix(0, i.LastWriteTime.Nanoseconds())
	return FileID{
		device: uint64(i.VolumeSerialNumber),
		inode:  uint64(i.FileIndexHigh)<<32 | uint64(i.FileIndexLow),
	}, mtime, nil
}