...
1 package sysfs
2
3 import (
4 "os"
5 "syscall"
6
7 "github.com/tetratelabs/wazero/experimental/sys"
8 )
9
10 func rename(from, to string) sys.Errno {
11 if from == to {
12 return 0
13 }
14
15 var fromIsDir, toIsDir bool
16 if fromStat, errno := stat(from); errno != 0 {
17 return errno
18 } else {
19 fromIsDir = fromStat.Mode.IsDir()
20 }
21 if toStat, errno := stat(to); errno == sys.ENOENT {
22 return syscallRename(from, to)
23 } else if errno != 0 {
24 return errno
25 } else {
26 toIsDir = toStat.Mode.IsDir()
27 }
28
29
30 switch {
31 case !fromIsDir && toIsDir:
32 return sys.EISDIR
33 case !fromIsDir && !toIsDir:
34
35
36 return sys.UnwrapOSError(os.Rename(from, to))
37 case fromIsDir && !toIsDir:
38 return sys.ENOTDIR
39 default:
40
41
42
43
44
45 if errno := rmdir(to); errno == 0 || errno == sys.ENOENT {
46 return syscallRename(from, to)
47 } else {
48 return errno
49 }
50 }
51 }
52
53 func syscallRename(from string, to string) sys.Errno {
54 return sys.UnwrapOSError(syscall.Rename(from, to))
55 }
56
View as plain text