Git designed for AI agents.
//go:build unix
package store
import (
"os"
"syscall"
)
// lockFile takes an exclusive flock on path, creating the file if needed. The
// returned function releases the lock and closes the file.
func lockFile(path string) (func(), error) {
f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0o644)
if err != nil {
return nil, err
}
if err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX); err != nil {
_ = f.Close()
return nil, err
}
return func() {
_ = syscall.Flock(int(f.Fd()), syscall.LOCK_UN)
_ = f.Close()
}, nil
}
// syncDir fsyncs a directory so that a rename inside it is durable.
func syncDir(dir string) error {
d, err := os.Open(dir)
if err != nil {
return err
}
defer func() { _ = d.Close() }()
return d.Sync()
}