-
Notifications
You must be signed in to change notification settings - Fork 1
feat: two-phase rename for path collision safety #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "log" | ||
| "os" | ||
| "path/filepath" | ||
| "sort" | ||
| ) | ||
|
|
||
| type renamePlan struct { | ||
| from string | ||
| to string | ||
| temp string | ||
| } | ||
|
|
||
| func (fr *findReplace) queueRename(from, to string) { | ||
| fr.renameMu.Lock() | ||
| fr.renames = append(fr.renames, renamePlan{from: from, to: to}) | ||
| fr.renameMu.Unlock() | ||
| } | ||
|
|
||
| func (fr *findReplace) applyRenames() error { | ||
| if len(fr.renames) == 0 { | ||
| return nil | ||
| } | ||
|
|
||
| active := make([]renamePlan, 0, len(fr.renames)) | ||
| sources := make(map[string]bool, len(fr.renames)) | ||
| targets := make(map[string]bool, len(fr.renames)) | ||
| for _, r := range fr.renames { | ||
| if r.from == r.to { | ||
| continue | ||
| } | ||
| if targets[r.to] { | ||
| return fmt.Errorf("duplicate rename target %q", r.to) | ||
| } | ||
| active = append(active, r) | ||
| sources[r.from] = true | ||
| targets[r.to] = true | ||
| } | ||
|
|
||
| if len(active) == 0 { | ||
| return nil | ||
| } | ||
|
|
||
| needsTwoPhase := false | ||
| for _, r := range active { | ||
| if sources[r.to] { | ||
| needsTwoPhase = true | ||
| break | ||
| } | ||
| } | ||
|
|
||
| sort.Slice(active, func(i, j int) bool { | ||
| if len(active[i].from) != len(active[j].from) { | ||
| return len(active[i].from) > len(active[j].from) | ||
| } | ||
| return active[i].from < active[j].from | ||
| }) | ||
|
|
||
| if needsTwoPhase { | ||
| return fr.applyRenamesTwoPhase(active) | ||
| } | ||
| return fr.applyRenamesDirect(active) | ||
| } | ||
|
|
||
| func (fr *findReplace) applyRenamesDirect(plans []renamePlan) error { | ||
| var errs []error | ||
| for _, r := range plans { | ||
| if err := fr.renamePath(r.from, r.to); err != nil { | ||
| errs = append(errs, err) | ||
| } | ||
| } | ||
| return errors.Join(errs...) | ||
| } | ||
|
|
||
| func (fr *findReplace) applyRenamesTwoPhase(plans []renamePlan) error { | ||
| rollback := func() { | ||
| for _, r := range plans { | ||
| if r.temp != "" { | ||
| _ = os.Remove(r.temp) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| seq := 0 | ||
| for i := range plans { | ||
| r := &plans[i] | ||
| r.temp = filepath.Join(filepath.Dir(r.from), fmt.Sprintf(".find-replace.tmp.%d.%d", os.Getpid(), seq)) | ||
| seq++ | ||
| log.Printf("Renaming %v to %v (phase 1)", r.from, filepath.Base(r.temp)) | ||
| if err := os.Rename(r.from, r.temp); err != nil { | ||
| rollback() | ||
| return fmt.Errorf("phase 1 rename %q: %w", r.from, err) | ||
| } | ||
| } | ||
|
|
||
| phase2 := append([]renamePlan(nil), plans...) | ||
| sort.Slice(phase2, func(i, j int) bool { | ||
| if len(phase2[i].to) != len(phase2[j].to) { | ||
| return len(phase2[i].to) > len(phase2[j].to) | ||
| } | ||
| return phase2[i].to < phase2[j].to | ||
| }) | ||
|
|
||
| for _, r := range phase2 { | ||
| log.Printf("Renaming %v to %v (phase 2)", filepath.Base(r.temp), filepath.Base(r.to)) | ||
| if err := os.Rename(r.temp, r.to); err != nil { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When any source/target chain triggers the two-phase path, phase 2 uses raw Useful? React with 👍 / 👎. |
||
| return fmt.Errorf("phase 2 rename %q -> %q: %w", r.temp, r.to, err) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (fr *findReplace) renamePath(from, to string) error { | ||
| if _, err := os.Stat(to); err == nil { | ||
| return fmt.Errorf("refusing to rename %v to %v: %v already exists", from, filepath.Base(to), to) | ||
| } else if !errors.Is(err, os.ErrNotExist) { | ||
| return fmt.Errorf("stat rename destination %v: %w", to, err) | ||
| } | ||
|
|
||
| log.Printf("Renaming %v to %v", from, filepath.Base(to)) | ||
| if err := os.Rename(from, to); err != nil { | ||
| return fmt.Errorf("rename %v to %v: %w", from, filepath.Base(to), err) | ||
| } | ||
| return nil | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The phase-1 temp is created inside the source's parent directory, so a descendant rename can be invalidated when an ancestor directory is also renamed later in the same two-phase batch. For example,
find-replace a aawith a directorya/containing fileaand a siblingaa/queues botha/a -> a/aaanda -> aa; phase 1 movesa/atoa/.find-replace.tmp..., then movesaitself, so phase 2 later looks for the child temp at the olda/...path and fails, leaving data stranded in the directory temp.Useful? React with 👍 / 👎.