Add a clear cache action
This commit is contained in:
parent
37da04aad6
commit
fae50137a9
4 changed files with 49 additions and 2 deletions
|
|
@ -748,6 +748,15 @@ var default_cwd string
|
|||
var use_light_colors bool
|
||||
|
||||
func main(_ *cli.Command, opts *Options, args []string) (rc int, err error) {
|
||||
if opts.ClearCache {
|
||||
c, err := preview_cache()
|
||||
if err != nil {
|
||||
return 1, err
|
||||
}
|
||||
if err = c.Clear(); err != nil {
|
||||
return 1, err
|
||||
}
|
||||
}
|
||||
write_output := func(selections []string, interrupted bool, current_filter string) {
|
||||
payload := make(map[string]any)
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -217,6 +217,11 @@ def handle_result(args: list[str], data: dict[str, Any], target_window_id: int,
|
|||
|
||||
--write-pid-to
|
||||
Path to a file to which to write the process ID (PID) of this process to.
|
||||
|
||||
|
||||
--clear-cache
|
||||
type=bool-set
|
||||
Clear the caches used by this kitten.
|
||||
'''.format(config_help=CONFIG_HELP.format(conf_name='choose-files', appname=appname)).format
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -66,6 +66,12 @@ func (dc *DiskCache) Remove(key string) (err error) {
|
|||
return dc.remove(key)
|
||||
}
|
||||
|
||||
func (dc *DiskCache) Clear() (err error) {
|
||||
dc.lock()
|
||||
defer dc.unlock()
|
||||
return dc.clear()
|
||||
}
|
||||
|
||||
func (dc *DiskCache) AddPath(path, key string, items map[string][]byte) (ans map[string]string, err error) {
|
||||
dc.lock()
|
||||
defer dc.unlock()
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ import (
|
|||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
|
|
@ -60,6 +61,8 @@ func get_file_state_from_path(path string) (*file_state, error) {
|
|||
}
|
||||
}
|
||||
|
||||
const GET_DIR_PREFIX = "getdir-"
|
||||
|
||||
func new_disk_cache(path string, max_size int64) (dc *DiskCache, err error) {
|
||||
if path, err = filepath.Abs(path); err != nil {
|
||||
return
|
||||
|
|
@ -81,7 +84,7 @@ func new_disk_cache(path string, max_size int64) (dc *DiskCache, err error) {
|
|||
if _, err := ans.prune(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if ans.get_dir, err = os.MkdirTemp(ans.Path, "getdir-*"); err != nil {
|
||||
if ans.get_dir, err = os.MkdirTemp(ans.Path, GET_DIR_PREFIX+"*"); err != nil {
|
||||
return
|
||||
}
|
||||
if err = utils.AtExitRmtree(ans.get_dir); err != nil {
|
||||
|
|
@ -129,7 +132,9 @@ func (dc *DiskCache) unlock() {
|
|||
}
|
||||
}
|
||||
|
||||
func (dc *DiskCache) entries_path() string { return filepath.Join(dc.Path, "entries.json") }
|
||||
const ENTRIES_NAME = "entries.json"
|
||||
|
||||
func (dc *DiskCache) entries_path() string { return filepath.Join(dc.Path, ENTRIES_NAME) }
|
||||
|
||||
func (dc *DiskCache) write_entries_if_dirty() (err error) {
|
||||
if !dc.entries_dirty {
|
||||
|
|
@ -321,6 +326,28 @@ func (dc *DiskCache) get(key string, items []string) (map[string]string, error)
|
|||
return ans, dc.write_entries_if_dirty()
|
||||
}
|
||||
|
||||
func (dc *DiskCache) clear() (err error) {
|
||||
if entries, err := os.ReadDir(dc.Path); err != nil {
|
||||
return err
|
||||
} else {
|
||||
defer func() {
|
||||
if we := dc.write_entries_if_dirty(); we != nil && err == nil {
|
||||
err = we
|
||||
}
|
||||
}()
|
||||
for _, x := range entries {
|
||||
if x.IsDir() && !strings.HasPrefix(x.Name(), GET_DIR_PREFIX) {
|
||||
_ = os.RemoveAll(filepath.Join(dc.Path, x.Name()))
|
||||
}
|
||||
}
|
||||
dc.entries_dirty = true
|
||||
dc.entry_map = make(map[string]*Entry)
|
||||
dc.entries.SortedEntries = make([]*Entry, 0)
|
||||
dc.entries.TotalSize = 0
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (dc *DiskCache) remove(key string) (err error) {
|
||||
if err = dc.ensure_entries(); err != nil {
|
||||
return
|
||||
|
|
|
|||
Loading…
Reference in a new issue