|
| 1 | +package apt |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "qp/internal/consts" |
| 8 | + "qp/internal/pkgdata" |
| 9 | + "strconv" |
| 10 | + "strings" |
| 11 | +) |
| 12 | + |
| 13 | +func parseStatusFile(data []byte, origin string) ([]*pkgdata.PkgInfo, error) { |
| 14 | + reasonMap, err := loadInstallReasons() |
| 15 | + if err != nil { |
| 16 | + return []*pkgdata.PkgInfo{}, err |
| 17 | + } |
| 18 | + |
| 19 | + var collectedErrors []error |
| 20 | + pkgs := []*pkgdata.PkgInfo{} |
| 21 | + |
| 22 | + for block := range bytes.SplitSeq(data, []byte("\n\n")) { |
| 23 | + if len(block) < 1 { |
| 24 | + continue |
| 25 | + } |
| 26 | + |
| 27 | + pkg, err := parseStatusBlock(block, reasonMap, origin) |
| 28 | + if err != nil { |
| 29 | + collectedErrors = append(collectedErrors, err) |
| 30 | + } |
| 31 | + |
| 32 | + pkgs = append(pkgs, pkg) |
| 33 | + } |
| 34 | + |
| 35 | + if len(collectedErrors) > 0 { |
| 36 | + return pkgs, errors.Join(collectedErrors...) |
| 37 | + } |
| 38 | + |
| 39 | + return pkgs, nil |
| 40 | +} |
| 41 | + |
| 42 | +func parseStatusFields(block []byte) map[string]string { |
| 43 | + fields := make(map[string]string) |
| 44 | + |
| 45 | + for line := range bytes.SplitSeq(block, []byte("\n")) { |
| 46 | + parts := bytes.SplitN(line, []byte(":"), 2) |
| 47 | + if len(parts) != 2 { |
| 48 | + continue |
| 49 | + } |
| 50 | + |
| 51 | + key := strings.TrimSpace(string(parts[0])) |
| 52 | + value := strings.TrimSpace(string(parts[1])) |
| 53 | + fields[key] = value |
| 54 | + } |
| 55 | + |
| 56 | + return fields |
| 57 | +} |
| 58 | + |
| 59 | +func parseStatusBlock(block []byte, reasonMap map[string]string, origin string) (*pkgdata.PkgInfo, error) { |
| 60 | + fields := parseStatusFields(block) |
| 61 | + var collected []error |
| 62 | + pkg := &pkgdata.PkgInfo{} |
| 63 | + meta := map[string]string{} |
| 64 | + |
| 65 | + for key, value := range fields { |
| 66 | + switch key { |
| 67 | + case fieldInstalledSize: |
| 68 | + size, err := strconv.Atoi(value) |
| 69 | + if err != nil { |
| 70 | + collected = append(collected, fmt.Errorf("invalid install size for %s: %v", pkg.Name, err)) |
| 71 | + continue |
| 72 | + } |
| 73 | + pkg.Size = consts.KB * int64(size) |
| 74 | + |
| 75 | + case fieldPackage: |
| 76 | + pkg.Name = value |
| 77 | + |
| 78 | + case fieldVersion: |
| 79 | + pkg.Version = value |
| 80 | + |
| 81 | + case fieldArchitecture: |
| 82 | + pkg.Arch = value |
| 83 | + |
| 84 | + case fieldDescription: |
| 85 | + pkg.Description = value |
| 86 | + |
| 87 | + case fieldHomepage: |
| 88 | + pkg.Url = value |
| 89 | + |
| 90 | + case fieldMaintainer: |
| 91 | + pkg.Packager = value |
| 92 | + |
| 93 | + case fieldConflicts, fieldBreaks: |
| 94 | + pkg.Conflicts = append(pkg.Conflicts, parseRelations(value)...) |
| 95 | + |
| 96 | + case fieldReplaces: |
| 97 | + pkg.Replaces = parseRelations(value) |
| 98 | + |
| 99 | + case fieldDepends, fieldPreDepends: |
| 100 | + pkg.Depends = append(pkg.Depends, parseRelations(value)...) |
| 101 | + |
| 102 | + case fieldReccommends, fieldSuggests: |
| 103 | + pkg.OptDepends = append(pkg.OptDepends, parseRelations(value)...) |
| 104 | + |
| 105 | + case fieldProvides: |
| 106 | + pkg.Provides = parseRelations(value) |
| 107 | + |
| 108 | + case fieldPriority, fieldEssential: |
| 109 | + meta[key] = value |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + if err := getInstallTime(pkg); err != nil { |
| 114 | + collected = append(collected, err) |
| 115 | + } |
| 116 | + if err := extractLicense(pkg); err != nil { |
| 117 | + collected = append(collected, err) |
| 118 | + } |
| 119 | + |
| 120 | + pkg.Origin = origin |
| 121 | + |
| 122 | + // TODO: for dpkg-only systems, perhaps return "unknown" for non-system packages |
| 123 | + if isSystem(meta) { |
| 124 | + pkg.Reason = "system" |
| 125 | + } else if reasonMap[pkg.Name] == "dependency" { |
| 126 | + pkg.Reason = "dependency" |
| 127 | + } else { |
| 128 | + pkg.Reason = "explicit" |
| 129 | + } |
| 130 | + |
| 131 | + return pkg, errors.Join(collected...) |
| 132 | +} |
| 133 | + |
| 134 | +func isSystem(meta map[string]string) bool { |
| 135 | + priority := strings.ToLower(meta[fieldPriority]) |
| 136 | + essential := strings.ToLower(meta[fieldEssential]) |
| 137 | + |
| 138 | + return priority == "required" || essential == "yes" |
| 139 | +} |
0 commit comments