I'm still getting a 0 for directory size

Does your script run on a Windows Perl (Strawberry, ActiveState or the like)?

Windows does not have a stat() system call. Perl emulates it, but not perfectly. I assume - without looking at the perl sources - that the struct stat is simply zeroed out and then filled depending on what the Windows API functions return. For directories, size is simply not touched and stays 0.

Note that FAT and NTFS are very different from filesystems found on Unix systems, so having a "size" field for a directory may not make any sense at all.

Also note that the "size" field is not at all related to the size of the files in the directory. On Unix systems, there is -- depending on the filesystem type -- a relation to the highest number of files in the directory over the lifetime of the directory, and maybe the length of the filenames. If you want the sum of the file sizes, calculate it by calling stat for each file in the directory.

POSIX leaves the meaning of the st_size field undefined for directories. It is defined for regular files, symlinks, shared memory objects, and typed memory objects, but not for directories. ("For other file types, the use of this field is unspecified.") So, depending on your operating system, stat() may legally return 0 or just garbage for the size of a directory.

My result for stat on a directory on Win7 NTFS and FAT, Strawberry Perl 5.14.2 is 0, too.

On Linux 3.10.17, perl 5.18.1, ext3, ext2, procfs, sysfs, tmpfs, the results vary from 0 (procfs, sysfs) to 12288 (heavily used directory on ext3), matching the results from ls. The directory size of 0 on procfs is documented in stat(2), sysfs behaves in the same way.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

In reply to Re^3: File::Find stat question by afoken
in thread File::Find stat question by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.