summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-10-14 09:34:30 -0500
committerJeff Carr <[email protected]>2025-10-14 10:48:58 -0500
commita8613af4ff4a347ad461a5852591e13d690db9e6 (patch)
tree3d54d27aa55436d3a9a8deff75c5f1e6a8246cc6
parentbbe67531d56f5179603ba66a08d37474b47bc79f (diff)
moved main dpkg -I handling code here
-rw-r--r--parseDpkgOutputIntoPB.go61
1 files changed, 61 insertions, 0 deletions
diff --git a/parseDpkgOutputIntoPB.go b/parseDpkgOutputIntoPB.go
index ecddf4b..cb1692f 100644
--- a/parseDpkgOutputIntoPB.go
+++ b/parseDpkgOutputIntoPB.go
@@ -2,15 +2,76 @@ package debian
import (
"bufio"
+ "crypto/md5"
+ "crypto/sha256"
+ "errors"
+ "fmt"
+ "io"
+ "os"
"strings"
"go.wit.com/lib/cobol"
"go.wit.com/lib/config"
+ "go.wit.com/lib/gui/shell"
"go.wit.com/lib/protobuf/zoopb"
"go.wit.com/log"
"google.golang.org/protobuf/types/known/timestamppb"
)
+// runs dpkg -I on a .deb and returns a PB of the information
+func RunDpkg(p *zoopb.Package, filename string) error {
+ // SIMPLE SANITY CHECKS
+ if p.DebInfo != nil {
+ // already added p.DebInfo
+ return nil
+ }
+ // filename := filepath.Join(me.pb.BaseDir, p.Filename)
+ cmd := []string{"dpkg", "-I", filename}
+ r := shell.Run(cmd)
+ if r.Error != nil {
+ return r.Error
+ }
+ if r.Exit != 0 {
+ return errors.New("dpkg returned -1")
+ }
+ stat, err := os.Stat(filename)
+ if err != nil {
+ return err
+ }
+ filedata, err := os.Open(filename)
+ if err != nil {
+ return err
+ }
+ defer filedata.Close()
+ // SIMPLE SANITY CHECKS END
+
+ // SHA256 HASH
+ p.DebInfo = new(zoopb.DebInfo)
+
+ hSHA256 := sha256.New()
+ hMD5 := md5.New() // probably deprecate, but love md5sum too much
+ // hSHA1 := sha1.New() // deprecated
+
+ // TeeReader allows writing to multiple hashers at once
+ // multiWriter := io.MultiWriter(hMD5, hSHA1, hSHA256)
+ multiWriter := io.MultiWriter(hSHA256, hMD5)
+ if _, err := io.Copy(multiWriter, filedata); err != nil {
+ return err
+ }
+ p.DebInfo.SHA256 = fmt.Sprintf("%x", hSHA256.Sum(nil)) // should be the standard now
+ p.DebInfo.MD5SUM = fmt.Sprintf("%x", hMD5.Sum(nil)) // probably deprecate
+ // p.DebInfo.SHA1 = fmt.Sprintf("%x", hSHA1.Sum(nil)) // deprecated
+ // SHA256 HASH END
+
+ // set file create time
+ p.Ctime = timestamppb.New(stat.ModTime())
+
+ // PARSE "dpkg -I <p.Filename>", then exit as we are done
+ all := strings.Join(r.Stdout, "\n")
+ ParseDpkgOutputIntoPB(p, all)
+ return nil
+}
+
// PARSES "dpkg -I <p.Filename>"
// panic() on anything missing or unknown
func ParseDpkgOutputIntoPB(pb *zoopb.Package, all string) {