2018-10-28 07:54:20 +00:00
|
|
|
#include "FileSystemPath.h"
|
|
|
|
#include "Vector.h"
|
|
|
|
#include "kstdio.h"
|
|
|
|
#include "StringBuilder.h"
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
FileSystemPath::FileSystemPath(const String& s)
|
|
|
|
: m_string(s)
|
|
|
|
{
|
2018-12-03 00:38:22 +00:00
|
|
|
m_is_valid = canonicalize();
|
2018-10-28 07:54:20 +00:00
|
|
|
}
|
|
|
|
|
2018-12-03 00:38:22 +00:00
|
|
|
bool FileSystemPath::canonicalize(bool resolve_symbolic_links)
|
2018-10-28 07:54:20 +00:00
|
|
|
{
|
2018-12-21 16:45:42 +00:00
|
|
|
// FIXME: Implement "resolve_symbolic_links"
|
2018-12-03 00:38:22 +00:00
|
|
|
(void) resolve_symbolic_links;
|
2018-10-28 07:54:20 +00:00
|
|
|
auto parts = m_string.split('/');
|
2018-12-03 00:38:22 +00:00
|
|
|
Vector<String> canonical_parts;
|
2018-10-28 07:54:20 +00:00
|
|
|
|
|
|
|
for (auto& part : parts) {
|
|
|
|
if (part == ".")
|
|
|
|
continue;
|
|
|
|
if (part == "..") {
|
2018-12-21 01:10:45 +00:00
|
|
|
if (!canonical_parts.is_empty())
|
2018-12-03 00:38:22 +00:00
|
|
|
canonical_parts.takeLast();
|
2018-10-28 07:54:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
2018-12-21 01:10:45 +00:00
|
|
|
if (!part.is_empty())
|
2018-12-03 00:38:22 +00:00
|
|
|
canonical_parts.append(part);
|
2018-10-28 07:54:20 +00:00
|
|
|
}
|
2018-12-21 01:10:45 +00:00
|
|
|
if (canonical_parts.is_empty()) {
|
2018-11-18 22:28:43 +00:00
|
|
|
m_string = m_basename = "/";
|
2018-10-28 07:54:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-11-18 13:57:41 +00:00
|
|
|
|
2018-12-03 00:38:22 +00:00
|
|
|
m_basename = canonical_parts.last();
|
2018-11-18 22:28:43 +00:00
|
|
|
StringBuilder builder;
|
2018-12-03 00:38:22 +00:00
|
|
|
for (auto& cpart : canonical_parts) {
|
2018-11-18 22:28:43 +00:00
|
|
|
builder.append('/');
|
|
|
|
builder.append(move(cpart));
|
2018-10-28 07:54:20 +00:00
|
|
|
}
|
2018-11-18 22:28:43 +00:00
|
|
|
m_string = builder.build();
|
2018-10-28 07:54:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|