How otiai10/copy works
Project type and purpose
• Project type: Go library/package for recursively copying directories/files with rich options.
• Purpose: Expose a high-level Copy(src, dst, opts) API that drives a pluggable, concurrent file‐copy engine preserving metadata, handling symlinks, errors, skips, etc.
File structure
• Top-level Go sources:
– copy.go: orchestration entrypoint (Copy, CopyWithOptions)
– options.go: definition of Options struct and default option values
– permission_control.go: strategy for setting file permissions
– preserve_*.go, stat_times_*.go: OS/platform‐specific metadata‐preservation logic
– copy_namedpipes.go, copy_namedpipes_x.go: named-pipe support
• Tests under root (*_test.go) and test data under test/data/*: drive various copy scenarios.
• CI workflows in .github/workflows/: go build/test, coverage, Docker/Vagrant, WASM, GopherJS.
Readme insights
• Public API surface: Copy(src, dst) plus Options for callbacks and flags.
• Key features: OnError, Skip, OnSymlink, OnDirExists, PermissionControl, PreserveTimes, PreserveOwner, concurrency control.
Main components & relationships
a. Public API Layer
– User code depends on this.
b. Core Copy Engine
– Component: “Engine/Orchestrator”
• Walks source tree (recursive or concurrent).
• For each entry, delegates to either “CopyFile”, “CopyDir”, or “HandleSymlink”.
• Applies buffering (CopyBufferSize), Sync, concurrency limiter.
c. Strategy Modules (injection points from Options)
– OnError handler
– Skip predicate
– OnSymlink action
– OnDirExists action
– RenameDestination hook
– PermissionControl hook
– PreferConcurrent decision
d. Filesystem Abstraction
– Default: OS filesystem via os and io packages.
– Optional: user-provided fs.FS.
– Named pipes: special handlers.
e. Metadata Preservation
– Submodules in preserve_*.go & stat_times_*.go
– Platform‐specific implementations (darwin, freebsd, js, windows, x)
f. Concurrency Control
– Uses golang.org/x/sync/semaphore when NumOfWorkers > 1
– PreferConcurrent determines per-directory parallelism
g. Testing Harness
– Unit tests (*_test.go)
– Integration tests under test/data/...
– Mock FS (embed) examples
h. CI/CD
– GitHub Actions workflows
– CodeQL, coverage, Docker/Vagrant/GopherJS/WASM builds
– Public API → Engine
– Engine → FS Abstraction (“ReadDir”, “OpenFile”, “CreateFile”)
– Engine → Metadata Preservation (calls to preserveTimes, preserveOwner, permission_control)
– Engine → Concurrency Controller (acquire/release semaphore)
– FS Abstraction → OS / Physical FS
– Optionally: Engine → Tests (for visualizing coverage of behavior)
Architectural patterns
– Strategy Pattern: Options struct injects behavior at runtime
– Decorator/Wrapper: Wrapping readers via WrapReader
– Adapter: fs.FS adapter for embedded filesystems
– Concurrency Limiting via semaphore
– Have you captured every major Go package/file concern?
– Are all injection points (Options) clearly shown?
– Have you annotated platform-specific modules?