Skip to content
This repository was archived by the owner on Mar 27, 2024. It is now read-only.

Fix concurrent map write for hardlink #324

Merged
merged 1 commit into from
Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
out/*
.idea/
*.iml
22 changes: 16 additions & 6 deletions pkg/util/tar_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ import (
"path/filepath"
"strings"

"sync"
"sync/atomic"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

// Map of target:linkname
var hardlinks = make(map[string]string)
// Thread safe Map of target:linkname
var hardlinks sync.Map

type OriginalPerm struct {
path string
Expand Down Expand Up @@ -134,19 +137,26 @@ func unpackTar(tr *tar.Reader, path string, whitelist []string) error {
// If it exists, create the hard link
resolveHardlink(linkname, target)
} else {
hardlinks[target] = linkname
hardlinks.Store(target, linkname)
}
}
}

for target, linkname := range hardlinks {
var resolveError atomic.Value
hardlinks.Range(func(key, value interface{}) bool {
target := key.(string)
linkname := value.(string)
logrus.Info("Resolving hard links")
if _, err := os.Stat(linkname); !os.IsNotExist(err) {
// If it exists, create the hard link
if err := resolveHardlink(linkname, target); err != nil {
return errors.Wrap(err, fmt.Sprintf("Unable to create hard link from %s to %s", linkname, target))
resolveError.Store(errors.Wrap(err, fmt.Sprintf("Unable to create hard link from %s to %s", linkname, target)))
return false
}
}
return true
})
if resolveError.Load() != nil {
return resolveError.Load().(error)
}

// reset all original file
Expand Down