Querying Filesystem Usage
Given the methods of controlling filesystem usage we've just explored, it is only natural to want to keep track of how well they work. To end this chapter, let's look at a method for querying the filesystem usage on each of the operating systems found in this tutorial.
MacOS is the operating system for which this task is hardest. MacOS does have a Macintosh Toolbox routine (PBHGetVInfo
) to retrieve volume information, but at the current time there is no MacPerl module available to make calling this function easy. Instead, we have to take a roundabout approach and ask the Finder
to query this information for us. This is easy in practice thanks to a glue module, but the setup needed for this method makes MacOS the more difficult operating system to deal with.
All the materials for the following involve work by Chris Nandor and can be found at http://pudge.net or on CPAN. Bear with me as we go over this setup step by step:
- Install the
cpan-mac
bundle.cpan-mac
includes theCPAN.pm
module by Andreas J. König and other handy modules we mentioned in "Introduction". Even if you don't want to query filesystem usage from MacOS, you'll still be well served by installing this bundle. When you install this bundle, be sure to follow all of the directions found in the README file. - Install the latest
Mac::AppleEvents::Simple
module by dropping the distribution file on top of theinstallme
droplet. - Install the
Mac::Glue
module. Theinstallme
droplet decompresses and unpacks the contents of theMac::Glue
distribution file into a new folder as part of the installation process. Be sure to run thegluedialect
andgluescriptadds
setup scripts from the scripts subfolder of the unpacked distribution. - Create the finder glue file. Open your System Folder and drag the Finder file on top of the
gluemac
droplet to create the necessary glue file (and, in a particularly nice touch by Nandor, to create pod documentation for the glue).
This complex setup process allows us to write the following simple-looking code:
use Mac::Glue qw(:all); $fobj = new Mac::Glue 'Finder'; $volumename = "Macintosh HD"; # the name of one of our mounted disks $total = $fobj->get($fobj->prop('capacity', disk => $volumename), as => 'doub'); $free = $fobj->get($fobj->prop('free_space', disk => $volumename), as => 'doub'); print "$free bytes of $total bytes free\n";
Let's move to easier territory. If we wanted to query the same information from a Win32 machine, we could use Dave Roth's Win32::AdminMisc
module:
use Win32::AdminMisc; ($total,$free) = Win32::AdminMisc::GetDriveSpace("c:\\"); print "$free bytes of $total bytes free\n";
Finally, let's end this chapter by looking at the Unix equivalent. There are several Unix modules available, including Filesys::DiskSpace
by Fabien Tassin, Filesys::Df
by Ian Guthrie, and Filesys::DiskFree
by Alan R. Barclay. The first two of these make use of the Unix system call statvfs( )
while the last one actually parses the output of the Unix command df
on all of the systems it supports. Choosing between these modules is mostly a matter of personal preference and operating system support. I prefer Filesys::Df
because it offers a rich feature set and does not spawn another process (a potential security risk, as discussed in "Introduction") as part of a query. Here's one way to write code equivalent to the previous two examples:
use Filesys::Df; $fobj = df("/"); print $fobj->{su_bavail}*1024." bytes of ". $fobj->{su_blocks}*1024." bytes free\n";
We have to do a little bit of arithmetic (i.e., *1024
) because Filesys::Df
returns values in terms of blocks, and each block is 1024 bytes on our system. The df( )
function for this module can be passed a second optional argument for block size if necessary. Also worth noting about this code are the two hash values we've requested. su_bavail
and su_blocks
are the values returned by this module for the "real" size and disk usage information. On most Unix filesystems, the df
command would show a value that hides the standard 10% of a disk set aside for superuser overflow. If we wanted to see the total amount of space available and the current amount free from a normal user's perspective, we would have used user_blocks
and user_bavail
instead.
With the key pieces of Perl code we've just seen, it is possible to build more sophisticated disk monitoring and management systems. These filesystem watchdogs will help you deal with space problems before they occur.