package local import ( "bytes" "context" "errors" "fmt" "io" "os" "os/exec" "path/filepath" "strings" "sync" "time" "slackerbot/internal/model" "slackerbot/internal/storage" "slackerbot/internal/util" ) type transfer struct { cmd *exec.Cmd out io.ReadCloser in io.WriteCloser stderr bytes.Buffer snapshot string tmpDir string drive string machine string seen map[string]bool latest string } type transfers struct { mu sync.Mutex m map[string]*transfer } func (t *transfers) add(x *transfer) string { id := util.RandomHex(8) t.mu.Lock() defer t.mu.Unlock() if t.m == nil { t.m = map[string]*transfer{} } t.m[id] = x return id } func (t *transfers) get(id string) (*transfer, error) { t.mu.Lock() defer t.mu.Unlock() if x := t.m[id]; x != nil { return x, nil } return nil, fmt.Errorf("no transfer %s", id) } func (t *transfers) take(id string) *transfer { t.mu.Lock() defer t.mu.Unlock() x := t.m[id] delete(t.m, id) return x } func (l *Local) home(drive, machine string) (string, error) { if _, err := l.drive(drive); err != nil { return "", err } if !model.IsOurs(machine) { return "", fmt.Errorf("unexpected machine name %q", machine) } return filepath.Join(drive, machine), nil } func (l *Local) WaitStopped(ctx context.Context, machine string) error { ctx, cancel := context.WithTimeout(ctx, 2*time.Minute) defer cancel() for l.IsRunning(machine) { select { case <-ctx.Done(): return fmt.Errorf("%s didn't stop in time", machine) case <-time.After(500 * time.Millisecond): } } return nil } // With a parent export, only the changes since the parent's snapshot are sent. func (l *Local) ExportHome(ctx context.Context, drive, machine, parent string) (string, error) { home, err := l.home(drive, machine) if err != nil { return "", err } args := []string{"send", "-q"} if parent != "" { p, err := l.exports.get(parent) if err != nil { return "", err } args = append(args, "-p", p.snapshot) } snapshot := filepath.Join(drive, ".move-"+machine+"-"+util.RandomHex(4)) if out, err := exec.Command("btrfs", "subvolume", "snapshot", "-r", home, snapshot).CombinedOutput(); err != nil { return "", fmt.Errorf("snapshot: %v: %s", err, strings.TrimSpace(string(out))) } x := &transfer{snapshot: snapshot, drive: drive, machine: machine} x.cmd = exec.Command("btrfs", append(args, snapshot)...) x.cmd.Stderr = &x.stderr if x.out, err = x.cmd.StdoutPipe(); err == nil { err = x.cmd.Start() } if err != nil { storage.DeleteSubvolume(snapshot) return "", err } return l.exports.add(x), nil } func (l *Local) ReadExport(ctx context.Context, id string, max int) ([]byte, bool, error) { x, err := l.exports.get(id) if err != nil { return nil, false, err } buf := make([]byte, max) n, err := io.ReadFull(x.out, buf) if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) { if werr := x.cmd.Wait(); werr != nil { return nil, false, fmt.Errorf("btrfs send: %v: %s", werr, strings.TrimSpace(x.stderr.String())) } return buf[:n], true, nil } return buf[:n], false, err } func (l *Local) CloseExport(ctx context.Context, id string) error { x := l.exports.take(id) if x == nil { return nil } if x.cmd.ProcessState == nil { _ = x.cmd.Process.Kill() _ = x.cmd.Wait() } return storage.DeleteSubvolume(x.snapshot) } func (l *Local) ImportHome(ctx context.Context, drive, machine string) (string, error) { home, err := l.home(drive, machine) if err != nil { return "", err } if _, err := os.Stat(home); err == nil { return "", fmt.Errorf("%s already has a home folder for %s; remove it first", l.name, machine) } x := &transfer{drive: drive, machine: machine, seen: map[string]bool{}, tmpDir: filepath.Join(drive, ".incoming-"+machine+"-"+util.RandomHex(4))} if err := os.Mkdir(x.tmpDir, 0o700); err != nil { return "", err } return l.imports.add(x), nil } func (l *Local) WriteImport(ctx context.Context, id string, data []byte) error { x, err := l.imports.get(id) if err != nil { return err } if x.cmd == nil { x.stderr.Reset() x.cmd = exec.Command("btrfs", "receive", "-q", x.tmpDir) x.cmd.Stderr = &x.stderr if x.in, err = x.cmd.StdinPipe(); err == nil { err = x.cmd.Start() } if err != nil { x.cmd = nil return err } } if _, err := x.in.Write(data); err != nil { return fmt.Errorf("btrfs receive: %v: %s", err, strings.TrimSpace(x.stderr.String())) } return nil } func (l *Local) EndReceive(ctx context.Context, id string) error { x, err := l.imports.get(id) if err != nil { return err } if x.cmd == nil { return errors.New("nothing was received") } x.in.Close() err = x.cmd.Wait() x.cmd = nil if err != nil { return fmt.Errorf("btrfs receive: %v: %s", err, strings.TrimSpace(x.stderr.String())) } entries, err := os.ReadDir(x.tmpDir) if err != nil { return err } for _, e := range entries { if !x.seen[e.Name()] { x.seen[e.Name()] = true x.latest = e.Name() } } return nil } // Received subvolumes are read-only; a writable snapshot of the latest one // becomes the new home. func (l *Local) FinishImport(ctx context.Context, id string, quotaMB int64) error { x := l.imports.take(id) if x == nil { return fmt.Errorf("no transfer %s", id) } defer cleanupIncoming(x.tmpDir) if x.latest == "" { return errors.New("nothing was received") } home := filepath.Join(x.drive, x.machine) if out, err := exec.Command("btrfs", "subvolume", "snapshot", filepath.Join(x.tmpDir, x.latest), home).CombinedOutput(); err != nil { return fmt.Errorf("snapshot: %v: %s", err, strings.TrimSpace(string(out))) } return storage.SetQuota(home, quotaMB) } func (l *Local) AbortImport(ctx context.Context, id string) error { x := l.imports.take(id) if x == nil { return nil } if x.cmd != nil { x.in.Close() _ = x.cmd.Process.Kill() _ = x.cmd.Wait() } cleanupIncoming(x.tmpDir) return nil } func (l *Local) DeleteHome(ctx context.Context, drive, machine string) error { home, err := l.home(drive, machine) if err != nil { return err } if l.IsRunning(machine) { return fmt.Errorf("%s is running", machine) } return storage.DeleteSubvolume(home) } func cleanupIncoming(dir string) { entries, _ := os.ReadDir(dir) for _, e := range entries { _ = storage.DeleteSubvolume(filepath.Join(dir, e.Name())) } _ = os.RemoveAll(dir) }