Skip to content

Commit

Permalink
expand_path: better handle paths with spaces
Browse files Browse the repository at this point in the history
I.e., work around surprising behavior of wordexp

Fixes #2566.
  • Loading branch information
djcb committed Oct 6, 2023
1 parent 2c4c3d8 commit fcd8903
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions lib/utils/mu-utils-file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -352,18 +352,21 @@ Mu::play (const std::string& path)


Result<std::string>
Mu::expand_path(const std::string& str)
expand_path_real(const std::string& str)
{
#ifndef HAVE_WORDEXP_H
return Ok(std::string{str});
#else
int res;
wordexp_t result;
memset(&result, 0, sizeof(result));
wordexp_t result{};

res = wordexp(str.c_str(), &result, 0);
if (res != 0 || result.we_wordc == 0)
return Err(Error::Code::File, "cannot expand '%s'; err=%d", str.c_str(), res);
if (res != 0)
return Err(Error::Code::File, "cannot expand {}; err={}", str, res);
else if (auto&n = result.we_wordc; n != 1) {
wordfree(&result);
return Err(Error::Code::File, "expected 1 expansions, but got {} for {}", n, str);
}

std::string expanded{result.we_wordv[0]};
wordfree(&result);
Expand All @@ -374,6 +377,18 @@ Mu::expand_path(const std::string& str)
}


Result<std::string>
Mu::expand_path(const std::string& str)
{
if (auto&& res{expand_path_real(str)}; res)
return res;

// failed... try quoting.
auto qstr{to_string_gchar(g_shell_quote(str.c_str()))};
return expand_path_real(qstr);
}



#ifdef BUILD_TESTS

Expand Down

0 comments on commit fcd8903

Please sign in to comment.