Skip to content

Commit 416931d

Browse files
committed
added keys directory handling
1 parent eff973c commit 416931d

File tree

1 file changed

+42
-27
lines changed

1 file changed

+42
-27
lines changed

hls/ngx_rtmp_hls_module.c

+42-27
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ static void * ngx_rtmp_hls_create_app_conf(ngx_conf_t *cf);
2525
static char * ngx_rtmp_hls_merge_app_conf(ngx_conf_t *cf,
2626
void *parent, void *child);
2727
static ngx_int_t ngx_rtmp_hls_flush_audio(ngx_rtmp_session_t *s);
28-
static ngx_int_t ngx_rtmp_hls_ensure_directory(ngx_rtmp_session_t *s);
28+
static ngx_int_t ngx_rtmp_hls_ensure_directory(ngx_rtmp_session_t *s,
29+
ngx_str_t *path);
2930

3031

3132
#define NGX_RTMP_HLS_BUFSIZE (1024*1024)
@@ -846,15 +847,22 @@ ngx_rtmp_hls_open_fragment(ngx_rtmp_session_t *s, uint64_t ts,
846847
ngx_rtmp_hls_app_conf_t *hacf;
847848

848849
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_hls_module);
850+
849851
if (ctx->opened) {
850852
return NGX_OK;
851853
}
852854

853-
if (ngx_rtmp_hls_ensure_directory(s) != NGX_OK) {
855+
hacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_hls_module);
856+
857+
if (ngx_rtmp_hls_ensure_directory(s, &hacf->path) != NGX_OK) {
854858
return NGX_ERROR;
855859
}
856860

857-
hacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_hls_module);
861+
if (hacf->keys &&
862+
ngx_rtmp_hls_ensure_directory(s, &hacf->keys_path) != NGX_OK)
863+
{
864+
return NGX_ERROR;
865+
}
858866

859867
id = ngx_rtmp_hls_get_fragment_id(s, ts);
860868

@@ -1096,51 +1104,53 @@ ngx_rtmp_hls_restore_stream(ngx_rtmp_session_t *s)
10961104

10971105

10981106
static ngx_int_t
1099-
ngx_rtmp_hls_ensure_directory(ngx_rtmp_session_t *s)
1107+
ngx_rtmp_hls_ensure_directory(ngx_rtmp_session_t *s, ngx_str_t *path)
11001108
{
11011109
size_t len;
11021110
ngx_file_info_t fi;
11031111
ngx_rtmp_hls_ctx_t *ctx;
11041112
ngx_rtmp_hls_app_conf_t *hacf;
11051113

1106-
static u_char path[NGX_MAX_PATH + 1];
1114+
static u_char zpath[NGX_MAX_PATH + 1];
11071115

11081116
hacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_hls_module);
11091117

1110-
*ngx_snprintf(path, sizeof(path) - 1, "%V", &hacf->path) = 0;
1118+
if (path->len + 1 > sizeof(zpath)) {
1119+
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0, "hls: too long path");
1120+
return NGX_ERROR;
1121+
}
1122+
1123+
ngx_snprintf(zpath, sizeof(zpath), "%V%Z", path);
11111124

1112-
if (ngx_file_info(path, &fi) == NGX_FILE_ERROR) {
1125+
if (ngx_file_info(zpath, &fi) == NGX_FILE_ERROR) {
11131126

11141127
if (ngx_errno != NGX_ENOENT) {
11151128
ngx_log_error(NGX_LOG_ERR, s->connection->log, ngx_errno,
1116-
"hls: " ngx_file_info_n " failed on '%V'",
1117-
&hacf->path);
1129+
"hls: " ngx_file_info_n " failed on '%V'", path);
11181130
return NGX_ERROR;
11191131
}
11201132

11211133
/* ENOENT */
11221134

1123-
if (ngx_create_dir(path, NGX_RTMP_HLS_DIR_ACCESS) == NGX_FILE_ERROR) {
1135+
if (ngx_create_dir(zpath, NGX_RTMP_HLS_DIR_ACCESS) == NGX_FILE_ERROR) {
11241136
ngx_log_error(NGX_LOG_ERR, s->connection->log, ngx_errno,
1125-
"hls: " ngx_create_dir_n " failed on '%V'",
1126-
&hacf->path);
1137+
"hls: " ngx_create_dir_n " failed on '%V'", path);
11271138
return NGX_ERROR;
11281139
}
11291140

11301141
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
1131-
"hls: directory '%V' created", &hacf->path);
1142+
"hls: directory '%V' created", path);
11321143

