2014-12-23 17:38:01 -05:00
|
|
|
// Send and receive files via SFTP using multiple download streams concurrently (for downloads).
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-12-29 18:58:23 -05:00
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
2014-12-23 17:38:01 -05:00
|
|
|
"errors"
|
|
|
|
"github.com/pkg/sftp"
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2014-12-29 18:58:23 -05:00
|
|
|
"time"
|
2014-12-23 17:38:01 -05:00
|
|
|
)
|
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
const (
|
|
|
|
// NoProgress no progress for current file
|
|
|
|
NoProgress int64 = -1
|
|
|
|
)
|
|
|
|
|
|
|
|
// Sync Keeps track of the connection information.
|
2014-12-23 17:38:01 -05:00
|
|
|
type Sync struct {
|
2015-01-24 11:45:28 -05:00
|
|
|
sshClient *ssh.Client
|
|
|
|
sftpClients []*sftp.Client
|
2014-12-23 17:38:01 -05:00
|
|
|
sftpClientCount int
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
// NewSync Create a new Sync object, connect to the SSH server, and create sftp clients
|
2014-12-23 17:38:01 -05:00
|
|
|
func NewSync(threads string, user string, pass string, server string, port string) (*Sync, error) {
|
|
|
|
// convert the threads input to an int
|
2014-12-29 18:58:23 -05:00
|
|
|
clientCount, err := strconv.Atoi(threads)
|
2014-12-23 17:38:01 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
if clientCount < 1 {
|
2014-12-23 17:38:01 -05:00
|
|
|
return nil, errors.New("Must have a thread count >= 1")
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
sshClient, err := newSSHClient(user, pass, server, port)
|
2014-12-23 17:38:01 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialize a total of client_count sftp clients
|
2014-12-29 18:58:23 -05:00
|
|
|
sftpClients := make([]*sftp.Client, clientCount)
|
|
|
|
for i := 0; i < clientCount; i++ {
|
|
|
|
sftpClient, err := sftp.NewClient(sshClient)
|
2014-12-23 17:38:01 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
sftpClients[i] = sftpClient
|
2014-12-23 17:38:01 -05:00
|
|
|
}
|
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
return &Sync{sshClient, sftpClients, clientCount}, nil
|
2014-12-23 17:38:01 -05:00
|
|
|
}
|
|
|
|
|
2015-01-24 11:45:28 -05:00
|
|
|
// Close Closes all of the ssh and sftp connections to the SSH server.
|
|
|
|
func (s Sync) Close() error {
|
|
|
|
var returnError error
|
|
|
|
for i := 0; i < s.sftpClientCount; i++ {
|
|
|
|
err := s.sftpClients[i].Close()
|
|
|
|
if err != nil {
|
|
|
|
returnError = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := s.sshClient.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnError
|
|
|
|
}
|
|
|
|
|
2014-12-23 17:38:01 -05:00
|
|
|
// Create a new SSH client instance and confirm that we can make sessions
|
|
|
|
func newSSHClient(user string, pass string, server string, port string) (*ssh.Client, error) {
|
2014-12-29 18:58:23 -05:00
|
|
|
sshConfig := &ssh.ClientConfig{
|
2014-12-23 17:38:01 -05:00
|
|
|
User: user,
|
|
|
|
Auth: []ssh.AuthMethod{
|
|
|
|
ssh.Password(pass),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-01-24 11:45:28 -05:00
|
|
|
sshClient, err := ssh.Dial("tcp", server+":"+port, sshConfig)
|
2014-12-23 17:38:01 -05:00
|
|
|
|
|
|
|
if err != nil {
|
2014-12-29 18:58:23 -05:00
|
|
|
return sshClient, err
|
2014-12-23 17:38:01 -05:00
|
|
|
}
|
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
session, err := sshClient.NewSession()
|
2014-12-23 17:38:01 -05:00
|
|
|
if err != nil {
|
2014-12-29 18:58:23 -05:00
|
|
|
return sshClient, err
|
2014-12-23 17:38:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
defer session.Close()
|
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
return sshClient, err
|
2014-12-23 17:38:01 -05:00
|
|
|
}
|
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
// SendFiles Send a list of files in the format of {"source_path": "destination"} to the SSH server. This does not handle directories.
|
2014-12-23 17:38:01 -05:00
|
|
|
func (s Sync) SendFiles(files map[string]string) error {
|
|
|
|
return SendFiles(s.sftpClients[0], files)
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
// SendFiles Send a list of files in the format of {"source_path": "destination"} to the SSH server. This does not handle directories.
|
|
|
|
func SendFiles(sftpClient *sftp.Client, files map[string]string) error {
|
|
|
|
for sourceFile, destinationFile := range files {
|
2014-12-23 17:38:01 -05:00
|
|
|
// 512KB buffer for reading/sending data
|
|
|
|
data := make([]byte, 524288)
|
|
|
|
|
|
|
|
// Open file that we will be sending
|
2014-12-29 18:58:23 -05:00
|
|
|
sourceData, err := os.Open(sourceFile)
|
2014-12-23 17:38:01 -05:00
|
|
|
if err != nil {
|
2014-12-29 18:58:23 -05:00
|
|
|
log.Println("SendFiles: Failed to open source file " + sourceFile)
|
2014-12-23 17:38:01 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the info of the file that we will be sending
|
2014-12-29 18:58:23 -05:00
|
|
|
sourceStat, err := sourceData.Stat()
|
2014-12-23 17:38:01 -05:00
|
|
|
if err != nil {
|
2014-12-29 18:58:23 -05:00
|
|
|
log.Println("SendFiles: Failed to stat source file " + sourceFile)
|
2014-12-23 17:38:01 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract the size of the file that we will be sending
|
2014-12-29 18:58:23 -05:00
|
|
|
sourceSize := sourceStat.Size()
|
2014-12-23 17:38:01 -05:00
|
|
|
// Create the destination file for the source file we're sending
|
2014-12-29 18:58:23 -05:00
|
|
|
newFile, err := sftpClient.Create(destinationFile)
|
2014-12-23 17:38:01 -05:00
|
|
|
if err != nil {
|
2014-12-29 18:58:23 -05:00
|
|
|
log.Println("SendFiles: Failed to create destination file " + destinationFile)
|
2014-12-23 17:38:01 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Track our position in reading/writing the file
|
2014-12-29 18:58:23 -05:00
|
|
|
var currentPosition int64
|
|
|
|
for currentPosition < sourceSize {
|
2014-12-23 17:38:01 -05:00
|
|
|
// If the next iteration will be greater than the file size, reduce to the data size
|
2015-01-24 11:45:28 -05:00
|
|
|
if currentPosition+int64(len(data)) > sourceSize {
|
|
|
|
data = make([]byte, sourceSize-currentPosition)
|
2014-12-23 17:38:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read data from the source file
|
2014-12-29 18:58:23 -05:00
|
|
|
read, err := sourceData.Read(data)
|
2014-12-23 17:38:01 -05:00
|
|
|
if err != nil {
|
|
|
|
// If it's the end of the file and we didn't read anything, break
|
|
|
|
if err == io.EOF {
|
|
|
|
if read == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the data from the source file to the destination file
|
2014-12-29 18:58:23 -05:00
|
|
|
_, err = newFile.Write(data)
|
2014-12-23 17:38:01 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the current position in the file
|
2014-12-29 18:58:23 -05:00
|
|
|
currentPosition += int64(read)
|
2014-12-23 17:38:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// close the source file
|
2014-12-29 18:58:23 -05:00
|
|
|
err = sourceData.Close()
|
2014-12-23 17:38:01 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// close the destination file
|
2014-12-29 18:58:23 -05:00
|
|
|
err = newFile.Close()
|
2014-12-23 17:38:01 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
// GetFile Get a file from the source_file path to be stored in destination_file path
|
|
|
|
func (s Sync) GetFile(sourceFile string, destinationFile string) error {
|
2014-12-23 17:38:01 -05:00
|
|
|
// Store channels for all the concurrent download parts
|
|
|
|
channels := make([]chan error, s.sftpClientCount)
|
|
|
|
|
|
|
|
// Make channels for all the concurrent downloads
|
|
|
|
for i := 0; i < s.sftpClientCount; i++ {
|
|
|
|
channels[i] = make(chan error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start the concurrent downloads
|
|
|
|
for i := 0; i < s.sftpClientCount; i++ {
|
2015-01-24 11:45:28 -05:00
|
|
|
go GetFile(s.sftpClients[i], sourceFile, destinationFile, i+1, s.sftpClientCount, channels[i])
|
2014-12-23 17:38:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Block until all downloads are completed or one errors
|
2014-12-29 18:58:23 -05:00
|
|
|
allDone := false
|
|
|
|
for !allDone {
|
|
|
|
allDone = true
|
|
|
|
for i, channel := range channels {
|
|
|
|
if channel == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2015-01-24 11:45:28 -05:00
|
|
|
case err := <-channel:
|
2014-12-29 18:58:23 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
channels[i] = nil
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
// still running
|
|
|
|
if allDone {
|
|
|
|
allDone = false
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2014-12-23 17:38:01 -05:00
|
|
|
}
|
2014-12-29 18:58:23 -05:00
|
|
|
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := destroyProgress(destinationFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-12-23 17:38:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
// GetFile Get a file from the source_file path to be stored in destination_file path.
|
2014-12-23 17:38:01 -05:00
|
|
|
// worker_number and work_total are not zero indexed, but 1 indexed
|
2015-01-24 11:45:28 -05:00
|
|
|
func GetFile(sftpClient *sftp.Client, sourceFile string, destinationFile string, workerNumber int, workerTotal int, com chan<- error) error {
|
2014-12-23 17:38:01 -05:00
|
|
|
// Open source_data for reading
|
2014-12-29 18:58:23 -05:00
|
|
|
sourceData, err := sftpClient.OpenFile(sourceFile, os.O_RDONLY)
|
2014-12-23 17:38:01 -05:00
|
|
|
if err != nil {
|
|
|
|
com <- err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get info for source_data
|
2014-12-29 18:58:23 -05:00
|
|
|
stat, err := sourceData.Stat()
|
2014-12-23 17:38:01 -05:00
|
|
|
if err != nil {
|
|
|
|
com <- err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract the size of source_data
|
2014-12-29 18:58:23 -05:00
|
|
|
statSize := stat.Size()
|
|
|
|
|
2014-12-23 17:38:01 -05:00
|
|
|
// Calculate which byte to start reading data from
|
2014-12-29 18:58:23 -05:00
|
|
|
start, err := getProgress(destinationFile, workerNumber)
|
|
|
|
if err != nil {
|
|
|
|
com <- err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if start == NoProgress {
|
|
|
|
if workerNumber == 1 {
|
|
|
|
start = 0
|
|
|
|
} else {
|
2015-01-24 11:45:28 -05:00
|
|
|
start = (statSize * int64(workerNumber-1)) / int64(workerTotal)
|
2014-12-29 18:58:23 -05:00
|
|
|
}
|
2014-12-23 17:38:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate which byte to stop reading data from
|
|
|
|
var stop int64
|
2014-12-29 18:58:23 -05:00
|
|
|
if workerNumber == workerTotal {
|
|
|
|
stop = statSize
|
2014-12-23 17:38:01 -05:00
|
|
|
} else {
|
2014-12-29 18:58:23 -05:00
|
|
|
stop = (statSize * int64(workerNumber)) / int64(workerTotal)
|
2014-12-23 17:38:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the new file for writing
|
2015-01-24 11:45:28 -05:00
|
|
|
newFile, err := os.OpenFile(destinationFile, os.O_WRONLY|os.O_CREATE, 0777)
|
2014-12-23 17:38:01 -05:00
|
|
|
if err != nil {
|
|
|
|
com <- err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Seek to the computed start point
|
2014-12-29 18:58:23 -05:00
|
|
|
offset, err := sourceData.Seek(start, 0)
|
2014-12-23 17:38:01 -05:00
|
|
|
if err != nil {
|
|
|
|
com <- err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Seeking messed up real bad
|
|
|
|
if offset != start {
|
2014-12-29 18:58:23 -05:00
|
|
|
err = errors.New("Returned incorrect offset for source " + sourceFile)
|
2014-12-23 17:38:01 -05:00
|
|
|
com <- err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Seek to the computed start point
|
2014-12-29 18:58:23 -05:00
|
|
|
offset, err = newFile.Seek(start, 0)
|
2014-12-23 17:38:01 -05:00
|
|
|
if err != nil {
|
|
|
|
com <- err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Seeking messed up real bad
|
|
|
|
if offset != start {
|
2014-12-29 18:58:23 -05:00
|
|
|
err = errors.New("Return incorrect offset for destination " + destinationFile)
|
2014-12-23 17:38:01 -05:00
|
|
|
com <- err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 512KB chunks
|
2014-12-29 18:58:23 -05:00
|
|
|
var dataSize int64 = 524288
|
2014-12-23 17:38:01 -05:00
|
|
|
// Change the size if the chunk is larger than the file
|
2014-12-29 18:58:23 -05:00
|
|
|
chunkDifference := stop - start
|
|
|
|
if chunkDifference < dataSize {
|
|
|
|
dataSize = chunkDifference
|
2014-12-23 17:38:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the buffer for reading/writing
|
2014-12-29 18:58:23 -05:00
|
|
|
data := make([]byte, dataSize)
|
|
|
|
var currentSize int64
|
|
|
|
for currentSize = start; currentSize < stop; currentSize += dataSize {
|
|
|
|
err = updateProgress(destinationFile, currentSize, workerNumber)
|
|
|
|
if err != nil {
|
|
|
|
com <- err
|
|
|
|
return err
|
|
|
|
}
|
2014-12-23 17:38:01 -05:00
|
|
|
|
|
|
|
// Adjust the size of the buffer if the next iteration will be greater than what has yet to be read
|
2015-01-24 11:45:28 -05:00
|
|
|
if currentSize+dataSize > stop {
|
2014-12-29 18:58:23 -05:00
|
|
|
dataSize = stop - currentSize
|
|
|
|
data = make([]byte, dataSize)
|
2014-12-23 17:38:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read the chunk
|
2014-12-29 18:58:23 -05:00
|
|
|
read, err := sourceData.Read(data)
|
2014-12-23 17:38:01 -05:00
|
|
|
if err != nil {
|
|
|
|
// Exit the loop if we're at the end of the file and no data was read
|
|
|
|
if err == io.EOF {
|
|
|
|
if read == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
com <- err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the chunk
|
2014-12-29 18:58:23 -05:00
|
|
|
_, err = newFile.Write(data)
|
2014-12-23 17:38:01 -05:00
|
|
|
if err != nil {
|
|
|
|
com <- err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
err = updateProgress(destinationFile, currentSize, workerNumber)
|
|
|
|
if err != nil {
|
|
|
|
com <- err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-12-23 17:38:01 -05:00
|
|
|
// Close out the files
|
2014-12-29 18:58:23 -05:00
|
|
|
err = sourceData.Close()
|
2014-12-23 17:38:01 -05:00
|
|
|
if err != nil {
|
|
|
|
com <- err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
err = newFile.Close()
|
2014-12-23 17:38:01 -05:00
|
|
|
if err != nil {
|
|
|
|
com <- err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
com <- nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
// GetPath Get a given directory or file defined by source_path and save it to destination_path
|
|
|
|
func (s Sync) GetPath(sourcePath string, destinationPath string) error {
|
2014-12-23 17:38:01 -05:00
|
|
|
// Get all the dirs and files underneath source_path
|
2014-12-29 18:58:23 -05:00
|
|
|
dirs, files, err := s.getChildren(sourcePath)
|
2014-12-23 17:38:01 -05:00
|
|
|
|
|
|
|
// Remove the trailing slash if it exists
|
2015-01-24 11:45:28 -05:00
|
|
|
if sourcePath[len(sourcePath)-1] == '/' {
|
|
|
|
sourcePath = sourcePath[:len(sourcePath)-1]
|
2014-12-23 17:38:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the parent path of source_path
|
2014-12-29 18:58:23 -05:00
|
|
|
sourceBase := filepath.Dir(sourcePath)
|
|
|
|
sourceBaseLen := len(sourceBase)
|
2014-12-23 17:38:01 -05:00
|
|
|
|
|
|
|
// Make all the directories in destination_path
|
|
|
|
for _, dir := range dirs {
|
2014-12-29 18:58:23 -05:00
|
|
|
dir = filepath.Join(destinationPath, filepath.FromSlash(dir[sourceBaseLen:]))
|
2014-12-23 17:38:01 -05:00
|
|
|
err = os.MkdirAll(dir, 0777)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get all the files and place them in destination_path
|
|
|
|
for _, file := range files {
|
2014-12-29 18:58:23 -05:00
|
|
|
newFile := filepath.Join(destinationPath, filepath.FromSlash(file[sourceBaseLen:]))
|
|
|
|
err = s.GetFile(file, newFile)
|
2014-12-23 17:38:01 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the directories and files underneath a given sftp root path
|
|
|
|
func (s Sync) getChildren(root string) ([]string, []string, error) {
|
|
|
|
// Used to walk through the path
|
|
|
|
walker := s.sftpClients[0].Walk(root)
|
|
|
|
|
|
|
|
// Keep track of the directories
|
2014-12-29 18:58:23 -05:00
|
|
|
var dirs []string
|
2014-12-23 17:38:01 -05:00
|
|
|
// Keep track of the files
|
2014-12-29 18:58:23 -05:00
|
|
|
var files []string
|
2014-12-23 17:38:01 -05:00
|
|
|
|
|
|
|
// Walk through the files and directories
|
|
|
|
for walker.Step() {
|
|
|
|
err := walker.Err()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
stat := walker.Stat()
|
|
|
|
if stat.IsDir() {
|
|
|
|
dirs = append(dirs, walker.Path())
|
|
|
|
} else {
|
|
|
|
files = append(files, walker.Path())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := walker.Err()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return dirs, files, nil
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
// Exists Determine if a directory, file, or link exists
|
2014-12-23 17:38:01 -05:00
|
|
|
func (s Sync) Exists(path string) (bool, error) {
|
|
|
|
_, err := s.sftpClients[0].Lstat(path)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err.Error() == "sftp: \"No such file\" (SSH_FX_NO_SUCH_FILE)" {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
2014-12-29 18:58:23 -05:00
|
|
|
|
|
|
|
func getProgress(filePath string, workerNumber int) (int64, error) {
|
|
|
|
file, err := os.Open(getProgressPath(filePath))
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return NoProgress, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fileStat, err := file.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if fileStat.Size() == 0 {
|
|
|
|
return NoProgress, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var progress int64
|
|
|
|
progressSize := int64(binary.Size(progress))
|
2015-01-24 11:45:28 -05:00
|
|
|
offset := progressSize * int64(workerNumber-1)
|
2014-12-29 18:58:23 -05:00
|
|
|
|
|
|
|
realOffset, err := file.Seek(offset, os.SEEK_SET)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if realOffset != offset {
|
|
|
|
return 0, errors.New("getProgress: Tried to seek to " + string(offset) + " but got " + string(realOffset) + " instead")
|
|
|
|
}
|
|
|
|
|
|
|
|
progressData := make([]byte, progressSize)
|
|
|
|
|
|
|
|
read, err := file.Read(progressData)
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
return NoProgress, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if int64(read) != progressSize {
|
|
|
|
return NoProgress, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err = binary.Read(bytes.NewReader(progressData), binary.BigEndian, &progress)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return progress, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateProgress(filePath string, written int64, workerNumber int) error {
|
2015-01-24 11:45:28 -05:00
|
|
|
file, err := os.OpenFile(getProgressPath(filePath), os.O_WRONLY|os.O_CREATE, 0777)
|
2014-12-29 18:58:23 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
writtenSize := int64(binary.Size(written))
|
2015-01-24 11:45:28 -05:00
|
|
|
offset := writtenSize * int64(workerNumber-1)
|
2014-12-29 18:58:23 -05:00
|
|
|
|
|
|
|
realOffset, err := file.Seek(offset, os.SEEK_SET)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if realOffset != offset {
|
|
|
|
return errors.New("updateProgress: Tried to seek to " + string(offset) + " but got " + string(realOffset) + " instead")
|
|
|
|
}
|
|
|
|
|
|
|
|
return binary.Write(file, binary.BigEndian, written)
|
|
|
|
}
|
|
|
|
|
|
|
|
func destroyProgress(filePath string) error {
|
|
|
|
err := os.Remove(getProgressPath(filePath))
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getProgressPath(filePath string) string {
|
2015-01-24 11:45:28 -05:00
|
|
|
return filepath.Join(filepath.Dir(filePath), "."+filepath.Base(filePath)+".progress")
|
2014-12-29 18:58:23 -05:00
|
|
|
}
|