mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 06:54: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;
|
||||
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)) {
|
||||
warnln("{}: {}: {}", argv[0], path, file->error_string());
|
||||
fail = true;
|
||||
continue;
|
||||
}
|
||||
auto file_buffer = file->read_all();
|
||||
auto bytes = file_buffer.bytes().size();
|
||||
struct stat st;
|
||||
if (fstat(file->fd(), &st) < 0) {
|
||||
warnln("{}: Failed to fstat {}: {}", argv[0], filepath, strerror(errno));
|
||||
fail = true;
|
||||
continue;
|
||||
}
|
||||
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") {
|
||||
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 {
|
||||
warnln("{}: Unknown checksum algorithm: {}", argv[0], algorithm);
|
||||
exit(1);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue