-
Notifications
You must be signed in to change notification settings - Fork 1
/
scp.go
187 lines (158 loc) · 3.83 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright (c) 2016 Niklas Wolber
// This file is licensed under the MIT license.
// See the LICENSE file for more information.
package scp
import (
"bufio"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"time"
"github.com/djherbis/atime"
)
var (
errInvalidLength = errors.New("invalid length")
)
func args(command string) (name string, recursive, transfer, source, verbose, times bool, err error) {
parts := strings.SplitN(command, " ", -1)
if len(parts) < 2 {
err = errors.New("expected at least 'scp' and one parameter")
return
}
set := flag.NewFlagSet(parts[0], flag.ContinueOnError)
set.BoolVar(&recursive, "r", false, "recursive")
set.BoolVar(&transfer, "t", false, "sink mode")
set.BoolVar(&source, "f", false, "source mode")
set.BoolVar(&verbose, "v", false, "verbose")
set.BoolVar(×, "p", false, "include access and modification timestamps")
set.Bool("d", false, "dummy value, currently ignored")
err = set.Parse(parts[1:])
name = set.Arg(0)
return
}
type scpImp struct {
name, dir string
sink, source bool
times, recursive bool
verbose bool
in *bufio.Reader
out io.Writer
l logger
mTime, aTime time.Time
timeSet bool
openFile func(name string, flag int, perm os.FileMode) (io.ReadWriteCloser, error)
mkdir func(name string, perm os.FileMode) error
chtimes func(name string, aTime, mTime time.Time) error
stat func(name string) (FileInfo, error)
readDir func(name string) ([]FileInfo, error)
}
// New starts a new SCP file transfer.
func New(command string, in io.Reader, out io.Writer, verbose bool) error {
s, err := scp(command, in, out, verbose)
if err != nil {
return err
}
return s.run()
}
func scp(command string, in io.Reader, out io.Writer, verbose bool) (*scpImp, error) {
var (
s scpImp
err error
)
s.name, s.recursive, s.sink, s.source, s.verbose, s.times, err = args(command)
if err != nil {
return nil, err
}
s.verbose = s.verbose || verbose
path, err := filepath.Abs(s.name)
if err != nil {
return nil, err
}
s.dir = path
s.in = bufio.NewReader(in)
s.out = out
s.openFile = openFile
s.mkdir = os.MkdirAll
s.chtimes = os.Chtimes
s.stat = stat
s.readDir = readDir
output := ioutil.Discard
if s.verbose {
output = os.Stderr
}
s.l = log.New(output, "scp ", log.Flags())
s.l.Println(command)
return &s, nil
}
func (s *scpImp) run() error {
var err error
if s.sink && s.source {
return errors.New("either -f or -t can be specified")
}
if s.sink {
err = s.runSink()
} else if s.source {
err = s.runSource()
} else {
return errors.New("either -f or -t has to be specified")
}
if err != nil {
fmt.Fprintf(s.out, "\x02%s\n", err)
}
return err
}
type fileInfo struct {
os.FileInfo
}
func (f *fileInfo) AccessTime() time.Time {
return atime.Get(f.FileInfo)
}
// FileInfo provides informations about a file system object,
// like files or directories.
type FileInfo interface {
os.FileInfo
AccessTime() time.Time
}
func openFile(name string, flag int, perm os.FileMode) (io.ReadWriteCloser, error) {
return os.OpenFile(name, flag, perm)
}
func stat(name string) (FileInfo, error) {
f, err := os.Stat(name)
if err != nil {
return nil, err
}
fi := fileInfo{
FileInfo: f,
}
return &fi, nil
}
func readDir(name string) ([]FileInfo, error) {
f, err := ioutil.ReadDir(name)
if err != nil {
return nil, err
}
files := make([]FileInfo, len(f))
for i := 0; i < len(f); i++ {
files[i] = &fileInfo{
FileInfo: f[i],
}
}
return files, nil
}
func filePath(commandPath, filePath string) string {
if filepath.Dir(commandPath) == commandPath {
return filepath.Join(commandPath, filePath)
}
return commandPath
}
type scpMessage interface {
fmt.Stringer
binders() []binder
process(s *scpImp) error
}