qtsone/learning / skills/tutor
tutor
FlaggedTurn this agent into a personal programming tutor with a versioned 0-to-expert curriculum (Go available; more languages coming). Use when the user wants to learn a programming language, start or resume lessons, get their exercise code graded, check learning progress, or contribute curriculum fixes — e.g. "/tutor go", "/tutor resume", "teach me Go", "continue my lessons", "grade my exercise".
curriculum/content/focus/cli-tooling/files-processes/solution/atomicfile.go
View on GitHubpackage main
import (
"fmt"
"io/fs"
"os"
"path/filepath"
)
// WriteFileAtomic replaces the contents of path with data, so that a reader
// arriving at any moment sees either the whole old file or the whole new one,
// and a crash mid-write loses neither.
//
// The recipe: create a temp file *in the same directory as path*, write data
// into it, give it perm, flush it to disk, close it, then rename it over path.
// A rename within one filesystem is atomic; a temp file in os.TempDir() is on a
// different filesystem often enough that the rename fails with "invalid
// cross-device link" on your user's machine and never on yours.
//
// Whatever happens, no temp file survives the call.
func WriteFileAtomic(path string, data []byte, perm fs.FileMode) error {
dir, name := filepath.Split(path)
if dir == "" {
dir = "."
}
f, err := os.CreateTemp(dir, "."+name+".tmp-*")
if err != nil {
return fmt.Errorf("creating temp file for %s: %w", path, err)
}
tmp := f.Name()
// Removing a name that a successful rename has already moved is a harmless
// no-op, so one defer covers every failure path below.
defer os.Remove(tmp)
if err := writeAndClose(f, data, perm); err != nil {
return fmt.Errorf("writing %s: %w", tmp, err)
}
if err := os.Rename(tmp, path); err != nil {
return fmt.Errorf("replacing %s: %w", path, err)
}
return nil
}
// writeAndClose fills f, fixes its permissions and flushes it to disk. Close is
// deliberately checked: on a network or full filesystem it is where the write
// error finally surfaces.
func writeAndClose(f *os.File, data []byte, perm fs.FileMode) error {
if _, err := f.Write(data); err != nil {
f.Close()
return err
}
// os.CreateTemp always makes the file 0600. Chmod, unlike the mode argument
// to os.OpenFile, is not filtered through the umask, so this lands exactly.
if err := f.Chmod(perm); err != nil {
f.Close()
return err
}
// Without Sync the rename can outlive the data on a crash: the directory
// entry points at a file whose blocks were never written.
if err := f.Sync(); err != nil {
f.Close()
return err
}
return f.Close()
}
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis. | Stars: 470k | Updated | Claude CodeCodex | Verified | Install |
| diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness. | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependencies | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is available | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |