Debian Codegraph @

mount/flags_unix.go

raw symbols SCIP
1 //go:build !darwin && !windows
2
3 package mount
4
5 import (
6 "fmt"
7 "strings"
8 )
9
10 var flags = map[string]struct {
11 clear bool
12 flag int
13 }{
14 "defaults": {false, 0},
15 "ro": {false, RDONLY},
16 "rw": {true, RDONLY},
17 "suid": {true, NOSUID},
18 "nosuid": {false, NOSUID},
19 "dev": {true, NODEV},
20 "nodev": {false, NODEV},
21 "exec": {true, NOEXEC},
22 "noexec": {false, NOEXEC},
23 "sync": {false, SYNCHRONOUS},
24 "async": {true, SYNCHRONOUS},
25 "dirsync": {false, DIRSYNC},
26 "remount": {false, REMOUNT},
27 "mand": {false, MANDLOCK},
28 "nomand": {true, MANDLOCK},
29 "atime": {true, NOATIME},
30 "noatime": {false, NOATIME},
31 "diratime": {true, NODIRATIME},
32 "nodiratime": {false, NODIRATIME},
33 "bind": {false, BIND},
34 "rbind": {false, RBIND},
35 "unbindable": {false, UNBINDABLE},
36 "runbindable": {false, RUNBINDABLE},
37 "private": {false, PRIVATE},
38 "rprivate": {false, RPRIVATE},
39 "shared": {false, SHARED},
40 "rshared": {false, RSHARED},
41 "slave": {false, SLAVE},
42 "rslave": {false, RSLAVE},
43 "relatime": {false, RELATIME},
44 "norelatime": {true, RELATIME},
45 "strictatime": {false, STRICTATIME},
46 "nostrictatime": {true, STRICTATIME},
47 }
48
49 var validFlags = map[string]bool{
50 "": true,
51 "size": true,
52 "mode": true,
53 "uid": true,
54 "gid": true,
55 "nr_inodes": true,
56 "nr_blocks": true,
57 "mpol": true,
58 }
59
60 var propagationFlags = map[string]bool{
61 "bind": true,
62 "rbind": true,
63 "unbindable": true,
64 "runbindable": true,
65 "private": true,
66 "rprivate": true,
67 "shared": true,
68 "rshared": true,
69 "slave": true,
70 "rslave": true,
71 }
72
73 // MergeTmpfsOptions merge mount options to make sure there is no duplicate.
74 func MergeTmpfsOptions(options []string) ([]string, error) {
75 // We use collisions maps to remove duplicates.
76 // For flag, the key is the flag value (the key for propagation flag is -1)
77 // For data=value, the key is the data
78 flagCollisions := map[int]bool{}
79 dataCollisions := map[string]bool{}
80
81 var newOptions []string
82 // We process in reverse order
83 for i := len(options) - 1; i >= 0; i-- {
84 option := options[i]
85 if option == "defaults" {
86 continue
87 }
88 if f, ok := flags[option]; ok && f.flag != 0 {
89 // There is only one propagation mode
90 key := f.flag
91 if propagationFlags[option] {
92 key = -1
93 }
94 // Check to see if there is collision for flag
95 if !flagCollisions[key] {
96 // We prepend the option and add to collision map
97 newOptions = append([]string{option}, newOptions...)
98 flagCollisions[key] = true
99 }
100 continue
101 }
102 opt, _, ok := strings.Cut(option, "=")
103 if !ok || !validFlags[opt] {
104 return nil, fmt.Errorf("invalid tmpfs option %q", opt)
105 }
106 if !dataCollisions[opt] {
107 // We prepend the option and add to collision map
108 newOptions = append([]string{option}, newOptions...)
109 dataCollisions[opt] = true
110 }
111 }
112
113 return newOptions, nil
114 }
115
116 // Parse fstab type mount options into mount() flags
117 // and device specific data
118 func parseOptions(options string) (int, string) {
119 var (
120 flag int
121 data []string
122 )
123
124 for _, o := range strings.Split(options, ",") {
125 // If the option does not exist in the flags table or the flag
126 // is not supported on the platform,
127 // then it is a data value for a specific fs type
128 if f, exists := flags[o]; exists && f.flag != 0 {
129 if f.clear {
130 flag &= ^f.flag
131 } else {
132 flag |= f.flag
133 }
134 } else {
135 data = append(data, o)
136 }
137 }
138 return flag, strings.Join(data, ",")
139 }