mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 12:47:35 +00:00
cksum: Port to Core::Stream
This commit is contained in:
parent
d071a93047
commit
83366e2b2e
1 changed files with 30 additions and 19 deletions
|
@ -5,7 +5,8 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <LibCore/ArgsParser.h>
|
#include <LibCore/ArgsParser.h>
|
||||||
#include <LibCore/File.h>
|
#include <LibCore/Stream.h>
|
||||||
|
#include <LibCore/System.h>
|
||||||
#include <LibCrypto/Checksum/Adler32.h>
|
#include <LibCrypto/Checksum/Adler32.h>
|
||||||
#include <LibCrypto/Checksum/CRC32.h>
|
#include <LibCrypto/Checksum/CRC32.h>
|
||||||
#include <LibMain/Main.h>
|
#include <LibMain/Main.h>
|
||||||
|
@ -42,38 +43,48 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
paths.append("-");
|
paths.append("-");
|
||||||
|
|
||||||
bool fail = false;
|
bool fail = false;
|
||||||
|
Array<u8, PAGE_SIZE> buffer;
|
||||||
|
|
||||||
for (auto& path : paths) {
|
for (auto& path : paths) {
|
||||||
|
auto file_or_error = Core::Stream::File::open_file_or_standard_stream(path, Core::Stream::OpenMode::Read);
|
||||||
auto filepath = (path == "-") ? "/dev/stdin" : path;
|
auto filepath = (path == "-") ? "/dev/stdin" : path;
|
||||||
auto file = Core::File::construct(filepath);
|
if (file_or_error.is_error()) {
|
||||||
if (!file->open(Core::OpenMode::ReadOnly)) {
|
warnln("{}: {}: {}", arguments.strings[0], filepath, file_or_error.error());
|
||||||
warnln("{}: {}: {}", arguments.strings[0], path, file->error_string());
|
|
||||||
fail = true;
|
fail = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
struct stat st;
|
auto file = file_or_error.release_value();
|
||||||
if (fstat(file->fd(), &st) < 0) {
|
|
||||||
warnln("{}: Failed to fstat {}: {}", arguments.strings[0], filepath, strerror(errno));
|
auto stat_or_error = Core::System::stat(filepath);
|
||||||
|
if (stat_or_error.is_error()) {
|
||||||
|
warnln("{}: Failed to fstat {}: {}", arguments.strings[0], filepath, stat_or_error.error());
|
||||||
fail = true;
|
fail = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
auto st = stat_or_error.release_value();
|
||||||
|
|
||||||
if (algorithm == "crc32") {
|
if (algorithm == "crc32") {
|
||||||
Crypto::Checksum::CRC32 crc32;
|
Crypto::Checksum::CRC32 crc32;
|
||||||
while (!file->eof() && !file->has_error())
|
while (!file->is_eof()) {
|
||||||
crc32.update(file->read(PAGE_SIZE));
|
auto data_or_error = file->read(buffer);
|
||||||
if (file->has_error()) {
|
if (data_or_error.is_error()) {
|
||||||
warnln("{}: Failed to read {}: {}", arguments.strings[0], filepath, file->error_string());
|
warnln("{}: Failed to read {}: {}", arguments.strings[0], filepath, data_or_error.error());
|
||||||
fail = true;
|
fail = true;
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
crc32.update(data_or_error.value());
|
||||||
}
|
}
|
||||||
outln("{:08x} {} {}", crc32.digest(), st.st_size, path);
|
outln("{:08x} {} {}", crc32.digest(), st.st_size, path);
|
||||||
} else if (algorithm == "adler32") {
|
} else if (algorithm == "adler32") {
|
||||||
Crypto::Checksum::Adler32 adler32;
|
Crypto::Checksum::Adler32 adler32;
|
||||||
while (!file->eof() && !file->has_error())
|
while (!file->is_eof()) {
|
||||||
adler32.update(file->read(PAGE_SIZE));
|
auto data_or_error = file->read(buffer);
|
||||||
if (file->has_error()) {
|
if (data_or_error.is_error()) {
|
||||||
warnln("{}: Failed to read {}: {}", arguments.strings[0], filepath, file->error_string());
|
warnln("{}: Failed to read {}: {}", arguments.strings[0], filepath, data_or_error.error());
|
||||||
fail = true;
|
fail = true;
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
adler32.update(data_or_error.value());
|
||||||
}
|
}
|
||||||
outln("{:08x} {} {}", adler32.digest(), st.st_size, path);
|
outln("{:08x} {} {}", adler32.digest(), st.st_size, path);
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue