mirror of
https://github.com/octoleo/restic.git
synced 2024-11-04 12:34:13 +00:00
sftp: Reduce duplicate code, add error check
This commit is contained in:
parent
0bbad683c5
commit
1528d1ca83
@ -64,6 +64,18 @@ func startClient(program string, args ...string) (*SFTP, error) {
|
|||||||
return &SFTP{c: client, cmd: cmd}, nil
|
return &SFTP{c: client, cmd: cmd}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func paths(dir string) []string {
|
||||||
|
return []string{
|
||||||
|
dir,
|
||||||
|
Join(dir, backend.Paths.Data),
|
||||||
|
Join(dir, backend.Paths.Snapshots),
|
||||||
|
Join(dir, backend.Paths.Index),
|
||||||
|
Join(dir, backend.Paths.Locks),
|
||||||
|
Join(dir, backend.Paths.Keys),
|
||||||
|
Join(dir, backend.Paths.Temp),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Open opens an sftp backend. When the command is started via
|
// Open opens an sftp backend. When the command is started via
|
||||||
// exec.Command, it is expected to speak sftp on stdin/stdout. The backend
|
// exec.Command, it is expected to speak sftp on stdin/stdout. The backend
|
||||||
// is expected at the given path.
|
// is expected at the given path.
|
||||||
@ -74,16 +86,7 @@ func Open(dir string, program string, args ...string) (*SFTP, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// test if all necessary dirs and files are there
|
// test if all necessary dirs and files are there
|
||||||
items := []string{
|
for _, d := range paths(dir) {
|
||||||
dir,
|
|
||||||
Join(dir, backend.Paths.Data),
|
|
||||||
Join(dir, backend.Paths.Snapshots),
|
|
||||||
Join(dir, backend.Paths.Index),
|
|
||||||
Join(dir, backend.Paths.Locks),
|
|
||||||
Join(dir, backend.Paths.Keys),
|
|
||||||
Join(dir, backend.Paths.Temp),
|
|
||||||
}
|
|
||||||
for _, d := range items {
|
|
||||||
if _, err := sftp.c.Lstat(d); err != nil {
|
if _, err := sftp.c.Lstat(d); err != nil {
|
||||||
return nil, fmt.Errorf("%s does not exist", d)
|
return nil, fmt.Errorf("%s does not exist", d)
|
||||||
}
|
}
|
||||||
@ -118,16 +121,6 @@ func Create(dir string, program string, args ...string) (*SFTP, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
dirs := []string{
|
|
||||||
dir,
|
|
||||||
Join(dir, backend.Paths.Data),
|
|
||||||
Join(dir, backend.Paths.Snapshots),
|
|
||||||
Join(dir, backend.Paths.Index),
|
|
||||||
Join(dir, backend.Paths.Locks),
|
|
||||||
Join(dir, backend.Paths.Keys),
|
|
||||||
Join(dir, backend.Paths.Temp),
|
|
||||||
}
|
|
||||||
|
|
||||||
// test if config file already exists
|
// test if config file already exists
|
||||||
_, err = sftp.c.Lstat(Join(dir, backend.Paths.Config))
|
_, err = sftp.c.Lstat(Join(dir, backend.Paths.Config))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@ -135,7 +128,7 @@ func Create(dir string, program string, args ...string) (*SFTP, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// create paths for data, refs and temp blobs
|
// create paths for data, refs and temp blobs
|
||||||
for _, d := range dirs {
|
for _, d := range paths(dir) {
|
||||||
err = sftp.mkdirAll(d, backend.Modes.Dir)
|
err = sftp.mkdirAll(d, backend.Modes.Dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -457,7 +450,8 @@ func (r *SFTP) Close() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
r.c.Close()
|
err := r.c.Close()
|
||||||
|
debug.Log("sftp.Close", "Close returned error %v", err)
|
||||||
|
|
||||||
if err := r.cmd.Process.Kill(); err != nil {
|
if err := r.cmd.Process.Kill(); err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -38,8 +38,7 @@ func init() {
|
|||||||
|
|
||||||
for _, dir := range strings.Split(TestSFTPPath, ":") {
|
for _, dir := range strings.Split(TestSFTPPath, ":") {
|
||||||
testpath := filepath.Join(dir, "sftp-server")
|
testpath := filepath.Join(dir, "sftp-server")
|
||||||
fd, err := os.Open(testpath)
|
_, err := os.Stat(testpath)
|
||||||
fd.Close()
|
|
||||||
if !os.IsNotExist(err) {
|
if !os.IsNotExist(err) {
|
||||||
sftpserver = testpath
|
sftpserver = testpath
|
||||||
break
|
break
|
||||||
|
Loading…
Reference in New Issue
Block a user