-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy_other.go
40 lines (35 loc) · 893 Bytes
/
copy_other.go
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
// copy_other.go - non-Linux file copy
//
// (c) 2021 Sudhi Herle <sudhi@herle.net>
//
// Licensing Terms: GPLv2
//
// If you need a commercial license for this work, please contact
// the author.
//
// This software does not come with any express or implied
// warranty; it is provided "as is". No claim is made to its
// suitability for any purpose.
//go:build !linux
package utils
import (
"fmt"
"os"
"github.com/opencoff/go-mmap"
)
// TODO macOS, FreeBSD optimizations
// Use mmap(2) to copy src to dst.
func copyFile(dst, src *os.File) error {
_, err := mmap.Reader(src, func(b []byte) error {
_, err := fullWrite(dst, b)
return err
})
if err != nil {
return fmt.Errorf("safefile: can't read %s: %w", src.Name(), err)
}
_, err = dst.Seek(0, os.SEEK_SET)
if err != nil {
return fmt.Errorf("safefile: can't seek to start of %s: %w", dst.Name(), err)
}
return nil
}