179 lines
4.8 KiB
Go
179 lines
4.8 KiB
Go
package rsync
|
|
|
|
import (
|
|
"math"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"syscall"
|
|
)
|
|
|
|
// The upper bound is independent of transfer order: all positive growth plus
|
|
// one complete temporary file. Shrinks and end-of-run deletions earn no credit.
|
|
// Old sparse files earn only their allocated bytes, never their logical size.
|
|
type spaceEstimate struct {
|
|
target string
|
|
growth, largest, files, bytes, entries int64
|
|
problem string
|
|
sawStats bool
|
|
sawFiles bool
|
|
device uint64
|
|
}
|
|
|
|
func newSpaceEstimate(target string) *spaceEstimate {
|
|
p, err := filepath.EvalSymlinks(target)
|
|
e := &spaceEstimate{target: p}
|
|
if err != nil {
|
|
e.problem = "Zielpfad nicht lesbar: " + err.Error()
|
|
}
|
|
if err == nil {
|
|
info, statErr := os.Stat(p)
|
|
if statErr != nil {
|
|
e.problem = statErr.Error()
|
|
} else if st, ok := info.Sys().(*syscall.Stat_t); ok {
|
|
e.device = uint64(st.Dev)
|
|
} else {
|
|
e.problem = "Zieldateisystem nicht bestimmbar"
|
|
}
|
|
}
|
|
return e
|
|
}
|
|
|
|
// rsync escapes control/non-ASCII bytes as \#ooo. Decode exactly once: a literal
|
|
// backslash preceding #ooo is itself escaped, so recursive decoding is unsafe.
|
|
func decodeRsyncName(s string) string {
|
|
var b strings.Builder
|
|
for i := 0; i < len(s); {
|
|
if i+5 <= len(s) && s[i] == '\\' && s[i+1] == '#' {
|
|
if n, err := strconv.ParseUint(s[i+2:i+5], 8, 8); err == nil {
|
|
b.WriteByte(byte(n))
|
|
i += 5
|
|
continue
|
|
}
|
|
}
|
|
b.WriteByte(s[i])
|
|
i++
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
func (e *spaceEstimate) add(dst *int64, n int64) {
|
|
if n < 0 || *dst > math.MaxInt64-n {
|
|
e.problem = "Größenüberlauf"
|
|
return
|
|
}
|
|
*dst += n
|
|
}
|
|
|
|
func (e *spaceEstimate) consume(line string) {
|
|
if bytesPattern.MatchString(line) {
|
|
e.sawStats = true
|
|
}
|
|
if filesPattern.MatchString(line) {
|
|
e.sawFiles = true
|
|
}
|
|
if !strings.HasPrefix(line, "SPACE|") || e.problem != "" {
|
|
return
|
|
}
|
|
parts := strings.SplitN(line, "|", 4)
|
|
if len(parts) != 4 || len(parts[1]) != 11 {
|
|
e.problem = "Ungültige Rsync-Dateiliste"
|
|
return
|
|
}
|
|
code := parts[1]
|
|
if strings.HasPrefix(code, "*deleting") {
|
|
return
|
|
}
|
|
e.add(&e.entries, 1)
|
|
size, err := strconv.ParseInt(parts[2], 10, 64)
|
|
if err != nil || size < 0 {
|
|
e.problem = "Ungültige Dateigröße"
|
|
return
|
|
}
|
|
if code[1] != 'f' || (code[0] != '>' && code[0] != '<') {
|
|
return
|
|
}
|
|
rel := decodeRsyncName(parts[3])
|
|
if filepath.IsAbs(rel) || strings.ContainsRune(rel, 0) {
|
|
e.problem = "Ungültiger Ziel-Dateipfad"
|
|
return
|
|
}
|
|
clean := filepath.Clean(rel)
|
|
if clean == ".." || strings.HasPrefix(clean, ".."+string(os.PathSeparator)) {
|
|
e.problem = "Ziel-Dateipfad verlässt das Ziel"
|
|
return
|
|
}
|
|
// Never follow an existing destination symlink, including intermediate ones.
|
|
// A missing ancestor simply means that rsync will create the directory tree.
|
|
path := e.target
|
|
components := strings.Split(clean, string(os.PathSeparator))
|
|
var old os.FileInfo
|
|
for i, c := range components {
|
|
path = filepath.Join(path, c)
|
|
info, err := os.Lstat(path)
|
|
if os.IsNotExist(err) {
|
|
break
|
|
}
|
|
if err != nil {
|
|
e.problem = "Zieldatei nicht prüfbar: " + err.Error()
|
|
return
|
|
}
|
|
if st, ok := info.Sys().(*syscall.Stat_t); !ok || uint64(st.Dev) != e.device {
|
|
e.problem = "Weiteres Dateisystem im Zielpfad"
|
|
return
|
|
}
|
|
if info.Mode()&os.ModeSymlink != 0 {
|
|
e.problem = "Symlink im Zielpfad; Platzbedarf nicht sicher bestimmbar"
|
|
return
|
|
}
|
|
if i < len(components)-1 && !info.IsDir() {
|
|
e.problem = "Dateitypwechsel im Zielpfad"
|
|
return
|
|
}
|
|
if i == len(components)-1 {
|
|
old = info
|
|
}
|
|
}
|
|
var reclaim int64
|
|
if old != nil && old.Mode().IsRegular() {
|
|
stat, ok := old.Sys().(*syscall.Stat_t)
|
|
if !ok {
|
|
e.problem = "Belegter Zielspeicher nicht ermittelbar"
|
|
return
|
|
}
|
|
if mayReclaimBlocks(path) && stat.Nlink == 1 && stat.Blocks >= 0 && stat.Blocks <= math.MaxInt64/512 {
|
|
reclaim = min(old.Size(), stat.Blocks*512)
|
|
}
|
|
} // Directory/special-file replacement: no space credit.
|
|
e.add(&e.files, 1)
|
|
e.add(&e.bytes, size)
|
|
e.add(&e.growth, max(int64(0), size-reclaim))
|
|
e.largest = max(e.largest, size)
|
|
}
|
|
|
|
func (e *spaceEstimate) finish(s Summary) (int64, string) {
|
|
if e.problem != "" {
|
|
return 0, e.problem
|
|
}
|
|
if !e.sawStats || !e.sawFiles || e.files != s.Files || e.bytes != s.Bytes {
|
|
return 0, "Rsync-Dateiliste und Abschlussstatistik sind unvollständig oder widersprüchlich"
|
|
}
|
|
if e.entries == 0 {
|
|
return 0, ""
|
|
}
|
|
required := e.growth
|
|
e.add(&required, e.largest)
|
|
// Budget allocation rounding/metadata for changed entries, plus 1% or 64 MiB.
|
|
if e.entries > math.MaxInt64/(64*1024) {
|
|
return 0, "Zu viele Dateieinträge"
|
|
}
|
|
e.add(&required, e.entries*64*1024)
|
|
reserve := max(int64(64*1024*1024), required/100)
|
|
e.add(&required, reserve)
|
|
if e.problem != "" {
|
|
return 0, e.problem
|
|
}
|
|
return required, ""
|
|
}
|