ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#!/usr/bin/perl
strict;
use Digest::MD5 qw(md5_hex);
use Digest::SHA1 qw(sha1_hex);
my $content = "";
open(my $fh, '<:raw', "/tmp/a.tgz" ) or die "cannot open file $filename";
{
local $/;
$content = <$fh>;
}
close($fh);
print length($content) . "\n";
# Check that sha1 and md5 are correct
my $md5Value=md5_hex($content);
print "md5sum is ok\n" if $debug;
my $sha1Value=sha1_hex($content);
print "sha1sum is ok\n" if $debug;
this code segfaults if /tmp/a.tgz is big enough (3 GB).
Do you know how can I handle it?
this was occurred on several different hosts with huge amount of ram (like 256 GB).
This is perl 5, version 16, subversion 3 (v5.16.3) built for x86_64-linux-thread-multi
#!/usr/bin/perl
strict;
use Digest::MD5 qw(md5_hex);
use Digest::SHA1 qw(sha1_hex);
my $content = "";
open(my $fh, '<:raw', "/tmp/a.tgz" ) or die "cannot open file $filename";
{
local $/;
$content = <$fh>;
}
close($fh);
print length($content) . "\n";
# Check that sha1 and md5 are correct
my $md5Value=md5_hex($content);
print "md5sum is ok\n" if $debug;
my $sha1Value=sha1_hex($content);
print "sha1sum is ok\n" if $debug;
this code segfaults if /tmp/a.tgz is big enough (3 GB). Do you know how can I handle it?
That would be an expensive (memory wise) thing to do, reading the entire huge file handle at once. I'd personally read it in chunks.
Code:
my $MD5SUM = do {
open(my $fh, '<:raw', "/tmp/a.tgz" ) or die "cannot open file $filename";
my $MDChunk = Digest::MD5->new;
local $/ = \131072; # Define a 128k chunk
while (<$fh>) {
$MDChunk->add($_);
}
$MDChunk->hexdigest;
};
<...rest of code....>
..would process it one 128k chunk at a time. Totally untested. Just a thought.
yes, you are almost right. There are workarounds, now I'm forced to "find another way".
I need to download a file and check the md5sum/sha1sum and the perl script can do that (not only this, but even more - like find out the name and location of the file and other special things). It works perfectly, the only problem is when the file itself is too big.
The current workaround is to save the file and run checksum calculation on file.
But this thread is about the segfault of *_hex calls which looks like a bug.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.