diff mbox series

[FFmpeg-devel,3/3] avformat/hlsenc: Factor out deleting files from deleting segments

Message ID 20200408133544.18933-3-andreas.rheinhardt@gmail.com
State Accepted
Headers show
Series [FFmpeg-devel,1/3] avformat/hlsenc: Fix memleak when deleting old segments | expand

Checks

Context Check Description
andriy/ffmpeg-patchwork warning Failed to apply patch

Commit Message

Andreas Rheinhardt April 8, 2020, 1:35 p.m. UTC
Removes code duplication.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
This will make hls_delete_old_segments() return 1 when io_open fails and
IO errors are ignored, but the only caller of hls_delete_old_segments()
(namely hls_append_segment()) doesn't care.

 libavformat/hlsenc.c | 52 ++++++++++++++++++++------------------------
 1 file changed, 24 insertions(+), 28 deletions(-)

Comments

Steven Liu April 8, 2020, 2:13 p.m. UTC | #1
> 2020年4月8日 下午9:35,Andreas Rheinhardt <andreas.rheinhardt@gmail.com> 写道:
> 
> Removes code duplication.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
> ---
> This will make hls_delete_old_segments() return 1 when io_open fails and
> IO errors are ignored, but the only caller of hls_delete_old_segments()
> (namely hls_append_segment()) doesn't care.
> 
> libavformat/hlsenc.c | 52 ++++++++++++++++++++------------------------
> 1 file changed, 24 insertions(+), 28 deletions(-)
> 
> diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
> index d7562ad20c..79d1559699 100644
> --- a/libavformat/hlsenc.c
> +++ b/libavformat/hlsenc.c
> @@ -483,6 +483,26 @@ static void reflush_dynbuf(VariantStream *vs, int *range_length)
> #define SEPARATOR '/'
> #endif
> 
> +static int hls_delete_file(HLSContext *hls, AVFormatContext *avf,
> +                           const char *path, const char *proto)
> +{
> +    if (hls->method || (proto && !av_strcasecmp(proto, "http"))) {
> +        AVDictionary *opt = NULL;
> +        AVIOContext  *out = NULL;
> +        int ret;
> +        av_dict_set(&opt, "method", "DELETE", 0);
> +        ret = avf->io_open(avf, &out, path, AVIO_FLAG_WRITE, &opt);
> +        av_dict_free(&opt);
> +        if (ret < 0)
> +            return hls->ignore_io_errors ? 1 : ret;
> +        ff_format_io_close(avf, &out);
> +    } else if (unlink(path) < 0) {
> +        av_log(hls, AV_LOG_ERROR, "failed to delete old segment %s: %s\n",
> +               path, strerror(errno));
> +    }
> +    return 0;
> +}
> +
> static int hls_delete_old_segments(AVFormatContext *s, HLSContext *hls,
>                                    VariantStream *vs)
> {
> @@ -497,8 +517,6 @@ static int hls_delete_old_segments(AVFormatContext *s, HLSContext *hls,
>     char *dirname_repl = NULL;
>     char *vtt_dirname = NULL;
>     char *vtt_dirname_r = NULL;
> -    AVDictionary *options = NULL;
> -    AVIOContext *out = NULL;
>     const char *proto = NULL;
> 
>     av_bprint_init(&path, 0, AV_BPRINT_SIZE_UNLIMITED);
> @@ -562,19 +580,8 @@ static int hls_delete_old_segments(AVFormatContext *s, HLSContext *hls,
>         }
> 
>         proto = avio_find_protocol_name(s->url);
> -        if (hls->method || (proto && !av_strcasecmp(proto, "http"))) {
> -            av_dict_set(&options, "method", "DELETE", 0);
> -            if ((ret = vs->avf->io_open(vs->avf, &out, path.str,
> -                                        AVIO_FLAG_WRITE, &options)) < 0) {
> -                if (hls->ignore_io_errors)
> -                    ret = 0;
> -                goto fail;
> -            }
> -            ff_format_io_close(vs->avf, &out);
> -        } else if (unlink(path.str) < 0) {
> -            av_log(hls, AV_LOG_ERROR, "failed to delete old segment %s: %s\n",
> -                   path.str, strerror(errno));
> -        }
> +        if (ret = hls_delete_file(hls, vs->avf, path.str, proto))
> +            goto fail;
> 
>         if ((segment->sub_filename[0] != '\0')) {
>             vtt_dirname_r = av_strdup(vs->vtt_avf->url);
> @@ -590,19 +597,8 @@ static int hls_delete_old_segments(AVFormatContext *s, HLSContext *hls,
>                 goto fail;
>             }
> 
> -            if (hls->method || (proto && !av_strcasecmp(proto, "http"))) {
> -                av_dict_set(&options, "method", "DELETE", 0);
> -                if ((ret = vs->vtt_avf->io_open(vs->vtt_avf, &out, path.str,
> -                                                AVIO_FLAG_WRITE, &options)) < 0) {
> -                    if (hls->ignore_io_errors)
> -                        ret = 0;
> -                    goto fail;
> -                }
> -                ff_format_io_close(vs->vtt_avf, &out);
> -            } else if (unlink(path.str) < 0) {
> -                av_log(hls, AV_LOG_ERROR, "failed to delete old segment %s: %s\n",
> -                       path.str, strerror(errno));
> -            }
> +            if (ret = hls_delete_file(hls, vs->vtt_avf, path.str, proto))
> +                goto fail;
>         }
>         av_bprint_clear(&path);
>         previous_segment = segment;
> -- 
> 2.20.1
> 
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> 
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

LGTM

Thanks

Steven Liu
diff mbox series

Patch

diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index d7562ad20c..79d1559699 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -483,6 +483,26 @@  static void reflush_dynbuf(VariantStream *vs, int *range_length)
 #define SEPARATOR '/'
 #endif
 
+static int hls_delete_file(HLSContext *hls, AVFormatContext *avf,
+                           const char *path, const char *proto)
+{
+    if (hls->method || (proto && !av_strcasecmp(proto, "http"))) {
+        AVDictionary *opt = NULL;
+        AVIOContext  *out = NULL;
+        int ret;
+        av_dict_set(&opt, "method", "DELETE", 0);
+        ret = avf->io_open(avf, &out, path, AVIO_FLAG_WRITE, &opt);
+        av_dict_free(&opt);
+        if (ret < 0)
+            return hls->ignore_io_errors ? 1 : ret;
+        ff_format_io_close(avf, &out);
+    } else if (unlink(path) < 0) {
+        av_log(hls, AV_LOG_ERROR, "failed to delete old segment %s: %s\n",
+               path, strerror(errno));
+    }
+    return 0;
+}
+
 static int hls_delete_old_segments(AVFormatContext *s, HLSContext *hls,
                                    VariantStream *vs)
 {
@@ -497,8 +517,6 @@  static int hls_delete_old_segments(AVFormatContext *s, HLSContext *hls,
     char *dirname_repl = NULL;
     char *vtt_dirname = NULL;
     char *vtt_dirname_r = NULL;
-    AVDictionary *options = NULL;
-    AVIOContext *out = NULL;
     const char *proto = NULL;
 
     av_bprint_init(&path, 0, AV_BPRINT_SIZE_UNLIMITED);
@@ -562,19 +580,8 @@  static int hls_delete_old_segments(AVFormatContext *s, HLSContext *hls,
         }
 
         proto = avio_find_protocol_name(s->url);
-        if (hls->method || (proto && !av_strcasecmp(proto, "http"))) {
-            av_dict_set(&options, "method", "DELETE", 0);
-            if ((ret = vs->avf->io_open(vs->avf, &out, path.str,
-                                        AVIO_FLAG_WRITE, &options)) < 0) {
-                if (hls->ignore_io_errors)
-                    ret = 0;
-                goto fail;
-            }
-            ff_format_io_close(vs->avf, &out);
-        } else if (unlink(path.str) < 0) {
-            av_log(hls, AV_LOG_ERROR, "failed to delete old segment %s: %s\n",
-                   path.str, strerror(errno));
-        }
+        if (ret = hls_delete_file(hls, vs->avf, path.str, proto))
+            goto fail;
 
         if ((segment->sub_filename[0] != '\0')) {
             vtt_dirname_r = av_strdup(vs->vtt_avf->url);
@@ -590,19 +597,8 @@  static int hls_delete_old_segments(AVFormatContext *s, HLSContext *hls,
                 goto fail;
             }
 
-            if (hls->method || (proto && !av_strcasecmp(proto, "http"))) {
-                av_dict_set(&options, "method", "DELETE", 0);
-                if ((ret = vs->vtt_avf->io_open(vs->vtt_avf, &out, path.str,
-                                                AVIO_FLAG_WRITE, &options)) < 0) {
-                    if (hls->ignore_io_errors)
-                        ret = 0;
-                    goto fail;
-                }
-                ff_format_io_close(vs->vtt_avf, &out);
-            } else if (unlink(path.str) < 0) {
-                av_log(hls, AV_LOG_ERROR, "failed to delete old segment %s: %s\n",
-                       path.str, strerror(errno));
-            }
+            if (ret = hls_delete_file(hls, vs->vtt_avf, path.str, proto))
+                goto fail;
         }
         av_bprint_clear(&path);
         previous_segment = segment;