| 1 |
package user |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"os" |
| 6 |
) |
| 7 |
|
| 8 |
|
| 9 |
type MkdirOpt func(*mkdirOptions) |
| 10 |
|
| 11 |
type mkdirOptions struct { |
| 12 |
onlyNew bool |
| 13 |
} |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
func WithOnlyNew(o *mkdirOptions) { |
| 18 |
o.onlyNew = true |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
func MkdirAllAndChown(path string, mode os.FileMode, uid, gid int, opts ...MkdirOpt) error { |
| 26 |
var options mkdirOptions |
| 27 |
for _, opt := range opts { |
| 28 |
opt(&options) |
| 29 |
} |
| 30 |
|
| 31 |
return mkdirAs(path, mode, uid, gid, true, options.onlyNew) |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
func MkdirAndChown(path string, mode os.FileMode, uid, gid int, opts ...MkdirOpt) error { |
| 41 |
var options mkdirOptions |
| 42 |
for _, opt := range opts { |
| 43 |
opt(&options) |
| 44 |
} |
| 45 |
return mkdirAs(path, mode, uid, gid, false, options.onlyNew) |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
func getRootUIDGID(uidMap, gidMap []IDMap) (int, int, error) { |
| 51 |
uid, err := toHost(0, uidMap) |
| 52 |
if err != nil { |
| 53 |
return -1, -1, err |
| 54 |
} |
| 55 |
gid, err := toHost(0, gidMap) |
| 56 |
if err != nil { |
| 57 |
return -1, -1, err |
| 58 |
} |
| 59 |
return uid, gid, nil |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
func toContainer(hostID int, idMap []IDMap) (int, error) { |
| 66 |
if idMap == nil { |
| 67 |
return hostID, nil |
| 68 |
} |
| 69 |
for _, m := range idMap { |
| 70 |
if (int64(hostID) >= m.ParentID) && (int64(hostID) <= (m.ParentID + m.Count - 1)) { |
| 71 |
contID := int(m.ID + (int64(hostID) - m.ParentID)) |
| 72 |
return contID, nil |
| 73 |
} |
| 74 |
} |
| 75 |
return -1, fmt.Errorf("host ID %d cannot be mapped to a container ID", hostID) |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
func toHost(contID int, idMap []IDMap) (int, error) { |
| 82 |
if idMap == nil { |
| 83 |
return contID, nil |
| 84 |
} |
| 85 |
for _, m := range idMap { |
| 86 |
if (int64(contID) >= m.ID) && (int64(contID) <= (m.ID + m.Count - 1)) { |
| 87 |
hostID := int(m.ParentID + (int64(contID) - m.ID)) |
| 88 |
return hostID, nil |
| 89 |
} |
| 90 |
} |
| 91 |
return -1, fmt.Errorf("container ID %d cannot be mapped to a host ID", contID) |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
type IdentityMapping struct { |
| 97 |
UIDMaps []IDMap `json:"UIDMaps"` |
| 98 |
GIDMaps []IDMap `json:"GIDMaps"` |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
func (i IdentityMapping) RootPair() (int, int) { |
| 105 |
uid, gid, _ := getRootUIDGID(i.UIDMaps, i.GIDMaps) |
| 106 |
return uid, gid |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
func (i IdentityMapping) ToHost(uid, gid int) (int, int, error) { |
| 112 |
var err error |
| 113 |
ruid, rgid := i.RootPair() |
| 114 |
|
| 115 |
if uid != ruid { |
| 116 |
ruid, err = toHost(uid, i.UIDMaps) |
| 117 |
if err != nil { |
| 118 |
return ruid, rgid, err |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
if gid != rgid { |
| 123 |
rgid, err = toHost(gid, i.GIDMaps) |
| 124 |
} |
| 125 |
return ruid, rgid, err |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
func (i IdentityMapping) ToContainer(uid, gid int) (int, int, error) { |
| 130 |
ruid, err := toContainer(uid, i.UIDMaps) |
| 131 |
if err != nil { |
| 132 |
return -1, -1, err |
| 133 |
} |
| 134 |
rgid, err := toContainer(gid, i.GIDMaps) |
| 135 |
return ruid, rgid, err |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
func (i IdentityMapping) Empty() bool { |
| 140 |
return len(i.UIDMaps) == 0 && len(i.GIDMaps) == 0 |
| 141 |
} |