Skip to content

Commit 89dcb85

Browse files
committed
Fix concurrent map write for hardlink
Fix GoogleContainerTools#323
1 parent bca8ca4 commit 89dcb85

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
out/*
2+
.idea/
3+
*.iml

pkg/util/tar_utils.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@ import (
2424
"path/filepath"
2525
"strings"
2626

27+
"sync"
28+
"sync/atomic"
29+
2730
"github.com/pkg/errors"
2831
"github.com/sirupsen/logrus"
2932
)
3033

31-
// Map of target:linkname
32-
var hardlinks = make(map[string]string)
34+
// Thread safe Map of target:linkname
35+
var hardlinks sync.Map
3336

3437
type OriginalPerm struct {
3538
path string
@@ -134,19 +137,26 @@ func unpackTar(tr *tar.Reader, path string, whitelist []string) error {
134137
// If it exists, create the hard link
135138
resolveHardlink(linkname, target)
136139
} else {
137-
hardlinks[target] = linkname
140+
hardlinks.Store(target, linkname)
138141
}
139142
}
140143
}
141-
142-
for target, linkname := range hardlinks {
144+
var resolveError atomic.Value
145+
hardlinks.Range(func(key, value interface{}) bool {
146+
target := key.(string)
147+
linkname := value.(string)
143148
logrus.Info("Resolving hard links")
144149
if _, err := os.Stat(linkname); !os.IsNotExist(err) {
145150
// If it exists, create the hard link
146151
if err := resolveHardlink(linkname, target); err != nil {
147-
return errors.Wrap(err, fmt.Sprintf("Unable to create hard link from %s to %s", linkname, target))
152+
resolveError.Store(errors.Wrap(err, fmt.Sprintf("Unable to create hard link from %s to %s", linkname, target)))
153+
return false
148154
}
149155
}
156+
return true
157+
})
158+
if resolveError.Load() != nil {
159+
return resolveError.Load().(error)
150160
}
151161

152162
// reset all original file

0 commit comments

Comments
 (0)