Reserve recording paths atomically
This commit is contained in:
committed by
Brandon McGinty
parent
43d7addc15
commit
9fdcf6171d
+24
-1
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user