copier: rsync с возобновлением, папка назначения, права 777

- Заменить ручное копирование на rsync --partial --append-verify
- Структура на диске: <mount>/<dest_folder>/<rel path from /media>
- dest_folder настраивается (default: media)
- Права на диске: --no-perms --chmod=ugo=rwx
- rsync добавлен в Dockerfile
- Режим "удалить": удаляет только dest_folder, а не весь диск

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-23 21:56:26 +03:00
parent f2a7505378
commit 8f36d4e824
6 changed files with 47 additions and 62 deletions

View File

@@ -4,8 +4,8 @@ import (
"context"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"sync"
@@ -19,6 +19,7 @@ type Options struct {
DiskID string
MountPath string
MediaPath string
DestFolder string // subfolder on disk, default "media"
EnabledSources []string
ReserveFreeGB float64
OverwriteMode config.OverwriteMode
@@ -39,7 +40,6 @@ func New(tasks *task.Store) *Copier {
return &Copier{tasks: tasks}
}
// SetDB replaces the active disk database (called when a disk connects or disconnects).
func (c *Copier) SetDB(d *db.DB) {
c.dbMu.Lock()
c.db = d
@@ -65,6 +65,10 @@ func (c *Copier) Start(ctx context.Context, opts Options) (string, error) {
return "", errors.New("no disk database available")
}
if opts.DestFolder == "" {
opts.DestFolder = "media"
}
t := c.tasks.Create("copy")
copyCtx, cancel := context.WithCancel(ctx)
c.cancel = cancel
@@ -98,9 +102,11 @@ func (c *Copier) run(ctx context.Context, taskID string, opts Options, database
setStatus(task.StatusRunning, "Подготовка…", 0)
destRoot := filepath.Join(opts.MountPath, opts.DestFolder)
if opts.OverwriteMode == config.OverwriteDelete {
setStatus(task.StatusRunning, "Удаление данных с диска…", 0)
if err := deleteOurData(opts.MountPath); err != nil {
if err := os.RemoveAll(destRoot); err != nil {
fail(err)
return
}
@@ -161,8 +167,10 @@ func (c *Copier) run(ctx context.Context, taskID string, opts Options, database
prog := int(float64(i+1) / float64(total) * 100)
setStatus(task.StatusRunning, msg, prog)
dstAbs := filepath.Join(opts.MountPath, f.relPath)
if err := copyFile(ctx, f.srcAbs, dstAbs); err != nil {
// destination mirrors source structure under destRoot
dstAbs := filepath.Join(destRoot, f.relPath)
if err := rsyncFile(ctx, f.srcAbs, dstAbs); err != nil {
if errors.Is(err, context.Canceled) {
c.tasks.Update(taskID, func(t *task.Task) {
t.Status = task.StatusCanceled
@@ -187,7 +195,7 @@ func (c *Copier) run(ctx context.Context, taskID string, opts Options, database
type fileEntry struct {
srcAbs string
relPath string
relPath string // relative to /media
size int64
}
@@ -217,64 +225,29 @@ func buildFileList(mediaPath string, sources []string, skip map[string]struct{})
return result, nil
}
func deleteOurData(mountPath string) error {
entries, err := os.ReadDir(mountPath)
if err != nil {
return err
}
for _, e := range entries {
if e.Name() == ".jukebox" {
continue
}
if err := os.RemoveAll(filepath.Join(mountPath, e.Name())); err != nil {
return err
}
}
return nil
}
func copyFile(ctx context.Context, src, dst string) error {
// rsyncFile copies src to dst using rsync with resume support.
// --partial keeps partial files on interruption.
// --append-verify resumes partial transfers and verifies checksums.
func rsyncFile(ctx context.Context, src, dst string) error {
if err := os.MkdirAll(filepath.Dir(dst), 0o755); err != nil {
return err
}
in, err := os.Open(src)
cmd := exec.CommandContext(ctx, "rsync",
"--partial",
"--append-verify",
"--times",
"--no-perms",
"--no-owner",
"--no-group",
"--chmod=ugo=rwx",
src, dst,
)
out, err := cmd.CombinedOutput()
if err != nil {
return err
}
defer in.Close()
tmp := dst + ".juketmp"
out, err := os.Create(tmp)
if err != nil {
return err
}
buf := make([]byte, 512*1024)
for {
select {
case <-ctx.Done():
out.Close()
os.Remove(tmp)
if ctx.Err() != nil {
return ctx.Err()
default:
}
n, readErr := in.Read(buf)
if n > 0 {
if _, werr := out.Write(buf[:n]); werr != nil {
out.Close()
os.Remove(tmp)
return werr
}
}
if errors.Is(readErr, io.EOF) {
break
}
if readErr != nil {
out.Close()
os.Remove(tmp)
return readErr
}
return fmt.Errorf("rsync: %w: %s", err, out)
}
out.Close()
return os.Rename(tmp, dst)
return nil
}