From f83d35805b2d1f34a93754679cf698756f863bac Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Thu, 7 Dec 2023 10:20:40 -0300 Subject: [PATCH] fix: improve deb special files --- deb/deb.go | 91 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 52 insertions(+), 39 deletions(-) diff --git a/deb/deb.go b/deb/deb.go index ffc2a55f..a6a03eeb 100644 --- a/deb/deb.go +++ b/deb/deb.go @@ -13,6 +13,7 @@ import ( "io" "os" "path/filepath" + "sort" "strings" "text/template" "time" @@ -575,47 +576,50 @@ func createControl(instSize int64, md5sums []byte, info *nfpm.Info) (controlTarG mode int64 } - specialFiles := map[string]*fileAndMode{} - specialFiles[info.Scripts.PreInstall] = &fileAndMode{ - fileName: "preinst", - mode: 0o755, - } - specialFiles[info.Scripts.PostInstall] = &fileAndMode{ - fileName: "postinst", - mode: 0o755, - } - specialFiles[info.Scripts.PreRemove] = &fileAndMode{ - fileName: "prerm", - mode: 0o755, - } - specialFiles[info.Scripts.PostRemove] = &fileAndMode{ - fileName: "postrm", - mode: 0o755, - } - specialFiles[info.Overridables.Deb.Scripts.Rules] = &fileAndMode{ - fileName: "rules", - mode: 0o755, - } - specialFiles[info.Overridables.Deb.Scripts.Templates] = &fileAndMode{ - fileName: "templates", - mode: 0o644, - } - specialFiles[info.Overridables.Deb.Scripts.Config] = &fileAndMode{ - fileName: "config", - mode: 0o755, + specialFiles := map[string]*fileAndMode{ + "preinst": { + fileName: info.Scripts.PreInstall, + mode: 0o755, + }, + "postinst": { + fileName: info.Scripts.PostInstall, + mode: 0o755, + }, + "prerm": { + fileName: info.Scripts.PreRemove, + mode: 0o755, + }, + "postrm": { + fileName: info.Scripts.PostRemove, + mode: 0o755, + }, + "rules": { + fileName: info.Overridables.Deb.Scripts.Rules, + mode: 0o755, + }, + "templates": { + fileName: info.Overridables.Deb.Scripts.Templates, + mode: 0o644, + }, + "config": { + fileName: info.Overridables.Deb.Scripts.Config, + mode: 0o755, + }, } - for path, destMode := range specialFiles { - if path != "" { - if err := newFilePathInsideTar( - out, - path, - destMode.fileName, - destMode.mode, - info.MTime, - ); err != nil { - return nil, err - } + for _, filename := range keys(specialFiles) { + dets := specialFiles[filename] + if dets.fileName == "" { + continue + } + if err := newFilePathInsideTar( + out, + dets.fileName, + filename, + dets.mode, + info.MTime, + ); err != nil { + return nil, err } } @@ -797,3 +801,12 @@ func writeControl(w io.Writer, data controlData) error { }) return template.Must(tmpl.Parse(controlTemplate)).Execute(w, data) } + +func keys[T any](m map[string]T) []string { + keys := make([]string, 0, len(m)) + for key := range m { + keys = append(keys, key) + } + sort.Strings(keys) + return keys +}