11331144
} else {
11341145

11351146
if (!ngx_is_dir(&fi)) {
11361147
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
1137-
"hls: '%V' exists and is not a directory",
1138-
&hacf->path);
1148+
"hls: '%V' exists and is not a directory", path);
11391149
return NGX_ERROR;
11401150
}
11411151

11421152
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
1143-
"hls: directory '%V' exists", &hacf->path);
1153+
"hls: directory '%V' exists", path);
11441154
}
11451155

11461156
if (!hacf->nested) {
@@ -1149,44 +1159,49 @@ ngx_rtmp_hls_ensure_directory(ngx_rtmp_session_t *s)
11491159

11501160
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_hls_module);
11511161

1152-
len = hacf->path.len;
1153-
if (hacf->path.data[len - 1] == '/') {
1162+
len = path->len;
1163+
if (path->data[len - 1] == '/') {
11541164
len--;
11551165
}
11561166

1157-
*ngx_snprintf(path, sizeof(path) - 1, "%*s/%V", len, hacf->path.data,
1158-
&ctx->name) = 0;
1167+
if (len + 1 + ctx->name.len + 1 > sizeof(zpath)) {
1168+
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0, "hls: too long path");
1169+
return NGX_ERROR;
1170+
}
1171+
1172+
ngx_snprintf(zpath, sizeof(zpath) - 1, "%*s/%V%Z", len, path->data,
1173+
&ctx->name);
11591174

1160-
if (ngx_file_info(path, &fi) != NGX_FILE_ERROR) {
1175+
if (ngx_file_info(zpath, &fi) != NGX_FILE_ERROR) {
11611176

11621177
if (ngx_is_dir(&fi)) {
11631178
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
1164-
"hls: directory '%s' exists", path);
1179+
"hls: directory '%s' exists", zpath);
11651180
return NGX_OK;
11661181
}
11671182

11681183
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
1169-
"hls: '%s' exists and is not a directory", path);
1184+
"hls: '%s' exists and is not a directory", zpath);
11701185

11711186
return NGX_ERROR;
11721187
}
11731188

11741189
if (ngx_errno != NGX_ENOENT) {
11751190
ngx_log_error(NGX_LOG_ERR, s->connection->log, ngx_errno,
1176-
"hls: " ngx_file_info_n " failed on '%s'", path);
1191+
"hls: " ngx_file_info_n " failed on '%s'", zpath);
11771192
return NGX_ERROR;
11781193
}
11791194

11801195
/* NGX_ENOENT */
11811196

1182-
if (ngx_create_dir(path, NGX_RTMP_HLS_DIR_ACCESS) == NGX_FILE_ERROR) {
1197+
if (ngx_create_dir(zpath, NGX_RTMP_HLS_DIR_ACCESS) == NGX_FILE_ERROR) {
11831198
ngx_log_error(NGX_LOG_ERR, s->connection->log, ngx_errno,
1184-
"hls: " ngx_create_dir_n " failed on '%s'", path);
1199+
"hls: " ngx_create_dir_n " failed on '%s'", zpath);
11851200
return NGX_ERROR;
11861201
}
11871202

11881203
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
1189-
"hls: directory '%s' created", path);
1204+
"hls: directory '%s' created", zpath);
11901205

11911206
return NGX_OK;
11921207
}

0 commit comments

Comments
 (0)