File: uniform.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 (63 lines) | stat: -rw-r--r-- 1,332 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package filecache

import (
	"io"
	"os"

	"github.com/anacrolix/missinggo/resource"
)

type uniformResourceProvider struct {
	*Cache
}

var _ resource.Provider = &uniformResourceProvider{}

func (me *uniformResourceProvider) NewInstance(loc string) (resource.Instance, error) {
	return &uniformResource{me.Cache, loc}, nil
}

type uniformResource struct {
	Cache    *Cache
	Location string
}

func (me *uniformResource) Get() (io.ReadCloser, error) {
	return me.Cache.OpenFile(me.Location, os.O_RDONLY)
}

func (me *uniformResource) Put(r io.Reader) (err error) {
	f, err := me.Cache.OpenFile(me.Location, os.O_WRONLY|os.O_CREATE|os.O_TRUNC)
	if err != nil {
		return
	}
	defer f.Close()
	_, err = io.Copy(f, r)
	return
}

func (me *uniformResource) ReadAt(b []byte, off int64) (n int, err error) {
	f, err := me.Cache.OpenFile(me.Location, os.O_RDONLY)
	if err != nil {
		return
	}
	defer f.Close()
	return f.ReadAt(b, off)
}

func (me *uniformResource) WriteAt(b []byte, off int64) (n int, err error) {
	f, err := me.Cache.OpenFile(me.Location, os.O_CREATE|os.O_WRONLY)
	if err != nil {
		return
	}
	defer f.Close()
	return f.WriteAt(b, off)
}

func (me *uniformResource) Stat() (fi os.FileInfo, err error) {
	return me.Cache.Stat(me.Location)
}

func (me *uniformResource) Delete() error {
	return me.Cache.Remove(me.Location)
}