Skip to content

Commit e4f891f

Browse files
committed
[windows] convert a windows glob to posix
1 parent fcdcdb1 commit e4f891f

6 files changed

Lines changed: 35 additions & 0 deletions

File tree

src/base/fs_util.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@
5555

5656
namespace lnav::filesystem {
5757

58+
std::string
59+
escape_glob_for_win(std::string arg)
60+
{
61+
#if defined(__MSYS__)
62+
std::replace(arg.begin(), arg.end(), '\\', '/');
63+
std::replace(arg.begin(), arg.end(), '^', '\\');
64+
return arg;
65+
#else
66+
return arg;
67+
#endif
68+
}
69+
5870
std::optional<std::filesystem::path>
5971
self_path()
6072
{

src/base/fs_util.hh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ is_glob(const std::string& fn)
6060
|| fn.find('[') != std::string::npos);
6161
}
6262

63+
std::string escape_glob_for_win(std::string arg);
64+
6365
bool is_url(const std::string& fn);
6466

6567
enum class path_type {

src/base/fs_util.tests.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ TEST_CASE("fs_util::to_posix_path")
5959
CHECK("" == pt.pt_path);
6060
}
6161

62+
#if defined(__MSYS__)
63+
TEST_CASE("fs_util::escape_glob_for_win")
64+
{
65+
CHECK(R"(c:/abc/def/*.log)"
66+
== lnav::filesystem::escape_glob_for_win(R"(c:\abc\def\*.log)"));
67+
CHECK(R"(c:/abc/def/\*.log)"
68+
== lnav::filesystem::escape_glob_for_win(R"(c:\abc\def\^*.log)"));
69+
}
70+
#endif
71+
6272
TEST_CASE("fs_util::build_path")
6373
{
6474
auto* old_path = getenv("PATH");

src/cmds.io.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,6 +1184,7 @@ com_open(exec_context& ec, std::string cmdline, std::vector<std::string>& args)
11841184
} else if (lnav::filesystem::is_glob(fn_str)) {
11851185
static_root_mem<glob_t, globfree> gl;
11861186

1187+
fn_str = lnav::filesystem::escape_glob_for_win(fn_str);
11871188
if (glob(fn_str.c_str(), GLOB_NOCHECK, nullptr, gl.inout())
11881189
== 0)
11891190
{

src/file_collection.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,12 @@ file_collection::expand_filename(
655655
}
656656

657657
auto filename_key = loo.loo_filename.empty() ? path : loo.loo_filename;
658+
#if defined(__MSYS__)
659+
auto win_path = lnav::filesystem::escape_glob_for_win(path);
660+
auto glob_rc = glob(win_path.c_str(), GLOB_NOCHECK, nullptr, gl.inout());
661+
#else
658662
auto glob_rc = glob(path.c_str(), GLOB_NOCHECK, nullptr, gl.inout());
663+
#endif
659664
if (glob_rc == 0) {
660665
if (gl->gl_pathc == 1 /*&& gl.gl_matchc == 0*/) {
661666
/* It's a pattern that doesn't match any files

src/fstat_vtab.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,12 @@ rcFilter(sqlite3_vtab_cursor* pVtabCursor,
473473
#endif
474474

475475
log_debug("doing glob %s", pattern);
476+
#if defined(__MSYS__)
477+
auto win_path = lnav::filesystem::escape_glob_for_win(pattern);
478+
switch (glob(win_path.c_str(), glob_flags, nullptr, pCur->c_glob.inout())) {
479+
#else
476480
switch (glob(pattern, glob_flags, nullptr, pCur->c_glob.inout())) {
481+
#endif
477482
case GLOB_NOSPACE:
478483
pVtabCursor->pVtab->zErrMsg
479484
= sqlite3_mprintf("No space to perform glob()");

0 commit comments

Comments
 (0)