2023-07-02 14:38:24 +00:00
|
|
|
#include <luna/PathParser.h>
|
2023-07-08 16:28:04 +00:00
|
|
|
#include <luna/String.h>
|
2023-07-02 14:38:24 +00:00
|
|
|
#include <os/ArgumentParser.h>
|
|
|
|
#include <os/File.h>
|
|
|
|
#include <os/FileSystem.h>
|
2023-07-08 16:28:04 +00:00
|
|
|
#include <os/Prompt.h>
|
2023-07-02 14:38:24 +00:00
|
|
|
|
|
|
|
using os::File;
|
|
|
|
|
2023-07-08 16:28:04 +00:00
|
|
|
Result<void> copy_file(StringView source, StringView destination, bool verbose, bool interactive)
|
2023-07-02 14:38:24 +00:00
|
|
|
{
|
|
|
|
SharedPtr<File> source_file = TRY(os::File::open(source, File::ReadOnly));
|
|
|
|
SharedPtr<File> destination_file;
|
|
|
|
|
|
|
|
struct stat st;
|
|
|
|
TRY(os::FileSystem::stat(source_file->fd(), st, true));
|
|
|
|
umask(0);
|
|
|
|
|
|
|
|
if (os::FileSystem::is_directory(destination, true))
|
|
|
|
{
|
|
|
|
auto basename = TRY(PathParser::basename(source));
|
|
|
|
auto path = TRY(PathParser::join_paths(destination, basename.view()));
|
|
|
|
|
2023-07-08 16:28:04 +00:00
|
|
|
if (interactive && os::FileSystem::exists(path.view(), false))
|
|
|
|
{
|
|
|
|
auto prompt = TRY(String::format("cp: Overwrite %s with %s?"_sv, path.chars(), source.chars()));
|
|
|
|
if (!os::conditional_prompt(prompt.view(), os::DefaultNo)) return {};
|
|
|
|
}
|
|
|
|
|
2023-07-02 14:38:24 +00:00
|
|
|
destination_file = TRY(File::open_or_create(path.view(), File::ReadWrite, st.st_mode & ~S_IFMT));
|
|
|
|
}
|
|
|
|
else
|
2023-07-08 16:28:04 +00:00
|
|
|
{
|
|
|
|
if (interactive && os::FileSystem::exists(destination, false))
|
|
|
|
{
|
|
|
|
auto prompt = TRY(String::format("cp: Overwrite %s with %s?"_sv, destination.chars(), source.chars()));
|
|
|
|
if (!os::conditional_prompt(prompt.view(), os::DefaultNo)) return {};
|
|
|
|
}
|
2023-07-02 14:38:24 +00:00
|
|
|
destination_file = TRY(File::open_or_create(destination, File::ReadWrite, st.st_mode & ~S_IFMT));
|
2023-07-08 16:28:04 +00:00
|
|
|
}
|
2023-07-02 14:38:24 +00:00
|
|
|
|
|
|
|
if (verbose) os::eprintln("copying %s to %s", source.chars(), destination.chars());
|
|
|
|
|
|
|
|
auto buf = TRY(Buffer::create_sized(4096));
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
TRY(source_file->read(buf, 4096));
|
|
|
|
if (buf.is_empty()) break;
|
|
|
|
destination_file->write(buf);
|
|
|
|
}
|
|
|
|
|
2023-07-08 14:44:02 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<int> luna_main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
Vector<StringView> files;
|
|
|
|
StringView destination;
|
|
|
|
bool verbose { false };
|
2023-07-08 16:28:04 +00:00
|
|
|
bool interactive { false };
|
2023-07-08 14:44:02 +00:00
|
|
|
|
|
|
|
os::ArgumentParser parser;
|
|
|
|
parser.add_description("Copy files or directories."_sv);
|
|
|
|
parser.add_system_program_info("cp"_sv);
|
|
|
|
parser.set_vector_argument(files, "files"_sv, true);
|
|
|
|
parser.add_positional_argument(destination, "destination"_sv, true);
|
|
|
|
parser.add_switch_argument(verbose, 'v', "verbose"_sv, "show more information"_sv);
|
2023-07-08 16:28:04 +00:00
|
|
|
parser.add_switch_argument(interactive, 'i', "interactive"_sv, "prompt before overwriting existing files"_sv);
|
|
|
|
TRY(parser.parse(argc, argv));
|
2023-07-08 14:44:02 +00:00
|
|
|
|
2023-07-08 16:28:04 +00:00
|
|
|
for (const auto& file : files) { TRY(copy_file(file, destination, verbose, interactive)); }
|
2023-07-08 14:44:02 +00:00
|
|
|
|
2023-07-02 14:38:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|