Prevent recording output path replacement

This commit is contained in:
Brandon McGinty (chatgpt)
2026-08-10 11:25:08 -04:00
committed by Brandon McGinty
parent 57961ffc9f
commit 65489fd367
+26 -6
View File
@@ -57,18 +57,29 @@ func New(directory string, format string, now time.Time, frameSize int, interval
if err := os.MkdirAll(directory, 0755); err != nil {
return nil, err
}
path, err := reservePath(directory, now, format)
output, path, err := reserveOutput(directory, now, format)
if err != nil {
return nil, err
}
args := ffmpegArgs(format, path)
args := ffmpegArgs(format)
cmd := exec.Command("ffmpeg", args...)
// Pass the reserved file descriptor directly to ffmpeg. The file is never
// reopened by pathname, preventing replacement between reservation and use.
cmd.ExtraFiles = []*os.File{output}
stdin, err := cmd.StdinPipe()
if err != nil {
_ = output.Close()
_ = os.Remove(path)
return nil, err
}
if err := cmd.Start(); err != nil {
_ = output.Close()
_ = os.Remove(path)
return nil, err
}
if err := output.Close(); err != nil {
_ = cmd.Process.Kill()
_ = cmd.Wait()
_ = os.Remove(path)
return nil, err
}
@@ -96,13 +107,23 @@ func NormalizeFormat(format string) string {
return format
}
func reservePath(directory string, now time.Time, format string) (string, error) {
func reserveOutput(directory string, now time.Time, format string) (*os.File, 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 nil, "", err
}
return file, path, nil
}
}
// reservePath remains available for callers that only need to reserve a name.
func reservePath(directory string, now time.Time, format string) (string, error) {
file, path, err := reserveOutput(directory, now, format)
if err != nil {
return "", err
}
@@ -112,7 +133,6 @@ func reservePath(directory string, now time.Time, format string) (string, error)
}
return path, nil
}
}
func UniquePath(directory string, now time.Time, format string) string {
base := fmt.Sprintf("barnard-recording-%s", now.Format("20060102-150405"))
@@ -270,7 +290,7 @@ func writePCM(w io.Writer, samples []int16) error {
return err
}
func ffmpegArgs(format string, path string) []string {
func ffmpegArgs(format string) []string {
args := []string{
"-loglevel", "error",
"-f", "s16le",
@@ -281,5 +301,5 @@ func ffmpegArgs(format string, path string) []string {
if format == FormatOpus {
args = append(args, "-c:a", "libopus")
}
return append(args, "-y", path)
return append(args, "-f", format, "pipe:3")
}