1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2025-08-15 14:13:16 +02:00

smoothstreamingenc: Write to a temp file while updating the manifest

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.

Signed-off-by: Martin Storsjö <martin@martin.st>
This commit is contained in:
Martin Storsjö
2013-08-17 16:42:23 +01:00
parent b1f9cdc37f
commit 310cc4bf82

View File

@@ -210,14 +210,15 @@ static int write_manifest(AVFormatContext *s, int final)
{ {
SmoothStreamingContext *c = s->priv_data; SmoothStreamingContext *c = s->priv_data;
AVIOContext *out; 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; int ret, i, video_chunks = 0, audio_chunks = 0, video_streams = 0, audio_streams = 0;
int64_t duration = 0; int64_t duration = 0;
snprintf(filename, sizeof(filename), "%s/Manifest", s->filename); 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) { 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; return ret;
} }
avio_printf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"); 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_printf(out, "</SmoothStreamingMedia>\n");
avio_flush(out); avio_flush(out);
avio_close(out); avio_close(out);
rename(temp_filename, filename);
return 0; return 0;
} }