38 lines
933 B
Go
38 lines
933 B
Go
package bookfile
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
func DirKey(id int64, hash string) string { return fmt.Sprintf("%d-%s", id, hash) }
|
|
|
|
func CoverDir(cacheDir, key string) string { return filepath.Join(cacheDir, "covers", key) }
|
|
func PagesDir(cacheDir, key string) string { return filepath.Join(cacheDir, "pages", key) }
|
|
|
|
func SweepStale(cacheDir string, live map[string]bool) (int, error) {
|
|
removed := 0
|
|
for _, kind := range []string{"covers", "pages"} {
|
|
entries, err := os.ReadDir(filepath.Join(cacheDir, kind))
|
|
if err != nil {
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
continue // 首次运行还没有该层目录,不算错
|
|
}
|
|
return removed, err
|
|
}
|
|
for _, e := range entries {
|
|
if !e.IsDir() || live[e.Name()] {
|
|
continue
|
|
}
|
|
if err := os.RemoveAll(filepath.Join(cacheDir, kind, e.Name())); err != nil {
|
|
return removed, err
|
|
}
|
|
removed++
|
|
}
|
|
}
|
|
return removed, nil
|
|
}
|