ladybird/Userland/Utilities/pathchk.cpp

92 lines
3.2 KiB
C++
Raw Normal View History

2021-04-23 23:31:21 +03:00
/*
* Copyright (c) 2021, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/ByteString.h>
2021-04-23 23:31:21 +03:00
#include <LibCore/ArgsParser.h>
2022-02-27 16:56:44 +03:00
#include <LibCore/System.h>
#include <LibMain/Main.h>
2021-04-23 23:31:21 +03:00
#include <bits/posix1_lim.h>
#include <unistd.h>
2022-02-27 16:56:44 +03:00
ErrorOr<int> serenity_main(Main::Arguments arguments)
2021-04-23 23:31:21 +03:00
{
2022-02-27 16:56:44 +03:00
TRY(Core::System::pledge("stdio rpath"));
2021-04-23 23:31:21 +03:00
bool fail = false;
static bool flag_most_posix = false;
static bool flag_portability = false;
static bool flag_empty_name_and_leading_dash = false;
Vector<ByteString> paths;
2021-04-23 23:31:21 +03:00
Core::ArgsParser args_parser;
args_parser.add_option(flag_most_posix, "Check for most POSIX systems", nullptr, 'p');
args_parser.add_option(flag_empty_name_and_leading_dash, "Check for empty names and leading dash", nullptr, 'P');
args_parser.add_option(flag_portability, "Check portability (equivalent to -p and -P)", "portability", '\0');
args_parser.add_positional_argument(paths, "Path to check", "path", Core::ArgsParser::Required::Yes);
2022-02-27 16:56:44 +03:00
args_parser.parse(arguments);
2021-04-23 23:31:21 +03:00
if (flag_portability) {
flag_most_posix = true;
flag_empty_name_and_leading_dash = true;
}
for (auto& path : paths) {
unsigned long path_max = flag_most_posix ? _POSIX_PATH_MAX : pathconf(path.characters(), _PC_PATH_MAX);
unsigned long name_max = flag_most_posix ? _POSIX_NAME_MAX : pathconf(path.characters(), _PC_NAME_MAX);
2021-04-23 23:31:21 +03:00
if (path.length() > path_max) {
warnln("Limit {} exceeded by length {} of filename '{}'", path_max, path.length(), path);
2021-04-23 23:31:21 +03:00
fail = true;
continue;
}
if (flag_most_posix) {
2021-04-29 22:46:15 +03:00
// POSIX portable filename character set (a-z A-Z 0-9 . _ -)
for (long unsigned i = 0; i < path.length(); ++i) {
2021-04-23 23:31:21 +03:00
auto c = path[i];
if (!(c >= 'a' && c <= 'z') && !(c >= 'A' && c <= 'Z') && !(c >= '0' && c <= '9') && c != '/' && c != '.' && c != '-' && c != '_') {
warnln("Non-portable character '{}' in filename '{}'", path[i], path);
2021-04-23 23:31:21 +03:00
fail = true;
continue;
}
}
} else {
struct stat st;
if (lstat(path.characters(), &st) < 0) {
2021-04-23 23:31:21 +03:00
if (errno != ENOENT) {
warnln("Directory is not searchable '{}'", path);
2021-04-23 23:31:21 +03:00
fail = true;
continue;
}
}
}
if (flag_empty_name_and_leading_dash) {
if (path.is_empty()) {
2022-02-27 16:56:44 +03:00
warnln("Empty filename");
2021-04-23 23:31:21 +03:00
fail = true;
continue;
}
}
for (auto& component : path.split('/')) {
2021-04-23 23:31:21 +03:00
if (flag_empty_name_and_leading_dash) {
if (component.starts_with('-')) {
warnln("Leading '-' in a component of filename '{}'", path);
2021-04-23 23:31:21 +03:00
fail = true;
break;
}
}
if (component.length() > name_max) {
2022-02-27 16:56:44 +03:00
warnln("Limit {} exceeded by length {} of filename component '{}'", name_max, component.length(), component.characters());
2021-04-23 23:31:21 +03:00
fail = true;
break;
}
}
}
return fail;
}