summaryrefslogtreecommitdiff
path: root/common/copy.go
blob: bee6515acfbe903a353f3a4d0b19436fba2157e3 (plain)
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
64
package common

/*
Copied from https://ixday.github.io/post/golang-cancel-copy/
*/

import (
	"context"
	"io"
	"os"
)

// CopyToFileWithBuffer copies data from io.Reader to os.File with given buffer and read chunk size.
// The reader and file won't be closed.
// If syncFile is set, the file will be synced after every read.
func CopyToFileWithBuffer(
	ctx context.Context,
	out *os.File,
	in io.Reader,
	buffer []byte,
	chunkSize int,
	syncFile bool,
) (written int64, err error) {
	bufSize := len(buffer)
	off := 0   // offset to the end of data in buffer
	nRead := 0 // how many bytes were read in the last read
	defer func() {
		if off+nRead > 0 {
			// write unwritten data in buffer
			nWrite, _ := out.Write(buffer[:off+nRead])
			written += int64(nWrite)
			if syncFile {
				_ = out.Sync()
			}
		}
	}()

	for {
		if err = ctx.Err(); err != nil {
			return
		}
		nRead, err = in.Read(buffer[off:Min[int](off+chunkSize, bufSize)])
		if err != nil {
			return
		}
		off += nRead
		if off == bufSize {
			// buffer is full
			var nWritten int
			nWritten, err = out.Write(buffer)
			if err != nil {
				return
			}
			if syncFile {
				err = out.Sync()
				if err != nil {
					return
				}
			}
			written += int64(nWritten)
			off = 0
		}
	}
}