Get get or initialize the cache
(cache string)
| 10 | |
| 11 | // Get get or initialize the cache |
| 12 | func (p *Provider) Get(cache string) (layout.Path, error) { |
| 13 | // ensure the dir exists |
| 14 | if err := os.MkdirAll(cache, os.ModePerm); err != nil { |
| 15 | return "", fmt.Errorf("unable to create cache directory %s: %v", cache, err) |
| 16 | } |
| 17 | |
| 18 | // first try to read the layout from the path |
| 19 | // if it exists, we can use it |
| 20 | // if it does not exist, we will initialize it |
| 21 | // |
| 22 | // do not lock for first read, because we do not need the lock except for initialization |
| 23 | // and future writes, so why slow down reads? |
| 24 | l, err := layout.FromPath(cache) |
| 25 | |
| 26 | // initialize the cache path if needed |
| 27 | if err != nil { |
| 28 | if err := p.Lock(); err != nil { |
| 29 | return "", fmt.Errorf("unable to lock cache %s: %v", cache, err) |
| 30 | } |
| 31 | defer p.Unlock() |
| 32 | |
| 33 | // after lock, try to read the layout again |
| 34 | // in case another process initialized it while we were waiting for the lock |
| 35 | // if it still does not exist, we will initialize it |
| 36 | l, err = layout.FromPath(cache) |
| 37 | if err != nil { |
| 38 | l, err = layout.Write(cache, empty.Index) |
| 39 | if err != nil { |
| 40 | return l, fmt.Errorf("could not initialize cache at path %s: %v", cache, err) |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | return l, nil |
| 45 | } |
no test coverage detected