feat: add upstream and verbose flags to command line interface
All checks were successful
Release Tag / release (push) Successful in 13s

feat: add upstream support allowing to chain cache servers if needed
fix: tweaked garbage collection to be better
This commit is contained in:
2025-01-23 11:14:39 -06:00
parent 931c43d7a8
commit e24af47697
11 changed files with 310 additions and 103 deletions

View File

@@ -1,11 +1,13 @@
package disk
import (
"fmt"
"os"
"path/filepath"
"s1d3sw1ped/SteamCache2/steamcache/logger"
"s1d3sw1ped/SteamCache2/vfs"
"s1d3sw1ped/SteamCache2/vfs/vfserror"
"strings"
"sync"
"time"
@@ -82,6 +84,8 @@ func (d *DiskFS) init() {
Str("name", d.Name()).
Str("root", d.root).
Str("capacity", units.HumanSize(float64(d.capacity))).
Str("size", units.HumanSize(float64(d.Size()))).
Str("files", fmt.Sprint(len(d.info))).
Str("duration", time.Since(tstart).String()).
Msg("init")
}
@@ -105,7 +109,8 @@ func (d *DiskFS) walk(path string) {
}
d.mu.Lock()
k := npath[len(d.root)+1:]
k := strings.ReplaceAll(npath[len(d.root)+1:], "\\", "/")
logger.Logger.Debug().Str("name", k).Str("root", d.root).Msg("walk")
d.info[k] = vfs.NewFileInfoFromOS(info, k)
d.mu.Unlock()
@@ -124,9 +129,10 @@ func (d *DiskFS) Name() string {
}
func (d *DiskFS) Size() int64 {
var size int64
d.mu.Lock()
defer d.mu.Unlock()
var size int64
for _, v := range d.info {
size += v.Size()
}
@@ -134,24 +140,34 @@ func (d *DiskFS) Size() int64 {
}
func (d *DiskFS) Set(key string, src []byte) error {
if key == "" {
return vfserror.ErrInvalidKey
}
if key[0] == '/' {
return vfserror.ErrInvalidKey
}
if d.capacity > 0 {
if size := d.Size() + int64(len(src)); size > d.capacity {
return vfserror.ErrDiskFull
}
}
logger.Logger.Debug().Str("name", key).Str("root", d.root).Msg("set")
if _, err := d.Stat(key); err == nil {
logger.Logger.Debug().Str("name", key).Str("root", d.root).Msg("delete")
d.Delete(key)
}
d.mu.Lock()
defer d.mu.Unlock()
os.MkdirAll(filepath.Join(d.root, filepath.Dir(key)), 0755)
if err := os.WriteFile(filepath.Join(d.root, key), src, 0644); err != nil {
os.MkdirAll(d.root+"/"+filepath.Dir(key), 0755)
if err := os.WriteFile(d.root+"/"+key, src, 0644); err != nil {
return err
}
fi, err := os.Stat(filepath.Join(d.root, key))
fi, err := os.Stat(d.root + "/" + key)
if err != nil {
panic(err)
}
@@ -163,6 +179,13 @@ func (d *DiskFS) Set(key string, src []byte) error {
// Delete deletes the value of key.
func (d *DiskFS) Delete(key string) error {
if key == "" {
return vfserror.ErrInvalidKey
}
if key[0] == '/' {
return vfserror.ErrInvalidKey
}
_, err := d.Stat(key)
if err != nil {
return err
@@ -170,6 +193,7 @@ func (d *DiskFS) Delete(key string) error {
d.mu.Lock()
defer d.mu.Unlock()
delete(d.info, key)
if err := os.Remove(filepath.Join(d.root, key)); err != nil {
return err
@@ -180,6 +204,13 @@ func (d *DiskFS) Delete(key string) error {
// Get gets the value of key and returns it.
func (d *DiskFS) Get(key string) ([]byte, error) {
if key == "" {
return nil, vfserror.ErrInvalidKey
}
if key[0] == '/' {
return nil, vfserror.ErrInvalidKey
}
_, err := d.Stat(key)
if err != nil {
return nil, err
@@ -198,25 +229,23 @@ func (d *DiskFS) Get(key string) ([]byte, error) {
// Stat returns the FileInfo of key. If key is not found in the cache, it will stat the file on disk. If the file is not found on disk, it will return vfs.ErrNotFound.
func (d *DiskFS) Stat(key string) (*vfs.FileInfo, error) {
d.mu.Lock()
fi, ok := d.info[key]
d.mu.Unlock() // unlock before statting the file
if !ok {
fii, err := os.Stat(filepath.Join(d.root, key))
if err != nil {
return nil, vfserror.ErrNotFound
}
d.mu.Lock() // relock to update the info map
defer d.mu.Unlock() // nothing else needs to unlock before returning
d.info[key] = vfs.NewFileInfoFromOS(fii, key)
fi = d.info[key]
// fallthrough to return fi with shiny new info
if key == "" {
return nil, vfserror.ErrInvalidKey
}
if key[0] == '/' {
return nil, vfserror.ErrInvalidKey
}
return fi, nil
logger.Logger.Debug().Str("name", key).Str("root", d.root).Msg("stat")
d.mu.Lock()
defer d.mu.Unlock()
if fi, ok := d.info[key]; !ok {
return nil, vfserror.ErrNotFound
} else {
return fi, nil
}
}
func (m *DiskFS) StatAll() []*vfs.FileInfo {