Files
URBM/internal/rsync/rsync.go
T
Mikei386andClaude Opus 5.5 ac055497b7 Ignore shfs inodes, add low-priority jobs, fix retention grouping and CSRF; release 2026.11.22.r001
- Pass --ignore-inode for sources below /mnt/user and /mnt/user0. shfs hands
  out new inode numbers after every reboot, which made Restic re-read all
  data on the first run after a restart.
- Add a per-job lowPriority option that starts Restic and Rsync through
  ionice (idle) and nice 19 via the new internal/procprio package.
- Run restic forget with --group-by host,tags. Grouping by paths split a job
  into a new group whenever its sources changed, and keep-within never
  expired the old group's snapshots.
- Verify the Unraid CSRF token in webgui/api.php for POST, PUT and DELETE
  and reject other methods; Unraid's own check only covers form posts.
- Set $urbmVersion in URBM.page during the package build and fail the build
  if it does not match, so the page always loads the packaged JS asset.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 09:23:01 +01:00

375 lines
9.7 KiB
Go

package rsync
import (
"bufio"
"context"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
"syscall"
"git.casaderoll.de/michael/urbm/internal/model"
"git.casaderoll.de/michael/urbm/internal/procprio"
)
type Runner struct {
Binary string
mu sync.Mutex
processes map[string]*os.Process
}
func (r *Runner) SetBinary(path string) {
r.mu.Lock()
r.Binary = path
r.mu.Unlock()
}
func (r *Runner) binary() string {
r.mu.Lock()
defer r.mu.Unlock()
return r.Binary
}
type Progress struct {
Percent float64
Bytes int64
Files int64
CurrentFile string
Change string
}
type Summary struct {
Bytes int64
Files int64
Deleted int64
RequiredBytes int64
SpaceUnknown string
}
var (
progressPattern = regexp.MustCompile(`^\s*([0-9,]+)\s+([0-9]+)%`)
filesPattern = regexp.MustCompile(`^Number of regular files transferred:\s*([0-9,]+)`)
deletedPattern = regexp.MustCompile(`^Number of deleted files:\s*([0-9,]+)`)
bytesPattern = regexp.MustCompile(`^Total transferred file size:\s*([0-9,]+) bytes`)
)
func (r *Runner) Run(ctx context.Context, runID string, job model.Job, callback func(Progress)) (Summary, error) {
args := Arguments(job)
name, cmdArgs := procprio.Command(job.LowPriority, r.binary(), args)
cmd := exec.CommandContext(ctx, name, cmdArgs...)
cmd.Env = append(os.Environ(), "LC_ALL=C", "LANG=C")
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
cmd.Cancel = func() error {
if cmd.Process == nil {
return os.ErrProcessDone
}
if err := syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL); err != nil && !errors.Is(err, syscall.ESRCH) {
return err
}
return nil
}
stdout, err := cmd.StdoutPipe()
if err != nil {
return Summary{}, err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return Summary{}, err
}
if err := cmd.Start(); err != nil {
if errors.Is(err, exec.ErrNotFound) {
return Summary{}, fmt.Errorf("environment: rsync binary not found: %w", err)
}
return Summary{}, err
}
unregister := r.register(runID, cmd.Process)
defer unregister()
var summary Summary
var scanErr error
done := make(chan struct{})
go func() {
scanErr = scanOutput(stdout, func(line string) {
summary.parseLine(line)
if callback != nil {
if progress, ok := parseProgress(line); ok {
callback(progress)
} else if change, ok := parseChange(line); ok {
callback(change)
}
}
})
close(done)
}()
stderrDone := make(chan []byte, 1)
go func() { stderrDone <- drainLimited(stderr, 1024*1024) }()
// Drain both pipes before Wait closes them, including the final statistics.
<-done
errBytes := <-stderrDone
err = cmd.Wait()
if err != nil {
if errors.Is(ctx.Err(), context.Canceled) {
return Summary{}, fmt.Errorf("cancelled: %w", ctx.Err())
}
message := strings.TrimSpace(string(errBytes))
if message == "" {
message = err.Error()
}
return Summary{}, fmt.Errorf("rsync: %s", message)
}
if scanErr != nil {
return summary, fmt.Errorf("rsync output: %w", scanErr)
}
return summary, nil
}
// Estimate performs rsync's complete comparison without writing destination data.
func (r *Runner) Estimate(ctx context.Context, runID string, job model.Job) (Summary, error) {
job.Rsync.DryRun = true
if _, err := os.Stat(job.Rsync.Target); err != nil {
return Summary{}, fmt.Errorf("rsync preflight: target unavailable: %w", err)
}
seen := make(map[string]bool)
for _, source := range job.Sources {
name := filepath.Base(filepath.Clean(source.Path))
if seen[name] {
return Summary{}, fmt.Errorf("rsync preflight: ambiguous destination directory %q", name)
}
seen[name] = true
}
args := Arguments(job)
for index, arg := range args {
if strings.HasPrefix(arg, "--out-format=") {
args[index] = "--out-format=SPACE|%i|%l|%n"
}
}
name, cmdArgs := procprio.Command(job.LowPriority, r.binary(), args)
cmd := exec.CommandContext(ctx, name, cmdArgs...)
cmd.Env = append(os.Environ(), "LC_ALL=C", "LANG=C")
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
cmd.Cancel = func() error {
if cmd.Process == nil {
return os.ErrProcessDone
}
if err := syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL); err != nil && !errors.Is(err, syscall.ESRCH) {
return err
}
return nil
}
stdout, err := cmd.StdoutPipe()
if err != nil {
return Summary{}, err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return Summary{}, err
}
if err := cmd.Start(); err != nil {
if errors.Is(err, exec.ErrNotFound) {
return Summary{}, fmt.Errorf("environment: rsync binary not found: %w", err)
}
return Summary{}, err
}
unregister := r.register(runID, cmd.Process)
defer unregister()
var summary Summary
space := newSpaceEstimate(job.Rsync.Target)
done := make(chan struct{})
go func() {
if err := scanOutput(stdout, func(line string) {
summary.parseLine(line)
space.consume(line)
}); err != nil {
space.problem = err.Error()
}
close(done)
}()
stderrDone := make(chan []byte, 1)
go func() { stderrDone <- drainLimited(stderr, 1024*1024) }()
// Drain both pipes before Wait closes them, including the final statistics.
<-done
errBytes := <-stderrDone
err = cmd.Wait()
if err != nil {
if errors.Is(ctx.Err(), context.Canceled) {
return Summary{}, fmt.Errorf("cancelled: %w", ctx.Err())
}
return Summary{}, fmt.Errorf("rsync preflight: %s", strings.TrimSpace(string(errBytes)))
}
summary.RequiredBytes, summary.SpaceUnknown = space.finish(summary)
return summary, nil
}
func Arguments(job model.Job) []string {
options := job.Rsync
args := []string{"--recursive", "--info=progress2", "--stats", "--partial", "--outbuf=L", "--out-format=CHANGE|%i|%n%L"}
if !options.Overwrite {
args = append(args, "--ignore-existing")
}
if options.Delete {
args = append(args, "--delete-delay")
}
if options.PreservePermissions {
args = append(args, "--perms")
}
if options.PreserveOwner {
args = append(args, "--owner")
}
if options.PreserveGroup {
args = append(args, "--group")
}
if options.PreserveTimes {
args = append(args, "--times")
}
if options.PreserveLinks {
args = append(args, "--links")
}
if options.PreserveACLs {
args = append(args, "--acls")
}
if options.PreserveXattrs {
args = append(args, "--xattrs")
}
if options.Checksum {
args = append(args, "--checksum")
}
if options.DryRun {
args = append(args, "--dry-run")
}
for _, exclude := range job.Excludes {
args = append(args, "--exclude", exclude)
}
args = append(args, "--")
for _, source := range job.Sources {
args = append(args, source.Path)
}
args = append(args, strings.TrimRight(options.Target, "/")+"/")
return args
}
func (r *Runner) Pause(runID string) error { return r.signal(runID, syscall.SIGSTOP) }
func (r *Runner) Resume(runID string) error { return r.signal(runID, syscall.SIGCONT) }
func (r *Runner) signal(runID string, signal syscall.Signal) error {
r.mu.Lock()
process := r.processes[runID]
r.mu.Unlock()
if process == nil {
return errors.New("rsync process is not active yet")
}
if err := syscall.Kill(-process.Pid, signal); err != nil {
return fmt.Errorf("signal rsync process group: %w", err)
}
return nil
}
func (r *Runner) register(runID string, process *os.Process) func() {
r.mu.Lock()
if r.processes == nil {
r.processes = make(map[string]*os.Process)
}
r.processes[runID] = process
r.mu.Unlock()
return func() {
r.mu.Lock()
delete(r.processes, runID)
r.mu.Unlock()
}
}
func scanOutput(reader io.Reader, callback func(string)) error {
scanner := bufio.NewScanner(reader)
scanner.Split(splitLinesAndCarriageReturns)
scanner.Buffer(make([]byte, 64*1024), 2*1024*1024)
for scanner.Scan() {
if line := strings.TrimSpace(scanner.Text()); line != "" {
callback(line)
}
}
if err := scanner.Err(); err != nil {
// Keep draining so rsync cannot block on a full stdout pipe before Wait.
_, _ = io.Copy(io.Discard, reader)
return err
}
return nil
}
func drainLimited(reader io.Reader, limit int) []byte {
result := make([]byte, 0, limit)
buffer := make([]byte, 32*1024)
for {
count, err := reader.Read(buffer)
if count > 0 && len(result) < limit {
keep := count
if keep > limit-len(result) {
keep = limit - len(result)
}
result = append(result, buffer[:keep]...)
}
if err != nil {
return result
}
}
}
func splitLinesAndCarriageReturns(data []byte, atEOF bool) (advance int, token []byte, err error) {
for i, value := range data {
if value == '\n' || value == '\r' {
return i + 1, data[:i], nil
}
}
if atEOF && len(data) > 0 {
return len(data), data, nil
}
return 0, nil, nil
}
func parseProgress(line string) (Progress, bool) {
match := progressPattern.FindStringSubmatch(line)
if len(match) != 3 {
return Progress{}, false
}
bytes, _ := strconv.ParseInt(strings.ReplaceAll(match[1], ",", ""), 10, 64)
percent, _ := strconv.ParseFloat(match[2], 64)
return Progress{Bytes: bytes, Percent: percent}, true
}
func parseCount(pattern *regexp.Regexp, line string) (int64, bool) {
match := pattern.FindStringSubmatch(line)
if len(match) != 2 {
return 0, false
}
value, err := strconv.ParseInt(strings.ReplaceAll(match[1], ",", ""), 10, 64)
return value, err == nil
}
func (s *Summary) parseLine(line string) {
if value, ok := parseCount(filesPattern, line); ok {
s.Files = value
}
if value, ok := parseCount(bytesPattern, line); ok {
s.Bytes = value
}
if value, ok := parseCount(deletedPattern, line); ok {
s.Deleted = value
}
}
func parseChange(line string) (Progress, bool) {
if !strings.HasPrefix(line, "CHANGE|") {
return Progress{}, false
}
fields := strings.SplitN(strings.TrimPrefix(line, "CHANGE|"), "|", 2)
if len(fields) != 2 || len(fields[0]) != 11 {
return Progress{}, false
}
return Progress{Change: fields[0], CurrentFile: fields[1]}, true
}