2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2020-02-06 14:04:03 +00:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2022-10-17 19:24:52 +00:00
|
|
|
#include <LibCore/EventLoop.h>
|
|
|
|
#include <LibCore/FileWatcher.h>
|
|
|
|
#include <LibCore/Stream.h>
|
2022-01-23 17:08:44 +00:00
|
|
|
#include <LibCore/System.h>
|
2019-05-16 13:02:17 +00:00
|
|
|
|
2019-06-06 18:28:58 +00:00
|
|
|
#define DEFAULT_LINE_COUNT 10
|
|
|
|
|
2022-10-17 19:24:52 +00:00
|
|
|
static ErrorOr<void> tail_from_pos(Core::Stream::File& file, off_t startline)
|
2019-05-16 13:02:17 +00:00
|
|
|
{
|
2023-01-22 04:09:11 +00:00
|
|
|
TRY(file.seek(startline + 1, SeekMode::SetPosition));
|
2022-12-11 16:49:00 +00:00
|
|
|
auto buffer = TRY(file.read_until_eof());
|
2022-10-17 19:24:52 +00:00
|
|
|
out("{}", StringView { buffer });
|
|
|
|
return {};
|
2019-05-16 13:02:17 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 19:24:52 +00:00
|
|
|
static ErrorOr<off_t> find_seek_pos(Core::Stream::File& file, int wanted_lines)
|
2019-05-16 13:02:17 +00:00
|
|
|
{
|
|
|
|
// Rather than reading the whole file, start at the end and work backwards,
|
|
|
|
// stopping when we've found the number of lines we want.
|
2023-01-22 04:09:11 +00:00
|
|
|
off_t pos = TRY(file.seek(0, SeekMode::FromEndPosition));
|
2019-05-16 13:02:17 +00:00
|
|
|
|
|
|
|
off_t end = pos;
|
|
|
|
int lines = 0;
|
|
|
|
|
2019-06-07 09:49:31 +00:00
|
|
|
for (; pos >= 0; pos--) {
|
2023-01-22 04:09:11 +00:00
|
|
|
TRY(file.seek(pos, SeekMode::SetPosition));
|
2022-10-17 19:24:52 +00:00
|
|
|
|
|
|
|
if (file.is_eof())
|
|
|
|
break;
|
|
|
|
Array<u8, 1> buffer;
|
|
|
|
auto ch = TRY(file.read(buffer));
|
|
|
|
if (*ch.data() == '\n' && (end - pos) > 1) {
|
|
|
|
lines++;
|
|
|
|
if (lines == wanted_lines)
|
|
|
|
break;
|
2019-05-16 13:02:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2022-01-23 17:08:44 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-05-16 13:02:17 +00:00
|
|
|
{
|
2022-01-23 17:08:44 +00:00
|
|
|
TRY(Core::System::pledge("stdio rpath"));
|
2020-01-13 00:11:47 +00:00
|
|
|
|
2020-01-27 17:25:36 +00:00
|
|
|
bool follow = false;
|
2022-10-17 19:24:52 +00:00
|
|
|
size_t wanted_line_count = DEFAULT_LINE_COUNT;
|
|
|
|
StringView file;
|
2019-05-17 10:59:37 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
Core::ArgsParser args_parser;
|
2020-12-05 15:22:58 +00:00
|
|
|
args_parser.set_general_help("Print the end ('tail') of a file.");
|
2020-01-27 17:25:36 +00:00
|
|
|
args_parser.add_option(follow, "Output data as it is written to the file", "follow", 'f');
|
2022-10-17 19:24:52 +00:00
|
|
|
args_parser.add_option(wanted_line_count, "Fetch the specified number of lines", "lines", 'n', "number");
|
|
|
|
args_parser.add_positional_argument(file, "File path", "file", Core::ArgsParser::Required::No);
|
2022-01-23 17:08:44 +00:00
|
|
|
args_parser.parse(arguments);
|
2019-05-16 13:02:17 +00:00
|
|
|
|
2022-10-17 19:24:52 +00:00
|
|
|
auto f = TRY(Core::Stream::File::open_file_or_standard_stream(file, Core::Stream::OpenMode::Read));
|
|
|
|
if (!follow)
|
|
|
|
TRY(Core::System::pledge("stdio"));
|
2020-01-13 00:11:47 +00:00
|
|
|
|
2022-10-17 19:24:52 +00:00
|
|
|
auto file_is_seekable = !f->tell().is_error();
|
|
|
|
if (!file_is_seekable) {
|
|
|
|
do {
|
|
|
|
// FIXME: If f is the standard input, f->read_all() does not block
|
|
|
|
// anymore after sending EOF (^D), despite f->is_open() returning true.
|
2022-12-11 16:49:00 +00:00
|
|
|
auto buffer = TRY(f->read_until_eof(PAGE_SIZE));
|
2022-10-17 19:24:52 +00:00
|
|
|
auto line_count = StringView(buffer).count("\n"sv);
|
|
|
|
auto bytes = buffer.bytes();
|
|
|
|
size_t line_index = 0;
|
|
|
|
StringBuilder line;
|
|
|
|
|
|
|
|
if (!line_count && wanted_line_count) {
|
|
|
|
out("{}", StringView { bytes });
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < bytes.size(); i++) {
|
|
|
|
auto ch = bytes.at(i);
|
|
|
|
line.append(ch);
|
|
|
|
if (ch == '\n') {
|
|
|
|
if (wanted_line_count > line_count || line_index >= line_count - wanted_line_count)
|
2023-01-26 18:58:09 +00:00
|
|
|
out("{}", line.to_deprecated_string());
|
2022-10-17 19:24:52 +00:00
|
|
|
line_index++;
|
|
|
|
line.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since we can't have FileWatchers on the standard input either,
|
|
|
|
// we just loop forever if the -f option was passed.
|
|
|
|
} while (follow);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto pos = TRY(find_seek_pos(*f, wanted_line_count));
|
|
|
|
TRY(tail_from_pos(*f, pos));
|
|
|
|
|
|
|
|
if (follow) {
|
2023-01-22 04:09:11 +00:00
|
|
|
TRY(f->seek(0, SeekMode::FromEndPosition));
|
2022-10-17 19:24:52 +00:00
|
|
|
|
|
|
|
Core::EventLoop event_loop;
|
|
|
|
auto watcher = TRY(Core::FileWatcher::create());
|
|
|
|
watcher->on_change = [&](Core::FileWatcherEvent const& event) {
|
|
|
|
if (event.type == Core::FileWatcherEvent::Type::ContentModified) {
|
2022-12-11 16:49:00 +00:00
|
|
|
auto buffer_or_error = f->read_until_eof();
|
2022-10-17 19:24:52 +00:00
|
|
|
if (buffer_or_error.is_error()) {
|
2023-02-09 18:26:53 +00:00
|
|
|
auto error = buffer_or_error.release_error();
|
2022-10-17 19:24:52 +00:00
|
|
|
warnln(error.string_literal());
|
|
|
|
event_loop.quit(error.code());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto bytes = buffer_or_error.value().bytes();
|
|
|
|
out("{}", StringView { bytes });
|
|
|
|
|
2023-01-22 04:09:11 +00:00
|
|
|
auto potential_error = f->seek(0, SeekMode::FromEndPosition);
|
2022-10-17 19:24:52 +00:00
|
|
|
if (potential_error.is_error()) {
|
2023-02-09 18:26:53 +00:00
|
|
|
auto error = potential_error.release_error();
|
2022-10-17 19:24:52 +00:00
|
|
|
warnln(error.string_literal());
|
|
|
|
event_loop.quit(error.code());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
TRY(watcher->add_watch(file, Core::FileWatcherEvent::Type::ContentModified));
|
|
|
|
TRY(Core::System::pledge("stdio"));
|
|
|
|
return event_loop.exec();
|
|
|
|
}
|
|
|
|
return 0;
|
2019-05-16 13:02:17 +00:00
|
|
|
}
|