mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 09:44:58 +00:00
cksum: Don't read the entire input file in memory
Same as `checksum`, in some cases we just can't load whole file into memory.
This commit is contained in:
parent
dc80a258f6
commit
c5df5e504c
1 changed files with 26 additions and 5 deletions
|
@ -41,18 +41,39 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
bool fail = false;
|
bool fail = false;
|
||||||
for (auto& path : paths) {
|
for (auto& path : paths) {
|
||||||
auto file = Core::File::construct((StringView(path) == "-") ? "/dev/stdin" : path);
|
auto filepath = (StringView(path) == "-") ? "/dev/stdin" : path;
|
||||||
|
auto file = Core::File::construct(filepath);
|
||||||
if (!file->open(Core::OpenMode::ReadOnly)) {
|
if (!file->open(Core::OpenMode::ReadOnly)) {
|
||||||
warnln("{}: {}: {}", argv[0], path, file->error_string());
|
warnln("{}: {}: {}", argv[0], path, file->error_string());
|
||||||
fail = true;
|
fail = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
auto file_buffer = file->read_all();
|
struct stat st;
|
||||||
auto bytes = file_buffer.bytes().size();
|
if (fstat(file->fd(), &st) < 0) {
|
||||||
|
warnln("{}: Failed to fstat {}: {}", argv[0], filepath, strerror(errno));
|
||||||
|
fail = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if (algorithm == "crc32") {
|
if (algorithm == "crc32") {
|
||||||
outln("{} {} {}", Crypto::Checksum::CRC32 { file_buffer.bytes() }.digest(), bytes, path);
|
Crypto::Checksum::CRC32 crc32;
|
||||||
|
while (!file->eof() && !file->has_error())
|
||||||
|
crc32.update(file->read(PAGE_SIZE));
|
||||||
|
if (file->has_error()) {
|
||||||
|
warnln("Failed to read {}: {}", filepath, file->error_string());
|
||||||
|
fail = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
outln("{} {} {}", crc32.digest(), st.st_size, path);
|
||||||
} else if (algorithm == "adler32") {
|
} else if (algorithm == "adler32") {
|
||||||
outln("{} {} {}", Crypto::Checksum::Adler32 { file_buffer.bytes() }.digest(), bytes, path);
|
Crypto::Checksum::Adler32 adler32;
|
||||||
|
while (!file->eof() && !file->has_error())
|
||||||
|
adler32.update(file->read(PAGE_SIZE));
|
||||||
|
if (file->has_error()) {
|
||||||
|
warnln("Failed to read {}: {}", filepath, file->error_string());
|
||||||
|
fail = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
outln("{} {} {}", adler32.digest(), st.st_size, path);
|
||||||
} else {
|
} else {
|
||||||
warnln("{}: Unknown checksum algorithm: {}", argv[0], algorithm);
|
warnln("{}: Unknown checksum algorithm: {}", argv[0], algorithm);
|
||||||
exit(1);
|
exit(1);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue