2023-01-14 23:51:01 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2023-01-15 03:09:57 +00:00
|
|
|
"sync"
|
2023-01-15 20:17:30 +00:00
|
|
|
|
|
|
|
"github.com/aspect-build/bazel-lib/tools/common"
|
2023-01-14 23:51:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type pathSet map[string]bool
|
|
|
|
|
|
|
|
var srcPaths = pathSet{}
|
2023-01-15 03:09:57 +00:00
|
|
|
var hardlink = false
|
|
|
|
var verbose = false
|
2023-01-14 23:51:01 +00:00
|
|
|
|
2023-02-01 20:30:42 +00:00
|
|
|
type walker struct {
|
|
|
|
queue chan<- common.CopyOpts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *walker) copyDir(src string, dst string) error {
|
2023-01-14 23:51:01 +00:00
|
|
|
// filepath.WalkDir walks the file tree rooted at root, calling fn for each file or directory in
|
|
|
|
// the tree, including root. See https://pkg.go.dev/path/filepath#WalkDir for more info.
|
|
|
|
return filepath.WalkDir(src, func(p string, dirEntry fs.DirEntry, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-16 23:09:18 +00:00
|
|
|
r, err := common.FileRel(src, p)
|
2023-01-14 23:51:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d := filepath.Join(dst, r)
|
|
|
|
|
|
|
|
if dirEntry.IsDir() {
|
|
|
|
srcPaths[src] = true
|
2023-01-15 03:09:57 +00:00
|
|
|
return os.MkdirAll(d, os.ModePerm)
|
2023-01-15 20:17:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
info, err := dirEntry.Info()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.Mode()&os.ModeSymlink == os.ModeSymlink {
|
|
|
|
// symlink to directories are intentionally never followed by filepath.Walk to avoid infinite recursion
|
2023-01-17 05:46:50 +00:00
|
|
|
linkPath, err := common.Realpath(p)
|
2023-01-14 23:51:01 +00:00
|
|
|
if err != nil {
|
2024-05-28 16:42:06 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return fmt.Errorf("failed to get realpath of dangling symlink %s: %w", p, err)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("failed to get realpath of %s: %w", p, err)
|
2023-01-14 23:51:01 +00:00
|
|
|
}
|
2023-01-15 20:17:30 +00:00
|
|
|
if srcPaths[linkPath] {
|
|
|
|
// recursive symlink; silently ignore
|
|
|
|
return nil
|
2023-01-15 03:09:57 +00:00
|
|
|
}
|
2023-01-15 20:17:30 +00:00
|
|
|
stat, err := os.Stat(linkPath)
|
2023-01-15 03:09:57 +00:00
|
|
|
if err != nil {
|
2023-01-15 20:17:30 +00:00
|
|
|
return fmt.Errorf("failed to stat file %s pointed to by symlink %s: %w", linkPath, p, err)
|
|
|
|
}
|
|
|
|
if stat.IsDir() {
|
|
|
|
// symlink points to a directory
|
2023-02-01 20:30:42 +00:00
|
|
|
return w.copyDir(linkPath, d)
|
2023-01-15 20:17:30 +00:00
|
|
|
} else {
|
|
|
|
// symlink points to a regular file
|
2023-02-01 20:30:42 +00:00
|
|
|
w.queue <- common.NewCopyOpts(linkPath, d, stat, hardlink, verbose)
|
2023-01-15 20:17:30 +00:00
|
|
|
return nil
|
2023-01-15 03:09:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-15 20:17:30 +00:00
|
|
|
// a regular file
|
2023-02-01 20:30:42 +00:00
|
|
|
w.queue <- common.NewCopyOpts(p, d, info, hardlink, verbose)
|
2023-01-15 20:17:30 +00:00
|
|
|
return nil
|
|
|
|
})
|
2023-01-14 23:51:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
args := os.Args[1:]
|
|
|
|
|
|
|
|
if len(args) < 2 {
|
2023-01-15 20:17:30 +00:00
|
|
|
fmt.Println("Usage: copy_directory src dst [--hardlink] [--verbose]")
|
2023-01-14 23:51:01 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
src := args[0]
|
|
|
|
dst := args[1]
|
|
|
|
|
|
|
|
if len(args) > 2 {
|
|
|
|
for _, a := range os.Args[2:] {
|
|
|
|
if a == "--hardlink" {
|
|
|
|
hardlink = true
|
|
|
|
} else if a == "--verbose" {
|
|
|
|
verbose = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-01 20:30:42 +00:00
|
|
|
queue := make(chan common.CopyOpts, 100)
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
const numWorkers = 10
|
|
|
|
wg.Add(numWorkers)
|
|
|
|
for i := 0; i < numWorkers; i++ {
|
|
|
|
go common.NewCopyWorker(queue).Run(&wg)
|
|
|
|
}
|
|
|
|
|
|
|
|
walker := &walker{queue}
|
|
|
|
if err := walker.copyDir(src, dst); err != nil {
|
2023-01-14 23:51:01 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2023-02-01 20:30:42 +00:00
|
|
|
close(queue)
|
|
|
|
wg.Wait()
|
2023-01-14 23:51:01 +00:00
|
|
|
}
|