blob: 57e417c33db686d976d731b77bd193bb19b930bd [file] [log] [blame] [edit]
package dkms
import (
"context"
"fmt"
"os"
"cos.googlesource.com/cos/tools.git/src/pkg/gcs"
"cos.googlesource.com/cos/tools.git/src/pkg/utils"
"github.com/golang/glog"
)
// Remove removes a package from the DKMS source tree.
// This will also unbuild and uninstall the package, if applicable.
func Remove(pkg *Package) error {
if !IsAdded(pkg) {
glog.Info("module is not yet added; skipping removal from source tree")
return nil
}
if err := Unbuild(pkg); err != nil {
return err
}
sourceDir := pkg.SourceDir()
glog.Info("removing package sources", sourceDir)
if err := os.Remove(sourceDir); err != nil {
return fmt.Errorf("failed to remove package sources: %v", err)
}
if pkg.Config.PostRemove != "" {
if err := utils.RunCommandString(sourceDir, pkg.Config.PostRemove); err != nil {
return fmt.Errorf("failed to run post-remove script: %v", err)
}
}
return nil
}
// Remove removes a package from the local DKMS source tree and from the cache.
//
// This will also unbuild the package locally and in the cache, if applicable.
// If the package is installed, this will uninstall it.
func CachedRemove(ctx context.Context, pkg *Package, cache *gcs.GCSBucket) error {
if err := CachedUnbuild(ctx, pkg, cache); err != nil {
return err
}
if err := Remove(pkg); err != nil {
return err
}
if IsAddedInCache(ctx, pkg, cache) {
moduleSourceDir := pkg.CacheSourceDir()
if err := cache.DeleteDir(ctx, moduleSourceDir); err != nil {
return err
}
}
return nil
}