mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 21:37:34 +00:00
Shell: Expand glob to only directories if it ends with a slash
The glob "*/" should only expand to directories, and the directories should also have a trailing slash. This commit also replaces Shell::split_path with StringView::split_view since it accomplishes the same task.
This commit is contained in:
parent
0415d03b4f
commit
cc157629b4
2 changed files with 12 additions and 25 deletions
|
@ -252,28 +252,6 @@ bool Shell::is_glob(StringView s)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector<StringView> Shell::split_path(StringView path)
|
|
||||||
{
|
|
||||||
Vector<StringView> parts;
|
|
||||||
|
|
||||||
size_t substart = 0;
|
|
||||||
for (size_t i = 0; i < path.length(); i++) {
|
|
||||||
char ch = path[i];
|
|
||||||
if (ch != '/')
|
|
||||||
continue;
|
|
||||||
size_t sublen = i - substart;
|
|
||||||
if (sublen != 0)
|
|
||||||
parts.append(path.substring_view(substart, sublen));
|
|
||||||
substart = i + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t taillen = path.length() - substart;
|
|
||||||
if (taillen != 0)
|
|
||||||
parts.append(path.substring_view(substart, taillen));
|
|
||||||
|
|
||||||
return parts;
|
|
||||||
}
|
|
||||||
|
|
||||||
Vector<DeprecatedString> Shell::expand_globs(StringView path, StringView base)
|
Vector<DeprecatedString> Shell::expand_globs(StringView path, StringView base)
|
||||||
{
|
{
|
||||||
auto explicitly_set_base = false;
|
auto explicitly_set_base = false;
|
||||||
|
@ -281,7 +259,8 @@ Vector<DeprecatedString> Shell::expand_globs(StringView path, StringView base)
|
||||||
base = "/"sv;
|
base = "/"sv;
|
||||||
explicitly_set_base = true;
|
explicitly_set_base = true;
|
||||||
}
|
}
|
||||||
auto parts = split_path(path);
|
|
||||||
|
auto parts = path.split_view('/', SplitBehavior::KeepTrailingSeparator);
|
||||||
DeprecatedString base_string = base;
|
DeprecatedString base_string = base;
|
||||||
struct stat statbuf;
|
struct stat statbuf;
|
||||||
if (lstat(base_string.characters(), &statbuf) < 0) {
|
if (lstat(base_string.characters(), &statbuf) < 0) {
|
||||||
|
@ -326,12 +305,19 @@ Vector<DeprecatedString> Shell::expand_globs(Vector<StringView> path_segments, S
|
||||||
if (is_glob(first_segment)) {
|
if (is_glob(first_segment)) {
|
||||||
Vector<DeprecatedString> result;
|
Vector<DeprecatedString> result;
|
||||||
|
|
||||||
|
auto const is_glob_directory = first_segment.ends_with('/');
|
||||||
|
if (is_glob_directory)
|
||||||
|
first_segment = first_segment.substring_view(0, first_segment.length() - 1);
|
||||||
|
|
||||||
Core::DirIterator di(base, Core::DirIterator::SkipParentAndBaseDir);
|
Core::DirIterator di(base, Core::DirIterator::SkipParentAndBaseDir);
|
||||||
if (di.has_error())
|
if (di.has_error())
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
while (di.has_next()) {
|
while (di.has_next()) {
|
||||||
DeprecatedString path = di.next_path();
|
auto const entry = di.next().release_value();
|
||||||
|
auto const path = entry.name;
|
||||||
|
if (is_glob_directory && entry.type != Core::DirectoryEntry::Type::Directory)
|
||||||
|
continue;
|
||||||
|
|
||||||
// Dotfiles have to be explicitly requested
|
// Dotfiles have to be explicitly requested
|
||||||
if (path[0] == '.' && first_segment[0] != '.')
|
if (path[0] == '.' && first_segment[0] != '.')
|
||||||
|
@ -343,6 +329,8 @@ Vector<DeprecatedString> Shell::expand_globs(Vector<StringView> path_segments, S
|
||||||
if (!base.ends_with('/'))
|
if (!base.ends_with('/'))
|
||||||
builder.append('/');
|
builder.append('/');
|
||||||
builder.append(path);
|
builder.append(path);
|
||||||
|
if (is_glob_directory)
|
||||||
|
builder.append('/');
|
||||||
result.extend(expand_globs(path_segments, builder.string_view()));
|
result.extend(expand_globs(path_segments, builder.string_view()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -294,7 +294,6 @@ public:
|
||||||
static SpecialCharacterEscapeMode special_character_escape_mode(u32 c, EscapeMode);
|
static SpecialCharacterEscapeMode special_character_escape_mode(u32 c, EscapeMode);
|
||||||
|
|
||||||
static bool is_glob(StringView);
|
static bool is_glob(StringView);
|
||||||
static Vector<StringView> split_path(StringView);
|
|
||||||
|
|
||||||
enum class ExecutableOnly {
|
enum class ExecutableOnly {
|
||||||
Yes,
|
Yes,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue