refactor: moved the GC stuff around and corrected all tests
All checks were successful
PR Check / check-and-test (pull_request) Successful in 30s

This commit is contained in:
2025-07-13 04:20:12 -05:00
parent 1673e9554a
commit 539f14e8ec
9 changed files with 368 additions and 376 deletions

View File

@@ -61,7 +61,6 @@ type DiskFS struct {
size int64
mu sync.RWMutex
keyLocks sync.Map // map[string]*sync.RWMutex
sg sync.WaitGroup
LRU *lruList
}
@@ -135,7 +134,6 @@ func new(root string, capacity int64, skipinit bool) *DiskFS {
capacity: capacity,
mu: sync.RWMutex{},
keyLocks: sync.Map{},
sg: sync.WaitGroup{},
LRU: newLruList(),
}
@@ -162,8 +160,28 @@ func NewSkipInit(root string, capacity int64) *DiskFS {
func (d *DiskFS) init() {
tstart := time.Now()
d.walk(d.root)
d.sg.Wait()
err := filepath.Walk(d.root, func(npath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
d.mu.Lock()
k := strings.ReplaceAll(npath[len(d.root)+1:], "\\", "/")
fi := vfs.NewFileInfoFromOS(info, k)
d.info[k] = fi
d.LRU.Add(k, fi)
d.size += info.Size()
d.mu.Unlock()
return nil
})
if err != nil {
logger.Logger.Error().Err(err).Msg("Walk failed")
}
logger.Logger.Info().
Str("name", d.Name()).
@@ -175,37 +193,6 @@ func (d *DiskFS) init() {
Msg("init")
}
func (d *DiskFS) walk(path string) {
d.sg.Add(1)
go func() {
defer d.sg.Done()
filepath.Walk(path, func(npath string, info os.FileInfo, err error) error {
if path == npath {
return nil
}
if err != nil {
return err
}
if info.IsDir() {
d.walk(npath)
return filepath.SkipDir
}
d.mu.Lock()
k := strings.ReplaceAll(npath[len(d.root)+1:], "\\", "/")
fi := vfs.NewFileInfoFromOS(info, k)
d.info[k] = fi
d.LRU.Add(k, fi)
d.size += info.Size()
d.mu.Unlock()
return nil
})
}()
}
func (d *DiskFS) Capacity() int64 {
return d.capacity
}
@@ -235,6 +222,7 @@ func (d *DiskFS) Create(key string, size int64) (io.WriteCloser, error) {
// Sanitize key to prevent path traversal
key = filepath.Clean(key)
key = strings.ReplaceAll(key, "\\", "/") // Ensure forward slashes for consistency
if strings.Contains(key, "..") {
return nil, vfserror.ErrInvalidKey
}
@@ -257,11 +245,14 @@ func (d *DiskFS) Create(key string, size int64) (io.WriteCloser, error) {
if fi, exists := d.info[key]; exists {
d.size -= fi.Size()
d.LRU.Remove(key)
d.Delete(key)
delete(d.info, key)
path := filepath.Join(d.root, key)
os.Remove(path) // Ignore error, as file might not exist or other issues
}
d.mu.Unlock()
path := filepath.Join(d.root, key)
path = strings.ReplaceAll(path, "\\", "/") // Ensure forward slashes for consistency
dir := filepath.Dir(path)
if err := os.MkdirAll(dir, 0755); err != nil {
return nil, err
@@ -332,6 +323,7 @@ func (d *DiskFS) Delete(key string) error {
// Sanitize key to prevent path traversal
key = filepath.Clean(key)
key = strings.ReplaceAll(key, "\\", "/") // Ensure forward slashes for consistency
if strings.Contains(key, "..") {
return vfserror.ErrInvalidKey
}
@@ -352,6 +344,7 @@ func (d *DiskFS) Delete(key string) error {
d.mu.Unlock()
path := filepath.Join(d.root, key)
path = strings.ReplaceAll(path, "\\", "/") // Ensure forward slashes for consistency
if err := os.Remove(path); err != nil {
return err
}
@@ -372,6 +365,7 @@ func (d *DiskFS) Open(key string) (io.ReadCloser, error) {
// Sanitize key to prevent path traversal
key = filepath.Clean(key)
key = strings.ReplaceAll(key, "\\", "/") // Ensure forward slashes for consistency
if strings.Contains(key, "..") {
return nil, vfserror.ErrInvalidKey
}
@@ -391,6 +385,7 @@ func (d *DiskFS) Open(key string) (io.ReadCloser, error) {
d.mu.Unlock()
path := filepath.Join(d.root, key)
path = strings.ReplaceAll(path, "\\", "/") // Ensure forward slashes for consistency
file, err := os.Open(path)
if err != nil {
return nil, err
@@ -434,6 +429,7 @@ func (d *DiskFS) Stat(key string) (*vfs.FileInfo, error) {
// Sanitize key to prevent path traversal
key = filepath.Clean(key)
key = strings.ReplaceAll(key, "\\", "/") // Ensure forward slashes for consistency
if strings.Contains(key, "..") {
return nil, vfserror.ErrInvalidKey
}