Fixup config protect

This commit is contained in:
Ettore Di Giacinto 2021-10-24 13:01:51 +02:00
parent 6ff22d923c
commit 52ad2b5cfa
4 changed files with 27 additions and 23 deletions

View File

@ -53,7 +53,7 @@ func NewConfigProtect(annotationDir string) *ConfigProtect {
} }
return &ConfigProtect{ return &ConfigProtect{
AnnotationDir: annotationDir, AnnotationDir: annotationDir,
MapProtected: make(map[string]bool, 0), MapProtected: make(map[string]bool),
} }
} }
@ -97,7 +97,7 @@ func (c *ConfigProtect) Protected(file string) bool {
func (c *ConfigProtect) GetProtectFiles(withSlash bool) []string { func (c *ConfigProtect) GetProtectFiles(withSlash bool) []string {
ans := []string{} ans := []string{}
for key, _ := range c.MapProtected { for key := range c.MapProtected {
if withSlash { if withSlash {
ans = append(ans, key) ans = append(ans, key)
} else { } else {

View File

@ -47,16 +47,21 @@ func ExtractDeltaFiles(
excludeRegexp := compileRegexes(excludes) excludeRegexp := compileRegexes(excludes)
return func(h *tar.Header) (bool, error) { return func(h *tar.Header) (bool, error) {
fileName := filepath.Join(string(os.PathSeparator), h.Name)
ctx.Debug("Includes", includes)
ctx.Debug("Excludes", excludes)
ctx.Debug("Additions", d.Additions)
switch { switch {
case len(includes) == 0 && len(excludes) != 0: case len(includes) == 0 && len(excludes) != 0:
for _, a := range d.Additions { for _, a := range d.Additions {
if h.Name == a.Name { if h.Name == a.Name {
for _, i := range excludeRegexp { for _, i := range excludeRegexp {
if i.MatchString(a.Name) && h.Name == a.Name { if i.MatchString(filepath.Join(string(os.PathSeparator), a.Name)) &&
fileName == filepath.Join(string(os.PathSeparator), a.Name) {
return false, nil return false, nil
} }
} }
ctx.Debug("Adding name", h.Name) ctx.Debug("Adding name", fileName)
return true, nil return true, nil
} }
@ -65,8 +70,8 @@ func ExtractDeltaFiles(
case len(includes) > 0 && len(excludes) == 0: case len(includes) > 0 && len(excludes) == 0:
for _, a := range d.Additions { for _, a := range d.Additions {
for _, i := range includeRegexp { for _, i := range includeRegexp {
if i.MatchString(a.Name) && h.Name == a.Name { if i.MatchString(filepath.Join(string(os.PathSeparator), a.Name)) && fileName == filepath.Join(string(os.PathSeparator), a.Name) {
ctx.Debug("Adding name", h.Name) ctx.Debug("Adding name", fileName)
return true, nil return true, nil
} }
@ -76,13 +81,13 @@ func ExtractDeltaFiles(
case len(includes) != 0 && len(excludes) != 0: case len(includes) != 0 && len(excludes) != 0:
for _, a := range d.Additions { for _, a := range d.Additions {
for _, i := range includeRegexp { for _, i := range includeRegexp {
if i.MatchString(a.Name) && h.Name == a.Name { if i.MatchString(filepath.Join(string(os.PathSeparator), a.Name)) && fileName == filepath.Join(string(os.PathSeparator), a.Name) {
for _, e := range excludeRegexp { for _, e := range excludeRegexp {
if e.MatchString(a.Name) { if e.MatchString(fileName) {
return false, nil return false, nil
} }
} }
ctx.Debug("Adding name", h.Name) ctx.Debug("Adding name", fileName)
return true, nil return true, nil
} }
@ -91,8 +96,8 @@ func ExtractDeltaFiles(
return false, nil return false, nil
default: default:
for _, a := range d.Additions { for _, a := range d.Additions {
if h.Name == a.Name { if fileName == filepath.Join(string(os.PathSeparator), a.Name) {
ctx.Debug("Adding name", h.Name) ctx.Debug("Adding name", fileName)
return true, nil return true, nil
} }

View File

@ -419,7 +419,7 @@ func replaceFileTarWrapper(dst string, inputTarStream io.ReadCloser, mods []stri
} }
return nil return nil
} }
var remaining []string // var remaining []string
var err error var err error
var originalHeader *tar.Header var originalHeader *tar.Header
for { for {
@ -432,7 +432,7 @@ func replaceFileTarWrapper(dst string, inputTarStream io.ReadCloser, mods []stri
return return
} }
if helpers.Contains(mods, originalHeader.Name) { if !helpers.Contains(mods, originalHeader.Name) {
// No modifiers for this file, copy the header and data // No modifiers for this file, copy the header and data
if err := tarWriter.WriteHeader(originalHeader); err != nil { if err := tarWriter.WriteHeader(originalHeader); err != nil {
pipeWriter.CloseWithError(err) pipeWriter.CloseWithError(err)
@ -442,7 +442,6 @@ func replaceFileTarWrapper(dst string, inputTarStream io.ReadCloser, mods []stri
pipeWriter.CloseWithError(err) pipeWriter.CloseWithError(err)
return return
} }
remaining = append(remaining, originalHeader.Name)
continue continue
} }
@ -453,12 +452,6 @@ func replaceFileTarWrapper(dst string, inputTarStream io.ReadCloser, mods []stri
} }
// Apply the modifiers that haven't matched any files in the archive // Apply the modifiers that haven't matched any files in the archive
for _, name := range remaining {
if err := modify(name, nil, nil); err != nil {
pipeWriter.CloseWithError(err)
return
}
}
pipeWriter.Close() pipeWriter.Close()
@ -501,11 +494,15 @@ func tarModifierWrapperFunc(ctx *types.Context) func(dst, path string, header *t
} }
} }
ctx.Debug("Existing file hash: ", existingHash, "Tar file hashsum: ", tarHash) ctx.Debug(destPath, "- existing file hash: ", existingHash, "Tar file hashsum: ", tarHash)
if fileHelper.Exists(destPath) {
ctx.Debug(destPath, "already exists")
}
// We want to protect file only if the hash of the files are differing OR the file size are // We want to protect file only if the hash of the files are differing OR the file size are
differs := (existingHash != "" && existingHash != tarHash) || (err != nil && f != nil && header.Size != f.Size()) differs := (existingHash != "" && existingHash != tarHash) || (err != nil && f != nil && header.Size != f.Size())
// Check if exists // Check if exists
if fileHelper.Exists(destPath) && differs { if fileHelper.Exists(destPath) && differs {
ctx.Debug(destPath, "already exists and differs")
for i := 1; i < 1000; i++ { for i := 1; i < 1000; i++ {
name := filepath.Join(filepath.Join(filepath.Dir(path), name := filepath.Join(filepath.Join(filepath.Dir(path),
fmt.Sprintf("._cfg%04d_%s", i, filepath.Base(path)))) fmt.Sprintf("._cfg%04d_%s", i, filepath.Base(path))))
@ -577,10 +574,8 @@ func (a *PackageArtifact) Unpack(ctx *types.Context, dst string, keepPerms bool)
if err != nil { if err != nil {
return errors.Wrap(err, "Cannot open "+a.Path) return errors.Wrap(err, "Cannot open "+a.Path)
} }
defer decompressed.Close()
replacerArchive := replaceFileTarWrapper(dst, decompressed, protectedFiles, mod) replacerArchive := replaceFileTarWrapper(dst, decompressed, protectedFiles, mod)
defer replacerArchive.Close()
// or with filter? // or with filter?
// func(header *tar.Header) (bool, error) { // func(header *tar.Header) (bool, error) {

View File

@ -812,6 +812,10 @@ func (r *LuetSystemRepository) SyncBuildMetadata(ctx *types.Context, path string
defer os.RemoveAll(a.Path) defer os.RemoveAll(a.Path)
if !fileHelper.Exists(filepath.Join(path, "tree")) {
os.MkdirAll(filepath.Join(path, "tree"), 0600)
}
if err := a.Unpack(ctx, filepath.Join(path, "tree"), false); err != nil { if err := a.Unpack(ctx, filepath.Join(path, "tree"), false); err != nil {
return errors.Wrapf(err, "while unpacking: %s", REPOFILE_COMPILER_TREE_KEY) return errors.Wrapf(err, "while unpacking: %s", REPOFILE_COMPILER_TREE_KEY)
} }