From 6a201f7962b22d9f3a75aa04f1de676d735183b1 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sun, 26 Mar 2017 21:52:49 +0200 Subject: [PATCH] backend: Add Layout --- src/restic/backend/layout.go | 58 +++++++++++++++++++++++ src/restic/backend/layout_test.go | 79 +++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 src/restic/backend/layout.go create mode 100644 src/restic/backend/layout_test.go diff --git a/src/restic/backend/layout.go b/src/restic/backend/layout.go new file mode 100644 index 000000000..d14f47f89 --- /dev/null +++ b/src/restic/backend/layout.go @@ -0,0 +1,58 @@ +package backend + +import ( + "restic" +) + +// Layout computes paths for file name storage. +type Layout interface { + Filename(restic.Handle) string + Dirname(restic.Handle) string + Paths() []string +} + +// 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 +} + +var defaultLayoutPaths = map[restic.FileType]string{ + restic.DataFile: "data", + restic.SnapshotFile: "snapshots", + restic.IndexFile: "index", + restic.LockFile: "locks", + restic.KeyFile: "keys", +} + +// Dirname returns the directory path for a given file type and name. +func (l *DefaultLayout) Dirname(h restic.Handle) string { + p := defaultLayoutPaths[h.Type] + + if h.Type == restic.DataFile && len(h.Name) > 2 { + p = l.Join(p, h.Name[:2]) + } + + return l.Join(l.Path, p) +} + +// Filename returns a path to a file, including its name. +func (l *DefaultLayout) Filename(h restic.Handle) string { + name := h.Name + if h.Type == restic.ConfigFile { + name = "config" + } + + return l.Join(l.Dirname(h), name) +} + +// Paths returns all directory names +func (l *DefaultLayout) Paths() (dirs []string) { + for _, p := range defaultLayoutPaths { + dirs = append(dirs, l.Join(l.Path, p)) + } + return dirs +} diff --git a/src/restic/backend/layout_test.go b/src/restic/backend/layout_test.go new file mode 100644 index 000000000..17d3a30a3 --- /dev/null +++ b/src/restic/backend/layout_test.go @@ -0,0 +1,79 @@ +package backend + +import ( + "fmt" + "path/filepath" + "reflect" + "restic" + "restic/test" + "sort" + "testing" +) + +func TestDefaultLayout(t *testing.T) { + path, cleanup := test.TempDir(t) + defer cleanup() + + var tests = []struct { + restic.Handle + filename string + }{ + { + restic.Handle{Type: restic.DataFile, Name: "0123456"}, + filepath.Join(path, "data", "01", "0123456"), + }, + { + restic.Handle{Type: restic.ConfigFile, Name: "CFG"}, + filepath.Join(path, "config"), + }, + { + restic.Handle{Type: restic.SnapshotFile, Name: "123456"}, + filepath.Join(path, "snapshots", "123456"), + }, + { + restic.Handle{Type: restic.IndexFile, Name: "123456"}, + filepath.Join(path, "index", "123456"), + }, + { + restic.Handle{Type: restic.LockFile, Name: "123456"}, + filepath.Join(path, "locks", "123456"), + }, + { + restic.Handle{Type: restic.KeyFile, Name: "123456"}, + filepath.Join(path, "keys", "123456"), + }, + } + + l := &DefaultLayout{ + Path: path, + Join: filepath.Join, + } + + t.Run("Paths", func(t *testing.T) { + dirs := l.Paths() + + want := []string{ + filepath.Join(path, "data"), + filepath.Join(path, "snapshots"), + filepath.Join(path, "index"), + filepath.Join(path, "locks"), + filepath.Join(path, "keys"), + } + + sort.Sort(sort.StringSlice(want)) + sort.Sort(sort.StringSlice(dirs)) + + if !reflect.DeepEqual(dirs, want) { + t.Fatalf("wrong paths returned, want:\v %v\ngot:\n %v", want, dirs) + } + }) + + for _, test := range tests { + t.Run(fmt.Sprintf("%v/%v", test.Type, test.Handle.Name), func(t *testing.T) { + filename := l.Filename(test.Handle) + if filename != test.filename { + t.Fatalf("wrong filename, want %v, got %v", test.filename, filename) + } + }) + } +}