Skip to content

Commit

Permalink
incus-simplestreams: Handle removal of combined images
Browse files Browse the repository at this point in the history
Also adds --verbose flag to print deleted files

Closes #994

Signed-off-by: Alex Athanasopoulos <alex@melato.org>
  • Loading branch information
melato authored and stgraber committed Jul 16, 2024
1 parent f28f25f commit 3381b08
Showing 1 changed file with 61 additions and 32 deletions.
93 changes: 61 additions & 32 deletions cmd/incus-simplestreams/main_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/json"
"fmt"
"os"

"github.com/spf13/cobra"
Expand All @@ -12,6 +13,8 @@ import (

type cmdRemove struct {
global *cmdGlobal

flagVerbose bool
}

// Command generates the command definition.
Expand All @@ -25,10 +28,24 @@ func (c *cmdRemove) Command() *cobra.Command {
This command locates the image from its fingerprint and removes it from the index.
`)
cmd.RunE = c.Run
cmd.Flags().BoolVarP(&c.flagVerbose, "verbose", "v", false, "Show all information messages")

return cmd
}

func (c *cmdRemove) remove(path string) error {
if c.flagVerbose {
fmt.Printf("deleting: %s\n", path)
}

err := os.Remove(path)
if err != nil && !os.IsNotExist(err) {
return err
}

return nil
}

// Run runs the actual command logic.
func (c *cmdRemove) Run(cmd *cobra.Command, args []string) error {
// Quick checks.
Expand Down Expand Up @@ -67,47 +84,59 @@ func (c *cmdRemove) Run(cmd *cobra.Command, args []string) error {
for kVersion, version := range product.Versions {
// Get the metadata entry.
metaEntry, ok := version.Items["incus.tar.xz"]
if !ok {
// Image isn't using our normal structure.
continue
}

if metaEntry.CombinedSha256DiskKvmImg == image.Fingerprint {
// Deleting a VM image.
err = os.Remove(version.Items["disk-kvm.img"].Path)
if err != nil && !os.IsNotExist(err) {
return err
if ok {
if metaEntry.CombinedSha256DiskKvmImg == image.Fingerprint {
// Deleting a VM image.
err = c.remove(version.Items["disk-kvm.img"].Path)
if err != nil {
return err
}

delete(version.Items, "disk-kvm.img")
metaEntry.CombinedSha256DiskKvmImg = ""
} else if metaEntry.CombinedSha256SquashFs == image.Fingerprint {
// Deleting a container image.
err = c.remove(version.Items["squashfs"].Path)
if err != nil && !os.IsNotExist(err) {
return err
}

delete(version.Items, "squashfs")
metaEntry.CombinedSha256SquashFs = ""
} else {
continue
}

delete(version.Items, "disk-kvm.img")
metaEntry.CombinedSha256DiskKvmImg = ""
} else if metaEntry.CombinedSha256SquashFs == image.Fingerprint {
// Deleting a container image.
err = os.Remove(version.Items["squashfs"].Path)
if err != nil && !os.IsNotExist(err) {
return err
}
// Update the metadata entry.
version.Items["incus.tar.xz"] = metaEntry

delete(version.Items, "squashfs")
metaEntry.CombinedSha256SquashFs = ""
} else {
continue
// Delete the version if it's now empty.
if len(version.Items) == 1 {
err = c.remove(metaEntry.Path)
if err != nil {
return err
}

delete(product.Versions, kVersion)
}
}

// Update the metadata entry.
version.Items["incus.tar.xz"] = metaEntry
metaEntry, ok = version.Items["incus_combined.tar.gz"]
if ok {
if metaEntry.HashSha256 == image.Fingerprint {
err = c.remove(metaEntry.Path)
if err != nil {
return err
}

// Delete the version if it's now empty.
if len(version.Items) == 1 {
err = os.Remove(metaEntry.Path)
if err != nil && !os.IsNotExist(err) {
return err
delete(version.Items, "incus_combined.tar.gz")
}

delete(product.Versions, kVersion)
// Delete the version if it's now empty.
if len(version.Items) == 0 {
delete(product.Versions, kVersion)
}
}

break
}

if len(product.Versions) == 0 {
Expand Down

0 comments on commit 3381b08

Please sign in to comment.