-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.go
186 lines (162 loc) · 5.47 KB
/
files.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
package sftpClient
import (
"fmt"
"github.com/pkg/sftp"
log "github.com/sirupsen/logrus"
"io"
"os"
"time"
)
// GetFile retreives a file from remote location
func GetFile(sftpClient *sftp.Client, localFilename string, remoteFilename string) error {
stats, err := sftpClient.Lstat(remoteFilename)
if err != nil {
return fmt.Errorf("Could not determine type of file 'remote:%s': %v\n", remoteFilename, err)
}
if stats.IsDir() {
if err := os.Mkdir(localFilename, 0755); err != nil {
return fmt.Errorf("Could not create folder 'local:%s': %v\n", localFilename, err)
}
return nil
}
remote, err := sftpClient.Open(remoteFilename)
if err != nil {
return fmt.Errorf("Could not open file 'remote:%s': %v\n", remoteFilename, err)
}
defer remote.Close()
local, err := os.Create(localFilename)
if err != nil {
return fmt.Errorf("Could not open file 'local:%s': %v\n", localFilename, err)
}
defer local.Close()
if _, err := io.Copy(local, remote); err != nil {
return fmt.Errorf("Could not copy 'remote:%s' to 'local:%s': %v\n", remoteFilename, localFilename, err)
}
log.Infof("download: %s", remoteFilename)
return nil
}
// PutFile uploads a local file to remote location
func PutFile(sftpClient *sftp.Client, remoteFileName string, localFileName string) error {
start := time.Now()
localFile, err := os.Open(localFileName)
if err != nil {
return fmt.Errorf("Could not open file 'local:%s': %v\n", localFileName, err)
}
defer localFile.Close()
stats, err := localFile.Stat()
if err != nil {
return fmt.Errorf("Could not stat file 'local:%s': %v\n", localFileName, err)
}
if stats.IsDir() {
return CreateDirHierarchy(sftpClient, remoteFileName)
}
remoteFile, err := sftpClient.Create(remoteFileName)
if err != nil {
return fmt.Errorf("Could not create file 'remote:%s': %v\n", remoteFileName, err)
}
defer remoteFile.Close()
if _, err := io.Copy(remoteFile, localFile); err != nil {
fmt.Printf("Could not copy data from 'local:%s' to 'remote:%s': %v\n", localFileName, remoteFileName, err)
}
//if err := sftpClient.Chmod(remoteFileName, 0644); err != nil {
// return fmt.Errorf("Could not set file permissions on 'remote:%s': %v\n", remoteFileName, err)
//}
log.Infof("upload: %s (%v)", localFileName, time.Now().Sub(start))
return nil
}
//copyToDest copies the src file to dst and delete local file
func copyToDest(src string, dst string) error {
// Open the source file for reading
s, err := os.Open(src)
if err != nil {
log.Printf("ERROR: copyToDest: Could not read file: %v\r\n", src)
log.Printf("%v\r\n", err)
return err
}
defer s.Close()
// Open the destination file for writing
d, err := os.Create(dst)
if err != nil {
log.Printf("ERROR: copyToDest: Could not create remote file: %v\r\n", dst)
return err
}
// Copy the contents of the source file into the destination file
if _, err := io.Copy(d, s); err != nil {
log.Printf("ERROR: copyToDest: Error in copying file to %v\r\n", dst)
d.Close()
return err
}
// Return any errors that result from closing the destination file
// Will return nil if no errors occurred
if err := d.Close(); err != nil {
log.Printf("ERROR: copyToDest: Error closing remote file: %v\r\n", dst)
}
if err := s.Close(); err != nil {
log.Printf("ERROR: copyToDest: Error closing local file: %v\r\n", src)
}
log.Printf("copyToDest: File: %v copied to %v\r\n", src, dst)
//return nil if success
return nil
}
//moveToDest will use copyToDest to copy file and then delete the source file
func MoveToDest(src string, dst string) error {
err := copyToDest(src, dst)
if err != nil {
log.Printf("ERROR: moveToDest: ERROR in copying file: %v\r\n", src)
return err
}
log.Printf("moveToDest: Deleting File: %v\r\n", src)
err1 := os.Remove(src)
if err1 != nil {
log.Printf("ERROR: moveToDest: Error in Deleting File: %v\r\n", src)
return err1
}
return nil
}
//copyToDest copies the src file to dst and delete local file
func copyRemoteToDest(sftpClient *sftp.Client, src string, dst string) error {
// Open the source file for reading
s, err := sftpClient.Open(src)
if err != nil {
log.Printf("ERROR: copyToDest: Could not read file: %v\r\n", src)
log.Printf("%v\r\n", err)
return err
}
defer s.Close()
// Open the destination file for writing
d, err := sftpClient.Create(dst)
if err != nil {
log.Printf("ERROR: copyToDest: Could not create remote file: %v\r\n", dst)
return err
}
// Copy the contents of the source file into the destination file
if _, err := io.Copy(d, s); err != nil {
fmt.Printf("Could not copy data from 'local:%s' to 'remote:%s': %v\n", s, d, err)
}
// Return any errors that result from closing the destination file
// Will return nil if no errors occurred
if err := d.Close(); err != nil {
log.Printf("ERROR: copyToDest: Error closing remote file: %v\r\n", dst)
}
if err := s.Close(); err != nil {
log.Printf("ERROR: copyToDest: Error closing local file: %v\r\n", src)
}
log.Printf("copyToDest: File: %v copied to %v\r\n", src, dst)
//return nil if success
return nil
}
//moveToDest will use copyToDest to copy file and then delete the source file
func MoveRemoteToDest(sftpClient *sftp.Client, src string, dst string) error {
err := copyRemoteToDest(sftpClient, src, dst)
if err != nil {
log.Printf("ERROR: moveToDest: ERROR in copying file: %v\r\n", src)
return err
}
log.Printf("moveToDest: Deleting File: %v\r\n", src)
err1 := sftpClient.Remove(src)
if err1 != nil {
log.Printf("ERROR: moveToDest: Error in Deleting File: %v\r\n", src)
return err1
}
return nil
}