mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-19 02:17:09 +00:00
The guidance rules (status ranking, mixed_modes, multiple_blocks, the plan token, and check -> backup -> recheck -> write) were methods on *Server, so they could only be exercised over HTTP with a configured server, and the recheck after backup depended on callers using the right helper. internal/memory now owns status, plan, and apply behind GuidanceStatus, PlanGuidance, and ApplyGuidance. The server resolves targets, assignments, shared extras, and project reach into a GuidanceInput, syncs shared copies through a callback after each written file, and maps results to JSON. Only ApplyGuidance reaches the write steps, so the recheck cannot be skipped. Note creation and guidance creation shared a copied O_EXCL create with delete-on-failure; both now use createExclusive. Each keeps its own behaviour: notes still chmod to 0644, guidance still creates parents. HTTP routes, shapes, status codes, and error codes are unchanged. The raw write allowances follow the calls to their new functions. Refs #450
82 lines
1.8 KiB
Go
82 lines
1.8 KiB
Go
package memory
|
|
|
|
import "os"
|
|
|
|
// commitNote publishes a prepared draft after backup and temporary-file writes.
|
|
func commitNote(root, rel, draft, version string) error {
|
|
path, err := notePath(root, rel)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if version == "" {
|
|
// Another process may have created this note since the initial read.
|
|
return createNote(path, draft)
|
|
}
|
|
latest, err := Read(root, rel)
|
|
if os.IsNotExist(err) {
|
|
return ErrConflict
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if latest.Version != version {
|
|
return ErrConflict
|
|
}
|
|
return os.Rename(draft, path)
|
|
}
|
|
|
|
func createNote(path, draft string) error {
|
|
data, err := os.ReadFile(draft)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = createExclusive(path, func(file *os.File) error {
|
|
if err := file.Chmod(0644); err != nil {
|
|
return err
|
|
}
|
|
_, err := file.Write(data)
|
|
return err
|
|
})
|
|
if os.IsExist(err) {
|
|
return ErrConflict
|
|
}
|
|
return err
|
|
}
|
|
|
|
// createExclusive creates path and fills it with write. It fails with an
|
|
// os.IsExist error, leaving the file alone, when path already exists; a file
|
|
// it created but could not finish is removed.
|
|
func createExclusive(path string, write func(*os.File) error) error {
|
|
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = write(file)
|
|
if closeErr := file.Close(); err == nil {
|
|
err = closeErr
|
|
}
|
|
if err != nil {
|
|
os.Remove(path)
|
|
}
|
|
return err
|
|
}
|
|
|
|
// removeNote commits a deletion after the backup has completed.
|
|
func removeNote(root, rel, version string) error {
|
|
path, err := notePath(root, rel)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
latest, err := Read(root, rel)
|
|
if os.IsNotExist(err) {
|
|
return ErrConflict
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if version == "" || latest.Version != version {
|
|
return ErrConflict
|
|
}
|
|
return os.Remove(path)
|
|
}
|