Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: use io.Copy because go supports CopyFileRange #227

Merged
merged 1 commit into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions fs/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,13 @@ package fs

import (
"fmt"
"io"
"os"
"path/filepath"
"sync"

"github.com/sirupsen/logrus"
)

var bufferPool = &sync.Pool{
New: func() interface{} {
buffer := make([]byte, 32*1024)
return &buffer
},
}

// XAttrErrorHandler transform a non-nil xattr error.
// Return nil to ignore an error.
// xattrKey can be empty for listxattr operation.
Expand Down Expand Up @@ -199,5 +192,6 @@ func openAndCopyFile(target, source string) error {
}
defer tgt.Close()

return copyFileContent(tgt, src)
_, err = io.Copy(tgt, src)
return err
}
46 changes: 0 additions & 46 deletions fs/copy_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package fs

import (
"fmt"
"io"
"os"
"syscall"

Expand Down Expand Up @@ -62,51 +61,6 @@ func copyFileInfo(fi os.FileInfo, src, name string) error {
return nil
}

const maxSSizeT = int64(^uint(0) >> 1)

func copyFileContent(dst, src *os.File) error {
st, err := src.Stat()
if err != nil {
return fmt.Errorf("unable to stat source: %w", err)
}

size := st.Size()
first := true
srcFd := int(src.Fd())
dstFd := int(dst.Fd())

for size > 0 {
// Ensure that we are never trying to copy more than SSIZE_MAX at a
// time and at the same time avoids overflows when the file is larger
// than 4GB on 32-bit systems.
var copySize int
if size > maxSSizeT {
copySize = int(maxSSizeT)
} else {
copySize = int(size)
}
n, err := unix.CopyFileRange(srcFd, nil, dstFd, nil, copySize, 0)
if err != nil {
if (err != unix.ENOSYS && err != unix.EXDEV) || !first {
return fmt.Errorf("copy file range failed: %w", err)
}

buf := bufferPool.Get().(*[]byte)
_, err = io.CopyBuffer(dst, src, *buf)
bufferPool.Put(buf)
if err != nil {
return fmt.Errorf("userspace copy failed: %w", err)
}
return nil
}

first = false
size -= int64(n)
}

return nil
}

func copyXAttrs(dst, src string, excludes map[string]struct{}, errorHandler XAttrErrorHandler) error {
xattrKeys, err := sysx.LListxattr(src)
if err != nil {
Expand Down
9 changes: 0 additions & 9 deletions fs/copy_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package fs

import (
"fmt"
"io"
"os"
"runtime"
"syscall"
Expand Down Expand Up @@ -61,14 +60,6 @@ func copyFileInfo(fi os.FileInfo, src, name string) error {
return nil
}

func copyFileContent(dst, src *os.File) error {
buf := bufferPool.Get().(*[]byte)
_, err := io.CopyBuffer(dst, src, *buf)
bufferPool.Put(buf)

return err
}

func copyXAttrs(dst, src string, excludes map[string]struct{}, errorHandler XAttrErrorHandler) error {
xattrKeys, err := sysx.LListxattr(src)
if err != nil {
Expand Down
8 changes: 0 additions & 8 deletions fs/copy_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package fs
import (
"errors"
"fmt"
"io"
"os"

winio "github.com/Microsoft/go-winio"
Expand Down Expand Up @@ -72,13 +71,6 @@ func copyFileInfo(fi os.FileInfo, src, name string) error {
return nil
}

func copyFileContent(dst, src *os.File) error {
buf := bufferPool.Get().(*[]byte)
_, err := io.CopyBuffer(dst, src, *buf)
bufferPool.Put(buf)
return err
}

func copyXAttrs(dst, src string, excludes map[string]struct{}, errorHandler XAttrErrorHandler) error {
return nil
}
Expand Down