initial commit
This commit is contained in:
commit
3053f89410
39 changed files with 4567 additions and 0 deletions
52
pkg/fs/metadata.go
Normal file
52
pkg/fs/metadata.go
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
// Copyright (C) 2022 Marius Schellenberger
|
||||
|
||||
package fs
|
||||
|
||||
import (
|
||||
"os"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type Metadata struct {
|
||||
mu sync.RWMutex `json:"-"`
|
||||
f *os.File `json:"-"`
|
||||
Size int64 `json:"s"`
|
||||
Chunks Chunks `json:"c"`
|
||||
preload bool `json:"-"`
|
||||
}
|
||||
|
||||
func (md *Metadata) HasChunk(off int64, n int) bool {
|
||||
md.mu.RLock()
|
||||
defer md.mu.RUnlock()
|
||||
return md.Chunks.Exists(off, n, md.Size)
|
||||
}
|
||||
|
||||
func (md *Metadata) AddChunk(off int64, n int) {
|
||||
md.mu.Lock()
|
||||
defer md.mu.Unlock()
|
||||
md.Chunks.Add(off, n)
|
||||
}
|
||||
|
||||
func (md *Metadata) ReadAt(p []byte, pos int64) (int, error) {
|
||||
return md.f.ReadAt(p, pos)
|
||||
}
|
||||
|
||||
func (md *Metadata) WriteAt(data []byte, pos int64) (int, error) {
|
||||
return md.f.WriteAt(data, pos)
|
||||
}
|
||||
|
||||
func (md *Metadata) Preload() bool {
|
||||
md.mu.Lock()
|
||||
defer md.mu.Unlock()
|
||||
if md.preload {
|
||||
return true
|
||||
}
|
||||
md.preload = true
|
||||
return false
|
||||
}
|
||||
|
||||
func (md *Metadata) UnlockPreload() {
|
||||
md.mu.Lock()
|
||||
defer md.mu.Unlock()
|
||||
md.preload = false
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue