-
Notifications
You must be signed in to change notification settings - Fork 14
/
mount.go
211 lines (174 loc) · 4.99 KB
/
mount.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package fstab
import (
"fmt"
"reflect"
"strconv"
"strings"
)
// Mount represetnts the filesystem info
type Mount struct {
// The block special device or remote filesystem to be mounted
Spec string
// The mount point for the filesystem
File string
// The type of the filesystem
VfsType string
// Mount options associated with the filesystem
MntOps map[string]string
// Used by dump to determine which filesystems need to be dumped.
Freq int
// Used by the fsck(8) program to determine the order in which filesystem checks are done at reboot time
PassNo int
}
type DeviceIdentifierType int
const (
Path DeviceIdentifierType = iota
Label DeviceIdentifierType = iota
UUID DeviceIdentifierType = iota
PartUUID DeviceIdentifierType = iota
PartLabel DeviceIdentifierType = iota
)
// parseOptions parses the options field into an array of strings
func parseOptions(optionsString string) (options map[string]string) {
options = make(map[string]string)
for _, option := range strings.Split(optionsString, ",") {
bits := strings.Split(strings.TrimSpace(option), "=")
if len(bits) > 1 {
options[bits[0]] = bits[1]
} else {
options[bits[0]] = ""
}
}
return
}
// MntOpsString returns the serialized MntOps value
func (mount *Mount) MntOpsString() (opsstring string) {
first := true
for key, value := range mount.MntOps {
if first {
first = false
} else {
opsstring += ","
}
opsstring += key
if "" != value {
opsstring += "=" + value
}
}
return
}
// String serializes the object into fstab format
func (mount *Mount) String() string {
return mount.format("%s %s %s %s %d %d")
}
// format serializes the object according to the given format
func (mount *Mount) format(format string) string {
return fmt.Sprintf(format, mount.Spec, mount.File, mount.VfsType, mount.MntOpsString(), mount.Freq, mount.PassNo)
}
// PaddedString serializes the objet into fstab format with configurable column width.
// Each positional argument specifies the width for the column in order. Up to 6 arguments
// are supported, outstanding arguments will be ignored.
func (mount *Mount) PaddedString(paddings ...int) string {
stringPaddings := 4
intPaddings := 2
if len(paddings) < stringPaddings {
stringPaddings = len(paddings)
intPaddings = 0
} else {
intPaddings = len(paddings) - stringPaddings
if intPaddings > 2 {
intPaddings = 2
}
}
var fields []string = make([]string, 0, 6)
{
for _, padding := range paddings[:stringPaddings] {
fields = append(fields, "%-"+strconv.Itoa(padding)+"s")
}
for i := len(fields); i < 4; i++ {
fields = append(fields, "%s")
}
}
if intPaddings > 0 {
for _, padding := range paddings[4:(4 + intPaddings)] {
fields = append(fields, "%"+strconv.Itoa(padding)+"d")
}
}
for i := len(fields); i < 6; i++ {
fields = append(fields, "%d")
}
fmt.Printf("%d %d\n%v\n%v\n", stringPaddings, intPaddings, paddings, fields)
return mount.format(strings.Join(fields, " "))
}
func (mount *Mount) IsSwap() bool {
return "swap" == mount.VfsType
}
func (mount *Mount) IsNFS() bool {
return "nfs" == mount.VfsType
}
// Equals compares 2 Mount objects
func (mount *Mount) Equals(other *Mount) bool {
return reflect.DeepEqual(*mount, *other)
}
// SpecType returns the device identifier type
func (mount *Mount) SpecType() (spectype DeviceIdentifierType) {
bits := strings.Split(mount.Spec, "=")
switch strings.ToUpper(bits[0]) {
case "UUID":
spectype = UUID
case "LABEL":
spectype = Label
case "PARTUUID":
spectype = PartUUID
case "PARTLABEL":
spectype = PartLabel
default:
spectype = Path
}
return
}
// SpecType returns the device identifier value; that is if Spec is
// "UUID=vogons-ate-my-sandwich", it will return "vogons-ate-my-sandwich"
func (mount *Mount) SpecValue() string {
bits := strings.Split(mount.Spec, "=")
if 1 == len(bits) {
return mount.Spec
} else {
return bits[1]
}
}
// ParseLine parses a single line (of an fstab).
// It will most frequently return a Mount; however,
// If a parsing error occurs, `err` will be non-nil and provide an error message.
// If the line is either empy or a comment line, `mount` will also be nil.
func ParseLine(line string) (mount *Mount, err error) {
line = strings.TrimSpace(line)
// Lines starting with a pound sign (#) are comments, and are ignored. So are empty lines.
if ("" == line) || (line[0] == '#') {
return nil, nil
}
fields := strings.Fields(line)
if len(fields) < 4 {
return nil, fmt.Errorf("too few fields (%d), at least 4 are expected", len(fields))
} else {
mount = new(Mount)
mount.Spec = fields[0]
mount.File = fields[1]
mount.VfsType = fields[2]
mount.MntOps = parseOptions(fields[3])
var convErr error
if len(fields) > 4 {
mount.Freq, convErr = strconv.Atoi(fields[4])
if nil != convErr {
return nil, fmt.Errorf("%s is not a number", fields[4])
}
}
if len(fields) > 5 {
mount.PassNo, convErr = strconv.Atoi(fields[5])
if nil != convErr {
return nil, fmt.Errorf("%s it not a number", fields[5])
}
}
}
return
}