]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/hlsenc.c
Merge commit '82b6e451161f19ab90bfb9565b710021caf5dcbd'
[ffmpeg] / libavformat / hlsenc.c
index 1831c177ad6a2e3f8e2845137220016b1ad0ce6e..788535162a7d56b1088ec52ec20b334a51d14e57 100644 (file)
@@ -242,10 +242,18 @@ static int hls_window(AVFormatContext *s, int last)
     int target_duration = 0;
     int ret = 0;
     AVIOContext *out = NULL;
+    char temp_filename[1024];
     int64_t sequence = FFMAX(hls->start_sequence, hls->sequence - hls->nb_entries);
     int version = hls->flags & HLS_SINGLE_FILE ? 4 : 3;
+    const char *proto = avio_find_protocol_name(s->filename);
+    int use_rename = proto && !strcmp(proto, "file");
+    static unsigned warned_non_file;
 
-    if ((ret = avio_open2(&out, s->filename, AVIO_FLAG_WRITE,
+    if (!use_rename && !warned_non_file++)
+        av_log(s, AV_LOG_ERROR, "Cannot use rename on non file protocol, this may lead to races and temporarly partial files\n");
+
+    snprintf(temp_filename, sizeof(temp_filename), use_rename ? "%s.tmp" : "%s", s->filename);
+    if ((ret = avio_open2(&out, temp_filename, AVIO_FLAG_WRITE,
                           &s->interrupt_callback, NULL)) < 0)
         goto fail;
 
@@ -280,6 +288,8 @@ static int hls_window(AVFormatContext *s, int last)
 
 fail:
     avio_closep(&out);
+    if (ret >= 0 && use_rename)
+        ff_rename(temp_filename, s->filename, s);
     return ret;
 }