File: writer.go

package info (click to toggle)
golang-github-karpeleslab-reflink 1.0.1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 120 kB
  • sloc: makefile: 13
file content (39 lines) | stat: -rw-r--r-- 855 bytes parent folder | download
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
package reflink

import (
	"errors"
	"io"
)

// sectionWriter is a helper used when we need to fallback into copying data manually
type sectionWriter struct {
	w    io.WriterAt // target file
	base int64       // base position in file
	off  int64       // current relative offset
}

// Write writes & updates offset
func (s *sectionWriter) Write(p []byte) (int, error) {
	n, err := s.w.WriteAt(p, s.base+s.off)
	s.off += int64(n)
	return n, err
}

func (s *sectionWriter) Seek(offset int64, whence int) (int64, error) {
	switch whence {
	case io.SeekStart:
		// nothing needed
	case io.SeekCurrent:
		offset += s.off
	case io.SeekEnd:
		// we don't support io.SeekEnd
		fallthrough
	default:
		return s.off, errors.New("Seek: invalid whence")
	}
	if offset < 0 {
		return s.off, errors.New("Seek: invalid offset")
	}
	s.off = offset
	return offset, nil
}