MCPcopy
hub / github.com/docker/compose / Sync

Method Sync

internal/sync/tar.go:67–121  ·  view source on GitHub ↗
(ctx context.Context, service string, paths []*PathMapping)

Source from the content-addressed store, hash-verified

65}
66
67func (t *Tar) Sync(ctx context.Context, service string, paths []*PathMapping) error {
68 containers, err := t.client.ContainersForService(ctx, t.projectName, service)
69 if err != nil {
70 return err
71 }
72
73 var pathsToCopy []PathMapping
74 var pathsToDelete []string
75 for _, p := range paths {
76 if _, err := os.Stat(p.HostPath); err == nil {
77 pathsToCopy = append(pathsToCopy, *p)
78 } else if errors.Is(err, fs.ErrNotExist) {
79 pathsToDelete = append(pathsToDelete, p.ContainerPath)
80 } else {
81 return fmt.Errorf("stat %q: %w", p.HostPath, err)
82 }
83 }
84
85 var deleteCmd []string
86 if len(pathsToDelete) != 0 {
87 deleteCmd = append([]string{"rm", "-rf"}, pathsToDelete...)
88 }
89
90 var (
91 eg errgroup.Group
92 errMu sync.Mutex
93 errs = make([]error, 0, len(containers)*2) // max 2 errs per container
94 )
95
96 eg.SetLimit(16) // arbitrary limit, adjust to taste :D
97 for i := range containers {
98 containerID := containers[i].ID
99 tarReader := tarArchive(pathsToCopy)
100
101 eg.Go(func() error {
102 if len(deleteCmd) != 0 {
103 if err := t.client.Exec(ctx, containerID, deleteCmd, nil); err != nil {
104 errMu.Lock()
105 errs = append(errs, fmt.Errorf("deleting paths in %s: %w", containerID, err))
106 errMu.Unlock()
107 }
108 }
109
110 if err := t.client.Untar(ctx, containerID, tarReader); err != nil {
111 errMu.Lock()
112 errs = append(errs, fmt.Errorf("copying files to %s: %w", containerID, err))
113 errMu.Unlock()
114 }
115 return nil // don't fail-fast; collect all errors
116 })
117 }
118
119 _ = eg.Wait()
120 return errors.Join(errs...)
121}
122
123type ArchiveBuilder struct {
124 tw *tar.Writer

Callers 4

TestSync_ExistingPathFunction · 0.95
TestSync_NonExistentPathFunction · 0.95
TestSync_MixedPathsFunction · 0.95

Calls 6

tarArchiveFunction · 0.85
ContainersForServiceMethod · 0.65
ExecMethod · 0.65
UntarMethod · 0.65
WaitMethod · 0.65
LockMethod · 0.45

Tested by 4

TestSync_ExistingPathFunction · 0.76
TestSync_NonExistentPathFunction · 0.76
TestSync_MixedPathsFunction · 0.76