File: resource.go

package info (click to toggle)
golang-github-anacrolix-missinggo 2.1.0-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 872 kB
  • sloc: makefile: 4
file content (46 lines) | stat: -rw-r--r-- 867 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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)
	ReadAt([]byte, int64) (int, error)
	WriteAt([]byte, int64) (int, error)
	Delete() 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
}