Skip to content

Commit

Permalink
Add cache directory tagging support
Browse files Browse the repository at this point in the history
See: https://bford.info/cachedir/

This is a small tweak to make sure that the cache directory can be
identified as such.
  • Loading branch information
kgaughan committed Oct 26, 2024
1 parent 78c00e8 commit 6716491
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/mercury/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func main() {
log.Fatal(err)
}

utils.EnsureDir(config.Cache)
utils.EnsureCache(config.Cache)

if !*flags.NoBuild {
utils.EnsureDir(config.Output)
Expand Down
28 changes: 28 additions & 0 deletions internal/utils/paths.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package utils

import (
"bytes"
"log"
"os"
"path/filepath"
)

const cacheTagMarker = "Signature: 8a477f597d28d172789f06886806bc55\n"

func EnsureDir(path string) {
if fileInfo, err := os.Stat(path); os.IsNotExist(err) {
if err := os.MkdirAll(path, 0o755); err != nil {
Expand All @@ -14,3 +18,27 @@ func EnsureDir(path string) {
log.Fatalf("%s must be a directory\n", path)
}
}

func writeCacheTag(path string) error {
return os.WriteFile(path, []byte(cacheTagMarker), 0o600)

Check failure on line 23 in internal/utils/paths.go

View workflow job for this annotation

GitHub Actions / build

error returned from external package is unwrapped: sig: func os.WriteFile(name string, data []byte, perm io/fs.FileMode) error (wrapcheck)

Check failure on line 23 in internal/utils/paths.go

View workflow job for this annotation

GitHub Actions / build

error returned from external package is unwrapped: sig: func os.WriteFile(name string, data []byte, perm io/fs.FileMode) error (wrapcheck)
}

func EnsureCache(path string) {
EnsureDir(path)
cacheTag := filepath.Join(path, "CACHE.TAG")
if _, err := os.Stat(cacheTag); os.IsNotExist(err) {
if err = writeCacheTag(cacheTag); err != nil {
log.Fatal(err)
}
} else {
contents, err := os.ReadFile(cacheTag)
if err != nil {
log.Fatal(err)
}
if !bytes.HasPrefix(contents, []byte(cacheTagMarker)) {
if err = writeCacheTag(cacheTag); err != nil {
log.Fatal(err)
}
}
}
}

0 comments on commit 6716491

Please sign in to comment.