mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-18 18:04:17 +00:00
Review of the file-link fallback found paths that lost or misreported user data: - An identical local file was adopted as a skillshare copy and later pruned without backup. Only files sync writes are tracked now. - Clearing all own text in import mode was treated as no record, so switching back resurrected the attach-time content. - The mode API saved the new mode and returned success when a directory blocked the sync; it now keeps the old mode and returns 409. - Restore preview promised a delete for files restore leaves alone; both now share one ownership decision. - Prune forgot ownership even when removing the copy failed. Refs #299
132 lines
3.9 KiB
Go
132 lines
3.9 KiB
Go
package sync
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"skillshare/internal/utils"
|
|
)
|
|
|
|
// FileLinkFallbackWarning is reported when a mode that links single files
|
|
// syncs copies instead.
|
|
const FileLinkFallbackWarning = "file links need Windows Developer Mode; copying instead"
|
|
|
|
// canCreateFileLink reports whether this system can create symlinks to files.
|
|
// Windows junctions only work for directories, so without Developer Mode file
|
|
// link modes fall back to copy. A variable so tests can simulate that.
|
|
var canCreateFileLink = platformCanCreateFileLink
|
|
|
|
// CanCreateFileLink reports whether this system can create symlinks to files
|
|
// (false on Windows without Developer Mode). The probe runs once.
|
|
func CanCreateFileLink() bool { return canCreateFileLink() }
|
|
|
|
// SetFileLinksForTest overrides whether file links can be created and returns
|
|
// a func that restores the probe. Only for tests in other packages.
|
|
func SetFileLinksForTest(ok bool) (restore func()) {
|
|
prev := canCreateFileLink
|
|
canCreateFileLink = func() bool { return ok }
|
|
return func() { canCreateFileLink = prev }
|
|
}
|
|
|
|
// EffectiveAgentMode returns the mode agents actually sync with. Merge links
|
|
// each agent file, so it copies when file links are unavailable; symlink mode
|
|
// links the whole directory and is unaffected.
|
|
func EffectiveAgentMode(mode string) string {
|
|
m := EffectiveMode(mode)
|
|
if m == "merge" && !canCreateFileLink() {
|
|
return "copy"
|
|
}
|
|
return m
|
|
}
|
|
|
|
// ExtraTargetMode returns the mode an extras target actually syncs with.
|
|
// Merge links each file, and symlink mode of a single-file extra links that
|
|
// file, so both copy when file links are unavailable. Symlink mode of a
|
|
// directory extra links the directory and is unaffected.
|
|
func ExtraTargetMode(mode string, singleFile bool) string {
|
|
m := EffectiveMode(mode)
|
|
if !canCreateFileLink() && (m == "merge" || (singleFile && m == "symlink")) {
|
|
return "copy"
|
|
}
|
|
return m
|
|
}
|
|
|
|
// copyTracker records files copied in place of file links in the target's
|
|
// manifest, so later syncs can tell them from the user's own files. A tracked
|
|
// file that still matches its recorded hash belongs to skillshare and may be
|
|
// updated, relinked, or pruned; an edited one is left alone.
|
|
type copyTracker struct {
|
|
dir string
|
|
m *Manifest
|
|
changed bool
|
|
}
|
|
|
|
func loadCopyTracker(dir string) *copyTracker {
|
|
m, err := ReadManifest(dir)
|
|
if err != nil {
|
|
m = &Manifest{Managed: make(map[string]string), Mtimes: make(map[string]int64)}
|
|
}
|
|
return ©Tracker{dir: dir, m: m}
|
|
}
|
|
|
|
// owns reports whether rel is a tracked copy the user has not edited.
|
|
func (t *copyTracker) owns(rel string) bool {
|
|
sum, ok := t.m.Managed[filepath.ToSlash(rel)]
|
|
if !ok {
|
|
return false
|
|
}
|
|
h, err := utils.FileHash(filepath.Join(t.dir, rel))
|
|
return err == nil && h == sum
|
|
}
|
|
|
|
func (t *copyTracker) record(rel string) {
|
|
key := filepath.ToSlash(rel)
|
|
if h, err := utils.FileHash(filepath.Join(t.dir, rel)); err == nil && t.m.Managed[key] != h {
|
|
t.m.Managed[key] = h
|
|
t.changed = true
|
|
}
|
|
}
|
|
|
|
func (t *copyTracker) forget(rel string) {
|
|
key := filepath.ToSlash(rel)
|
|
if _, ok := t.m.Managed[key]; ok {
|
|
delete(t.m.Managed, key)
|
|
t.changed = true
|
|
}
|
|
}
|
|
|
|
// pruneOrphans removes tracked copies whose name is not in expected, keeping
|
|
// edited ones, and forgets them. It returns the removed names.
|
|
func (t *copyTracker) pruneOrphans(expected map[string]bool, dryRun bool) ([]string, error) {
|
|
var removed []string
|
|
for key := range t.m.Managed {
|
|
rel := filepath.FromSlash(key)
|
|
if expected[rel] {
|
|
continue
|
|
}
|
|
if t.owns(rel) {
|
|
if !dryRun {
|
|
if err := os.Remove(filepath.Join(t.dir, rel)); err != nil {
|
|
return removed, fmt.Errorf("failed to remove orphaned copy %s: %w", rel, err)
|
|
}
|
|
}
|
|
removed = append(removed, rel)
|
|
}
|
|
if !dryRun {
|
|
t.forget(rel)
|
|
}
|
|
}
|
|
return removed, nil
|
|
}
|
|
|
|
func (t *copyTracker) save() error {
|
|
if !t.changed {
|
|
return nil
|
|
}
|
|
if len(t.m.Managed) == 0 {
|
|
return RemoveManifest(t.dir)
|
|
}
|
|
return WriteManifest(t.dir, t.m)
|
|
}
|