Skip to content

Commit 3f86913

Browse files
committed
add debug messages in image.go
1 parent 0d43120 commit 3f86913

2 files changed

Lines changed: 35 additions & 3 deletions

File tree

image.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,35 @@ func ListImages() {
2828

2929
for _, entry := range entries {
3030
if entry.IsDir() {
31-
fmt.Printf("%s\tN/A\n", entry.Name())
31+
size, err := calculateDirSize(filepath.Join(imageDir, entry.Name()))
32+
if err != nil {
33+
fmt.Printf("%s\tError calculating size\n", entry.Name())
34+
} else {
35+
fmt.Printf("%s\t%d bytes\n", entry.Name(), size)
36+
}
3237
}
3338
}
3439
}
3540

41+
// calculateDirSize calculates the total size of a directory
42+
func calculateDirSize(dirPath string) (int64, error) {
43+
var totalSize int64
44+
err := filepath.Walk(dirPath, func(_ string, info os.FileInfo, err error) error {
45+
if err != nil {
46+
return err
47+
}
48+
if !info.IsDir() {
49+
totalSize += info.Size()
50+
}
51+
return nil
52+
})
53+
54+
if err != nil {
55+
return 0, err
56+
}
57+
return totalSize, nil
58+
}
59+
3660
// Image represents a container image
3761
type Image struct {
3862
Name string
@@ -110,6 +134,8 @@ type Manifest struct {
110134

111135
// Pull downloads an image using the provided registry
112136
func Pull(registry Registry, name string) (*Image, error) {
137+
fmt.Printf("[DEBUG] Starting to pull image '%s'\n", name)
138+
113139
// Split the image name into repository and tag
114140
parts := strings.Split(name, ":")
115141
repo := parts[0]
@@ -118,30 +144,36 @@ func Pull(registry Registry, name string) (*Image, error) {
118144
tag = parts[1]
119145
}
120146

147+
fmt.Printf("[DEBUG] Fetching manifest for repo '%s' and tag '%s'\n", repo, tag)
121148
// Fetch the image manifest
122149
manifest, err := registry.FetchManifest(repo, tag)
123150
if err != nil {
124151
return nil, fmt.Errorf("failed to fetch manifest: %w", err)
125152
}
126153

154+
fmt.Printf("[DEBUG] Manifest fetched successfully. Number of layers: %d\n", len(manifest.Layers))
155+
127156
// Download and extract layers
128157
rootfs := filepath.Join("/tmp/basic-docker/images", name, "rootfs")
129158
if err := os.MkdirAll(rootfs, 0755); err != nil {
130159
return nil, fmt.Errorf("failed to create rootfs: %w", err)
131160
}
132161

133162
for _, layer := range manifest.Layers {
163+
fmt.Printf("[DEBUG] Downloading layer with digest '%s'\n", layer.Digest)
134164
layerReader, err := registry.FetchLayer(repo, layer.Digest)
135165
if err != nil {
136166
return nil, fmt.Errorf("failed to download layer %s: %w", layer.Digest, err)
137167
}
138168
defer layerReader.Close()
139169

170+
fmt.Printf("[DEBUG] Extracting layer '%s'\n", layer.Digest)
140171
if err := extractLayer(layerReader, rootfs); err != nil {
141172
return nil, fmt.Errorf("failed to extract layer %s: %w", layer.Digest, err)
142173
}
143174
}
144175

176+
fmt.Printf("[DEBUG] Image '%s' pulled successfully. RootFS path: %s\n", name, rootfs)
145177
return &Image{
146178
Name: name,
147179
RootFS: rootfs,

main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -802,12 +802,12 @@ func listImages() {
802802

803803
// Check if the rootfs contains content
804804
contentVerified := "No"
805-
var totalSize int64 = 0
805+
totalSize := int64(0)
806806
if files, err := os.ReadDir(rootfsPath); err == nil && len(files) > 0 {
807807
contentVerified = "Yes"
808808
// Calculate the total size of the rootfs
809809
filepath.Walk(rootfsPath, func(_ string, info os.FileInfo, err error) error {
810-
if err == nil {
810+
if err == nil && !info.IsDir() {
811811
totalSize += info.Size()
812812
}
813813
return nil

0 commit comments

Comments
 (0)