This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathapt_diff.go
168 lines (144 loc) · 4.57 KB
/
apt_diff.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
Copyright 2018 Google, Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package differs
import (
"bufio"
"os"
"path/filepath"
"strconv"
"strings"
pkgutil "github.com/GoogleContainerTools/container-diff/pkg/util"
"github.com/GoogleContainerTools/container-diff/util"
"github.com/sirupsen/logrus"
)
// APT package database location
const dpkgStatusFile string = "var/lib/dpkg/status"
type AptAnalyzer struct {
}
func (a AptAnalyzer) Name() string {
return "AptAnalyzer"
}
// AptDiff compares the packages installed by apt-get.
func (a AptAnalyzer) Diff(image1, image2 pkgutil.Image) (util.Result, error) {
diff, err := singleVersionDiff(image1, image2, a)
return diff, err
}
func (a AptAnalyzer) Analyze(image pkgutil.Image) (util.Result, error) {
analysis, err := singleVersionAnalysis(image, a)
return analysis, err
}
func (a AptAnalyzer) getPackages(image pkgutil.Image) (map[string]util.PackageInfo, error) {
return readStatusFile(image.FSPath)
}
func readStatusFile(root string) (map[string]util.PackageInfo, error) {
packages := make(map[string]util.PackageInfo)
if _, err := os.Stat(root); err != nil {
// invalid image directory path
return packages, err
}
statusFile := filepath.Join(root, dpkgStatusFile)
if _, err := os.Stat(statusFile); err != nil {
// status file does not exist in this layer
return packages, nil
}
if file, err := os.Open(statusFile); err == nil {
// make sure it gets closed
defer file.Close()
// create a new scanner and read the file line by line
scanner := bufio.NewScanner(file)
var currPackage string
for scanner.Scan() {
currPackage = parseLine(scanner.Text(), currPackage, packages)
}
} else {
return packages, err
}
return packages, nil
}
func parseLine(text string, currPackage string, packages map[string]util.PackageInfo) string {
line := strings.Split(text, ": ")
if len(line) == 2 {
key := line[0]
value := line[1]
switch key {
case "Package":
return value
case "Version":
if packages[currPackage].Version != "" {
logrus.Warningln("Multiple versions of same package detected. Diffing such multi-versioning not yet supported.")
return currPackage
}
modifiedValue := strings.Replace(value, "+", " ", 1)
currPackageInfo, ok := packages[currPackage]
if !ok {
currPackageInfo = util.PackageInfo{}
}
currPackageInfo.Version = modifiedValue
packages[currPackage] = currPackageInfo
return currPackage
case "Installed-Size":
currPackageInfo, ok := packages[currPackage]
if !ok {
currPackageInfo = util.PackageInfo{}
}
var size int64
var err error
size, err = strconv.ParseInt(value, 10, 64)
if err != nil {
logrus.Errorf("Could not get size for %s: %s", currPackage, err)
size = -1
}
// Installed-Size is in KB, so we convert it to bytes to keep consistent with the tool's size units
currPackageInfo.Size = size * 1024
packages[currPackage] = currPackageInfo
return currPackage
default:
return currPackage
}
}
return currPackage
}
type AptLayerAnalyzer struct {
}
func (a AptLayerAnalyzer) Name() string {
return "AptLayerAnalyzer"
}
// AptDiff compares the packages installed by apt-get.
func (a AptLayerAnalyzer) Diff(image1, image2 pkgutil.Image) (util.Result, error) {
diff, err := singleVersionLayerDiff(image1, image2, a)
return diff, err
}
func (a AptLayerAnalyzer) Analyze(image pkgutil.Image) (util.Result, error) {
analysis, err := singleVersionLayerAnalysis(image, a)
return analysis, err
}
func (a AptLayerAnalyzer) getPackages(image pkgutil.Image) ([]map[string]util.PackageInfo, error) {
var packages []map[string]util.PackageInfo
if _, err := os.Stat(image.FSPath); err != nil {
// invalid image directory path
return packages, err
}
statusFile := filepath.Join(image.FSPath, dpkgStatusFile)
if _, err := os.Stat(statusFile); err != nil {
// status file does not exist in this image
return packages, nil
}
for _, layer := range image.Layers {
layerPackages, err := readStatusFile(layer.FSPath)
if err != nil {
return packages, err
}
packages = append(packages, layerPackages)
}
return packages, nil
}