2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2023-05-24 01:27:21 +00:00
|
|
|
* Copyright (c) 2023, the SerenityOS developers.
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2021-05-31 14:43:25 +00:00
|
|
|
#include <AK/Format.h>
|
2023-05-24 01:25:04 +00:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2023-05-24 01:27:21 +00:00
|
|
|
#include <LibCore/FilePermissionsMask.h>
|
2022-01-14 16:23:48 +00:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2019-05-03 20:59:58 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/stat.h>
|
2021-11-26 15:55:45 +00:00
|
|
|
#include <sys/sysmacros.h>
|
2019-05-03 20:59:58 +00:00
|
|
|
|
2022-01-14 16:23:48 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-05-03 20:59:58 +00:00
|
|
|
{
|
2022-01-14 16:23:48 +00:00
|
|
|
TRY(Core::System::pledge("stdio dpath"));
|
2020-01-27 20:38:36 +00:00
|
|
|
|
2023-05-24 01:27:21 +00:00
|
|
|
StringView mode_string { "0666"sv };
|
2023-05-24 01:25:04 +00:00
|
|
|
StringView name;
|
|
|
|
StringView type_string;
|
|
|
|
StringView major_string;
|
|
|
|
StringView minor_string;
|
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
args_parser.set_general_help("Creates a file system node.");
|
2023-05-24 01:27:21 +00:00
|
|
|
args_parser.add_option(mode_string, "File mode in octal or symbolic notation", "mode", 'm', "mode");
|
2023-05-24 01:25:04 +00:00
|
|
|
args_parser.add_positional_argument(name, "Pathname to create", "name", Core::ArgsParser::Required::Yes);
|
2023-05-24 01:27:21 +00:00
|
|
|
args_parser.add_positional_argument(
|
|
|
|
type_string,
|
|
|
|
"Type of file to create <b|c|u|p>"
|
|
|
|
"\n\t\tb\tcreate a block special file"
|
|
|
|
"\n\t\tc, u\tcreate a character special file"
|
|
|
|
"\n\t\tp\tcreate a FIFO",
|
|
|
|
"type",
|
|
|
|
Core::ArgsParser::Required::Yes);
|
2023-05-24 01:25:04 +00:00
|
|
|
args_parser.add_positional_argument(major_string, "Major device number", "major", Core::ArgsParser::Required::No);
|
|
|
|
args_parser.add_positional_argument(minor_string, "Minor device number", "minor", Core::ArgsParser::Required::No);
|
|
|
|
args_parser.parse(arguments);
|
|
|
|
|
2023-05-24 01:27:21 +00:00
|
|
|
auto mask = TRY(Core::FilePermissionsMask::parse(mode_string));
|
2023-05-24 01:25:04 +00:00
|
|
|
char type = type_string[0];
|
|
|
|
|
2023-05-24 01:27:21 +00:00
|
|
|
mode_t mode;
|
2023-05-24 01:25:04 +00:00
|
|
|
switch (type) {
|
2019-05-03 20:59:58 +00:00
|
|
|
case 'c':
|
|
|
|
case 'u':
|
2023-05-24 01:27:21 +00:00
|
|
|
mode = mask.apply(S_IFCHR);
|
2019-05-03 20:59:58 +00:00
|
|
|
break;
|
|
|
|
case 'b':
|
2023-05-24 01:27:21 +00:00
|
|
|
mode = mask.apply(S_IFBLK);
|
2019-05-03 20:59:58 +00:00
|
|
|
break;
|
|
|
|
case 'p':
|
2023-05-24 01:27:21 +00:00
|
|
|
mode = mask.apply(S_IFIFO);
|
2019-05-03 20:59:58 +00:00
|
|
|
break;
|
|
|
|
default:
|
2023-05-24 01:25:04 +00:00
|
|
|
warnln("Invalid device type {}", type);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-05-24 01:27:21 +00:00
|
|
|
auto maybe_major = major_string.to_int();
|
|
|
|
auto maybe_minor = minor_string.to_int();
|
|
|
|
dev_t device;
|
2023-05-24 01:25:04 +00:00
|
|
|
if (type == 'p') {
|
2023-05-24 01:27:21 +00:00
|
|
|
if (maybe_major.has_value() || maybe_minor.has_value()) {
|
2023-05-24 01:25:04 +00:00
|
|
|
warnln("Do not set device numbers when creating FIFO");
|
|
|
|
return 1;
|
|
|
|
}
|
2023-05-24 01:27:21 +00:00
|
|
|
device = makedev(0, 0);
|
2023-05-24 01:25:04 +00:00
|
|
|
} else {
|
2023-05-24 01:27:21 +00:00
|
|
|
if (!(maybe_major.has_value() && maybe_minor.has_value())) {
|
2023-05-24 01:25:04 +00:00
|
|
|
warnln("Major and minor device numbers are required");
|
|
|
|
return 1;
|
|
|
|
}
|
2023-05-24 01:27:21 +00:00
|
|
|
device = makedev(maybe_major.value(), maybe_minor.value());
|
2019-05-03 20:59:58 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 01:27:21 +00:00
|
|
|
TRY(Core::System::mknod(name, mode, device));
|
2022-01-14 16:23:48 +00:00
|
|
|
|
2019-05-03 20:59:58 +00:00
|
|
|
return 0;
|
|
|
|
}
|