Reserve recording paths atomically

This commit is contained in:
Brandon McGinty (chatgpt)
2026-08-10 08:55:25 -04:00
committed by Brandon McGinty
parent 43d7addc15
commit 9fdcf6171d
2 changed files with 58 additions and 1 deletions
+24 -1
View File
@@ -57,14 +57,19 @@ func New(directory string, format string, now time.Time, frameSize int, interval
if err := os.MkdirAll(directory, 0755); err != nil {
return nil, err
}
path := UniquePath(directory, now, format)
path, err := reservePath(directory, now, format)
if err != nil {
return nil, err
}
args := ffmpegArgs(format, path)
cmd := exec.Command("ffmpeg", args...)
stdin, err := cmd.StdinPipe()
if err != nil {
_ = os.Remove(path)
return nil, err
}
if err := cmd.Start(); err != nil {
_ = os.Remove(path)
return nil, err
}
recorder := &Recorder{
@@ -91,6 +96,24 @@ func NormalizeFormat(format string) string {
return format
}
func reservePath(directory string, now time.Time, format string) (string, error) {
for {
path := UniquePath(directory, now, format)
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
if os.IsExist(err) {
continue
}
if err != nil {
return "", err
}
if err := file.Close(); err != nil {
_ = os.Remove(path)
return "", err
}
return path, nil
}
}
func UniquePath(directory string, now time.Time, format string) string {
base := fmt.Sprintf("barnard-recording-%s", now.Format("20060102-150405"))
path := filepath.Join(directory, base+"."+format)