* Use 32KB buffer for `copyFile()` (reduces copy time by 30%) This makes `:paste` comparable in performance to `cp --reflink=never` for large files. For large number of small files improvements are less substantial (compared to `cp -r --reflink=never`), though still noticeable. In both cases the copy takes about 30% less time than with `buf` size at 4096. 32KB is the same number `io.Copy()` uses internally (when it can), and is about where improvements stop. Context: https://github.com/gokcehan/lf/issues/1685#issuecomment-2163934296 * Apply suggested changes * Just delete incomplete an file on all errors * Mention new progress UI update frequency * Return the old (each 4MB copied) progress update frequency (progress UI updates don't seem to impact performance much)
168 lines
3.6 KiB
Go
168 lines
3.6 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/djherbis/times"
|
|
)
|
|
|
|
type ProgressWriter struct {
|
|
writer io.Writer
|
|
nums chan<- int64
|
|
}
|
|
|
|
func NewProgressWriter(writer io.Writer, nums chan<- int64) *ProgressWriter {
|
|
return &ProgressWriter{
|
|
writer: writer,
|
|
nums: nums,
|
|
}
|
|
}
|
|
|
|
func (progressWriter *ProgressWriter) Write(b []byte) (int, error) {
|
|
n, err := progressWriter.writer.Write(b)
|
|
progressWriter.nums <- int64(n)
|
|
return n, err
|
|
}
|
|
|
|
func copySize(srcs []string) (int64, error) {
|
|
var total int64
|
|
|
|
for _, src := range srcs {
|
|
_, err := os.Lstat(src)
|
|
if os.IsNotExist(err) {
|
|
return total, fmt.Errorf("src does not exist: %q", src)
|
|
}
|
|
|
|
err = filepath.Walk(src, func(_ string, info os.FileInfo, err error) error {
|
|
if err != nil {
|
|
return fmt.Errorf("walk: %s", err)
|
|
}
|
|
total += info.Size()
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return total, err
|
|
}
|
|
}
|
|
|
|
return total, nil
|
|
}
|
|
|
|
func copyFile(src, dst string, preserve []string, info os.FileInfo, nums chan int64) error {
|
|
var dst_mode os.FileMode = 0o666
|
|
preserve_timestamps := false
|
|
for _, s := range preserve {
|
|
switch s {
|
|
case "timestamps":
|
|
preserve_timestamps = true
|
|
case "mode":
|
|
dst_mode = info.Mode()
|
|
}
|
|
}
|
|
|
|
r, err := os.Open(src)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer r.Close()
|
|
|
|
w, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, dst_mode)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = io.Copy(NewProgressWriter(w, nums), r)
|
|
if err != nil {
|
|
w.Close()
|
|
os.Remove(dst)
|
|
return err
|
|
}
|
|
|
|
if err := w.Close(); err != nil {
|
|
os.Remove(dst)
|
|
return err
|
|
}
|
|
|
|
if preserve_timestamps {
|
|
ts := times.Get(info)
|
|
mtime := info.ModTime()
|
|
atime := ts.AccessTime()
|
|
if err := os.Chtimes(dst, atime, mtime); err != nil {
|
|
os.Remove(dst)
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func copyAll(srcs []string, dstDir string, preserve []string) (nums chan int64, errs chan error) {
|
|
nums = make(chan int64, 1024)
|
|
errs = make(chan error, 1024)
|
|
|
|
go func() {
|
|
for _, src := range srcs {
|
|
file := filepath.Base(src)
|
|
dst := filepath.Join(dstDir, file)
|
|
|
|
lstat, err := os.Lstat(dst)
|
|
if !os.IsNotExist(err) {
|
|
ext := getFileExtension(lstat)
|
|
basename := file[:len(file)-len(ext)]
|
|
var newPath string
|
|
for i := 1; !os.IsNotExist(err); i++ {
|
|
file = strings.ReplaceAll(gOpts.dupfilefmt, "%f", basename+ext)
|
|
file = strings.ReplaceAll(file, "%b", basename)
|
|
file = strings.ReplaceAll(file, "%e", ext)
|
|
file = strings.ReplaceAll(file, "%n", strconv.Itoa(i))
|
|
newPath = filepath.Join(dstDir, file)
|
|
_, err = os.Lstat(newPath)
|
|
}
|
|
dst = newPath
|
|
}
|
|
|
|
filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
|
|
if err != nil {
|
|
errs <- fmt.Errorf("walk: %s", err)
|
|
return nil
|
|
}
|
|
rel, err := filepath.Rel(src, path)
|
|
if err != nil {
|
|
errs <- fmt.Errorf("relative: %s", err)
|
|
return nil
|
|
}
|
|
newPath := filepath.Join(dst, rel)
|
|
if info.IsDir() {
|
|
if err := os.MkdirAll(newPath, info.Mode()); err != nil {
|
|
errs <- fmt.Errorf("mkdir: %s", err)
|
|
}
|
|
nums <- info.Size()
|
|
} else if info.Mode()&os.ModeSymlink != 0 { /* Symlink */
|
|
if rlink, err := os.Readlink(path); err != nil {
|
|
errs <- fmt.Errorf("symlink: %s", err)
|
|
} else {
|
|
if err := os.Symlink(rlink, newPath); err != nil {
|
|
errs <- fmt.Errorf("symlink: %s", err)
|
|
}
|
|
}
|
|
nums <- info.Size()
|
|
} else {
|
|
if err := copyFile(path, newPath, preserve, info, nums); err != nil {
|
|
errs <- fmt.Errorf("copy: %s", err)
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
close(errs)
|
|
}()
|
|
|
|
return nums, errs
|
|
}
|