You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: clean.go
    path of file: ./repos/baetyl/engine
    
    the code of the file until where you have to start completion: package engine

import (
	"os"
	"path/filepath"
	"time"

	"github.com/baetyl/baetyl-go/v2/errors"
	"github.com/baetyl/baetyl-go/v2/log"
	specv1 "github.com/baetyl/baetyl-go/v2/spec/v1"

	"github.com/baetyl/baetyl/v2/utils"
)

func (e *engineImpl) recycle() error {
	e.log.Info("start recycling useless object storage space")
	nod, err := e.nod.Get()
	if err != nil {
		return errors.Trace(err)
	}
	rSysApps := nod.Report.AppInfos(true)
	rApps := nod.Report.AppInfos(false)
	rApps = append(rApps, rSysApps...)

	dSysApps := nod.Desire.AppInfos(true)
	dApps := nod.Desire.AppInfos(false)
	rApps = append(rApps, dApps...)
	rApps = append(rApps, dSysApps...)
	usedCfg := map[string]struct{}{}
	for _, info := range rApps {
		app := new(specv1.Applicatio
    
func (e *engineImpl) cleanObjectStorage() (int, error) {
	node, err := e.nod.Get()
	if err != nil {
		return 0, errors.Trace(err)
	}
	objectCfgs := map[string]*specv1.Configuration{}
	err = e.sto.ForEach(nil, func(cfg *specv1.Configuration) error {
		if isObjectConfig(cfg) {
			key := utils.MakeKey(specv1.KindConfiguration, cfg.Name, cfg.Version)
			if key != "" {
				objectCfgs[key] = cfg
			}
		}
		return nil
	})
	if err != nil {
		return 0, errors.Trace(err)
	}

	var infos []specv1.AppInfo
	infos = append(infos, node.Report.AppInfos(false)...)
	infos = append(infos, node.Desire.AppInfos(false)...)
	infos = append(infos, node.Report.AppInfos(true)...)
	infos = append(infos, node.Desire.AppInfos(true)...)
	occupied := map[string]string{}
	for _, info := range infos {
		occupied[info.Name] = info.Version
	}
	obsoleteApps := make(map[string]*specv1.Application)
	occupiedApps := make(map[string]*specv1.Application)
	err = e.sto.ForEach(nil, func(app *specv1.Application) error {
		if ver, ok := occupied[app.Name]; ok && ver == app.Version {
			occupiedApps[app.Name] = app
			return nil
		}
		if prev, ok := obsoleteApps[app.Name]; !ok || app.UpdateTime.After(prev.UpdateTime) {
			obsoleteApps[app.Name] = app
		}
		return nil
	})
	finishedJobs := getFinishedJobs(occupiedApps, node)
	usedObjectCfgs := getUsedObjectCfgs(occupiedApps, finishedJobs)
	for name, ver := range usedObjectCfgs {
		key := utils.MakeKey(specv1.KindConfiguration, name, ver)
		if _, ok := objectCfgs[key]; ok && key != "" {
			delete(objectCfgs, key)
		}
	}

	dels := getDelObjectCfgs(occupiedApps, obsoleteApps, objectCfgs, finishedJobs)
	var subs []os.DirEntry
	for k, v := range dels {
		if err = e.sto.Delete(k, specv1.Configuration{}); err != nil {
			e.log.Error("failed to delete configuration", log.Error(err))
		}
		dir := filepath.Join(e.cfg.Sync.Download.Path, v.Name)
		subs, err = os.ReadDir(dir)
		if err != nil {
			e.log.Error("failed to read sub dirs", log.Error(err))
			continue
		}
		verDir := filepath.Join(dir, v.Version)
		if err = os.RemoveAll(verDir); err != nil {
			e.log.Error("failed to clean dir", log.Any("dir", verDir))
		}
		if len(subs) == 1 && subs[0].Name() == v.Version {
			if err = os.RemoveAll(dir); err != nil {
				e.log.Error("failed to clean dir", log.Any("dir", dir))
			}
		}
	}
	e.log.Info("complete cleaning useless object storage space")
	return len(dels), nil
}