github.com/mika/distribution@v2.2.2-0.20160108133430-a75790e3d8e0+incompatible/registry/storage/vacuum.go (about)

     1  package storage
     2  
     3  import (
     4  	"path"
     5  
     6  	"github.com/docker/distribution/context"
     7  	"github.com/docker/distribution/digest"
     8  	"github.com/docker/distribution/registry/storage/driver"
     9  )
    10  
    11  // vacuum contains functions for cleaning up repositories and blobs
    12  // These functions will only reliably work on strongly consistent
    13  // storage systems.
    14  // https://en.wikipedia.org/wiki/Consistency_model
    15  
    16  // NewVacuum creates a new Vacuum
    17  func NewVacuum(ctx context.Context, driver driver.StorageDriver) Vacuum {
    18  	return Vacuum{
    19  		ctx:    ctx,
    20  		driver: driver,
    21  	}
    22  }
    23  
    24  // Vacuum removes content from the filesystem
    25  type Vacuum struct {
    26  	driver driver.StorageDriver
    27  	ctx    context.Context
    28  }
    29  
    30  // RemoveBlob removes a blob from the filesystem
    31  func (v Vacuum) RemoveBlob(dgst string) error {
    32  	d, err := digest.ParseDigest(dgst)
    33  	if err != nil {
    34  		return err
    35  	}
    36  
    37  	blobPath, err := pathFor(blobDataPathSpec{digest: d})
    38  	if err != nil {
    39  		return err
    40  	}
    41  	context.GetLogger(v.ctx).Infof("Deleting blob: %s", blobPath)
    42  	err = v.driver.Delete(v.ctx, blobPath)
    43  	if err != nil {
    44  		return err
    45  	}
    46  
    47  	return nil
    48  }
    49  
    50  // RemoveRepository removes a repository directory from the
    51  // filesystem
    52  func (v Vacuum) RemoveRepository(repoName string) error {
    53  	rootForRepository, err := pathFor(repositoriesRootPathSpec{})
    54  	if err != nil {
    55  		return err
    56  	}
    57  	repoDir := path.Join(rootForRepository, repoName)
    58  	context.GetLogger(v.ctx).Infof("Deleting repo: %s", repoDir)
    59  	err = v.driver.Delete(v.ctx, repoDir)
    60  	if err != nil {
    61  		return err
    62  	}
    63  
    64  	return nil
    65  }