summaryrefslogtreecommitdiffhomepage
path: root/ipn/ipnlocal/diskcache.go
blob: 03ced79670bd2bbe1050196c146c4dd4d335c03f (plain)
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
// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause

package ipnlocal

import (
	"context"
	"errors"
	"fmt"

	"tailscale.com/feature/buildfeatures"
	"tailscale.com/ipn/ipnlocal/netmapcache"
	"tailscale.com/types/netmap"
)

// diskCache is the state netmap caching to disk.
type diskCache struct {
	// all fields guarded by LocalBackend.mu

	dir   string // active profile cache directory
	cache *netmapcache.Cache
}

func (b *LocalBackend) writeNetmapToDiskLocked(nm *netmap.NetworkMap) error {
	if !buildfeatures.HasCacheNetMap || nm == nil || nm.Cached {
		return nil
	}
	b.logf("writing netmap to disk cache")

	dir, err := b.profileMkdirAllLocked(b.pm.CurrentProfile().ID(), "netmap-cache")
	if err != nil {
		return err
	}
	if c := b.diskCache; c.cache == nil || c.dir != dir {
		b.diskCache.cache = netmapcache.NewCache(netmapcache.FileStore(dir))
		b.diskCache.dir = dir
	}
	return b.diskCache.cache.Store(b.currentNode().Context(), nm)
}

func (b *LocalBackend) loadDiskCacheLocked() (om *netmap.NetworkMap, ok bool) {
	if !buildfeatures.HasCacheNetMap {
		return nil, false
	}
	dir, err := b.profileMkdirAllLocked(b.pm.CurrentProfile().ID(), "netmap-cache")
	if err != nil {
		b.logf("profile data directory: %v", err)
		return nil, false
	}
	if c := b.diskCache; c.cache == nil || c.dir != dir {
		b.diskCache.cache = netmapcache.NewCache(netmapcache.FileStore(dir))
		b.diskCache.dir = dir
	}
	nm, err := b.diskCache.cache.Load(b.currentNode().Context())
	if err != nil {
		b.logf("load netmap from cache: %v", err)
		return nil, false
	}
	return nm, true
}

// discardDiskCacheLocked removes a cached network map for the current node, if
// one exists, and disables the cache.
func (b *LocalBackend) discardDiskCacheLocked() {
	if !buildfeatures.HasCacheNetMap {
		return
	}
	if b.diskCache.cache == nil {
		return // nothing to do, we do not have a cache
	}
	// Reaching here, we have a cache directory that needs to be purged.
	// Log errors but do not fail for them.
	store := netmapcache.FileStore(b.diskCache.dir)
	if err := b.clearStoreLocked(b.currentNode().Context(), store); err != nil {
		b.logf("clearing netmap cache: %v", err)
	}
	b.diskCache = diskCache{} // drop in-memory state
}

// clearStoreLocked discards all the keys in the specified store.
func (b *LocalBackend) clearStoreLocked(ctx context.Context, store netmapcache.Store) error {
	var errs []error
	for key, err := range store.List(ctx, "") {
		if err != nil {
			errs = append(errs, fmt.Errorf("list cache contest: %w", err))
			break
		}
		if err := store.Remove(ctx, key); err != nil {
			errs = append(errs, fmt.Errorf("discard cache key %q: %w", key, err))
		}
	}
	return errors.Join(errs...)
}

// ClearNetmapCache discards stored netmap caches (if any) for profiles for the
// current user of b. It also drops any cache from the active backend session,
// if there is one.
func (b *LocalBackend) ClearNetmapCache(ctx context.Context) error {
	if !buildfeatures.HasCacheNetMap {
		return nil // disabled
	}

	b.mu.Lock()
	defer b.mu.Unlock()

	var errs []error
	for _, p := range b.pm.Profiles() {
		store := netmapcache.FileStore(b.profileDataPathLocked(p.ID(), "netmap-cache"))
		err := b.clearStoreLocked(ctx, store)
		if err != nil {
			errs = append(errs, fmt.Errorf("clear netmap cache for profile %q: %w", p.ID(), err))
		}
	}

	b.diskCache = diskCache{} // drop in-memory state
	return errors.Join(errs...)
}