2023-01-15 20:17:30 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/fs"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"sync"
|
2024-08-11 05:08:56 +00:00
|
|
|
"time"
|
2023-01-15 20:17:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// From https://opensource.com/article/18/6/copying-files-go
|
|
|
|
func CopyFile(src string, dst string) error {
|
|
|
|
source, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer source.Close()
|
|
|
|
|
|
|
|
destination, err := os.Create(dst)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer destination.Close()
|
|
|
|
_, err = io.Copy(destination, source)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-01 20:30:42 +00:00
|
|
|
func Copy(opts CopyOpts) {
|
2024-08-11 05:08:56 +00:00
|
|
|
if !opts.srcInfo.Mode().IsRegular() {
|
2023-02-01 20:30:42 +00:00
|
|
|
log.Fatalf("%s is not a regular file", opts.src)
|
2023-01-15 20:17:30 +00:00
|
|
|
}
|
2024-08-08 23:29:47 +00:00
|
|
|
|
|
|
|
opModifier := ""
|
|
|
|
const fallback = " (fallback)"
|
|
|
|
|
2023-02-01 20:30:42 +00:00
|
|
|
if opts.hardlink {
|
2023-01-15 20:17:30 +00:00
|
|
|
// hardlink this file
|
2023-02-01 20:30:42 +00:00
|
|
|
if opts.verbose {
|
2024-08-08 23:29:47 +00:00
|
|
|
fmt.Printf("hardlink%s %v => %v\n", opModifier, opts.src, opts.dst)
|
2023-01-15 20:17:30 +00:00
|
|
|
}
|
2023-02-01 20:30:42 +00:00
|
|
|
err := os.Link(opts.src, opts.dst)
|
2023-01-15 20:17:30 +00:00
|
|
|
if err != nil {
|
2023-02-01 20:30:42 +00:00
|
|
|
if opts.verbose {
|
2023-01-15 20:17:30 +00:00
|
|
|
fmt.Printf("hardlink failed: %v\n", err)
|
2024-08-08 23:29:47 +00:00
|
|
|
opModifier = fallback
|
2023-01-15 20:17:30 +00:00
|
|
|
}
|
2024-08-08 23:29:47 +00:00
|
|
|
// fallback to clonefile or copy
|
|
|
|
} else {
|
|
|
|
return
|
2023-01-15 20:17:30 +00:00
|
|
|
}
|
2024-08-08 23:29:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// clone this file
|
|
|
|
if opts.verbose {
|
|
|
|
fmt.Printf("clonefile%s %v => %v\n", opModifier, opts.src, opts.dst)
|
|
|
|
}
|
|
|
|
switch supported, err := cloneFile(opts.src, opts.dst); {
|
|
|
|
case !supported:
|
2023-02-01 20:30:42 +00:00
|
|
|
if opts.verbose {
|
2024-08-08 23:29:47 +00:00
|
|
|
fmt.Print("clonefile skipped: not supported by platform\n")
|
2023-01-15 20:17:30 +00:00
|
|
|
}
|
2024-08-08 23:29:47 +00:00
|
|
|
// fallback to copy
|
|
|
|
case supported && err == nil:
|
|
|
|
return
|
|
|
|
case supported && err != nil:
|
|
|
|
if opts.verbose {
|
|
|
|
fmt.Printf("clonefile failed: %v\n", err)
|
|
|
|
opModifier = fallback
|
2023-01-15 20:17:30 +00:00
|
|
|
}
|
2024-08-08 23:29:47 +00:00
|
|
|
// fallback to copy
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy this file
|
|
|
|
if opts.verbose {
|
|
|
|
fmt.Printf("copy%s %v => %v\n", opModifier, opts.src, opts.dst)
|
|
|
|
}
|
|
|
|
err := CopyFile(opts.src, opts.dst)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2023-01-15 20:17:30 +00:00
|
|
|
}
|
2024-08-11 05:08:56 +00:00
|
|
|
|
|
|
|
if opts.preserveMTime {
|
|
|
|
accessTime := time.Now()
|
|
|
|
err := os.Chtimes(opts.dst, accessTime, opts.srcInfo.ModTime())
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2023-01-15 20:17:30 +00:00
|
|
|
}
|
2023-02-01 20:30:42 +00:00
|
|
|
|
|
|
|
type CopyWorker struct {
|
|
|
|
queue <-chan CopyOpts
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCopyWorker(queue <-chan CopyOpts) *CopyWorker {
|
|
|
|
return &CopyWorker{queue: queue}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *CopyWorker) Run(wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done()
|
|
|
|
for opts := range w.queue {
|
|
|
|
Copy(opts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type CopyOpts struct {
|
2024-08-11 05:08:56 +00:00
|
|
|
src, dst string
|
|
|
|
srcInfo fs.FileInfo
|
|
|
|
hardlink bool
|
|
|
|
verbose bool
|
|
|
|
preserveMTime bool
|
2023-02-01 20:30:42 +00:00
|
|
|
}
|
|
|
|
|
2024-08-11 05:08:56 +00:00
|
|
|
func NewCopyOpts(src string, dst string, srcInfo fs.FileInfo, hardlink bool, verbose bool, preserveMTime bool) CopyOpts {
|
|
|
|
return CopyOpts{src: src, dst: dst, srcInfo: srcInfo, hardlink: hardlink, verbose: verbose, preserveMTime: preserveMTime}
|
2023-02-01 20:30:42 +00:00
|
|
|
}
|