2019-01-11 12:56:05 +00:00
|
|
|
// Copyright (C) 2019 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"time"
|
2020-07-28 09:13:15 +00:00
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
2019-01-11 12:56:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// fatal is the required common interface between *testing.B and *testing.T
|
|
|
|
type fatal interface {
|
|
|
|
Fatal(...interface{})
|
2019-02-05 18:01:05 +00:00
|
|
|
Helper()
|
2019-01-11 12:56:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type fatalOs struct {
|
|
|
|
fatal
|
|
|
|
}
|
|
|
|
|
2019-03-09 18:45:36 +00:00
|
|
|
func must(f fatal, err error) {
|
2019-02-05 18:01:05 +00:00
|
|
|
f.Helper()
|
2019-03-09 18:45:36 +00:00
|
|
|
if err != nil {
|
2019-01-11 12:56:05 +00:00
|
|
|
f.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-28 09:13:15 +00:00
|
|
|
func mustRemove(f fatal, err error) {
|
|
|
|
f.Helper()
|
|
|
|
if err != nil && !fs.IsNotExist(err) {
|
|
|
|
f.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-05 18:01:05 +00:00
|
|
|
func (f *fatalOs) Chmod(name string, mode os.FileMode) {
|
|
|
|
f.Helper()
|
2019-03-09 18:45:36 +00:00
|
|
|
must(f, os.Chmod(name, mode))
|
2019-01-11 12:56:05 +00:00
|
|
|
}
|
|
|
|
|
2019-02-05 18:01:05 +00:00
|
|
|
func (f *fatalOs) Chtimes(name string, atime time.Time, mtime time.Time) {
|
|
|
|
f.Helper()
|
2019-03-09 18:45:36 +00:00
|
|
|
must(f, os.Chtimes(name, atime, mtime))
|
2019-01-11 12:56:05 +00:00
|
|
|
}
|
|
|
|
|
2019-02-05 18:01:05 +00:00
|
|
|
func (f *fatalOs) Create(name string) *os.File {
|
|
|
|
f.Helper()
|
2019-01-11 12:56:05 +00:00
|
|
|
file, err := os.Create(name)
|
2019-03-09 18:45:36 +00:00
|
|
|
must(f, err)
|
2019-02-05 18:01:05 +00:00
|
|
|
return file
|
2019-01-11 12:56:05 +00:00
|
|
|
}
|
|
|
|
|
2019-02-05 18:01:05 +00:00
|
|
|
func (f *fatalOs) Mkdir(name string, perm os.FileMode) {
|
|
|
|
f.Helper()
|
2019-03-09 18:45:36 +00:00
|
|
|
must(f, os.Mkdir(name, perm))
|
2019-01-11 12:56:05 +00:00
|
|
|
}
|
|
|
|
|
2019-02-05 18:01:05 +00:00
|
|
|
func (f *fatalOs) MkdirAll(name string, perm os.FileMode) {
|
|
|
|
f.Helper()
|
2019-03-09 18:45:36 +00:00
|
|
|
must(f, os.MkdirAll(name, perm))
|
2019-01-11 12:56:05 +00:00
|
|
|
}
|
|
|
|
|
2019-02-05 18:01:05 +00:00
|
|
|
func (f *fatalOs) Remove(name string) {
|
|
|
|
f.Helper()
|
2019-01-11 12:56:05 +00:00
|
|
|
if err := os.Remove(name); err != nil && !os.IsNotExist(err) {
|
|
|
|
f.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-05 18:01:05 +00:00
|
|
|
func (f *fatalOs) RemoveAll(name string) {
|
|
|
|
f.Helper()
|
2019-01-11 12:56:05 +00:00
|
|
|
if err := os.RemoveAll(name); err != nil && !os.IsNotExist(err) {
|
|
|
|
f.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-05 18:01:05 +00:00
|
|
|
func (f *fatalOs) Rename(oldname, newname string) {
|
|
|
|
f.Helper()
|
2019-03-09 18:45:36 +00:00
|
|
|
must(f, os.Rename(oldname, newname))
|
2019-01-11 12:56:05 +00:00
|
|
|
}
|
|
|
|
|
2019-02-05 18:01:05 +00:00
|
|
|
func (f *fatalOs) Stat(name string) os.FileInfo {
|
|
|
|
f.Helper()
|
2019-01-11 12:56:05 +00:00
|
|
|
info, err := os.Stat(name)
|
2019-03-09 18:45:36 +00:00
|
|
|
must(f, err)
|
2019-02-05 18:01:05 +00:00
|
|
|
return info
|
2019-01-11 12:56:05 +00:00
|
|
|
}
|