-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags.go
136 lines (113 loc) · 3.28 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package disko
import (
"os"
)
////////////////////////////////////////////////////////////////////////////////
// File attribute flags
const (
S_IXOTH = os.FileMode(1 << iota)
S_IWOTH = os.FileMode(1 << iota)
S_IROTH = os.FileMode(1 << iota)
S_IXGRP = os.FileMode(1 << iota)
S_IWGRP = os.FileMode(1 << iota)
S_IRGRP = os.FileMode(1 << iota)
S_IXUSR = os.FileMode(1 << iota)
S_IWUSR = os.FileMode(1 << iota)
S_IRUSR = os.FileMode(1 << iota)
S_ISVTX = os.FileMode(1 << iota)
S_ISGID = os.FileMode(1 << iota)
S_ISUID = os.FileMode(1 << iota)
S_IFIFO = os.FileMode(1 << iota)
S_IFCHR = os.FileMode(1 << iota)
S_IFDIR = os.FileMode(1 << iota)
S_IFREG = os.FileMode(1 << iota)
)
const S_IEXEC = S_IXUSR
const S_IWRITE = S_IWUSR
const S_IREAD = S_IRUSR
const S_IFBLK = 0x6000 // 0110 0000 0000 0000
const S_IFLNK = 0xa000 // 1010 0000 0000 0000
const S_IFSOCK = 0xc000 // 1100 0000 0000 0000
const S_IFMT = 0xf000
const S_IRWXO = S_IXOTH | S_IWOTH | S_IROTH
const S_IRWXG = S_IXGRP | S_IWGRP | S_IRGRP
const S_IRWXU = S_IXUSR | S_IWUSR | S_IRUSR
const DefaultFileModeFlags = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
const DefaultDirModeFlags = os.ModeDir | S_IRWXU | S_IXGRP | S_IRGRP | S_IXOTH | S_IROTH
////////////////////////////////////////////////////////////////////////////////
type IOFlags int
const O_RDONLY = IOFlags(0x00000000)
const O_WRONLY = IOFlags(0x00000001)
const O_RDWR = IOFlags(0x00000002)
const O_APPEND = IOFlags(0x00000008)
const O_CREATE = IOFlags(0x00000200)
const O_TRUNC = IOFlags(0x00000400)
const O_EXCL = IOFlags(0x00000800)
const O_SYNC = IOFlags(0x00002000)
const O_NOFOLLOW = IOFlags(0x00100000)
const O_DIRECTORY = IOFlags(0x00200000)
const O_TMPFILE = IOFlags(0x00800000) // Probably don't need this
const O_NOATIME = IOFlags(0x01000000)
const O_PATH = IOFlags(0x02000000)
const O_ACCMODE = O_RDONLY | O_RDWR | O_WRONLY
const osModeFlagMask = os.O_RDONLY | os.O_RDWR | os.O_WRONLY
// OSFlagsToIOFlags converts mode flags used for [os.OpenFile] into [IOFlags]
// recognized by Disko.
func OSFlagsToIOFlags(flags int) IOFlags {
var ioFlags IOFlags
switch flags & osModeFlagMask {
case os.O_WRONLY:
ioFlags = O_WRONLY
case os.O_RDWR:
ioFlags = O_RDWR
default:
ioFlags = O_RDONLY
}
if flags&os.O_APPEND != 0 {
ioFlags |= O_APPEND
}
if flags&os.O_CREATE != 0 {
ioFlags |= O_CREATE
}
if flags&os.O_EXCL != 0 {
ioFlags |= O_EXCL
}
if flags&os.O_TRUNC != 0 {
ioFlags |= O_TRUNC
}
if flags&os.O_SYNC != 0 {
ioFlags |= O_SYNC
}
return ioFlags
}
// Append indicates if the mode flags require appending to the end of a file
// stream.
func (flags IOFlags) Append() bool {
return flags&O_APPEND != 0
}
func (flags IOFlags) Read() bool {
masked := flags & O_ACCMODE
return masked == O_RDWR || masked == O_RDONLY
}
func (flags IOFlags) Write() bool {
masked := flags & O_ACCMODE
return masked == O_RDWR || masked == O_WRONLY
}
func (flags IOFlags) Create() bool {
return flags&O_CREATE != 0
}
func (flags IOFlags) Truncate() bool {
return flags&O_TRUNC != 0
}
func (flags IOFlags) Exclusive() bool {
return flags&O_EXCL != 0
}
func (flags IOFlags) Synchronous() bool {
return flags&O_SYNC != 0
}
func (flags IOFlags) NoFollow() bool {
return flags&O_NOFOLLOW != 0
}
func (flags IOFlags) RequiresWritePerm() bool {
return flags.Write() || flags.Truncate()
}