-
Notifications
You must be signed in to change notification settings - Fork 1
/
scp.go
122 lines (97 loc) · 2.72 KB
/
scp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package scp
import (
"fmt"
"golang.org/x/crypto/ssh"
"io"
"io/ioutil"
"os"
"path/filepath"
)
// Copy `from` to `target`
func Copy(session *ssh.Session, from, target string) error {
return DefaultOptions.Copy(session, from, target)
}
func CopyFile(session *ssh.Session, file io.Reader, filename string, size int64, mode os.FileMode, target string) error {
return DefaultOptions.CopyFile(session, file, filename, size, mode, target)
}
func CopyFolder(session *ssh.Session, from string, mode os.FileMode, target string) error {
return DefaultOptions.CopyFolder(session, from, mode, target)
}
// Copy `from` to `target`
func (o *Options) Copy(session *ssh.Session, from, target string) error {
stat, err := os.Stat(from)
if err != nil {
return fmt.Errorf("cannot stat file %s: %w", from, err)
}
if stat.IsDir() {
return o.CopyFolder(session, from, stat.Mode(), target)
}
f, err := os.Open(from)
if err != nil {
return fmt.Errorf("cannot open file %s: %w", from, err)
}
defer f.Close()
return o.CopyFile(session, f, stat.Name(), stat.Size(), stat.Mode(), target)
}
func (o *Options) CopyFile(session *ssh.Session, file io.Reader, filename string, size int64, mode os.FileMode, target string) error {
client, err := o.NewClient(session)
if err != nil {
return err
}
err = client.Start(target, false)
if err != nil {
return err
}
return o.copyFile(client, ConvertFileModeToPermString(mode), size, filename, file)
}
func (o *Options) copyFile(client *RemoteClient, perm string, size int64, filename string, file io.Reader) error {
err := client.WriteFile(perm, size, filename, file)
if err != nil {
return err
}
return nil
}
func (o *Options) CopyFolder(session *ssh.Session, from string, mode os.FileMode, target string) error {
client, err := o.NewClient(session)
if err != nil {
return err
}
err = client.Start(target, true)
if err != nil {
return err
}
return o.copyFolder(client, ConvertFileModeToPermString(mode), from)
}
func (o *Options) copyFolder(client *RemoteClient, perm string, path string) error {
err := client.WriteDirectoryStart(perm, filepath.Base(path))
if err != nil {
return err
}
files, err := ioutil.ReadDir(path)
if err != nil {
return err
}
for _, file := range files {
if file.IsDir() {
err = o.copyFolder(client, ConvertFileModeToPermString(file.Mode()), filepath.Join(path, file.Name()))
if err != nil {
return err
}
} else {
f, err := os.Open(filepath.Join(path, file.Name()))
if err != nil {
return err
}
err = o.copyFile(client, ConvertFileModeToPermString(file.Mode()), file.Size(), file.Name(), f)
f.Close()
if err != nil {
return err
}
}
}
err = client.WriteDirectoryEnd()
if err != nil {
return err
}
return nil
}