diff --git a/res/res_monitor.c b/res/res_monitor.c index d4273dec98..1b537868d4 100644 --- a/res/res_monitor.c +++ b/res/res_monitor.c @@ -168,13 +168,14 @@ int ast_monitor_start( struct ast_channel *chan, const char *format_spec, if (!ast_strlen_zero(fname_base)) { int directory = strchr(fname_base, '/') ? 1 : 0; const char *absolute = *fname_base == '/' ? "" : ast_config_AST_MONITOR_DIR; + const char *absolute_suffix = *fname_base == '/' ? "" : "/"; - snprintf(monitor->read_filename, FILENAME_MAX, "%s/%s-in", - absolute, fname_base); - snprintf(monitor->write_filename, FILENAME_MAX, "%s/%s-out", - absolute, fname_base); - snprintf(monitor->filename_base, FILENAME_MAX, "%s/%s", - absolute, fname_base); + snprintf(monitor->read_filename, FILENAME_MAX, "%s%s%s-in", + absolute, absolute_suffix, fname_base); + snprintf(monitor->write_filename, FILENAME_MAX, "%s%s%s-out", + absolute, absolute_suffix, fname_base); + snprintf(monitor->filename_base, FILENAME_MAX, "%s%s%s", + absolute, absolute_suffix, fname_base); /* try creating the directory just in case it doesn't exist */ if (directory) { @@ -330,11 +331,9 @@ int ast_monitor_stop(struct ast_channel *chan, int need_lock) char tmp[1024]; char tmp2[1024]; const char *format = !strcasecmp(chan->monitor->format,"wav49") ? "WAV" : chan->monitor->format; - char *name = chan->monitor->filename_base; - int directory = strchr(name, '/') ? 1 : 0; - const char *dir = directory ? "" : ast_config_AST_MONITOR_DIR; + char *fname_base = chan->monitor->filename_base; const char *execute, *execute_args; - const char *absolute = *name == '/' ? "" : "/"; + /* at this point, fname_base really is the full path */ /* Set the execute application */ execute = pbx_builtin_getvar_helper(chan, "MONITOR_EXEC"); @@ -352,9 +351,10 @@ int ast_monitor_stop(struct ast_channel *chan, int need_lock) execute_args = ""; } - snprintf(tmp, sizeof(tmp), "%s \"%s%s%s-in.%s\" \"%s%s%s-out.%s\" \"%s%s%s.%s\" %s &", execute, dir, absolute, name, format, dir, absolute, name, format, dir, absolute, name, format,execute_args); + snprintf(tmp, sizeof(tmp), "%s \"%s-in.%s\" \"%s-out.%s\" \"%s.%s\" %s &", + execute, fname_base, format, fname_base, format, fname_base, format,execute_args); if (delfiles) { - snprintf(tmp2,sizeof(tmp2), "( %s& rm -f \"%s%s%s-\"* ) &",tmp, dir, absolute, name); /* remove legs when done mixing */ + snprintf(tmp2,sizeof(tmp2), "( %s& rm -f \"%s-\"* ) &",tmp, fname_base); /* remove legs when done mixing */ ast_copy_string(tmp, tmp2, sizeof(tmp)); } ast_debug(1,"monitor executing %s\n",tmp); @@ -424,11 +424,18 @@ int ast_monitor_change_fname(struct ast_channel *chan, const char *fname_base, i if (chan->monitor) { int directory = strchr(fname_base, '/') ? 1 : 0; const char *absolute = *fname_base == '/' ? "" : ast_config_AST_MONITOR_DIR; + const char *absolute_suffix = *fname_base == '/' ? "" : "/"; char tmpstring[sizeof(chan->monitor->filename_base)] = ""; int i, fd[2] = { -1, -1 }, doexit = 0; /* before continuing, see if we're trying to rename the file to itself... */ - snprintf(tmpstring, sizeof(tmpstring), "%s/%s", absolute, fname_base); + snprintf(tmpstring, sizeof(tmpstring), "%s%s%s", absolute, absolute_suffix, fname_base); + + /* try creating the directory just in case it doesn't exist */ + if (directory) { + char *name = ast_strdupa(tmpstring); + ast_mkdir(dirname(name), 0777); + } /*!\note We cannot just compare filenames, due to symlinks, relative * paths, and other possible filesystem issues. We could use @@ -460,6 +467,7 @@ int ast_monitor_change_fname(struct ast_channel *chan, const char *fname_base, i } } unlink(tmpstring); + /* if previous monitor file existed in a subdirectory, the directory will not be removed */ unlink(chan->monitor->filename_base); if (doexit) { @@ -467,12 +475,6 @@ int ast_monitor_change_fname(struct ast_channel *chan, const char *fname_base, i return 0; } - /* try creating the directory just in case it doesn't exist */ - if (directory) { - char *name = ast_strdupa(fname_base); - ast_mkdir(dirname(name), 0777); - } - ast_copy_string(chan->monitor->filename_base, tmpstring, sizeof(chan->monitor->filename_base)); chan->monitor->filename_changed = 1; } else {