Merge pull request #2322 from brualan/master

minor refactoring
This commit is contained in:
Alexander Neumann 2019-07-27 09:37:06 +02:00
commit cfa2ac69e0
17 changed files with 21 additions and 112 deletions

View file

@ -56,16 +56,6 @@ func verbose(f string, args ...interface{}) {
fmt.Printf(f, args...) 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) { func rm(file string) {
err := os.Remove(file) 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) { func mkdir(dir string) {
err := os.MkdirAll(dir, 0755) err := os.MkdirAll(dir, 0755)
if err != nil { 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 { func abs(dir string) string {
absDir, err := filepath.Abs(dir) absDir, err := filepath.Abs(dir)
if err != nil { if err != nil {

View file

@ -47,9 +47,7 @@ func ParseConfig(s string) (interface{}, error) {
return nil, errors.New("azure: invalid format: bucket name or path not found") return nil, errors.New("azure: invalid format: bucket name or path not found")
} }
container, path := data[0], path.Clean(data[1]) container, path := data[0], path.Clean(data[1])
if strings.HasPrefix(path, "/") { path = strings.TrimPrefix(path, "/")
path = path[1:]
}
cfg := NewConfig() cfg := NewConfig()
cfg.Container = container cfg.Container = container
cfg.Prefix = path cfg.Prefix = path

View file

@ -49,9 +49,7 @@ func ParseConfig(s string) (interface{}, error) {
bucket, path := data[0], path.Clean(data[1]) bucket, path := data[0], path.Clean(data[1])
if strings.HasPrefix(path, "/") { path = strings.TrimPrefix(path, "/")
path = path[1:]
}
cfg := NewConfig() cfg := NewConfig()
cfg.Bucket = bucket cfg.Bucket = bucket

View file

@ -116,8 +116,8 @@ func TestDefaultLayout(t *testing.T) {
want = append(want, filepath.Join(tempdir, "data", fmt.Sprintf("%02x", i))) want = append(want, filepath.Join(tempdir, "data", fmt.Sprintf("%02x", i)))
} }
sort.Sort(sort.StringSlice(want)) sort.Strings(want)
sort.Sort(sort.StringSlice(dirs)) sort.Strings(dirs)
if !reflect.DeepEqual(dirs, want) { if !reflect.DeepEqual(dirs, want) {
t.Fatalf("wrong paths returned, want:\n %v\ngot:\n %v", want, dirs) 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"), filepath.Join(path, "keys"),
} }
sort.Sort(sort.StringSlice(want)) sort.Strings(want)
sort.Sort(sort.StringSlice(dirs)) sort.Strings(dirs)
if !reflect.DeepEqual(dirs, want) { if !reflect.DeepEqual(dirs, want) {
t.Fatalf("wrong paths returned, want:\n %v\ngot:\n %v", want, dirs) 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"), filepath.Join(path, "key"),
} }
sort.Sort(sort.StringSlice(want)) sort.Strings(want)
sort.Sort(sort.StringSlice(dirs)) sort.Strings(dirs)
if !reflect.DeepEqual(dirs, want) { if !reflect.DeepEqual(dirs, want) {
t.Fatalf("wrong paths returned, want:\n %v\ngot:\n %v", want, dirs) t.Fatalf("wrong paths returned, want:\n %v\ngot:\n %v", want, dirs)

View file

@ -25,25 +25,6 @@ var _ restic.Backend = &Local{}
const defaultLayout = "default" 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. // Open opens the local backend as specified by config.
func Open(cfg Config) (*Local, error) { func Open(cfg Config) (*Local, error) {
debug.Log("open local backend at %v (layout %q)", cfg.Path, cfg.Layout) debug.Log("open local backend at %v (layout %q)", cfg.Path, cfg.Layout)

View file

@ -231,22 +231,6 @@ func (be *Backend) Path() string {
return be.cfg.Prefix 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. // Save stores data in the backend at the handle.
func (be *Backend) Save(ctx context.Context, h restic.Handle, rd restic.RewindReader) error { func (be *Backend) Save(ctx context.Context, h restic.Handle, rd restic.RewindReader) error {
debug.Log("Save %v", h) debug.Log("Save %v", h)

View file

@ -17,8 +17,6 @@ import (
"github.com/ncw/swift" "github.com/ncw/swift"
) )
const connLimit = 10
// beSwift is a backend which stores the data on a swift endpoint. // beSwift is a backend which stores the data on a swift endpoint.
type beSwift struct { type beSwift struct {
conn *swift.Connection conn *swift.Connection

View file

@ -24,7 +24,7 @@ type Cache struct {
} }
const dirMode = 0700 const dirMode = 0700
const fileMode = 0600 const fileMode = 0644
func readVersion(dir string) (v uint, err error) { func readVersion(dir string) (v uint, err error) {
buf, err := ioutil.ReadFile(filepath.Join(dir, "version")) buf, err := ioutil.ReadFile(filepath.Join(dir, "version"))
@ -68,7 +68,7 @@ func writeCachedirTag(dir string) error {
return errors.Wrap(err, "Lstat") 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 err != nil {
if os.IsExist(errors.Cause(err)) { if os.IsExist(errors.Cause(err)) {
return nil return nil
@ -138,7 +138,7 @@ func New(id string, basedir string) (c *Cache, err error) {
} }
if v < cacheVersion { 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 { if err != nil {
return nil, errors.Wrap(err, "WriteFile") return nil, errors.Wrap(err, "WriteFile")
} }
@ -175,11 +175,7 @@ const MaxCacheAge = 30 * 24 * time.Hour
func validCacheDirName(s string) bool { func validCacheDirName(s string) bool {
r := regexp.MustCompile(`^[a-fA-F0-9]{64}$`) r := regexp.MustCompile(`^[a-fA-F0-9]{64}$`)
if !r.MatchString(s) { return r.MatchString(s)
return false
}
return true
} }
// listCacheDirs returns the list of cache directories. // listCacheDirs returns the list of cache directories.

View file

@ -214,9 +214,5 @@ func (c *Cache) Has(h restic.Handle) bool {
} }
_, err := fs.Stat(c.filename(h)) _, err := fs.Stat(c.filename(h))
if err == nil { return err == nil
return true
}
return false
} }

View file

@ -72,8 +72,8 @@ func verifyDirectoryContents(t testing.TB, fs FS, dir string, want []string) {
t.Fatal(err) t.Fatal(err)
} }
sort.Sort(sort.StringSlice(want)) sort.Strings(want)
sort.Sort(sort.StringSlice(entries)) sort.Strings(entries)
if !cmp.Equal(want, entries) { if !cmp.Equal(want, entries) {
t.Error(cmp.Diff(want, entries)) t.Error(cmp.Diff(want, entries))

View file

@ -22,10 +22,8 @@ func Register(ns string, cfg interface{}) {
// List returns a list of all registered options (using Register()). // List returns a list of all registered options (using Register()).
func List() (list []Help) { func List() (list []Help) {
list = make([]Help, 0, len(opts)) list = make([]Help, len(opts))
for _, opt := range opts { copy(list, opts)
list = append(list, opt)
}
return list return list
} }

View file

@ -696,8 +696,3 @@ func (node *Node) fillTimes(stat statT) {
node.ChangeTime = time.Unix(ctim.Unix()) node.ChangeTime = time.Unix(ctim.Unix())
node.AccessTime = time.Unix(atim.Unix()) node.AccessTime = time.Unix(atim.Unix())
} }
func changeTime(stat statT) time.Time {
ctim := stat.ctim()
return time.Unix(ctim.Unix())
}

View file

@ -36,16 +36,6 @@ func (TagList) Type() string {
// TagLists consists of several TagList. // TagLists consists of several TagList.
type TagLists []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 { func (l TagLists) String() string {
return fmt.Sprintf("%v", []TagList(l)) return fmt.Sprintf("%v", []TagList(l))
} }

View file

@ -176,7 +176,7 @@ func (b *Backup) update(total, processed counter, errors uint, currentFiles map[
for filename := range currentFiles { for filename := range currentFiles {
lines = append(lines, filename) lines = append(lines, filename)
} }
sort.Sort(sort.StringSlice(lines)) sort.Strings(lines)
lines = append([]string{status}, lines...) lines = append([]string{status}, lines...)
b.term.SetStatus(lines) b.term.SetStatus(lines)

View file

@ -159,7 +159,7 @@ func (b *Backup) update(total, processed counter, errors uint, currentFiles map[
for filename := range currentFiles { for filename := range currentFiles {
status.CurrentFiles = append(status.CurrentFiles, filename) status.CurrentFiles = append(status.CurrentFiles, filename)
} }
sort.Sort(sort.StringSlice(status.CurrentFiles)) sort.Strings(status.CurrentFiles)
json.NewEncoder(b.StdioWrapper.Stdout()).Encode(status) json.NewEncoder(b.StdioWrapper.Stdout()).Encode(status)
} }

View file

@ -129,7 +129,7 @@ func (t *Table) Write(w io.Writer) error {
return err return err
} }
row = append(row, string(buf.Bytes())) row = append(row, buf.String())
buf.Reset() buf.Reset()
} }
lines = append(lines, row) lines = append(lines, row)

View file

@ -154,7 +154,7 @@ foo 2018-08-19 22:22:22 xxx other /home/user/other
} }
want := strings.TrimLeft(test.output, "\n") 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()) t.Errorf("wrong output\n---- want ---\n%s\n---- got ---\n%s\n-------\n", want, buf.Bytes())
} }
}) })