From 16eeed2ad5981ca6d93ae9d8cd1af4543c357295 Mon Sep 17 00:00:00 2001 From: Alexandr Bruyako Date: Sun, 30 Jun 2019 23:20:32 +0300 Subject: [PATCH 1/6] simplified string sorting by using a more suitable function --- internal/backend/layout_test.go | 12 ++++++------ internal/fs/fs_reader_test.go | 4 ++-- internal/ui/backup.go | 2 +- internal/ui/jsonstatus/status.go | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/internal/backend/layout_test.go b/internal/backend/layout_test.go index 0c52b32bc..09b6e80c2 100644 --- a/internal/backend/layout_test.go +++ b/internal/backend/layout_test.go @@ -116,8 +116,8 @@ func TestDefaultLayout(t *testing.T) { want = append(want, filepath.Join(tempdir, "data", fmt.Sprintf("%02x", i))) } - sort.Sort(sort.StringSlice(want)) - sort.Sort(sort.StringSlice(dirs)) + sort.Strings(want) + sort.Strings(dirs) if !reflect.DeepEqual(dirs, want) { t.Fatalf("wrong paths returned, want:\n %v\ngot:\n %v", want, dirs) @@ -189,8 +189,8 @@ func TestRESTLayout(t *testing.T) { filepath.Join(path, "keys"), } - sort.Sort(sort.StringSlice(want)) - sort.Sort(sort.StringSlice(dirs)) + sort.Strings(want) + sort.Strings(dirs) if !reflect.DeepEqual(dirs, want) { t.Fatalf("wrong paths returned, want:\n %v\ngot:\n %v", want, dirs) @@ -335,8 +335,8 @@ func TestS3LegacyLayout(t *testing.T) { filepath.Join(path, "key"), } - sort.Sort(sort.StringSlice(want)) - sort.Sort(sort.StringSlice(dirs)) + sort.Strings(want) + sort.Strings(dirs) if !reflect.DeepEqual(dirs, want) { t.Fatalf("wrong paths returned, want:\n %v\ngot:\n %v", want, dirs) diff --git a/internal/fs/fs_reader_test.go b/internal/fs/fs_reader_test.go index bb45732f3..173aec10d 100644 --- a/internal/fs/fs_reader_test.go +++ b/internal/fs/fs_reader_test.go @@ -72,8 +72,8 @@ func verifyDirectoryContents(t testing.TB, fs FS, dir string, want []string) { t.Fatal(err) } - sort.Sort(sort.StringSlice(want)) - sort.Sort(sort.StringSlice(entries)) + sort.Strings(want) + sort.Strings(entries) if !cmp.Equal(want, entries) { t.Error(cmp.Diff(want, entries)) diff --git a/internal/ui/backup.go b/internal/ui/backup.go index cda2d429d..e47f19f04 100644 --- a/internal/ui/backup.go +++ b/internal/ui/backup.go @@ -176,7 +176,7 @@ func (b *Backup) update(total, processed counter, errors uint, currentFiles map[ for filename := range currentFiles { lines = append(lines, filename) } - sort.Sort(sort.StringSlice(lines)) + sort.Strings(lines) lines = append([]string{status}, lines...) b.term.SetStatus(lines) diff --git a/internal/ui/jsonstatus/status.go b/internal/ui/jsonstatus/status.go index cc5b60543..3c8bc5545 100644 --- a/internal/ui/jsonstatus/status.go +++ b/internal/ui/jsonstatus/status.go @@ -159,7 +159,7 @@ func (b *Backup) update(total, processed counter, errors uint, currentFiles map[ for filename := range currentFiles { status.CurrentFiles = append(status.CurrentFiles, filename) } - sort.Sort(sort.StringSlice(status.CurrentFiles)) + sort.Strings(status.CurrentFiles) json.NewEncoder(b.StdioWrapper.Stdout()).Encode(status) } From 02014be76cb5fa7f92b0d69766c81d690c55d1ed Mon Sep 17 00:00:00 2001 From: Alexandr Bruyako Date: Sun, 30 Jun 2019 23:34:47 +0300 Subject: [PATCH 2/6] simplified prefix removal, removed unnecessary if-else statements --- internal/backend/azure/config.go | 4 +--- internal/backend/gs/config.go | 4 +--- internal/cache/cache.go | 6 +----- internal/cache/file.go | 6 +----- 4 files changed, 4 insertions(+), 16 deletions(-) diff --git a/internal/backend/azure/config.go b/internal/backend/azure/config.go index 15a2e5cf9..66f0125e4 100644 --- a/internal/backend/azure/config.go +++ b/internal/backend/azure/config.go @@ -47,9 +47,7 @@ func ParseConfig(s string) (interface{}, error) { return nil, errors.New("azure: invalid format: bucket name or path not found") } container, path := data[0], path.Clean(data[1]) - if strings.HasPrefix(path, "/") { - path = path[1:] - } + path = strings.TrimPrefix(path, "/") cfg := NewConfig() cfg.Container = container cfg.Prefix = path diff --git a/internal/backend/gs/config.go b/internal/backend/gs/config.go index e18066ef9..026ef3dfd 100644 --- a/internal/backend/gs/config.go +++ b/internal/backend/gs/config.go @@ -49,9 +49,7 @@ func ParseConfig(s string) (interface{}, error) { bucket, path := data[0], path.Clean(data[1]) - if strings.HasPrefix(path, "/") { - path = path[1:] - } + path = strings.TrimPrefix(path, "/") cfg := NewConfig() cfg.Bucket = bucket diff --git a/internal/cache/cache.go b/internal/cache/cache.go index fd5743b94..0b264e256 100644 --- a/internal/cache/cache.go +++ b/internal/cache/cache.go @@ -175,11 +175,7 @@ const MaxCacheAge = 30 * 24 * time.Hour func validCacheDirName(s string) bool { r := regexp.MustCompile(`^[a-fA-F0-9]{64}$`) - if !r.MatchString(s) { - return false - } - - return true + return r.MatchString(s) } // listCacheDirs returns the list of cache directories. diff --git a/internal/cache/file.go b/internal/cache/file.go index 36c38ba97..52a767665 100644 --- a/internal/cache/file.go +++ b/internal/cache/file.go @@ -214,9 +214,5 @@ func (c *Cache) Has(h restic.Handle) bool { } _, err := fs.Stat(c.filename(h)) - if err == nil { - return true - } - - return false + return err == nil } From 8b22fe29cff3e20a9bb4c48a3baf34edb6ded05f Mon Sep 17 00:00:00 2001 From: Alexandr Bruyako Date: Sun, 30 Jun 2019 23:56:36 +0300 Subject: [PATCH 3/6] improved slice copying --- internal/options/options.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/internal/options/options.go b/internal/options/options.go index 2a3be2254..f03eb6097 100644 --- a/internal/options/options.go +++ b/internal/options/options.go @@ -22,10 +22,8 @@ func Register(ns string, cfg interface{}) { // List returns a list of all registered options (using Register()). func List() (list []Help) { - list = make([]Help, 0, len(opts)) - for _, opt := range opts { - list = append(list, opt) - } + list = make([]Help, len(opts)) + copy(list, opts) return list } From 76d1866444a8cc6a1ce122bff276610fed3dd0fd Mon Sep 17 00:00:00 2001 From: Alexandr Bruyako Date: Sun, 30 Jun 2019 23:58:00 +0300 Subject: [PATCH 4/6] avoiding unnecessary type conversions --- internal/ui/table/table.go | 2 +- internal/ui/table/table_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/ui/table/table.go b/internal/ui/table/table.go index 8939f2ac9..e14ea263c 100644 --- a/internal/ui/table/table.go +++ b/internal/ui/table/table.go @@ -129,7 +129,7 @@ func (t *Table) Write(w io.Writer) error { return err } - row = append(row, string(buf.Bytes())) + row = append(row, buf.String()) buf.Reset() } lines = append(lines, row) diff --git a/internal/ui/table/table_test.go b/internal/ui/table/table_test.go index 47b180a91..db116bbc5 100644 --- a/internal/ui/table/table_test.go +++ b/internal/ui/table/table_test.go @@ -154,7 +154,7 @@ foo 2018-08-19 22:22:22 xxx other /home/user/other } want := strings.TrimLeft(test.output, "\n") - if string(buf.Bytes()) != want { + if buf.String() != want { t.Errorf("wrong output\n---- want ---\n%s\n---- got ---\n%s\n-------\n", want, buf.Bytes()) } }) From 38ea7ed4f6d1d1b2bb1de6c10150a2a5c381672f Mon Sep 17 00:00:00 2001 From: Alexandr Bruyako Date: Mon, 1 Jul 2019 00:24:45 +0300 Subject: [PATCH 5/6] remove unused code --- helpers/build-release-binaries/main.go | 25 ------------------------- internal/backend/local/local.go | 19 ------------------- internal/backend/s3/s3.go | 16 ---------------- internal/backend/swift/swift.go | 2 -- internal/restic/node.go | 5 ----- internal/restic/tag_list.go | 10 ---------- 6 files changed, 77 deletions(-) diff --git a/helpers/build-release-binaries/main.go b/helpers/build-release-binaries/main.go index f9e25e447..95ca76617 100644 --- a/helpers/build-release-binaries/main.go +++ b/helpers/build-release-binaries/main.go @@ -56,16 +56,6 @@ func verbose(f string, args ...interface{}) { fmt.Printf(f, args...) } -func run(cmd string, args ...string) { - c := exec.Command(cmd, args...) - c.Stdout = os.Stdout - c.Stderr = os.Stderr - err := c.Run() - if err != nil { - die("error running %s %s: %v", cmd, args, err) - } -} - func rm(file string) { err := os.Remove(file) @@ -78,13 +68,6 @@ func rm(file string) { } } -func rmdir(dir string) { - err := os.RemoveAll(dir) - if err != nil { - die("error removing %v: %v", dir, err) - } -} - func mkdir(dir string) { err := os.MkdirAll(dir, 0755) if err != nil { @@ -92,14 +75,6 @@ func mkdir(dir string) { } } -func getwd() string { - pwd, err := os.Getwd() - if err != nil { - die("Getwd(): %v", err) - } - return pwd -} - func abs(dir string) string { absDir, err := filepath.Abs(dir) if err != nil { diff --git a/internal/backend/local/local.go b/internal/backend/local/local.go index 6792241e1..4dbee8a6b 100644 --- a/internal/backend/local/local.go +++ b/internal/backend/local/local.go @@ -25,25 +25,6 @@ var _ restic.Backend = &Local{} const defaultLayout = "default" -// dirExists returns true if the name exists and is a directory. -func dirExists(name string) bool { - f, err := fs.Open(name) - if err != nil { - return false - } - - fi, err := f.Stat() - if err != nil { - return false - } - - if err = f.Close(); err != nil { - return false - } - - return fi.IsDir() -} - // Open opens the local backend as specified by config. func Open(cfg Config) (*Local, error) { debug.Log("open local backend at %v (layout %q)", cfg.Path, cfg.Layout) diff --git a/internal/backend/s3/s3.go b/internal/backend/s3/s3.go index e3c95001d..02eda53be 100644 --- a/internal/backend/s3/s3.go +++ b/internal/backend/s3/s3.go @@ -231,22 +231,6 @@ func (be *Backend) Path() string { return be.cfg.Prefix } -// lenForFile returns the length of the file. -func lenForFile(f *os.File) (int64, error) { - fi, err := f.Stat() - if err != nil { - return 0, errors.Wrap(err, "Stat") - } - - pos, err := f.Seek(0, io.SeekCurrent) - if err != nil { - return 0, errors.Wrap(err, "Seek") - } - - size := fi.Size() - pos - return size, nil -} - // Save stores data in the backend at the handle. func (be *Backend) Save(ctx context.Context, h restic.Handle, rd restic.RewindReader) error { debug.Log("Save %v", h) diff --git a/internal/backend/swift/swift.go b/internal/backend/swift/swift.go index dd83e0b06..58f061d59 100644 --- a/internal/backend/swift/swift.go +++ b/internal/backend/swift/swift.go @@ -17,8 +17,6 @@ import ( "github.com/ncw/swift" ) -const connLimit = 10 - // beSwift is a backend which stores the data on a swift endpoint. type beSwift struct { conn *swift.Connection diff --git a/internal/restic/node.go b/internal/restic/node.go index 95f209113..260e5da2a 100644 --- a/internal/restic/node.go +++ b/internal/restic/node.go @@ -696,8 +696,3 @@ func (node *Node) fillTimes(stat statT) { node.ChangeTime = time.Unix(ctim.Unix()) node.AccessTime = time.Unix(atim.Unix()) } - -func changeTime(stat statT) time.Time { - ctim := stat.ctim() - return time.Unix(ctim.Unix()) -} diff --git a/internal/restic/tag_list.go b/internal/restic/tag_list.go index 0f8232e5b..553ea332c 100644 --- a/internal/restic/tag_list.go +++ b/internal/restic/tag_list.go @@ -36,16 +36,6 @@ func (TagList) Type() string { // TagLists consists of several TagList. type TagLists []TagList -// splitTagLists splits a slice of strings into a slice of TagLists using -// SplitTagList. -func splitTagLists(s []string) (l TagLists) { - l = make([]TagList, 0, len(s)) - for _, t := range s { - l = append(l, splitTagList(t)) - } - return l -} - func (l TagLists) String() string { return fmt.Sprintf("%v", []TagList(l)) } From e19622e4b1239d49eba93625faa6cf67dcef4b53 Mon Sep 17 00:00:00 2001 From: Alexandr Bruyako Date: Mon, 1 Jul 2019 00:26:00 +0300 Subject: [PATCH 6/6] start using fileMode const --- internal/cache/cache.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/cache/cache.go b/internal/cache/cache.go index 0b264e256..ca59b0dc0 100644 --- a/internal/cache/cache.go +++ b/internal/cache/cache.go @@ -24,7 +24,7 @@ type Cache struct { } const dirMode = 0700 -const fileMode = 0600 +const fileMode = 0644 func readVersion(dir string) (v uint, err error) { buf, err := ioutil.ReadFile(filepath.Join(dir, "version")) @@ -68,7 +68,7 @@ func writeCachedirTag(dir string) error { return errors.Wrap(err, "Lstat") } - f, err := fs.OpenFile(tagfile, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644) + f, err := fs.OpenFile(tagfile, os.O_CREATE|os.O_EXCL|os.O_WRONLY, fileMode) if err != nil { if os.IsExist(errors.Cause(err)) { return nil @@ -138,7 +138,7 @@ func New(id string, basedir string) (c *Cache, err error) { } if v < cacheVersion { - err = ioutil.WriteFile(filepath.Join(cachedir, "version"), []byte(fmt.Sprintf("%d", cacheVersion)), 0644) + err = ioutil.WriteFile(filepath.Join(cachedir, "version"), []byte(fmt.Sprintf("%d", cacheVersion)), fileMode) if err != nil { return nil, errors.Wrap(err, "WriteFile") }