aboutsummaryrefslogtreecommitdiff
path: root/object/store/packed/refresh.go
blob: f06e985972a2d45447b9b6e1719ca558457bc9cb (about) (plain) (blame)
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
package packed

import (
	"fmt"
	"io/fs"
	"strings"
)

// Refresh rescans the pack directory
// and replaces the store's view of available packs.
//
// Every index found must parse
// and have its pack data present and consistent;
// otherwise Refresh fails without changing the view.
func (packed *Packed) Refresh() error {
	packed.refreshMu.Lock()
	defer packed.refreshMu.Unlock()

	dirEntries, err := fs.ReadDir(packed.root.FS(), ".")
	if err != nil {
		return fmt.Errorf("object/store/packed: %w", err)
	}

	next := make(map[string]*pack, len(packed.byName))

	opened := make([]*pack, 0, len(dirEntries))

	for _, dirEntry := range dirEntries {
		name, ok := strings.CutSuffix(dirEntry.Name(), ".idx")
		if !ok || dirEntry.IsDir() {
			continue
		}

		if existing, ok := packed.byName[name]; ok {
			next[name] = existing

			continue
		}

		p, err := openPack(packed.root, name, packed.objectFormat)
		if err != nil {
			for _, p := range opened {
				_ = p.close()
			}

			return err
		}

		opened = append(opened, p)
		next[name] = p
	}

	for name, p := range packed.byName {
		if _, ok := next[name]; !ok {
			packed.retired = append(packed.retired, p)
		}
	}

	packed.byName = next

	present := make(map[*pack]struct{}, len(next))
	for _, p := range next {
		present[p] = struct{}{}
	}

	packed.order.Sync(present)

	return nil
}