File: copy.go

package info (click to toggle)
golang-github-hanwen-go-fuse 0.0~git20190214.58dcd77-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,076 kB
  • sloc: cpp: 78; sh: 77; makefile: 16
file content (70 lines) | stat: -rw-r--r-- 1,247 bytes parent folder | download | duplicates (6)
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
65
66
67
68
69
70
// Copyright 2016 the Go-FUSE Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package splice

import (
	"io"
	"os"
)

func SpliceCopy(dst *os.File, src *os.File, p *Pair) (int64, error) {
	total := int64(0)

	for {
		n, err := p.LoadFrom(src.Fd(), p.size)
		if err != nil {
			return total, err
		}
		if n == 0 {
			break
		}
		m, err := p.WriteTo(dst.Fd(), n)
		total += int64(m)
		if err != nil {
			return total, err
		}
		if m < n {
			return total, err
		}
		if int(n) < p.size {
			break
		}
	}

	return total, nil
}

// Argument ordering follows io.Copy.
func CopyFile(dstName string, srcName string, mode int) error {
	src, err := os.Open(srcName)
	if err != nil {
		return err
	}
	defer src.Close()

	dst, err := os.OpenFile(dstName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.FileMode(mode))
	if err != nil {
		return err
	}
	defer dst.Close()

	return CopyFds(dst, src)
}

func CopyFds(dst *os.File, src *os.File) (err error) {
	p, err := splicePool.get()
	if p != nil {
		p.Grow(256 * 1024)
		_, err := SpliceCopy(dst, src, p)
		splicePool.done(p)
		return err
	} else {
		_, err = io.Copy(dst, src)
	}
	if err == io.EOF {
		err = nil
	}
	return err
}