status-go/vendor/github.com/anacrolix/missinggo/v2/resource/resource.go

51 lines
910 B
Go
Raw Normal View History

2022-03-10 09:44:48 +00:00
package resource
import (
"io"
"os"
)
// An Instance represents the content at some location accessed through some
// Provider. It's the data at some URL.
type Instance interface {
Get() (io.ReadCloser, error)
Put(io.Reader) error
Stat() (os.FileInfo, error)
io.ReaderAt
WriteAt([]byte, int64) (int, error)
Delete() error
}
type DirInstance interface {
Readdirnames() ([]string, error)
}
// Creates a io.ReadSeeker to an Instance.
func ReadSeeker(r Instance) io.ReadSeeker {
fi, err := r.Stat()
if err != nil {
return nil
}
return io.NewSectionReader(r, 0, fi.Size())
}
// Move instance content, deleting the source if it succeeds.
func Move(from, to Instance) (err error) {
rc, err := from.Get()
if err != nil {
return
}
defer rc.Close()
err = to.Put(rc)
if err != nil {
return
}
from.Delete()
return
}
func Exists(i Instance) bool {
_, err := i.Stat()
return err == nil
}