From 4897d06fc1c9c4d9d302942b6e3ac8a8e25aa793 Mon Sep 17 00:00:00 2001
From: Steven Liu <lingjiujianke@gmail.com>
Date: Mon, 26 Sep 2016 13:50:31 +0800
Subject: [PATCH 1/3] avformat/hlsenc: support mkdir_p for use_localtime_mkdir

when use use_localtime_mkdir to create multi level dir,
ffmpeg give error message:
ffmpeg -i ~/Movies/objectC/facebook.mp4 -c copy -use_localtime 1
-use_localtime_mkdir 1 -hls_segment_filename '%Y%m%d/file-%Y%m%d/%s.ts'
out.m3u8
error message:
Could not create directory 20160926/file-20160926 with use_localtime_mkdir
add mkdir_p for support the multi level dir

Signed-off-by: Steven Liu <lingjiujianke@gmail.com>
---
 libavformat/hlsenc.c | 31 ++++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)

diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index 428bae4..ac79759 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -133,6 +133,35 @@ typedef struct HLSContext {
     double initial_prog_date_time;
 } HLSContext;
 
+static int mkdir_p(const char *path) {
+    char *temp = strdup(path);
+    char *pos = temp;
+
+    if (!path) {
+        return -1;
+    }
+
+    if (!strncmp(temp, "/", 1)) {
+        pos++;
+    } else if (!strncmp(temp, "./", 2)) {
+        pos += 2;
+    }
+    for ( ; *pos != '\0'; ++pos) {
+        if (*pos == '/') {
+            *pos = '\0';
+            mkdir(temp, 0755);
+            *pos = '/';
+        }
+    }
+
+    if (*(pos - 1) != '/') {
+        mkdir(temp, 0755);
+    }
+
+    av_free(temp);
+    return 0;
+}
+
 static int hls_delete_old_segments(HLSContext *hls) {
 
     HLSSegment *segment, *previous_segment = NULL;
@@ -656,7 +685,7 @@ static int hls_start(AVFormatContext *s)
                     return AVERROR(ENOMEM);
                 }
                 dir = av_dirname(fn_copy);
-                if (mkdir(dir, 0777) == -1 && errno != EEXIST) {
+                if (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);
-- 
2.8.4 (Apple Git-73)

