~rbn/neinp

ref: 280ca11dc4f314d4bb506de2f219eb70dd959227 neinp/stat/consts.go -rw-r--r-- 2.4 KiB
280ca11dRuben Schuller update go.mod 1 year, 2 months ago
                                                                                
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
137
138
139
140
141
142
143
package stat

import (
	"os"
)

/*Mode represents modes of a directory entry.*/
type Mode uint32

const (
	// SetGid is setgid (Unix, 9P2000.u)
	SetGid Mode = 0x00040000

	// SetUid is setuid (Unix, 9P2000.u)
	SetUid Mode = 0x00080000

	// Socket is a socket (Unix, 9P2000.u)
	Socket Mode = 0x00100000

	// NamedPipe is a named pipe (Unix, 9P2000.u)
	NamedPipe Mode = 0x00200000

	// Device is a device file (Unix, 9P2000.u)
	Device Mode = 0x00800000

	// Symlink is a symbolic link (Unix, 9P2000.u)
	Symlink Mode = 0x02000000

	// Tmp is a non-backed-up file
	Tmp Mode = 0x04000000

	// Auth is a authentication file
	Auth Mode = 0x08000000

	// Mount is a mounted channel
	Mount Mode = 0x10000000

	// Excl is a exclusive use file
	Excl Mode = 0x20000000

	// Append is a append only file
	Append Mode = 0x40000000

	// Dir is a directory
	Dir Mode = 0x80000000
)

//NeinMode converts os.FileMode to Mode.
func NeinMode(osmode os.FileMode) Mode {
	var mode Mode

	if osmode&os.ModeDir == os.ModeDir {
		mode |= Dir
	}

	if osmode&os.ModeAppend == os.ModeAppend {
		mode |= Append
	}

	if osmode&os.ModeExclusive == os.ModeExclusive {
		mode |= Excl
	}

	if osmode%os.ModeTemporary == os.ModeTemporary {
		mode |= Tmp
	}

	if osmode&os.ModeSymlink == os.ModeSymlink {
		mode |= Symlink
	}

	if osmode&os.ModeDevice == os.ModeDevice {
		mode |= Device
	}

	if osmode&os.ModeNamedPipe == os.ModeNamedPipe {
		mode |= NamedPipe
	}

	if osmode&os.ModeSocket == os.ModeSocket {
		mode |= Socket
	}

	if osmode&os.ModeSetgid == os.ModeSetgid {
		mode |= SetGid
	}

	if osmode&os.ModeSetuid == os.ModeSetuid {
		mode |= SetUid
	}

	mode |= Mode(uint32(osmode) & 0777)

	return mode
}

//OsMode translates Mode to os.FileMode.
func OsMode(mode Mode) os.FileMode {
	var osmode os.FileMode
	if mode&Dir == Dir {
		osmode |= os.ModeDir
	}

	if mode&Append == Append {
		osmode |= os.ModeAppend
	}

	if mode&Excl == Excl {
		osmode |= os.ModeExclusive
	}

	if mode&Tmp == Tmp {
		osmode |= os.ModeTemporary
	}

	if mode&Symlink == Symlink {
		osmode |= os.ModeSymlink
	}

	if mode&Device == Device {
		osmode |= os.ModeDevice
	}

	if mode&NamedPipe == NamedPipe {
		osmode |= os.ModeNamedPipe
	}

	if mode&Socket == Socket {
		osmode |= os.ModeSocket
	}

	if mode&SetGid == SetGid {
		osmode |= os.ModeSetgid
	}

	if mode&SetUid == SetUid {
		osmode |= os.ModeSetuid
	}

	osmode |= os.FileMode(mode & 0777)

	return os.FileMode(osmode)
}