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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
package storage
import (
"fmt"
"strings"
"github.com/containers/storage/pkg/idtools"
"github.com/containers/storage/types"
"github.com/google/go-intervals/intervalset"
)
// idSet represents a set of integer IDs. It is stored as an ordered set of intervals.
type idSet struct {
set *intervalset.ImmutableSet
}
func newIDSet(intervals []interval) *idSet {
s := intervalset.Empty()
for _, i := range intervals {
s.Add(intervalset.NewSet([]intervalset.Interval{i}))
}
return &idSet{set: s.ImmutableSet()}
}
// getHostIDs returns all the host ids in the id map.
func getHostIDs(idMaps []idtools.IDMap) *idSet {
var intervals []interval
for _, m := range idMaps {
intervals = append(intervals, interval{start: m.HostID, end: m.HostID + m.Size})
}
return newIDSet(intervals)
}
// getContainerIDs returns all the container ids in the id map.
func getContainerIDs(idMaps []idtools.IDMap) *idSet {
var intervals []interval
for _, m := range idMaps {
intervals = append(intervals, interval{start: m.ContainerID, end: m.ContainerID + m.Size})
}
return newIDSet(intervals)
}
// subtract returns the subtraction of `s` and `t`. `s` and `t` are unchanged.
func (s *idSet) subtract(t *idSet) *idSet {
if s == nil || t == nil {
return s
}
return &idSet{set: s.set.Sub(t.set)}
}
// union returns the union of `s` and `t`. `s` and `t` are unchanged.
func (s *idSet) union(t *idSet) *idSet {
if s == nil {
return t
} else if t == nil {
return s
}
return &idSet{set: s.set.Union(t.set)}
}
// Methods to iterate over the intervals of the idSet. intervalset doesn't provide one :-(
// iterator to idSet. Returns nil if iteration finishes.
type iteratorFn func() *interval
// cancelFn must be called exactly once unless iteratorFn returns nil, otherwise go routine might
// leak.
type cancelFn func()
func (s *idSet) iterator() (iteratorFn, cancelFn) {
if s == nil {
return func() *interval { return nil }, func() {}
}
cancelCh := make(chan byte)
dataCh := make(chan interval)
go func() {
s.set.Intervals(func(ii intervalset.Interval) bool {
select {
case <-cancelCh:
return false
case dataCh <- ii.(interval):
return true
}
})
close(dataCh)
}()
iterator := func() *interval {
i, ok := <-dataCh
if !ok {
return nil
}
return &i
}
return iterator, func() { close(cancelCh) }
}
// size returns the total number of ids in the ID set.
func (s *idSet) size() int {
var size int
iterator, cancel := s.iterator()
defer cancel()
for i := iterator(); i != nil; i = iterator() {
size += i.length()
}
return size
}
// findAvailable finds the `n` ids from `s`.
func (s *idSet) findAvailable(n int) (*idSet, error) {
var intervals []intervalset.Interval
iterator, cancel := s.iterator()
defer cancel()
for i := iterator(); n > 0 && i != nil; i = iterator() {
i.end = minInt(i.end, i.start+n)
intervals = append(intervals, *i)
n -= i.length()
}
if n > 0 {
return nil, types.ErrNoAvailableIDs
}
return &idSet{set: intervalset.NewImmutableSet(intervals)}, nil
}
// zip creates an id map from `s` (host ids) and container ids.
func (s *idSet) zip(container *idSet) []idtools.IDMap {
hostIterator, hostCancel := s.iterator()
defer hostCancel()
containerIterator, containerCancel := container.iterator()
defer containerCancel()
var out []idtools.IDMap
for h, c := hostIterator(), containerIterator(); h != nil && c != nil; {
if n := minInt(h.length(), c.length()); n > 0 {
out = append(out, idtools.IDMap{
ContainerID: c.start,
HostID: h.start,
Size: n,
})
h.start += n
c.start += n
}
if h.IsZero() {
h = hostIterator()
}
if c.IsZero() {
c = containerIterator()
}
}
return out
}
// interval represents an interval of integers [start, end). Note it is allowed to have
// start >= end, in which case it is treated as an empty interval. It implements interface
// intervalset.Interval.
type interval struct {
// Start of the interval (inclusive).
start int
// End of the interval (exclusive).
end int
}
func (i interval) length() int {
return maxInt(0, i.end-i.start)
}
func (i interval) Intersect(other intervalset.Interval) intervalset.Interval {
j := other.(interval)
return interval{start: maxInt(i.start, j.start), end: minInt(i.end, j.end)}
}
func (i interval) Before(other intervalset.Interval) bool {
j := other.(interval)
return !i.IsZero() && !j.IsZero() && i.end < j.start
}
func (i interval) IsZero() bool {
return i.length() <= 0
}
func (i interval) Bisect(other intervalset.Interval) (intervalset.Interval, intervalset.Interval) {
j := other.(interval)
if j.IsZero() {
return i, interval{}
}
// Subtracting [j.start, j.end) is equivalent to the union of intersecting (-inf, j.start) and
// [j.end, +inf).
left := interval{start: i.start, end: minInt(i.end, j.start)}
right := interval{start: maxInt(i.start, j.end), end: i.end}
return left, right
}
func (i interval) Adjoin(other intervalset.Interval) intervalset.Interval {
j := other.(interval)
if !i.IsZero() && !j.IsZero() && (i.end == j.start || j.end == i.start) {
return interval{start: minInt(i.start, j.start), end: maxInt(i.end, j.end)}
}
return interval{}
}
func (i interval) Encompass(other intervalset.Interval) intervalset.Interval {
j := other.(interval)
switch {
case i.IsZero():
return j
case j.IsZero():
return i
default:
return interval{start: minInt(i.start, j.start), end: maxInt(i.end, j.end)}
}
}
func minInt(a, b int) int {
if a < b {
return a
}
return b
}
func maxInt(a, b int) int {
if a < b {
return b
}
return a
}
func hasOverlappingRanges(mappings []idtools.IDMap) error {
hostIntervals := intervalset.Empty()
containerIntervals := intervalset.Empty()
var conflicts []string
for _, m := range mappings {
c := interval{start: m.ContainerID, end: m.ContainerID + m.Size}
h := interval{start: m.HostID, end: m.HostID + m.Size}
added := false
overlaps := false
containerIntervals.IntervalsBetween(c, func(x intervalset.Interval) bool {
overlaps = true
return false
})
if overlaps {
conflicts = append(conflicts, fmt.Sprintf("%v:%v:%v", m.ContainerID, m.HostID, m.Size))
added = true
}
containerIntervals.Add(intervalset.NewSet([]intervalset.Interval{c}))
hostIntervals.IntervalsBetween(h, func(x intervalset.Interval) bool {
overlaps = true
return false
})
if overlaps && !added {
conflicts = append(conflicts, fmt.Sprintf("%v:%v:%v", m.ContainerID, m.HostID, m.Size))
}
hostIntervals.Add(intervalset.NewSet([]intervalset.Interval{h}))
}
if conflicts != nil {
if len(conflicts) == 1 {
return fmt.Errorf("the specified UID and/or GID mapping %s conflicts with other mappings: %w", conflicts[0], ErrInvalidMappings)
}
return fmt.Errorf("the specified UID and/or GID mappings %s conflict with other mappings: %w", strings.Join(conflicts, ", "), ErrInvalidMappings)
}
return nil
}
|