• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

numtide / treefmt / 15227078419

24 May 2025 12:32PM UTC coverage: 37.541% (+0.2%) from 37.358%
15227078419

Pull #593

github

brianmcgee
fix: force-remove db file when clearing cache

It also improves logging of cache file location when the db cannot be opened.

Fixes #592

Signed-off-by: Brian McGee <brian@bmcgee.ie>
Pull Request #593: fix: force-remove db file when clearing cache

0 of 25 new or added lines in 2 files covered. (0.0%)

42 existing lines in 2 files now uncovered.

690 of 1838 relevant lines covered (37.54%)

17.28 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/walk/cache/cache.go
1
package cache
2

3
import (
4
        "crypto/sha256"
5
        "encoding/hex"
6
        "fmt"
7
        "os"
8
        "time"
9

10
        "github.com/adrg/xdg"
11
        bolt "go.etcd.io/bbolt"
12
)
13

14
const (
15
        bucketPaths = "paths"
16
)
17

18
// Path returns a unique local cache file path for the given root string, using its SHA-256 hash.
NEW
19
func Path(root string) (string, error) {
×
20
        digest := sha256.Sum256([]byte(root))
×
21

×
22
        name := hex.EncodeToString(digest[:])
×
NEW
23

×
NEW
24
        path, err := xdg.CacheFile(fmt.Sprintf("treefmt/eval-cache/%v.db", name))
×
NEW
25
        if err != nil {
×
NEW
26
                return "", fmt.Errorf("could not resolve local path for the cache: %w", err)
×
NEW
27
        }
×
28

NEW
29
        return path, nil
×
30
}
31

32
// Open initialises and opens a Bolt database for the specified root path.
33
// Returns a pointer to the opened database or an error if initialisation fails.
NEW
34
func Open(root string) (*bolt.DB, error) {
×
NEW
35
        // determine the db location
×
NEW
36
        path, err := Path(root)
×
NEW
37
        if err != nil {
×
NEW
38
                return nil, err
×
UNCOV
39
        }
×
40

41
        // open db
42
        db, err := bolt.Open(path, 0o600, &bolt.Options{Timeout: 1 * time.Second})
×
43
        if err != nil {
×
NEW
44
                return nil, fmt.Errorf("failed to open cache db at %s: %w", path, err)
×
45
        }
×
46

47
        // ensure bucket exist
48
        err = db.Update(func(tx *bolt.Tx) error {
×
49
                _, err := tx.CreateBucketIfNotExists([]byte(bucketPaths))
×
50
                if err != nil {
×
51
                        return fmt.Errorf("failed to create bucket: %w", err)
×
52
                }
×
53

54
                return nil
×
55
        })
56
        if err != nil {
×
57
                return nil, fmt.Errorf("failed to create bucket: %w", err)
×
58
        }
×
59

60
        return db, nil
×
61
}
62

63
func PathsBucket(tx *bolt.Tx) *bolt.Bucket {
×
64
        return tx.Bucket([]byte("paths"))
×
65
}
×
66

NEW
67
func Clear(root string) error {
×
NEW
68
        // determine the db location
×
NEW
69
        path, err := Path(root)
×
NEW
70
        if err != nil {
×
NEW
71
                return err
×
UNCOV
72
        }
×
73

74
        // force-remove the db file
NEW
75
        if err = os.Remove(path); !(err == nil || os.IsNotExist(err)) {
×
NEW
76
                return fmt.Errorf("failed to remove cache db at %s: %w", path, err)
×
NEW
77
        }
×
78

NEW
79
        return nil
×
80
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc