2015-04-07 22:52:48 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2017-08-11 22:31:16 +00:00
|
|
|
"context"
|
2015-04-07 22:52:48 +00:00
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2020-08-24 11:18:39 +00:00
|
|
|
storageDriver "github.com/distribution/distribution/v3/registry/storage/driver"
|
|
|
|
"github.com/distribution/distribution/v3/uuid"
|
2017-08-11 22:31:16 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-04-07 22:52:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// uploadData stored the location of temporary files created during a layer upload
|
|
|
|
// along with the date the upload was started
|
|
|
|
type uploadData struct {
|
|
|
|
containingDir string
|
|
|
|
startedAt time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func newUploadData() uploadData {
|
|
|
|
return uploadData{
|
|
|
|
containingDir: "",
|
|
|
|
// default to far in future to protect against missing startedat
|
2018-08-06 21:34:15 +00:00
|
|
|
startedAt: time.Now().Add(10000 * time.Hour),
|
2015-04-07 22:52:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PurgeUploads deletes files from the upload directory
|
|
|
|
// created before olderThan. The list of files deleted and errors
|
|
|
|
// encountered are returned
|
2015-04-27 22:58:58 +00:00
|
|
|
func PurgeUploads(ctx context.Context, driver storageDriver.StorageDriver, olderThan time.Time, actuallyDelete bool) ([]string, []error) {
|
2017-08-11 22:31:16 +00:00
|
|
|
logrus.Infof("PurgeUploads starting: olderThan=%s, actuallyDelete=%t", olderThan, actuallyDelete)
|
2015-04-27 22:58:58 +00:00
|
|
|
uploadData, errors := getOutstandingUploads(ctx, driver)
|
2015-04-07 22:52:48 +00:00
|
|
|
var deleted []string
|
|
|
|
for _, uploadData := range uploadData {
|
|
|
|
if uploadData.startedAt.Before(olderThan) {
|
|
|
|
var err error
|
2017-08-11 22:31:16 +00:00
|
|
|
logrus.Infof("Upload files in %s have older date (%s) than purge date (%s). Removing upload directory.",
|
2015-04-07 22:52:48 +00:00
|
|
|
uploadData.containingDir, uploadData.startedAt, olderThan)
|
|
|
|
if actuallyDelete {
|
2015-04-27 22:58:58 +00:00
|
|
|
err = driver.Delete(ctx, uploadData.containingDir)
|
2015-04-07 22:52:48 +00:00
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
deleted = append(deleted, uploadData.containingDir)
|
|
|
|
} else {
|
|
|
|
errors = append(errors, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-11 22:31:16 +00:00
|
|
|
logrus.Infof("Purge uploads finished. Num deleted=%d, num errors=%d", len(deleted), len(errors))
|
2015-04-07 22:52:48 +00:00
|
|
|
return deleted, errors
|
|
|
|
}
|
|
|
|
|
|
|
|
// getOutstandingUploads walks the upload directory, collecting files
|
|
|
|
// which could be eligible for deletion. The only reliable way to
|
|
|
|
// classify the age of a file is with the date stored in the startedAt
|
|
|
|
// file, so gather files by UUID with a date from startedAt.
|
2015-04-27 22:58:58 +00:00
|
|
|
func getOutstandingUploads(ctx context.Context, driver storageDriver.StorageDriver) (map[string]uploadData, []error) {
|
2015-04-07 22:52:48 +00:00
|
|
|
var errors []error
|
2019-02-05 00:01:04 +00:00
|
|
|
uploads := make(map[string]uploadData)
|
2015-04-07 22:52:48 +00:00
|
|
|
|
|
|
|
inUploadDir := false
|
2015-08-18 01:51:05 +00:00
|
|
|
root, err := pathFor(repositoriesRootPathSpec{})
|
2015-04-07 22:52:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return uploads, append(errors, err)
|
|
|
|
}
|
2015-08-18 01:51:05 +00:00
|
|
|
|
2018-01-18 20:10:20 +00:00
|
|
|
err = driver.Walk(ctx, root, func(fileInfo storageDriver.FileInfo) error {
|
2015-04-07 22:52:48 +00:00
|
|
|
filePath := fileInfo.Path()
|
|
|
|
_, file := path.Split(filePath)
|
|
|
|
if file[0] == '_' {
|
|
|
|
// Reserved directory
|
|
|
|
inUploadDir = (file == "_uploads")
|
|
|
|
|
|
|
|
if fileInfo.IsDir() && !inUploadDir {
|
2017-11-29 19:17:39 +00:00
|
|
|
return storageDriver.ErrSkipDir
|
2015-04-07 22:52:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-01-30 17:18:22 +00:00
|
|
|
uuid, isContainingDir := uuidFromPath(filePath)
|
2015-04-07 22:52:48 +00:00
|
|
|
if uuid == "" {
|
|
|
|
// Cannot reliably delete
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ud, ok := uploads[uuid]
|
|
|
|
if !ok {
|
|
|
|
ud = newUploadData()
|
|
|
|
}
|
|
|
|
if isContainingDir {
|
|
|
|
ud.containingDir = filePath
|
|
|
|
}
|
|
|
|
if file == "startedat" {
|
|
|
|
if t, err := readStartedAtFile(driver, filePath); err == nil {
|
|
|
|
ud.startedAt = t
|
|
|
|
} else {
|
|
|
|
errors = pushError(errors, filePath, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uploads[uuid] = ud
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errors = pushError(errors, root, err)
|
|
|
|
}
|
|
|
|
return uploads, errors
|
|
|
|
}
|
|
|
|
|
2017-01-30 17:18:22 +00:00
|
|
|
// uuidFromPath extracts the upload UUID from a given path
|
2015-04-07 22:52:48 +00:00
|
|
|
// If the UUID is the last path component, this is the containing
|
|
|
|
// directory for all upload files
|
2017-01-30 17:18:22 +00:00
|
|
|
func uuidFromPath(path string) (string, bool) {
|
2015-04-07 22:52:48 +00:00
|
|
|
components := strings.Split(path, "/")
|
|
|
|
for i := len(components) - 1; i >= 0; i-- {
|
2015-05-22 22:55:54 +00:00
|
|
|
if u, err := uuid.Parse(components[i]); err == nil {
|
|
|
|
return u.String(), i == len(components)-1
|
2015-04-07 22:52:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
// readStartedAtFile reads the date from an upload's startedAtFile
|
|
|
|
func readStartedAtFile(driver storageDriver.StorageDriver, path string) (time.Time, error) {
|
2015-04-27 22:58:58 +00:00
|
|
|
// todo:(richardscothern) - pass in a context
|
|
|
|
startedAtBytes, err := driver.GetContent(context.Background(), path)
|
2015-04-07 22:52:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return time.Now(), err
|
|
|
|
}
|
|
|
|
startedAt, err := time.Parse(time.RFC3339, string(startedAtBytes))
|
|
|
|
if err != nil {
|
|
|
|
return time.Now(), err
|
|
|
|
}
|
|
|
|
return startedAt, nil
|
|
|
|
}
|