File: helper.go

package info (click to toggle)
docker.io 26.1.5%2Bdfsg1-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 68,576 kB
  • sloc: sh: 5,748; makefile: 912; ansic: 664; asm: 228; python: 162
file content (63 lines) | stat: -rw-r--r-- 943 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 iohelper

import (
	"io"
	"sync"

	"github.com/pkg/errors"
)

type NopWriteCloser struct {
	io.Writer
}

func (w *NopWriteCloser) Close() error {
	return nil
}

type ReadCloser struct {
	io.ReadCloser
	CloseFunc func() error
}

func (rc *ReadCloser) Close() error {
	err1 := rc.ReadCloser.Close()
	err2 := rc.CloseFunc()
	if err1 != nil {
		return errors.Wrapf(err1, "failed to close: %v", err2)
	}
	return err2
}

type WriteCloser struct {
	io.WriteCloser
	CloseFunc func() error
}

func (wc *WriteCloser) Close() error {
	err1 := wc.WriteCloser.Close()
	err2 := wc.CloseFunc()
	if err1 != nil {
		return errors.Wrapf(err1, "failed to close: %v", err2)
	}
	return err2
}

type Counter struct {
	n  int64
	mu sync.Mutex
}

func (c *Counter) Write(p []byte) (n int, err error) {
	c.mu.Lock()
	c.n += int64(len(p))
	c.mu.Unlock()
	return len(p), nil
}

func (c *Counter) Size() (n int64) {
	c.mu.Lock()
	n = c.n
	c.mu.Unlock()
	return
}