smoothstreamingenc: Write to a temp file while updating the manifest

Message ID 1377619534-70016-1-git-send-email-martin@martin.st
State Committed
Commit 310cc4bf82824f09bdd0b9147ed725cdbeaf9bdd
Headers show

Commit Message

Martin Storsjö Aug. 27, 2013, 4:05 p.m.
If a client tries to read the file while it's being updated, the client
would get an incomplete manifest. Instead write to a separate temp file
and atomically rename it to replace the previous one.
---
 libavformat/smoothstreamingenc.c |    8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

Comments

Luca Barbato Aug. 27, 2013, 4:25 p.m. | #1
On 27/08/13 18:05, Martin Storsjö wrote:
> If a client tries to read the file while it's being updated, the client
> would get an incomplete manifest. Instead write to a separate temp file
> and atomically rename it to replace the previous one.
> ---
>  libavformat/smoothstreamingenc.c |    8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
> 

Probably ok. I doubt using a memory backed avio would work better.

lu
Martin Storsjö Aug. 27, 2013, 4:32 p.m. | #2
On Tue, 27 Aug 2013, Luca Barbato wrote:

> On 27/08/13 18:05, Martin Storsjö wrote:
>> If a client tries to read the file while it's being updated, the client
>> would get an incomplete manifest. Instead write to a separate temp file
>> and atomically rename it to replace the previous one.
>> ---
>>  libavformat/smoothstreamingenc.c |    8 +++++---
>>  1 file changed, 5 insertions(+), 3 deletions(-)
>> 
>
> Probably ok. I doubt using a memory backed avio would work better.

Indeed, that's probably more work and less obvious/robust.

// Martin

Patch

diff --git a/libavformat/smoothstreamingenc.c b/libavformat/smoothstreamingenc.c
index d26af05..7007264 100644
--- a/libavformat/smoothstreamingenc.c
+++ b/libavformat/smoothstreamingenc.c
@@ -210,14 +210,15 @@  static int write_manifest(AVFormatContext *s, int final)
 {
     SmoothStreamingContext *c = s->priv_data;
     AVIOContext *out;
-    char filename[1024];
+    char filename[1024], temp_filename[1024];
     int ret, i, video_chunks = 0, audio_chunks = 0, video_streams = 0, audio_streams = 0;
     int64_t duration = 0;
 
     snprintf(filename, sizeof(filename), "%s/Manifest", s->filename);
-    ret = avio_open2(&out, filename, AVIO_FLAG_WRITE, &s->interrupt_callback, NULL);
+    snprintf(temp_filename, sizeof(temp_filename), "%s/Manifest.tmp", s->filename);
+    ret = avio_open2(&out, temp_filename, AVIO_FLAG_WRITE, &s->interrupt_callback, NULL);
     if (ret < 0) {
-        av_log(s, AV_LOG_ERROR, "Unable to open %s for writing\n", filename);
+        av_log(s, AV_LOG_ERROR, "Unable to open %s for writing\n", temp_filename);
         return ret;
     }
     avio_printf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
@@ -278,6 +279,7 @@  static int write_manifest(AVFormatContext *s, int final)
     avio_printf(out, "</SmoothStreamingMedia>\n");
     avio_flush(out);
     avio_close(out);
+    rename(temp_filename, filename);
     return 0;
 }