added preload status and cancel

This commit is contained in:
ston1th 2022-03-16 01:58:10 +01:00
commit a069baf48d
10 changed files with 153 additions and 35 deletions

View file

@ -3,6 +3,7 @@
package fs
import (
"context"
"errors"
"io"
"os"
@ -22,9 +23,15 @@ type preload struct {
f *File
skipped int64
written int64
ctx context.Context
}
func (p *preload) Read(data []byte) (n int, err error) {
select {
case <-p.ctx.Done():
return 0, p.ctx.Err()
default:
}
if p.f.offset >= p.f.size() {
return 0, io.EOF
}
@ -39,7 +46,7 @@ func (p *preload) Read(data []byte) (n int, err error) {
return
}
func (f *File) Preload(unlock func()) {
func (f *File) Preload(ctx context.Context, unlock func()) {
log := f.log
if f.offline {
log.V(2).Error(errors.New("no preload in offline mode"), "error preloading file")
@ -47,8 +54,13 @@ func (f *File) Preload(unlock func()) {
return
}
log.V(2).Info("preload started")
p := &preload{f: f}
p := &preload{f: f, ctx: ctx}
_, err := io.Copy(io.Discard, p)
if err == context.Canceled {
log.V(2).Info("preload canceled", "skipped", p.skipped, "written", p.written)
unlock()
return
}
if err != nil && err != io.EOF {
log.Error(err, "error preloading file")
}
@ -103,10 +115,12 @@ func (f *File) readWithoutCache(p []byte) (n int, err error) {
}
func (f *File) Seek(offset int64, whence int) (n int64, err error) {
n, err = f.f.Seek(offset, whence)
if err != nil {
f.log.Error(err, "error seeking source file")
return
if !f.offline {
n, err = f.f.Seek(offset, whence)
if err != nil {
f.log.Error(err, "error seeking source file")
return
}
}
switch whence {
case io.SeekStart:
@ -117,14 +131,20 @@ func (f *File) Seek(offset int64, whence int) (n int64, err error) {
return
}
func (f *File) Readdir(count int) ([]os.FileInfo, error) {
return f.f.Readdir(count)
func (f *File) Readdir(_ int) ([]os.FileInfo, error) {
return nil, nil
}
func (f *File) Stat() (os.FileInfo, error) {
if f.offline {
return f.md.Stat()
}
return f.f.Stat()
}
func (f *File) Close() error {
if f.offline {
return nil
}
return f.f.Close()
}