<p>Nathan Bruning has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/19797">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">media cache: make sure generated filenames cannot conflict<br><br>In rare cases, two entries of the media cache would have the same<br>base file name, only with different extensions. During media lookup,<br>the wrong file can be played, depending on the channel format.<br><br>ASTERISK-30408 #close<br><br>Change-Id: If6b5f4af9cbdf39009252594eb987b07bfb493a4<br>---<br>M include/asterisk/bucket.h<br>M main/bucket.c<br>2 files changed, 70 insertions(+), 7 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/97/19797/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/include/asterisk/bucket.h b/include/asterisk/bucket.h</span><br><span>index 47cc561..7765c22 100644</span><br><span>--- a/include/asterisk/bucket.h</span><br><span>+++ b/include/asterisk/bucket.h</span><br><span>@@ -454,6 +454,16 @@</span><br><span> struct ast_json *ast_bucket_file_json(const struct ast_bucket_file *file);</span><br><span> </span><br><span> /*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Return 1 is there are any files with the given glob pattern</span><br><span style="color: hsl(120, 100%, 40%);">+ * </span><br><span style="color: hsl(120, 100%, 40%);">+ * \param pattern glob pattern</span><br><span style="color: hsl(120, 100%, 40%);">+ * </span><br><span style="color: hsl(120, 100%, 40%);">+ * \retval 1 if any files match</span><br><span style="color: hsl(120, 100%, 40%);">+ * \retval 0 if not (or error)</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+int files_exist_with_pattern(const char *pattern);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span> * \brief Common file snapshot creation callback for creating a temporary file</span><br><span> *</span><br><span> * \param file Pointer to the file snapshot</span><br><span>diff --git a/main/bucket.c b/main/bucket.c</span><br><span>index 01934f3..10a4453 100644</span><br><span>--- a/main/bucket.c</span><br><span>+++ b/main/bucket.c</span><br><span>@@ -896,19 +896,57 @@</span><br><span> return json;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+/* return 1 if a file with the given pattern exists, 0 otherwise. On error, 0 is returned. */</span><br><span style="color: hsl(120, 100%, 40%);">+int files_exist_with_pattern(const char *pattern) {</span><br><span style="color: hsl(120, 100%, 40%);">+ int glob_ret;</span><br><span style="color: hsl(120, 100%, 40%);">+ glob_t globbuf;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ globbuf.gl_offs = 0; /* initialize it to silence gcc */</span><br><span style="color: hsl(120, 100%, 40%);">+ glob_ret = glob(pattern, MY_GLOB_FLAGS, NULL, &globbuf);</span><br><span style="color: hsl(120, 100%, 40%);">+ globfree(&globbuf);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (glob_ret == 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* glob succeeded and there are matches */</span><br><span style="color: hsl(120, 100%, 40%);">+ return 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (glob_ret == GLOB_NOMATCH) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* error during matching, return 0 for no match */</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> int ast_bucket_file_temporary_create(struct ast_bucket_file *file)</span><br><span> {</span><br><span> int fd;</span><br><span style="color: hsl(120, 100%, 40%);">+ int i;</span><br><span style="color: hsl(120, 100%, 40%);">+ char pattern[PATH_MAX+2];</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- snprintf(file->path, sizeof(file->path), "%s/bucket-XXXXXX", ast_config_AST_CACHE_DIR);</span><br><span style="color: hsl(120, 100%, 40%);">+ /* limit number of tries to avoid infinite loop if logic fails for some reason */</span><br><span style="color: hsl(120, 100%, 40%);">+ for(i=0; i<100; i++) {</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(file->path, sizeof(file->path), "%s/bucket-XXXXXX", ast_config_AST_CACHE_DIR);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- fd = mkstemp(file->path);</span><br><span style="color: hsl(0, 100%, 40%);">- if (fd < 0) {</span><br><span style="color: hsl(0, 100%, 40%);">- return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ fd = mkstemp(file->path);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (fd < 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ close(fd);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Asterisk media lookup will try different file extensions (wav, wav16) depending on the </span><br><span style="color: hsl(120, 100%, 40%);">+ * channel format. To avoid conflicts, each bucket entry should have a unique filename -</span><br><span style="color: hsl(120, 100%, 40%);">+ * not considering extensions. Ie: if there is a file x.wav, we shouldn't create x.wav16.</span><br><span style="color: hsl(120, 100%, 40%);">+ * (note that at this point, file->path is still without extension)</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(pattern, sizeof(pattern), "%s.*", file->path);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!files_exist_with_pattern(pattern)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* safe to return this filename */</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ unlink(file->path);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_NOTICE, "Media cache filename %s already in-use, retrying\n", file->path);</span><br><span> }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- close(fd);</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Could not decide on temp filename after 100 tries, giving up\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span> }</span><br><span> </span><br><span> void ast_bucket_file_temporary_destroy(struct ast_bucket_file *file)</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/19797">change 19797</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/c/asterisk/+/19797"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: If6b5f4af9cbdf39009252594eb987b07bfb493a4 </div>
<div style="display:none"> Gerrit-Change-Number: 19797 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Nathan Bruning <nathan@iperity.com> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>