2023-09-07 15:20:19 +02:00
|
|
|
package tools
|
|
|
|
|
|
|
|
import (
|
2023-09-22 15:50:03 +02:00
|
|
|
"errors"
|
|
|
|
"io"
|
2023-09-07 15:20:19 +02:00
|
|
|
"io/fs"
|
|
|
|
"os"
|
2023-09-22 15:50:03 +02:00
|
|
|
"os/exec"
|
2023-09-07 15:20:19 +02:00
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2023-09-22 10:54:10 +02:00
|
|
|
"strings"
|
2023-09-07 15:20:19 +02:00
|
|
|
"time"
|
|
|
|
"velvettear/gosync/log"
|
|
|
|
"velvettear/gosync/settings"
|
|
|
|
)
|
|
|
|
|
2023-09-22 15:50:03 +02:00
|
|
|
var sourceFiles map[string]int64
|
2023-09-07 15:20:19 +02:00
|
|
|
|
|
|
|
// unexported function(s)
|
2023-09-22 15:50:03 +02:00
|
|
|
func getSourceFiles() map[string]int64 {
|
2023-09-07 15:20:19 +02:00
|
|
|
timestamp := time.Now()
|
2023-09-22 15:50:03 +02:00
|
|
|
sourceFiles = map[string]int64{}
|
2023-09-22 10:54:10 +02:00
|
|
|
if settings.SourceIsRemote() {
|
2023-09-22 15:50:03 +02:00
|
|
|
log.Info("getting the list of remote source files...")
|
|
|
|
error := fillRemoteSourceFiles()
|
|
|
|
if error != nil {
|
|
|
|
log.Fatal("encountered an error getting the list of remote source files", error.Error())
|
|
|
|
}
|
2023-09-07 15:20:19 +02:00
|
|
|
} else {
|
2023-09-22 10:54:10 +02:00
|
|
|
source, _ := strings.CutSuffix(settings.Source, "/*")
|
|
|
|
stats, error := os.Stat(source)
|
|
|
|
if error != nil {
|
2023-09-22 15:50:03 +02:00
|
|
|
log.Fatal("encountered an error getting stats for source", error.Error())
|
2023-09-22 10:54:10 +02:00
|
|
|
}
|
|
|
|
if stats.IsDir() {
|
|
|
|
log.Info("scanning source...", source)
|
|
|
|
filepath.WalkDir(source, fillSourceFiles)
|
|
|
|
} else {
|
2023-09-22 15:50:03 +02:00
|
|
|
sourceFiles[settings.Source] = stats.Size()
|
2023-09-22 10:54:10 +02:00
|
|
|
}
|
2023-09-07 15:20:19 +02:00
|
|
|
}
|
2023-09-22 10:54:10 +02:00
|
|
|
log.InfoTimed("found "+strconv.Itoa(len(sourceFiles))+" source files", timestamp.UnixMilli())
|
2023-09-07 15:20:19 +02:00
|
|
|
return sourceFiles
|
|
|
|
}
|
|
|
|
|
2023-09-22 15:50:03 +02:00
|
|
|
func fillRemoteSourceFiles() error {
|
|
|
|
var arguments []string
|
|
|
|
if len(settings.Password) > 0 {
|
|
|
|
arguments = append(arguments, "-p", settings.Password)
|
|
|
|
}
|
|
|
|
arguments = append(arguments, "ssh")
|
|
|
|
remote, path, _ := strings.Cut(settings.Source, ":")
|
|
|
|
path, _ = strings.CutSuffix(path, "/*")
|
|
|
|
if len(settings.User) > 0 {
|
|
|
|
remote = settings.User + "@" + remote
|
|
|
|
}
|
|
|
|
arguments = append(arguments, remote, "find", "\""+path+"\"", "-type", "f", "-exec", "du", "-b", "{}", "\\;")
|
|
|
|
cmd := exec.Command("sshpass", arguments...)
|
|
|
|
stdout, stdoutError := cmd.StdoutPipe()
|
|
|
|
stderr, stderrError := cmd.StderrPipe()
|
|
|
|
cmd.Start()
|
|
|
|
if stdoutError != nil {
|
|
|
|
return stdoutError
|
|
|
|
}
|
|
|
|
if stderrError != nil {
|
|
|
|
return stderrError
|
|
|
|
}
|
|
|
|
outBytes, stdoutError := io.ReadAll(stdout)
|
|
|
|
if stdoutError != nil {
|
|
|
|
return stdoutError
|
|
|
|
}
|
|
|
|
errorBytes, stderrError := io.ReadAll(stderr)
|
|
|
|
if stderrError != nil {
|
|
|
|
return stderrError
|
|
|
|
}
|
|
|
|
cmd.Wait()
|
|
|
|
error := strings.TrimSpace(string(errorBytes))
|
|
|
|
if len(error) > 0 {
|
|
|
|
return errors.New(error)
|
|
|
|
}
|
|
|
|
for _, line := range strings.Split(string(outBytes), "\n") {
|
|
|
|
if !strings.Contains(line, "\t") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
parts := strings.Split(line, "\t")
|
|
|
|
size, error := strconv.ParseInt(parts[0], 10, 64)
|
|
|
|
if error != nil {
|
|
|
|
log.Warning("encountered an error getting the file size for file '"+path+"'", error.Error())
|
|
|
|
}
|
|
|
|
sourceFiles[parts[1]] = size
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-07 15:20:19 +02:00
|
|
|
func fillSourceFiles(path string, dir fs.DirEntry, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if dir.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
2023-09-22 15:50:03 +02:00
|
|
|
stats, error := os.Stat(path)
|
|
|
|
if error != nil {
|
|
|
|
log.Fatal("encountered an error getting stats for file", error.Error())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
sourceFiles[path] = stats.Size()
|
2023-09-07 15:20:19 +02:00
|
|
|
return nil
|
|
|
|
}
|
2023-09-22 15:50:03 +02:00
|
|
|
|
|
|
|
func getSourceFileSize() int64 {
|
|
|
|
var total int64
|
|
|
|
for _, size := range sourceFiles {
|
|
|
|
total += size
|
|
|
|
}
|
|
|
|
return total
|
|
|
|
}
|