@@ -753,6 +753,16 @@ func (ic *ContainerEngine) QuadletPrint(_ context.Context, quadlet string) (stri
753753 return string (contents ), nil
754754}
755755
756+ // addIfUnique appends item to list if it has not already been seen in seenMap.
757+ // it returns true if the item was newly added, false if it was already present.
758+ func addIfUnique (seenMap map [string ]struct {}, item string , list * []string ) {
759+ if _ , ok := seenMap [item ]; ok {
760+ return
761+ }
762+ * list = append (* list , item )
763+ seenMap [item ] = struct {}{}
764+ }
765+
756766// QuadletRemove removes one or more Quadlet files or applications and reloads systemd daemon as needed. The function returns a `QuadletRemoveReport`
757767// containing the removal status for each quadlet file or application, or returns an error if the entire function fails.
758768func (ic * ContainerEngine ) QuadletRemove (ctx context.Context , quadlets []string , options entities.QuadletRemoveOptions ) (* entities.QuadletRemoveReport , error ) {
@@ -765,6 +775,7 @@ func (ic *ContainerEngine) QuadletRemove(ctx context.Context, quadlets []string,
765775 if err != nil {
766776 return nil , fmt .Errorf ("unable to build app map: %w" , err )
767777 }
778+ uniqueQuadletFiles := make (map [string ]struct {})
768779 expandQuadletList := []string {}
769780 // Process all `.app` files in arguments, if `.app` file
770781 // is found then expand it to its respective quadlet files
@@ -777,19 +788,17 @@ func (ic *ContainerEngine) QuadletRemove(ctx context.Context, quadlets []string,
777788 if ok {
778789 for _ , file := range files {
779790 if ! systemdquadlet .IsExtSupported (file ) {
780- removeList = append ( removeList , file )
791+ addIfUnique ( uniqueQuadletFiles , file , & removeList )
781792 } else {
782- expandQuadletList = append ( expandQuadletList , file )
793+ addIfUnique ( uniqueQuadletFiles , file , & expandQuadletList )
783794 }
784795 }
785796 }
786797 // also add .app file itself to the remove list so it can
787798 // be cleaned after removal of all components in the list
788- if ! slices .Contains (removeList , quadlet ) {
789- removeList = append (removeList , quadlet )
790- }
799+ addIfUnique (uniqueQuadletFiles , quadlet , & removeList )
791800 } else {
792- expandQuadletList = append ( expandQuadletList , quadlet )
801+ addIfUnique ( uniqueQuadletFiles , quadlet , & expandQuadletList )
793802 }
794803 }
795804 quadlets = expandQuadletList
@@ -813,7 +822,9 @@ func (ic *ContainerEngine) QuadletRemove(ctx context.Context, quadlets []string,
813822
814823 if options .All {
815824 allQuadlets := getAllQuadletPaths ()
816- quadlets = allQuadlets
825+ for _ , quadlet := range allQuadlets {
826+ addIfUnique (uniqueQuadletFiles , quadlet , & quadlets )
827+ }
817828 }
818829
819830 // We are using index wise iteration here instead of `range`
@@ -843,26 +854,19 @@ func (ic *ContainerEngine) QuadletRemove(ctx context.Context, quadlets []string,
843854 // If this is part of app and we are cleaning entire .app
844855 // make sure to add .app file itself to the removal list
845856 // if it does not already exists.
846- if ! slices .Contains (removeList , value ) {
847- removeList = append (removeList , value )
848- }
857+ addIfUnique (uniqueQuadletFiles , value , & removeList )
849858 appFilePath := filepath .Join (systemdquadlet .GetInstallUnitDirPath (rootless .IsRootless ()), value )
850859 filesToRemove , err := getAssetListFromFile (appFilePath )
851860 if err != nil {
852861 return nil , fmt .Errorf ("unable to get list of files to remove: %w" , err )
853862 }
854863 for _ , entry := range filesToRemove {
855864 if ! systemdquadlet .IsExtSupported (entry ) {
856- removeList = append (removeList , entry )
857- if ! slices .Contains (removeList , value ) {
858- // In the last also clean .<quadlet>.app file
859- removeList = append (removeList , value )
860- }
865+ addIfUnique (uniqueQuadletFiles , entry , & removeList )
866+ addIfUnique (uniqueQuadletFiles , value , & removeList )
861867 continue
862868 }
863- if ! slices .Contains (quadlets , entry ) {
864- quadlets = append (quadlets , entry )
865- }
869+ addIfUnique (uniqueQuadletFiles , entry , & quadlets )
866870 }
867871 }
868872
@@ -937,6 +941,7 @@ func (ic *ContainerEngine) QuadletRemove(ctx context.Context, quadlets []string,
937941 continue
938942 }
939943 }
944+ // Clean up app and quadlet files
940945 for _ , entry := range removeList {
941946 os .Remove (filepath .Join (systemdquadlet .GetInstallUnitDirPath (rootless .IsRootless ()), entry ))
942947 }
0 commit comments