[FFmpeg-devel] Adding mkdir option for img2enc (4th attempt)

Submitted by Dr Alan Barclay on Feb. 4, 2018, 12:51 p.m.

Details

Message ID c0acb843-b7b2-5474-e64f-2b83c1d92de4@escribe.co.uk
State New
Headers show

Commit Message

Dr Alan Barclay Feb. 4, 2018, 12:51 p.m.
On 17/01/2018 11:15, Carl Eugen Hoyos wrote:
> 2018-01-17 11:56 GMT+01:00 Dr. Alan Barclay <alan@escribe.co.uk>:
> 
>> Attached in a further patch - adding the error checks.
> 
> Please merge this patch into your previous patch.

Both patches updated.

Alan.

> And please avoid top-posting here, Carl Eugen
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel

Comments

Michael Niedermayer Feb. 5, 2018, 1:12 a.m.
On Sun, Feb 04, 2018 at 12:51:59PM +0000, Dr. Alan Barclay wrote:
> On 17/01/2018 11:15, Carl Eugen Hoyos wrote:
> >2018-01-17 11:56 GMT+01:00 Dr. Alan Barclay <alan@escribe.co.uk>:
> >
> >>Attached in a further patch - adding the error checks.
> >
> >Please merge this patch into your previous patch.
> 
> Both patches updated.
> 
> Alan.
> 
> >And please avoid top-posting here, Carl Eugen
> >_______________________________________________
> >ffmpeg-devel mailing list
> >ffmpeg-devel@ffmpeg.org
> >http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> 
> 
[...]

> diff --git a/libavformat/internal.h b/libavformat/internal.h
> index 1e2a3e05a1..2e8fa85d08 100644
> --- a/libavformat/internal.h
> +++ b/libavformat/internal.h
> @@ -703,4 +703,11 @@ int ff_unlock_avformat(void);
>   */
>  void ff_format_set_url(AVFormatContext *s, char *url);
>  
> +/**
> + * Make the specified directory.
> + *
> + * @param path  path for directory
> + */
> +int ff_mkdir_p(const char *path);

the return code and its relation to errno should be documented or better
the function should return the error code like all other functions in ffmpeg
and not depend on errno

also the relation of pathes and url needs to be documented

is a "file://..." working or should it be a file path


[...]
>  img2enc.c |   16 ++++++++++++++++
>  1 file changed, 16 insertions(+)
> 020db7642303243c3e0170376d0826158e44616f  0002-Adding-mkdir-option-for-img2enc.patch
> From 0b35e014cf36499f0b4b5e064b7f0ce71287649a Mon Sep 17 00:00:00 2001
> From: "Dr. Alan Barclay" <alan@escribe.co.uk>
> Date: Sun, 4 Feb 2018 12:21:51 +0000
> Subject: [PATCH 2/2] Adding mkdir option for img2enc.

update to doc/muxers.texi is missing



