added offline mode
This commit is contained in:
parent
b13ff70a2d
commit
d2b91f2db4
4 changed files with 105 additions and 38 deletions
|
|
@ -3,6 +3,7 @@
|
|||
package fs
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
|
|
@ -10,10 +11,11 @@ import (
|
|||
)
|
||||
|
||||
type File struct {
|
||||
log logr.Logger
|
||||
f *os.File
|
||||
md *Metadata
|
||||
offset int64
|
||||
log logr.Logger
|
||||
f *os.File
|
||||
md *Metadata
|
||||
offset int64
|
||||
offline bool
|
||||
}
|
||||
|
||||
type preload struct {
|
||||
|
|
@ -38,23 +40,34 @@ func (p *preload) Read(data []byte) (n int, err error) {
|
|||
}
|
||||
|
||||
func (f *File) Preload(unlock func()) {
|
||||
f.log.V(2).Info("preload started")
|
||||
log := f.log
|
||||
if f.offline {
|
||||
log.V(2).Error(errors.New("no preload in offline mode"), "error preloading file")
|
||||
unlock()
|
||||
return
|
||||
}
|
||||
log.V(2).Info("preload started")
|
||||
p := &preload{f: f}
|
||||
_, err := io.Copy(io.Discard, p)
|
||||
if err != nil && err != io.EOF {
|
||||
f.log.Error(err, "error preloading file")
|
||||
log.Error(err, "error preloading file")
|
||||
}
|
||||
f.log.V(2).Info("preload finished", "skipped", p.skipped, "written", p.written)
|
||||
log.V(2).Info("preload finished", "skipped", p.skipped, "written", p.written)
|
||||
unlock()
|
||||
}
|
||||
|
||||
func (f *File) Read(p []byte) (n int, err error) {
|
||||
log := f.log
|
||||
if f.hasChunk(len(p)) {
|
||||
n, err = f.md.ReadAt(p, f.offset)
|
||||
if err != nil {
|
||||
log.Error(err, "error reading cache file")
|
||||
return
|
||||
}
|
||||
_, err = f.Seek(int64(n), io.SeekCurrent)
|
||||
if err != nil {
|
||||
log.Error(err, "error seeking source file")
|
||||
}
|
||||
return
|
||||
}
|
||||
return f.readWithoutCache(p)
|
||||
|
|
@ -69,8 +82,12 @@ func (f *File) hasChunk(n int) bool {
|
|||
}
|
||||
|
||||
func (f *File) readWithoutCache(p []byte) (n int, err error) {
|
||||
if f.offline {
|
||||
return 0, io.EOF
|
||||
}
|
||||
n, err = f.f.Read(p)
|
||||
if err != nil {
|
||||
f.log.Error(err, "error reading source file")
|
||||
return
|
||||
}
|
||||
f.md.WriteAt(p, f.offset)
|
||||
|
|
@ -82,6 +99,7 @@ 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
|
||||
}
|
||||
switch whence {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue