// CleanupPrefixComponentFiles removes all unreferenced files in published storage under prefix/component pair func (collection *PublishedRepoCollection) CleanupPrefixComponentFiles(prefix, component string, publishedStorage aptly.PublishedStorage, collectionFactory *CollectionFactory, progress aptly.Progress) error { var err error referencedFiles := []string{} if progress != nil { progress.Printf("Cleaning up prefix %#v component %#v...\n", prefix, component) } for _, r := range collection.list { if r.Prefix == prefix && r.Component == component { err = collection.LoadComplete(r, collectionFactory) if err != nil { return err } packageList, err := NewPackageListFromRefList(r.RefList(), collectionFactory.PackageCollection(), progress) if err != nil { return err } packageList.ForEach(func(p *Package) error { poolDir, err := p.PoolDirectory() if err != nil { return err } for _, f := range p.Files() { referencedFiles = append(referencedFiles, filepath.Join(poolDir, f.Filename)) } return nil }) } } sort.Strings(referencedFiles) rootPath := filepath.Join(prefix, "pool", component) existingFiles, err := publishedStorage.Filelist(rootPath) if err != nil { return err } sort.Strings(existingFiles) filesToDelete := utils.StrSlicesSubstract(existingFiles, referencedFiles) for _, file := range filesToDelete { err = publishedStorage.Remove(filepath.Join(rootPath, file)) if err != nil { return err } } return nil }
// CleanupPrefixComponentFiles removes all unreferenced files in published storage under prefix/component pair func (collection *PublishedRepoCollection) CleanupPrefixComponentFiles(prefix string, components []string, publishedStorage aptly.PublishedStorage, collectionFactory *CollectionFactory, progress aptly.Progress) error { var err error referencedFiles := map[string][]string{} if progress != nil { progress.Printf("Cleaning up prefix %#v components %s...\n", prefix, strings.Join(components, ", ")) } for _, r := range collection.list { if r.Prefix == prefix { matches := false repoComponents := r.Components() for _, component := range components { if utils.StrSliceHasItem(repoComponents, component) { matches = true break } } if !matches { continue } err = collection.LoadComplete(r, collectionFactory) if err != nil { return err } for _, component := range components { if utils.StrSliceHasItem(repoComponents, component) { packageList, err := NewPackageListFromRefList(r.RefList(component), collectionFactory.PackageCollection(), progress) if err != nil { return err } packageList.ForEach(func(p *Package) error { poolDir, err := p.PoolDirectory() if err != nil { return err } for _, f := range p.Files() { referencedFiles[component] = append(referencedFiles[component], filepath.Join(poolDir, f.Filename)) } return nil }) } } } } for _, component := range components { sort.Strings(referencedFiles[component]) rootPath := filepath.Join(prefix, "pool", component) existingFiles, err := publishedStorage.Filelist(rootPath) if err != nil { return err } sort.Strings(existingFiles) filesToDelete := utils.StrSlicesSubstract(existingFiles, referencedFiles[component]) for _, file := range filesToDelete { err = publishedStorage.Remove(filepath.Join(rootPath, file)) if err != nil { return err } } } return nil }