From 7d577a8dc2fe7cc71e6beb6c7d64aa8999b2e3e5 Mon Sep 17 00:00:00 2001 From: EnumDev Date: Mon, 26 Aug 2024 21:59:59 +0300 Subject: [PATCH] Improved 'keep' files/directory code and fixed said files from being removed as obsolete --- bpm_utils/package_utils.go | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/bpm_utils/package_utils.go b/bpm_utils/package_utils.go index 492c294..5d949e0 100644 --- a/bpm_utils/package_utils.go +++ b/bpm_utils/package_utils.go @@ -310,6 +310,9 @@ func ReadPackageInfo(contents string, defaultValues bool) (*PackageInfo, error) return nil, errors.New("this package contains no type") } } + for i := 0; i < len(pkgInfo.Keep); i++ { + pkgInfo.Keep[i] = strings.TrimPrefix(pkgInfo.Keep[i], "/") + } return &pkgInfo, nil } @@ -368,15 +371,33 @@ func extractPackage(pkgInfo *PackageInfo, verbose bool, filename, rootDir string } } case tar.TypeReg: + skip := false if _, err := os.Stat(extractFilename); err == nil { - if slices.Contains(pkgInfo.Keep, trimmedName) { - if verbose { - fmt.Println("Skipping File: " + extractFilename + "(File is configured to be kept during installs/updates)") + for _, k := range pkgInfo.Keep { + if strings.HasSuffix(k, "/") { + if strings.HasPrefix(trimmedName, k) { + if verbose { + fmt.Println("Skipping File: " + extractFilename + " (Containing directory is set to be kept during installs/updates)") + } + files = append(files, strings.TrimPrefix(header.Name, "files/")) + skip = true + continue + } + } else { + if trimmedName == k { + if verbose { + fmt.Println("Skipping File: " + extractFilename + " (File is configured to be kept during installs/updates)") + } + files = append(files, strings.TrimPrefix(header.Name, "files/")) + skip = true + continue + } } - files = append(files, trimmedName) - continue } } + if skip { + continue + } err := os.Remove(extractFilename) if err != nil && !os.IsNotExist(err) { return err, nil