2023-10-01 09:40:12 +00:00
|
|
|
package backend
|
2016-08-31 17:10:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
2016-08-31 17:10:10 +00:00
|
|
|
)
|
|
|
|
|
2016-08-31 18:29:54 +00:00
|
|
|
// FileType is the type of a file in the backend.
|
2022-10-16 08:36:37 +00:00
|
|
|
type FileType uint8
|
2016-08-31 18:29:54 +00:00
|
|
|
|
|
|
|
// These are the different data types a backend can store.
|
|
|
|
const (
|
2022-10-16 08:36:37 +00:00
|
|
|
PackFile FileType = 1 + iota
|
|
|
|
KeyFile
|
|
|
|
LockFile
|
|
|
|
SnapshotFile
|
|
|
|
IndexFile
|
|
|
|
ConfigFile
|
2016-08-31 18:29:54 +00:00
|
|
|
)
|
|
|
|
|
2022-10-16 08:36:37 +00:00
|
|
|
func (t FileType) String() string {
|
|
|
|
s := "invalid"
|
|
|
|
switch t {
|
|
|
|
case PackFile:
|
|
|
|
// Spelled "data" instead of "pack" for historical reasons.
|
|
|
|
s = "data"
|
|
|
|
case KeyFile:
|
|
|
|
s = "key"
|
|
|
|
case LockFile:
|
|
|
|
s = "lock"
|
|
|
|
case SnapshotFile:
|
|
|
|
s = "snapshot"
|
|
|
|
case IndexFile:
|
|
|
|
s = "index"
|
|
|
|
case ConfigFile:
|
|
|
|
s = "config"
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2016-08-31 17:10:10 +00:00
|
|
|
// Handle is used to store and access data in a backend.
|
|
|
|
type Handle struct {
|
2023-10-01 08:52:57 +00:00
|
|
|
Type FileType
|
|
|
|
IsMetadata bool
|
|
|
|
Name string
|
2016-08-31 17:10:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h Handle) String() string {
|
|
|
|
name := h.Name
|
|
|
|
if len(name) > 10 {
|
|
|
|
name = name[:10]
|
|
|
|
}
|
2016-09-01 19:19:30 +00:00
|
|
|
return fmt.Sprintf("<%s/%s>", h.Type, name)
|
2016-08-31 17:10:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Valid returns an error if h is not valid.
|
|
|
|
func (h Handle) Valid() error {
|
2016-09-01 19:19:30 +00:00
|
|
|
switch h.Type {
|
2020-08-16 09:16:38 +00:00
|
|
|
case PackFile:
|
2016-08-31 17:10:10 +00:00
|
|
|
case KeyFile:
|
|
|
|
case LockFile:
|
|
|
|
case SnapshotFile:
|
|
|
|
case IndexFile:
|
|
|
|
case ConfigFile:
|
|
|
|
default:
|
2022-10-16 08:36:37 +00:00
|
|
|
return errors.Errorf("invalid Type %d", h.Type)
|
2016-08-31 17:10:10 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 19:19:30 +00:00
|
|
|
if h.Type == ConfigFile {
|
2016-08-31 17:10:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if h.Name == "" {
|
|
|
|
return errors.New("invalid Name")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|