mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-24 02:31:18 -08:00
39 lines
1.1 KiB
Go
39 lines
1.1 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package netmapcache
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
|
|
"tailscale.com/types/logger"
|
|
)
|
|
|
|
// NewStore creates [Store] for backing the netmapcache. If the [Store] already
|
|
// exists, open the existing [Store]. If [HookNewStore] is set (e.g. by
|
|
// feature/netmapcachesqlite), that implementation is used instead of the
|
|
// default [FileStore].
|
|
func NewStore(ctx context.Context, dir string, logf logger.Logf) (Store, error) {
|
|
if f, ok := HookNewStore.GetOk(); ok {
|
|
return f(ctx, dir, logf)
|
|
}
|
|
return FileStore(dir), nil
|
|
}
|
|
|
|
// OpenStore opens an existing [Store]. Returns nil, nil, if the store does not
|
|
// exist. If [HookOpenStore] is set (e.g. by feature/netmapcachesqlite), that
|
|
// implementation is used instead of the default [FileStore].
|
|
func OpenStore(ctx context.Context, dir string, logf logger.Logf) (Store, error) {
|
|
if f, ok := HookOpenStore.GetOk(); ok {
|
|
return f(ctx, dir, logf)
|
|
}
|
|
if _, err := os.Stat(dir); errors.Is(err, os.ErrNotExist) {
|
|
return nil, nil
|
|
} else if err != nil {
|
|
return nil, err
|
|
}
|
|
return FileStore(dir), nil
|
|
}
|