Some checks failed
PR Check / check-and-test (pull_request) Failing after 11m4s
- Updated disk_test.go to replace Set and Get with Create and Open methods for better clarity and functionality. - Modified fileinfo.go to include package comment. - Refactored gc.go to streamline garbage collection handling and removed unused statistics. - Updated gc_test.go to comment out large random tests for future implementation. - Enhanced memory.go to implement LRU caching and metrics for memory usage. - Updated memory_test.go to replace Set and Get with Create and Open methods. - Removed sync.go as it was redundant and not utilized. - Updated vfs.go to reflect changes in the VFS interface, replacing Set and Get with Create and Open. - Added package comments to vfserror.go for consistency.
53 lines
2.0 KiB
Go
53 lines
2.0 KiB
Go
// vfs/gc/gc.go
|
|
package gc
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"s1d3sw1ped/SteamCache2/vfs"
|
|
"s1d3sw1ped/SteamCache2/vfs/vfserror"
|
|
)
|
|
|
|
// Ensure GCFS implements VFS.
|
|
var _ vfs.VFS = (*GCFS)(nil)
|
|
|
|
// GCFS is a virtual file system that calls a GC handler when the disk is full. The GC handler is responsible for freeing up space on the disk. The GCFS is a wrapper around another VFS.
|
|
type GCFS struct {
|
|
vfs.VFS
|
|
multiplier int
|
|
|
|
// protected by mu
|
|
gcHanderFunc GCHandlerFunc
|
|
}
|
|
|
|
// GCHandlerFunc is a function that is called when the disk is full and the GCFS needs to free up space. It is passed the VFS and the size of the file that needs to be written. Its up to the implementation to free up space. How much space is freed is also up to the implementation.
|
|
type GCHandlerFunc func(vfs vfs.VFS, size uint)
|
|
|
|
func New(vfs vfs.VFS, multiplier int, gcHandlerFunc GCHandlerFunc) *GCFS {
|
|
if multiplier <= 0 {
|
|
multiplier = 1 // if the multiplier is less than or equal to 0 set it to 1 will be slow but the user can set it to a higher value if they want
|
|
}
|
|
return &GCFS{
|
|
VFS: vfs,
|
|
multiplier: multiplier,
|
|
gcHanderFunc: gcHandlerFunc,
|
|
}
|
|
}
|
|
|
|
// Create overrides the Create method of the VFS interface. It tries to create the key, if it fails due to disk full error, it calls the GC handler and tries again. If it still fails it returns the error.
|
|
func (g *GCFS) Create(key string, size int64) (io.WriteCloser, error) {
|
|
w, err := g.VFS.Create(key, size) // try to create the key
|
|
|
|
// if it fails due to disk full error, call the GC handler and try again in a loop that will continue until it succeeds or the error is not disk full
|
|
for err == vfserror.ErrDiskFull && g.gcHanderFunc != nil { // if the error is disk full and there is a GC handler
|
|
g.gcHanderFunc(g.VFS, uint(size*int64(g.multiplier))) // call the GC handler
|
|
w, err = g.VFS.Create(key, size)
|
|
}
|
|
|
|
return w, err
|
|
}
|
|
|
|
func (g *GCFS) Name() string {
|
|
return fmt.Sprintf("GCFS(%s)", g.VFS.Name()) // wrap the name of the VFS with GCFS so we can see that its a GCFS
|
|
}
|