[FFmpeg-devel] [PATCH 06/33] fftools/ffmpeg: return errors from assert_file_overwrite() instead of aborting

Anton Khirnov anton at khirnov.net
Thu Jul 13 13:55:26 EEST 2023


---
 fftools/ffmpeg.h          |  2 +-
 fftools/ffmpeg_demux.c    |  4 +++-
 fftools/ffmpeg_mux_init.c | 11 ++++++++---
 fftools/ffmpeg_opt.c      | 12 +++++++-----
 4 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 3201163a4f..30020cd0f8 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -700,7 +700,7 @@ void show_usage(void);
 void remove_avoptions(AVDictionary **a, AVDictionary *b);
 void assert_avoptions(AVDictionary *m);
 
-void assert_file_overwrite(const char *filename);
+int assert_file_overwrite(const char *filename);
 char *file_read(const char *filename);
 AVDictionary *strip_specifiers(const AVDictionary *dict);
 const AVCodec *find_codec_or_die(void *logctx, const char *name,
diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c
index ab99daf9f8..bc915178ec 100644
--- a/fftools/ffmpeg_demux.c
+++ b/fftools/ffmpeg_demux.c
@@ -1277,7 +1277,9 @@ static int dump_attachment(InputStream *ist, const char *filename)
         return AVERROR(EINVAL);
     }
 
-    assert_file_overwrite(filename);
+    ret = assert_file_overwrite(filename);
+    if (ret < 0)
+        return ret;
 
     if ((ret = avio_open2(&out, filename, AVIO_FLAG_WRITE, &int_cb, NULL)) < 0) {
         av_log(ist, AV_LOG_FATAL, "Could not open file %s for writing.\n",
diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c
index 8a3e7b98cf..4d40ceda05 100644
--- a/fftools/ffmpeg_mux_init.c
+++ b/fftools/ffmpeg_mux_init.c
@@ -2442,7 +2442,9 @@ int of_open(const OptionsContext *o, const char *filename)
 
     if (!(oc->oformat->flags & AVFMT_NOFILE)) {
         /* test if it already exists to avoid losing precious files */
-        assert_file_overwrite(filename);
+        err = assert_file_overwrite(filename);
+        if (err < 0)
+            return err;
 
         /* open the file */
         if ((err = avio_open2(&oc->pb, filename, AVIO_FLAG_WRITE,
@@ -2452,8 +2454,11 @@ int of_open(const OptionsContext *o, const char *filename)
                    filename, av_err2str(err));
             return err;
         }
-    } else if (strcmp(oc->oformat->name, "image2")==0 && !av_filename_number_test(filename))
-        assert_file_overwrite(filename);
+    } else if (strcmp(oc->oformat->name, "image2")==0 && !av_filename_number_test(filename)) {
+        err = assert_file_overwrite(filename);
+        if (err < 0)
+            return err;
+    }
 
     if (o->mux_preload) {
         av_dict_set_int(&mux->opts, "preload", o->mux_preload*AV_TIME_BASE, 0);
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index 7f22b22604..300f042660 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -652,13 +652,13 @@ const AVCodec *find_codec_or_die(void *logctx, const char *name,
     return codec;
 }
 
-void assert_file_overwrite(const char *filename)
+int assert_file_overwrite(const char *filename)
 {
     const char *proto_name = avio_find_protocol_name(filename);
 
     if (file_overwrite && no_file_overwrite) {
         fprintf(stderr, "Error, both -y and -n supplied. Exiting.\n");
-        exit_program(1);
+        return AVERROR(EINVAL);
     }
 
     if (!file_overwrite) {
@@ -670,13 +670,13 @@ void assert_file_overwrite(const char *filename)
                 signal(SIGINT, SIG_DFL);
                 if (!read_yesno()) {
                     av_log(NULL, AV_LOG_FATAL, "Not overwriting - exiting\n");
-                    exit_program(1);
+                    return AVERROR_EXIT;
                 }
                 term_init();
             }
             else {
                 av_log(NULL, AV_LOG_FATAL, "File '%s' already exists. Exiting.\n", filename);
-                exit_program(1);
+                return AVERROR_EXIT;
             }
         }
     }
@@ -689,10 +689,12 @@ void assert_file_overwrite(const char *filename)
              if (!strcmp(filename, file->ctx->url)) {
                  av_log(NULL, AV_LOG_FATAL, "Output %s same as Input #%d - exiting\n", filename, i);
                  av_log(NULL, AV_LOG_WARNING, "FFmpeg cannot edit existing files in-place.\n");
-                 exit_program(1);
+                 return AVERROR(EINVAL);
              }
         }
     }
+
+    return 0;
 }
 
 /* read file contents into a string */
-- 
2.40.1



More information about the ffmpeg-devel mailing list