2018-02-05 21:05:59 +00:00
|
|
|
package graphdriver // import "github.com/docker/docker/daemon/graphdriver"
|
2016-04-18 23:41:29 +00:00
|
|
|
|
2016-05-06 19:04:26 +00:00
|
|
|
import "sync"
|
2016-05-02 22:44:20 +00:00
|
|
|
|
|
|
|
type minfo struct {
|
|
|
|
check bool
|
|
|
|
count int
|
|
|
|
}
|
2016-04-18 23:41:29 +00:00
|
|
|
|
|
|
|
// RefCounter is a generic counter for use by graphdriver Get/Put calls
|
|
|
|
type RefCounter struct {
|
2016-05-06 19:04:26 +00:00
|
|
|
counts map[string]*minfo
|
|
|
|
mu sync.Mutex
|
|
|
|
checker Checker
|
2016-04-18 23:41:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewRefCounter returns a new RefCounter
|
2016-05-06 19:04:26 +00:00
|
|
|
func NewRefCounter(c Checker) *RefCounter {
|
|
|
|
return &RefCounter{
|
|
|
|
checker: c,
|
|
|
|
counts: make(map[string]*minfo),
|
|
|
|
}
|
2016-04-18 23:41:29 +00:00
|
|
|
}
|
|
|
|
|
2017-02-16 12:08:57 +00:00
|
|
|
// Increment increases the ref count for the given id and returns the current count
|
2016-05-02 22:44:20 +00:00
|
|
|
func (c *RefCounter) Increment(path string) int {
|
2017-02-28 10:12:02 +00:00
|
|
|
return c.incdec(path, func(minfo *minfo) {
|
|
|
|
minfo.count++
|
|
|
|
})
|
2016-04-18 23:41:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Decrement decreases the ref count for the given id and returns the current count
|
2016-05-02 22:44:20 +00:00
|
|
|
func (c *RefCounter) Decrement(path string) int {
|
2017-02-28 10:12:02 +00:00
|
|
|
return c.incdec(path, func(minfo *minfo) {
|
|
|
|
minfo.count--
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RefCounter) incdec(path string, infoOp func(minfo *minfo)) int {
|
2016-04-18 23:41:29 +00:00
|
|
|
c.mu.Lock()
|
2016-05-02 22:44:20 +00:00
|
|
|
m := c.counts[path]
|
|
|
|
if m == nil {
|
2016-05-03 00:22:11 +00:00
|
|
|
m = &minfo{}
|
2016-05-02 22:44:20 +00:00
|
|
|
c.counts[path] = m
|
|
|
|
}
|
|
|
|
// if we are checking this path for the first time check to make sure
|
|
|
|
// if it was already mounted on the system and make sure we have a correct ref
|
|
|
|
// count if it is mounted as it is in use.
|
|
|
|
if !m.check {
|
|
|
|
m.check = true
|
2016-05-06 19:04:26 +00:00
|
|
|
if c.checker.IsMounted(path) {
|
2016-05-02 22:44:20 +00:00
|
|
|
m.count++
|
|
|
|
}
|
|
|
|
}
|
2017-02-28 10:12:02 +00:00
|
|
|
infoOp(m)
|
2017-02-03 16:47:55 +00:00
|
|
|
count := m.count
|
2018-02-08 07:44:20 +00:00
|
|
|
if count <= 0 {
|
|
|
|
delete(c.counts, path)
|
|
|
|
}
|
2016-04-18 23:41:29 +00:00
|
|
|
c.mu.Unlock()
|
2017-02-03 16:47:55 +00:00
|
|
|
return count
|
2017-02-28 10:12:06 +00:00
|
|
|
}
|