summaryrefslogtreecommitdiff
path: root/vendor/github.com/tetratelabs/wazero/internal/sysfs/rename_windows.go
blob: 5e81022391766697b717f683acab0220a0cb095e (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
52
53
54
55
package sysfs

import (
	"os"
	"syscall"

	"github.com/tetratelabs/wazero/experimental/sys"
)

func rename(from, to string) sys.Errno {
	if from == to {
		return 0
	}

	var fromIsDir, toIsDir bool
	if fromStat, errno := stat(from); errno != 0 {
		return errno // failed to stat from
	} else {
		fromIsDir = fromStat.Mode.IsDir()
	}
	if toStat, errno := stat(to); errno == sys.ENOENT {
		return syscallRename(from, to) // file or dir to not-exist is ok
	} else if errno != 0 {
		return errno // failed to stat to
	} else {
		toIsDir = toStat.Mode.IsDir()
	}

	// Now, handle known cases
	switch {
	case !fromIsDir && toIsDir: // file to dir
		return sys.EISDIR
	case !fromIsDir && !toIsDir: // file to file
		// Use os.Rename instead of syscall.Rename to overwrite a file.
		// This uses MoveFileEx instead of MoveFile (used by syscall.Rename).
		return sys.UnwrapOSError(os.Rename(from, to))
	case fromIsDir && !toIsDir: // dir to file
		return sys.ENOTDIR
	default: // dir to dir

		// We can't tell if a directory is empty or not, via stat information.
		// Reading the directory is expensive, as it can buffer large amounts
		// of data on fail. Instead, speculatively try to remove the directory.
		// This is only one syscall and won't buffer anything.
		if errno := rmdir(to); errno == 0 || errno == sys.ENOENT {
			return syscallRename(from, to)
		} else {
			return errno
		}
	}
}

func syscallRename(from string, to string) sys.Errno {
	return sys.UnwrapOSError(syscall.Rename(from, to))
}