1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
/*
* Copyright (c) 2021, Max Wipfli <max.wipfli@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <Kernel/KLexicalPath.h>
namespace Kernel::KLexicalPath {
static StringView const s_single_dot = "."sv;
bool is_absolute(StringView const& path)
{
return !path.is_empty() && path[0] == '/';
}
bool is_canonical(StringView const& path)
{
// FIXME: This can probably be done more efficiently.
if (path.is_empty())
return false;
if (path.ends_with('/') && path.length() != 1)
return false;
if (path.starts_with("./"sv) || path.contains("/./"sv) || path.ends_with("/."sv))
return false;
if (path.starts_with("../"sv) || path.contains("/../"sv) || path.ends_with("/.."))
return false;
if (path.contains("//"sv))
return false;
return true;
}
StringView basename(StringView const& a_path)
{
if (a_path == "/"sv)
return a_path;
if (a_path.is_empty())
return s_single_dot;
auto path = a_path.trim("/"sv, TrimMode::Right);
// NOTE: If it's empty now, it means the path was just a series of slashes.
if (path.is_empty())
return a_path.substring_view(0, 1);
auto slash_index = path.find_last('/');
if (!slash_index.has_value())
return path;
auto basename = path.substring_view(*slash_index + 1);
return basename;
}
StringView dirname(StringView const& path)
{
VERIFY(is_canonical(path));
auto slash_index = path.find_last('/');
VERIFY(slash_index.has_value());
return path.substring_view(0, *slash_index);
}
Vector<StringView> parts(StringView const& path)
{
VERIFY(is_canonical(path));
return path.split_view('/');
}
KResultOr<NonnullOwnPtr<KString>> try_join(StringView const& first, StringView const& second)
{
VERIFY(is_canonical(first));
VERIFY(is_canonical(second));
VERIFY(!is_absolute(second));
if (first == "/"sv) {
char* buffer;
auto string = TRY(KString::try_create_uninitialized(1 + second.length(), buffer));
buffer[0] = '/';
__builtin_memcpy(buffer + 1, second.characters_without_null_termination(), second.length());
buffer[string->length()] = 0;
return string;
}
char* buffer;
auto string = TRY(KString::try_create_uninitialized(first.length() + 1 + second.length(), buffer));
__builtin_memcpy(buffer, first.characters_without_null_termination(), first.length());
buffer[first.length()] = '/';
__builtin_memcpy(buffer + first.length() + 1, second.characters_without_null_termination(), second.length());
buffer[string->length()] = 0;
return string;
}
}
|