This repository has been archived by the owner on Feb 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
flags.go
121 lines (99 loc) · 2.42 KB
/
flags.go
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package sandbox
import (
"sort"
"strings"
)
// OpenFlags is a bitset of flags that can be passed to the Open method of
// File and FileSystem values.
type OpenFlags int
func makeOpenFlags(sysFlags int) OpenFlags {
return OpenFlags(sysFlags)
}
func (of OpenFlags) String() string {
var names []string
switch of & (O_RDWR | O_WRONLY | O_RDONLY) {
case O_RDWR:
names = append(names, "O_RDWR")
case O_WRONLY:
names = append(names, "O_WRONLY")
}
names = appendFlagNames(names, of, []flagName[OpenFlags]{
{O_APPEND, "O_APPEND"},
{O_CREAT, "O_CREAT"},
{O_EXCL, "O_EXCL"},
{O_SYNC, "O_SYNC"},
{O_TRUNC, "O_TRUNC"},
{O_DIRECTORY, "O_DIRECTORY"},
{O_NOFOLLOW, "O_NOFOLLOW"},
{O_NONBLOCK, "O_NONBLOCK"},
})
if len(names) == 0 {
names = append(names, "O_RDONLY")
}
return joinFlagNames(names)
}
func (of OpenFlags) LookupFlags() LookupFlags {
if (of & O_NOFOLLOW) != 0 {
return AT_SYMLINK_NOFOLLOW
} else {
return 0
}
}
func (of OpenFlags) sysFlags() int {
return int(of)
}
// LookupFlags is a bitset of flags that can be passed to methods of File and
// FileSystem values to customize the behavior of file name lookups.
type LookupFlags int
func (lf LookupFlags) String() string {
return formatFlagNames(lf, []flagName[LookupFlags]{
{AT_SYMLINK_NOFOLLOW, "AT_SYMLINK_NOFOLLOW"},
})
}
func (lf LookupFlags) OpenFlags() OpenFlags {
if (lf & AT_SYMLINK_NOFOLLOW) != 0 {
return O_NOFOLLOW
} else {
return 0
}
}
func (lf LookupFlags) sysFlags() int {
return int(lf)
}
// RenameFlags is a bitset of flags passed to the File.Rename method to
// configure the behavior of the rename operation.
type RenameFlags int
func (rf RenameFlags) String() string {
return formatFlagNames(rf, []flagName[RenameFlags]{
{RENAME_EXCHANGE, "RENAME_EXCHANGE"},
{RENAME_NOREPLACE, "RENAME_NOREPLACE"},
})
}
func (rf RenameFlags) sysFlags() int {
return int(rf)
}
type flag interface {
~int
}
type flagName[F flag] struct {
flag F
name string
}
func formatFlagNames[F flag](flags F, flagNames []flagName[F]) string {
return joinFlagNames(appendFlagNames(nil, flags, flagNames))
}
func appendFlagNames[F flag](names []string, flags F, flagNames []flagName[F]) []string {
for _, f := range flagNames {
if (flags & f.flag) != 0 {
names = append(names, f.name)
}
}
return names
}
func joinFlagNames(names []string) string {
if len(names) == 0 {
return "0"
}
sort.Strings(names)
return strings.Join(names, "|")
}