> 
> ---
>  libavformat/img2enc.c | 16 ++++++++++++++++
>  1 file changed, 16 insertions(+)
> 
> diff --git a/libavformat/img2enc.c b/libavformat/img2enc.c
> index a09cc8ec50..8f3fd98018 100644
> --- a/libavformat/img2enc.c
> +++ b/libavformat/img2enc.c
> @@ -42,6 +42,7 @@ typedef struct VideoMuxData {
>      char target[4][1024];
>      int update;
>      int use_strftime;
> +    int use_mkdir;
>      int frame_pts;
>      const char *muxer;
>      int use_rename;
> @@ -114,6 +115,20 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt)
>                     img->img_number, img->path);
>              return AVERROR(EINVAL);
>          }
> +        if (img->use_mkdir) {
> +            const char *temp_path;
> +            char *temp_filename = av_strdup(filename);
> +            if (!temp_filename) {
> +                return AVERROR(ENOMEM);
> +            }
> +            temp_path = av_dirname(temp_filename);
> +            if (ff_mkdir_p(temp_path) == -1 && errno != EEXIST) {
> +                av_log(s, AV_LOG_ERROR, "Could not create directory %s\n", temp_path);
> +                av_free(temp_filename);
> +                return AVERROR(errno);
> +            }
> +            av_free(temp_filename);
> +        }
>          for (i = 0; i < 4; i++) {
>              snprintf(img->tmp[i], sizeof(img->tmp[i]), "%s.tmp", filename);
>              av_strlcpy(img->target[i], filename, sizeof(img->target[i]));
> @@ -212,6 +227,7 @@ static const AVOption muxoptions[] = {
>      { "update",       "continuously overwrite one file", OFFSET(update),  AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0,       1, ENC },
>      { "start_number", "set first number in the sequence", OFFSET(img_number), AV_OPT_TYPE_INT,  { .i64 = 1 }, 0, INT_MAX, ENC },
>      { "strftime",     "use strftime for filename", OFFSET(use_strftime),  AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, ENC },
> +    { "mkdir",        "make sub-dirs as required", OFFSET(use_mkdir),  AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, ENC },
>      { "frame_pts",    "use current frame pts for filename", OFFSET(frame_pts),  AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, ENC },
>      { "atomic_writing", "write files atomically (using temporary files and renames)", OFFSET(use_rename), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, ENC },
>      { NULL },
> -- 
> 2.11.0
> 

> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel

Patch hide | download patch | download mbox

diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index cc13c94e97..a51248ec02 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -225,39 +225,6 @@  typedef struct HLSContext {
     AVIOContext *sub_m3u8_out;
 } HLSContext;
 
-static int mkdir_p(const char *path) {
-    int ret = 0;
-    char *temp = av_strdup(path);
-    char *pos = temp;
-    char tmp_ch = '\0';
-
-    if (!path || !temp) {
-        return -1;
-    }
-
-    if (!strncmp(temp, "/", 1) || !strncmp(temp, "\\", 1)) {
-        pos++;
-    } else if (!strncmp(temp, "./", 2) || !strncmp(temp, ".\\", 2)) {
-        pos += 2;
-    }
-
-    for ( ; *pos != '\0'; ++pos) {
-        if (*pos == '/' || *pos == '\\') {
-            tmp_ch = *pos;
-            *pos = '\0';
-            ret = mkdir(temp, 0755);
-            *pos = tmp_ch;
-        }
-    }
-
-    if ((*(pos - 1) != '/') || (*(pos - 1) != '\\')) {
-        ret = mkdir(temp, 0755);
-    }
-
-    av_free(temp);
-    return ret;
-}
-
 static int hlsenc_io_open(AVFormatContext *s, AVIOContext **pb, char *filename,
                           AVDictionary **options) {
     HLSContext *hls = s->priv_data;
@@ -1504,7 +1471,7 @@  static int hls_start(AVFormatContext *s, VariantStream *vs)
                     return AVERROR(ENOMEM);
                 }
                 dir = av_dirname(fn_copy);
-                if (mkdir_p(dir) == -1 && errno != EEXIST) {
+                if (ff_mkdir_p(dir) == -1 && errno != EEXIST) {
                     av_log(oc, AV_LOG_ERROR, "Could not create directory %s with use_localtime_mkdir\n", dir);
                     av_free(fn_copy);
                     return AVERROR(errno);
@@ -1730,7 +1697,7 @@  static int format_name(char *buf, int buf_len, int index)
         }
 
         dir = av_dirname(mod_buf_dup);
-        if (mkdir_p(dir) == -1 && errno != EEXIST) {
+        if (ff_mkdir_p(dir) == -1 && errno != EEXIST) {
             ret = AVERROR(errno);
             goto fail;
         }
diff --git a/libavformat/internal.h b/libavformat/internal.h
index 1e2a3e05a1..2e8fa85d08 100644
--- a/libavformat/internal.h
+++ b/libavformat/internal.h
@@ -703,4 +703,11 @@  int ff_unlock_avformat(void);
  */
 void ff_format_set_url(AVFormatContext *s, char *url);
 
+/**
+ * Make the specified directory.
+ *
+ * @param path  path for directory
+ */
+int ff_mkdir_p(const char *path);
+
 #endif /* AVFORMAT_INTERNAL_H */
diff --git a/libavformat/utils.c b/libavformat/utils.c
index 28ea071409..11bacdf1b4 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -5658,3 +5658,36 @@  FF_DISABLE_DEPRECATION_WARNINGS
 FF_ENABLE_DEPRECATION_WARNINGS
 #endif
 }
+
+int ff_mkdir_p(const char *path) {
+    int ret = 0;
+    char *temp = av_strdup(path);
+    char *pos = temp;
+    char tmp_ch = '\0';
+
+    if (!path || !temp) {
+        return -1;
+    }
+
+    if (!strncmp(temp, "/", 1) || !strncmp(temp, "\\", 1)) {
+        pos++;
+    } else if (!strncmp(temp, "./", 2) || !strncmp(temp, ".\\", 2)) {
+        pos += 2;
+    }
+
+    for ( ; *pos != '\0'; ++pos) {
+        if (*pos == '/' || *pos == '\\') {
+            tmp_ch = *pos;
+            *pos = '\0';
+            ret = mkdir(temp, 0755);
+            *pos = tmp_ch;
+        }
+    }
+
+    if ((*(pos - 1) != '/') || (*(pos - 1) != '\\')) {
+        ret = mkdir(temp, 0755);
+    }
+
+    av_free(temp);
+    return ret;
+}