restic/internal/backend/layout/layout_default.go

87 lines
2.1 KiB
Go
Raw Normal View History

package layout
2017-03-26 20:20:10 +00:00
2017-07-02 13:29:54 +00:00
import (
"encoding/hex"
2017-07-23 12:21:03 +00:00
"github.com/restic/restic/internal/backend"
2017-07-02 13:29:54 +00:00
)
2017-03-26 20:20:10 +00:00
// DefaultLayout implements the default layout for local and sftp backends, as
// described in the Design document. The `data` directory has one level of
// subdirs, two characters each (taken from the first two characters of the
// file name).
type DefaultLayout struct {
path string
join func(...string) string
2017-03-26 20:20:10 +00:00
}
var defaultLayoutPaths = map[backend.FileType]string{
backend.PackFile: "data",
backend.SnapshotFile: "snapshots",
backend.IndexFile: "index",
backend.LockFile: "locks",
backend.KeyFile: "keys",
2017-03-26 20:20:10 +00:00
}
2024-08-26 18:28:39 +00:00
func NewDefaultLayout(path string, join func(...string) string) *DefaultLayout {
return &DefaultLayout{
path: path,
join: join,
2024-08-26 18:28:39 +00:00
}
}
2017-06-07 19:59:41 +00:00
func (l *DefaultLayout) String() string {
return "<DefaultLayout>"
}
// Name returns the name for this layout.
func (l *DefaultLayout) Name() string {
return "default"
}
2017-03-26 20:20:10 +00:00
// Dirname returns the directory path for a given file type and name.
func (l *DefaultLayout) Dirname(h backend.Handle) string {
2017-03-26 20:20:10 +00:00
p := defaultLayoutPaths[h.Type]
if h.Type == backend.PackFile && len(h.Name) > 2 {
p = l.join(p, h.Name[:2]) + "/"
2017-03-26 20:20:10 +00:00
}
return l.join(l.path, p) + "/"
2017-03-26 20:20:10 +00:00
}
// Filename returns a path to a file, including its name.
func (l *DefaultLayout) Filename(h backend.Handle) string {
2017-03-26 20:20:10 +00:00
name := h.Name
if h.Type == backend.ConfigFile {
return l.join(l.path, "config")
2017-03-26 20:20:10 +00:00
}
return l.join(l.Dirname(h), name)
2017-03-26 20:20:10 +00:00
}
2017-07-02 13:29:54 +00:00
// Paths returns all directory names needed for a repo.
2017-03-26 20:20:10 +00:00
func (l *DefaultLayout) Paths() (dirs []string) {
for _, p := range defaultLayoutPaths {
dirs = append(dirs, l.join(l.path, p))
2017-03-26 20:20:10 +00:00
}
2017-07-02 13:29:54 +00:00
// also add subdirs
for i := 0; i < 256; i++ {
subdir := hex.EncodeToString([]byte{byte(i)})
dirs = append(dirs, l.join(l.path, defaultLayoutPaths[backend.PackFile], subdir))
2017-07-02 13:29:54 +00:00
}
2017-03-26 20:20:10 +00:00
return dirs
}
2017-04-10 21:21:23 +00:00
// Basedir returns the base dir name for type t.
func (l *DefaultLayout) Basedir(t backend.FileType) (dirname string, subdirs bool) {
if t == backend.PackFile {
subdirs = true
}
dirname = l.join(l.path, defaultLayoutPaths[t])
return
2017-04-10 21:21:23 +00:00
}