Files
acp/fs.go
T
Samuel Cui c3bcd856aa feat!: stream caller-owned items and reduce the content options
Replaces the streaming request/result API with caller-owned items and
consolidates the content options, following the Job execution performance
design.

- `Item`, `BatchSource` and `Run` replace `StreamRequest`, `StreamResult`,
  `StreamSource`, `StreamSink` and `RunStream`. Every accepted item receives
  exactly one terminal callback from a single goroutine and without doing
  I/O; an item ACP cannot process reports `Failed`, and a completion carries
  one outcome per requested target in request order with its error identity
  preserved. A graceful stop stops feeding, finishes the items in flight and
  reports the rest, and a hard stop abandons per-item accounting.
- One `HashPolicy` replaces `WithHash`, `WithSignatureCache` and
  `ForceRehash`. A stored hash is read whenever the policy uses the cache,
  reused only where the policy allows it, and refreshed only when it differs.
- A source-only `WithReadMode` selects buffered (the default) or mapped
  reads, and buffered sources open with `O_NOATIME` where the platform has
  it. `LinearDevice` keeps request ordering, append targets and abort-on-error
  but no longer selects the read mode.
- `Overwrite` becomes a target-device option, `WithReadBuffer` sets the read
  buffer, and `SelectFiles` returns the entries the command line copies,
  replacing `AccurateJob`, `WildcardJob` and the walk options.
- Copy consumers share one reference-counted chunk buffer each from a common
  pool, and the per-item write goroutines finish before the result channel
  closes, so no finished item can be dropped.

The command line tools, the documentation and the test suite move with the
API, including semantic tests for the callback contract, the stop and drain
paths, the hash policy matrix and the read modes.
2026-09-19 02:03:34 +08:00

73 lines
1.6 KiB
Go

package acp
import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
mapset "github.com/deckarep/golang-set/v2"
"github.com/moby/sys/mountinfo"
)
func getMountpointCache() (func(string) string, error) {
mounts, err := mountinfo.GetMounts(nil)
if err != nil {
return nil, fmt.Errorf("get mounts fail, %w", err)
}
mountPoints := mapset.NewThreadUnsafeSet[string]()
for _, mount := range mounts {
if mount == nil {
continue
}
if mount.Mountpoint == "" {
continue
}
mountPoints.Add(filepath.Clean(mount.Mountpoint))
}
mps := mountPoints.ToSlice()
return Cache(func(path string) string {
path, err := filepath.Abs(path)
if err != nil {
panic(fmt.Errorf("get abs from file path failed, path= '%s', %w", path, err))
}
return findMountpoint(path, mps)
}), nil
}
// openSource opens a source for buffered reading without updating its access time.
// O_NOATIME needs ownership or privilege, so a refused open falls back to an ordinary one.
func openSource(path string) (*os.File, error) {
file, err := os.OpenFile(path, os.O_RDONLY|openNoAtime, 0)
if err == nil || !errors.Is(err, fs.ErrPermission) {
return file, err
}
return os.Open(path)
}
func findMountpoint(path string, mountPoints []string) string {
matched := ""
for _, mountPoint := range mountPoints {
if path != mountPoint {
prefix := mountPoint
if !strings.HasSuffix(prefix, string(filepath.Separator)) {
prefix += string(filepath.Separator)
}
if !strings.HasPrefix(path, prefix) {
continue
}
}
if len(mountPoint) > len(matched) {
matched = mountPoint
}
}
return matched
}