2022-08-18 10:11:13 +02:00
|
|
|
// Copyright 2022 - Offen Authors <hioffen@posteo.de>
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2022-08-18 08:52:09 +02:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2023-11-04 12:19:44 +01:00
|
|
|
"fmt"
|
2022-08-18 08:52:09 +02:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Backend is an interface for defining functions which all storage providers support.
|
|
|
|
type Backend interface {
|
|
|
|
Copy(file string) error
|
|
|
|
Prune(deadline time.Time, pruningPrefix string) (*PruneStats, error)
|
|
|
|
Name() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// StorageBackend is a generic type of storage. Everything here are common properties of all storage types.
|
|
|
|
type StorageBackend struct {
|
|
|
|
DestinationPath string
|
|
|
|
Log Log
|
|
|
|
}
|
|
|
|
|
2022-08-18 10:11:13 +02:00
|
|
|
type LogLevel int
|
2022-08-18 08:52:09 +02:00
|
|
|
|
|
|
|
const (
|
2022-08-18 10:11:13 +02:00
|
|
|
LogLevelInfo LogLevel = iota
|
|
|
|
LogLevelWarning
|
|
|
|
LogLevelError
|
2022-08-18 08:52:09 +02:00
|
|
|
)
|
|
|
|
|
2023-03-16 19:48:12 +01:00
|
|
|
type Log func(logType LogLevel, context string, msg string, params ...any)
|
2022-08-18 08:52:09 +02:00
|
|
|
|
|
|
|
// PruneStats is a wrapper struct for returning stats after pruning
|
|
|
|
type PruneStats struct {
|
|
|
|
Total uint
|
|
|
|
Pruned uint
|
|
|
|
}
|
|
|
|
|
|
|
|
// DoPrune holds general control flow that applies to any kind of storage.
|
|
|
|
// Callers can pass in a thunk that performs the actual deletion of files.
|
2023-11-04 12:19:44 +01:00
|
|
|
func (b *StorageBackend) DoPrune(context string, lenMatches, lenCandidates int, deadline time.Time, doRemoveFiles func() error) error {
|
2022-08-18 08:52:09 +02:00
|
|
|
if lenMatches != 0 && lenMatches != lenCandidates {
|
|
|
|
if err := doRemoveFiles(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-04 12:19:44 +01:00
|
|
|
|
|
|
|
formattedDeadline, err := deadline.Local().MarshalText()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("(*StorageBackend).DoPrune: error marshaling deadline: %w", err)
|
|
|
|
}
|
2022-08-18 10:11:13 +02:00
|
|
|
b.Log(LogLevelInfo, context,
|
2023-11-04 12:19:44 +01:00
|
|
|
"Pruned %d out of %d backups as they were older than the given deadline of %s.",
|
2022-08-18 08:52:09 +02:00
|
|
|
lenMatches,
|
|
|
|
lenCandidates,
|
2023-11-04 12:19:44 +01:00
|
|
|
string(formattedDeadline),
|
2022-08-18 08:52:09 +02:00
|
|
|
)
|
|
|
|
} else if lenMatches != 0 && lenMatches == lenCandidates {
|
2023-08-25 12:44:43 +02:00
|
|
|
b.Log(LogLevelWarning, context, "The current configuration would delete all %d existing backups.", lenMatches)
|
2022-08-18 10:11:13 +02:00
|
|
|
b.Log(LogLevelWarning, context, "Refusing to do so, please check your configuration.")
|
2022-08-18 08:52:09 +02:00
|
|
|
} else {
|
2023-08-25 12:44:43 +02:00
|
|
|
b.Log(LogLevelInfo, context, "None of %d existing backups were pruned.", lenCandidates)
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|