From 334eb80a0949ff9b5d170b20cc1f9a0b54d90385 Mon Sep 17 00:00:00 2001 From: fanmin shi Date: Thu, 4 May 2017 11:28:15 -0700 Subject: [PATCH] snap: expose GetDBFilePathByID() as a class function --- snap/db.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/snap/db.go b/snap/db.go index ae3c743f80c1..e075e76ee268 100644 --- a/snap/db.go +++ b/snap/db.go @@ -15,6 +15,7 @@ package snap import ( + "errors" "fmt" "io" "io/ioutil" @@ -24,6 +25,8 @@ import ( "github.com/coreos/etcd/pkg/fileutil" ) +var ErrDBSnapFileNotFound = errors.New("snap: db snapshot file doesn't exist") + // SaveDBFrom saves snapshot of the database from the given reader. It // guarantees the save operation is atomic. func (s *Snapshotter) SaveDBFrom(r io.Reader, id uint64) (int64, error) { @@ -52,23 +55,26 @@ func (s *Snapshotter) SaveDBFrom(r io.Reader, id uint64) (int64, error) { return n, err } - plog.Infof("saved database snapshot to disk [total bytes: %d]", n) - + plog.Infof("saved database snapshot %v to disk [total bytes: %d]", fn, n) return n, nil } // DBFilePath returns the file path for the snapshot of the database with // given id. If the snapshot does not exist, it returns error. func (s *Snapshotter) DBFilePath(id uint64) (string, error) { - fns, err := fileutil.ReadDir(s.dir) + return GetDBFilePathByID(s.dir, id) +} + +func GetDBFilePathByID(snapDir string, id uint64) (string, error) { + fns, err := fileutil.ReadDir(snapDir) if err != nil { return "", err } wfn := fmt.Sprintf("%016x.snap.db", id) for _, fn := range fns { if fn == wfn { - return filepath.Join(s.dir, fn), nil + return filepath.Join(snapDir, fn), nil } } - return "", fmt.Errorf("snap: snapshot file doesn't exist") + return "", ErrDBSnapFileNotFound }