Files
skillshare/internal/sync/extras_restore_preview.go
T
Willie 86dca7924f feat(backup): browse and restore file history, delete snapshots
skillshare backs up AGENTS.md, CLAUDE.md and shared-file locations before
rewriting them, but nothing could list or restore those copies, and a
target snapshot could not be deleted on its own. The file backups now
record why they were taken in their name (<time>.<reason>.bak; older ones
have none), and the server and CLI can list them, show a version and
restore it, saving the current content first. A link is only replaced
when the caller asks for it.

Also:
- DELETE /api/backups/{timestamp} and `backup --delete`.
- Agents snapshots (<target>-agents) could not be restored from the
  dashboard: the name was looked up as a target.
- Project mode uses the project's backups folder for snapshots and lists
  only file backups inside the project.
- MCP backups report which servers they changed and when.

Refs #299
2026-09-28 19:23:16 +08:00

104 lines
3.3 KiB
Go

package sync
import (
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
// What restoring a single-file extra target leaves at its path.
const (
RestoreKindContent = "content" // a file with ExtraRestorePreview.Content
RestoreKindDelete = "delete" // no file: none existed before attaching
RestoreKindLink = "link" // the user's own link to ExtraRestorePreview.LinkTo
)
// ExtraRestorePreview describes what RestoreExtraTarget would put back.
type ExtraRestorePreview struct {
Kind string
Content string
LinkTo string
// RecordedAt is when the target was attached; zero without a record.
RecordedAt time.Time
// Drift: edits made after attaching are, or will be, kept as a drift
// backup rather than restored.
Drift bool
}
// PreviewRestoreExtraTarget reports what RestoreExtraTarget(f) would put back,
// read from the attach-time record, without changing anything.
func PreviewRestoreExtraTarget(f ExtraFile) ExtraRestorePreview {
dir := extraBackupDir(f.Target)
record, at := extraAttachRecord(f.Target)
p := ExtraRestorePreview{RecordedAt: at, Drift: hasExtraDriftSince(dir, at)}
if f.Mode == "import" {
// Only the import line goes, unless nothing else is left in the file.
data, _ := os.ReadFile(f.Target)
stripped, _ := f.removeImport(string(data))
if strings.TrimSpace(stripped) != "" || record == "" {
p.Kind, p.Content = RestoreKindContent, stripped
return p
}
} else if info, err := os.Lstat(f.Target); err == nil {
owned, drift := extraRestoreOwnership(f, info)
if !owned && !drift {
// Restore leaves a target it does not own unchanged.
if info.Mode()&os.ModeSymlink != 0 {
p.Kind = RestoreKindLink
p.LinkTo, _ = os.Readlink(f.Target)
} else {
data, _ := os.ReadFile(f.Target)
p.Kind, p.Content = RestoreKindContent, string(data)
}
return p
}
p.Drift = p.Drift || drift
}
switch record {
case attachCreated:
p.Kind = RestoreKindDelete
case attachRestoreLink, attachRestoreJunction:
dest, _ := os.ReadFile(filepath.Join(dir, record))
p.Kind, p.LinkTo = RestoreKindLink, string(dest)
case attachRestore:
data, _ := os.ReadFile(filepath.Join(dir, record))
p.Kind, p.Content = RestoreKindContent, string(data)
default:
// Without a record, restore falls back to the latest backup.
if data, ok, err := latestExtraBackup(f.Target); ok && err == nil {
p.Kind, p.Content = RestoreKindContent, string(data)
} else {
p.Kind = RestoreKindDelete
}
}
return p
}
// extraAttachRecord returns the attach-time record of path and when it was
// written, or "" when there is none.
func extraAttachRecord(path string) (string, time.Time) {
dir := extraBackupDir(path)
for _, name := range []string{attachCreated, attachRestore, attachRestoreLink, attachRestoreJunction} {
if info, err := os.Stat(filepath.Join(dir, name)); err == nil {
return name, info.ModTime()
}
}
return "", time.Time{}
}
// hasExtraDriftSince reports whether a drift backup in dir was made at or
// after since (any, when since is zero).
func hasExtraDriftSince(dir string, since time.Time) bool {
for _, name := range extraBackupNames(filepath.Join(dir, "drift")) {
n, err := strconv.ParseInt(extraBackupStamp(name), 10, 64)
if err == nil && (since.IsZero() || n >= since.UnixNano()) {
return true
}
}
return false
}