Eyck has asked for the wisdom of the Perl Monks concerning the following question:

I've got a little test that checks remote filesize against local. It goes like this:
my $lsize=-s $filename; my $rsize=$remote->size($filename); if ($lsize!=$rsize) { croak "Corruption!"; };

This works fine, until I run this on windows... then it seems like '-s' reports wrong filesize (maybe \r\n is counted as one character?)

Replies are listed 'Best First'.
Re: Portable filesize check? (Win32)
by Corion (Patriarch) on Mar 10, 2005 at 08:40 UTC

    I assume that you are remotely automating an ftp transfer. And my guess is that you did not enable binary mode for the transfer, and/or did not use binmode on the local filehandle when writing to the file. But unless you show code, we won't be able to tell.

    Did you do a sanity check and compare what size Perl reports via -s against what the operating system reports (via dir, or via the property sheet in Explorer) ?

      Yeah, that would be easy, but I've got access to the remote system, so I checked the file, it's identical it looks identical to the original.

      You were right about 'binmode', thanks.

Re: Portable filesize check? (Win32)
by eyepopslikeamosquito (Archbishop) on Mar 10, 2005 at 09:23 UTC

    then it seems like '-s' reports wrong filesize (maybe \r\n is counted as one character?)
    No. \r\n is not counted as one character. In my experience, the value returned by '-s' always agrees with that reported by the DIR command. Do a little test of that in isolation to convince yourself.

      As I said, the size reported by '-s' agrees with the one reported by dir.

      But 'dir' is not omnipotent, and there are things like sparse, encrypted and compressed files, where there's no obvious size to report. Check ls command manual, it's got myriad of switches for a reason.

      Problem was too much magic in perl file handling (or rather my lack of knowledge on that subject), and binmode fixed that.

        there are things like sparse, encrypted and compressed files, where there's no obvious size to report
        An operating system should not know anything about the contents of a file. What it should know is how many bytes on a disk that file occupies. So, the sparsity, encryptedness, or compressedness should not hinder the OS from knowing how many bytes that files takes up on disk.

        thor

        Feel the white light, the light within
        Be your own disciple, fan the sparks of will
        For all of us waiting, your kingdom will come