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...)
}
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 {

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")
}
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

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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")
}
@ -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.

View File

@ -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
}

View File

@ -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))

View File

@ -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
}

View File

@ -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())
}

View File

@ -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))
}

View File

@ -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)

View File

@ -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)
}

View File

@ -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)

View File

@ -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())
}
})