Skip to content

Commit 0aa3e2d

Browse files
committed
load the tar images locally first
1 parent ce576eb commit 0aa3e2d

1 file changed

Lines changed: 51 additions & 11 deletions

File tree

main.go

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -323,23 +323,29 @@ func printSystemInfo() {
323323
fmt.Printf(" - Filesystem isolation: true\n")
324324
}
325325

326-
// Update the run function to use the new Pull logic
327326
func run() {
328327
if len(os.Args) < 3 {
329328
fmt.Println("Error: Image name required for run")
330329
os.Exit(1)
331330
}
332331

333332
imageName := os.Args[2]
334-
registry := NewDockerHubRegistry()
333+
imagePath := filepath.Join(imagesDir, imageName, "rootfs")
335334

336-
fmt.Printf("Fetching image '%s'...\n", imageName)
337-
image, err := Pull(registry, imageName)
338-
if err != nil {
339-
fmt.Printf("Error: Failed to fetch image '%s': %v\n", imageName, err)
340-
os.Exit(1)
335+
// Check if the image exists locally
336+
if _, err := os.Stat(imagePath); err == nil {
337+
fmt.Printf("Using locally loaded image '%s'.\n", imageName)
338+
} else {
339+
fmt.Printf("Fetching image '%s' from registry...\n", imageName)
340+
registry := NewDockerHubRegistry()
341+
image, err := Pull(registry, imageName)
342+
if err != nil {
343+
fmt.Printf("Error: Failed to fetch image '%s': %v\n", imageName, err)
344+
os.Exit(1)
345+
}
346+
fmt.Printf("Image '%s' fetched successfully.\n", imageName)
347+
imagePath = image.RootFS
341348
}
342-
fmt.Printf("Image '%s' fetched successfully.\n", imageName)
343349

344350
// Create rootfs for this container
345351
containerID := fmt.Sprintf("container-%d", time.Now().Unix())
@@ -350,7 +356,7 @@ func run() {
350356
os.Exit(1)
351357
}
352358

353-
if err := copyDir(image.RootFS, rootfs); err != nil {
359+
if err := copyDir(imagePath, rootfs); err != nil {
354360
fmt.Printf("Error: Failed to copy rootfs for container '%s': %v\n", containerID, err)
355361
os.Exit(1)
356362
}
@@ -620,10 +626,10 @@ func saveLayerMetadata(layer ImageLayer) error {
620626
// Serialize the layer metadata to JSON
621627
metadataFile := filepath.Join(layersDir, layer.ID+".json")
622628
file, err := os.Create(metadataFile)
629+
defer file.Close()
623630
if err != nil {
624631
return fmt.Errorf("failed to create metadata file: %v", err)
625632
}
626-
defer file.Close()
627633

628634
encoder := json.NewEncoder(file)
629635
if err := encoder.Encode(layer); err != nil {
@@ -737,7 +743,41 @@ func listContainers() {
737743

738744
func listImages() {
739745
fmt.Println("[DEBUG] listImages: Starting to list images")
740-
ListImages()
746+
imageDir := "/tmp/basic-docker/images"
747+
fmt.Println("IMAGE NAME\tSIZE\tCONTENT VERIFIED")
748+
749+
if _, err := os.Stat(imageDir); os.IsNotExist(err) {
750+
return
751+
}
752+
753+
entries, err := os.ReadDir(imageDir)
754+
if err != nil {
755+
fmt.Printf("Error reading images: %v\n", err)
756+
return
757+
}
758+
759+
for _, entry := range entries {
760+
if entry.IsDir() {
761+
imageName := entry.Name()
762+
rootfsPath := filepath.Join(imageDir, imageName, "rootfs")
763+
764+
// Check if the rootfs contains content
765+
contentVerified := "No"
766+
var totalSize int64 = 0
767+
if files, err := os.ReadDir(rootfsPath); err == nil && len(files) > 0 {
768+
contentVerified = "Yes"
769+
// Calculate the total size of the rootfs
770+
filepath.Walk(rootfsPath, func(_ string, info os.FileInfo, err error) error {
771+
if err == nil {
772+
totalSize += info.Size()
773+
}
774+
return nil
775+
})
776+
}
777+
778+
fmt.Printf("%s\t%d bytes\t%s\n", imageName, totalSize, contentVerified)
779+
}
780+
}
741781
fmt.Println("[DEBUG] listImages: Finished listing images")
742782
}
743783

0 commit comments

Comments
 (0)