Laurent_R has asked for the wisdom of the Perl Monks concerning the following question:
(You don't need to tell me that this is a very old version of Perl, I know, I agree, and there isn't much I can do about it.)perl -v This is perl, v5.8.6 built for VMS_IA64 (with 2 registered patches, see perl -V for more detail) Copyright 1987-2004, Larry Wall (...)
I have a small program trying to list the files on a disk partitioned by age and by size, with some typical code like this:
My problem is that for a dozen or so very large files, the $size variable takes a negative value. For example, in one specific case, I got the following value: -426648576. Note that the stat function returns the same negative value in field # 7 (size in bytes) and an empty string for field # 12 (size in blocks).for my $entry (@files) { # @files contains the files I want to lo +ok at next if -z $entry; my $size = -s $entry; # more code
Obviously, the stat function and the -s file test operator are suffering from integer overflow.
Also note that POSIX's stat does not help and gives the same result.
I have found a work around: using a VMS system command within backticks to get the size blocks from VMS and then proceed with the necessary extraction and conversion. The specific file example above gives me a size of 7,555,310 blocks, which corresponds to about 3.8 GB (a VMS block is equal to 512 bytes). So, I am OK, my program does what I want with this work around.
I wonder, however, if there isn't a better way to get the size in pure Perl (i.e. without running a VMS system command within backticks), without having this integer overflow problem. Any idea?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Integer overflow in -s or file stat results
by pryrt (Abbot) on Mar 07, 2017 at 15:41 UTC | |
by Laurent_R (Canon) on Mar 07, 2017 at 18:02 UTC | |
|
Re: Integer overflow in -s or file stat results
by BrowserUk (Patriarch) on Mar 07, 2017 at 17:00 UTC | |
by Laurent_R (Canon) on Mar 07, 2017 at 18:30 UTC | |
|
Re: Integer overflow in -s or file stat results
by Anonymous Monk on Mar 07, 2017 at 14:57 UTC | |
by Anonymous Monk on Mar 07, 2017 at 15:28 UTC | |
by Laurent_R (Canon) on Mar 07, 2017 at 18:10 UTC |