Solved

Getting free disk space?

Posted on 1998-04-27
5
238 Views
Last Modified: 2013-12-26
Hi all-

I've been trying to figure out how to get my program to:
a) Figure out how much free disk space there is on the Startup volume
   and
b) Figure out how much free disk space there is available on all of the computer's (writable) hard disks (maybe by looking at a list of the mounted volumes and getting the free space on each one, then adding it up)

I've spent altogether too much time searching Inside Macintosh, and I haven't gotten very far.  I've found out about the "Drive Queue" (Inside Macintosh: Files / Chapter 2 - File Manager / Data Organization in Memory / The Drive Queue) and the "GetVInfo" function (Inside Macintosh: Files / Chapter 2 - File Manager / File Manager ReferenceHigh-Level Volume Access Routines / Obtaining Volume  Information / GetVInfo), but neither of those seem to solve this problem.

If it makes a difference, I'm using CodeWarrior Pro 2 with PowerPlant.

Any help/tips/pointers would be very much appreciated!

Thanks,

Ian
0
Comment
Question by:icrew
  • 2
  • 2
5 Comments
 

Author Comment

by:icrew
ID: 1291378
Adjusted points to 500
0
 
LVL 2

Accepted Solution

by:
roov earned 500 total points
ID: 1291379
Ian,

Try using GetVInfo.
The API is:
anErr = GetVInfo(drvNum, volName, vRefNum, freeBytes);

To iterate all volumes, use:
OSErr GetIndVolume(short index,
                           short* vRefNum,
                           Str32 name)
{
      ParamBlockRec pb;
      OSErr err;
      
      pb.volumeParam.ioCompletion = NULL;
      pb.volumeParam.ioNamePtr = name;
      pb.volumeParam.ioVolIndex = index;
      
      err= PBGetVInfo(&pb,false);
      
      *vRefNum = pb.volumeParam.ioVRefNum;
      return err;
}


0
 
LVL 2

Expert Comment

by:WileyKat
ID: 1291380
That's not quite what I would have done, but if it works, great. If not, I have an idea or two.
0
 

Author Comment

by:icrew
ID: 1291381
Actually, I already solved this problem.  THe function I came up with follows.  Thanks for your help, though.

Ian

/*****************************************************************/
/*GetFreeSpace*/
/*****************************************************************/
OSErr GetFreeSpace (double *bytesFree, double *sysBytesFree) {
      OSErr error;
      HParamBlockRec tempVolInfo;
      
      *bytesFree = 0;  //set the number of bytes free to 0.
      
      Str255 tempVolName/*, tempVolSize*/;
      short tempVolRef;
      long tempFreeBytes;
      
      tempVolInfo.volumeParam.ioNamePtr = nil;
      tempVolInfo.volumeParam.ioVolIndex = 1;
      
      error = PBHGetVInfo(&tempVolInfo, false);
      
      while (error == noErr) {
            //check to make sure (as much as we can) that the volume is local and writable before adding
            //to the available disk space.            

            //Get the Driver reference numbers for AppleShare and Floppies.
            short afpRefNum;
            short floppyRefNum;
            error = OpenDriver ("\p.AFPTranslator", &afpRefNum);
            if (error != noErr) afpRefNum = -1;
            error = OpenDriver ("\p.Sony", &floppyRefNum);
            if (error != noErr) floppyRefNum = -1;

            //Get the VRefNum of the System (boot) disk
            short sysVRefNum;
            long sysDirID; //don't really need this, but FindFolder does.
            
            error = FindFolder (kOnSystemDisk, kSystemFolderType, kDontCreateFolder, &sysVRefNum, &sysDirID);
            if (error != noErr) return error;  //if we can't find the system folder, this is pretty useless.
                  
            if ((tempVolInfo.volumeParam.ioVAtrb & ((1<<7)|(1<<15))) ||
                  (tempVolInfo.volumeParam.ioVDRefNum == afpRefNum) ||
                  (tempVolInfo.volumeParam.ioVDRefNum == floppyRefNum)) {
                  /*it's (a) locked, (b) an appleshare volume, or (c) a floppy, so we skip it*/
            }
            else {
                  //if the volume is writable
                  error = GetVInfo(tempVolInfo.volumeParam.ioVDrvInfo, tempVolName, &tempVolRef, &tempFreeBytes);
                  
                  if (error == noErr) {
                        *bytesFree += tempFreeBytes;                        
                        if (tempVolRef == sysVRefNum) {
                              //SignalPStr_("\pthis is the system volume");
                              *sysBytesFree = tempFreeBytes;
                        }
                  }
                  else {
                        return error;
                  }
            }
            
            //go on to the next volume
            tempVolInfo.volumeParam.ioVolIndex++;
            error = PBHGetVInfo(&tempVolInfo, false);
      }
      
      if (error == nsvErr) {
            //this just means that we've looked at all of the avialable volumes
            return noErr;
      }
      else {
            return error;
      }
}

0
 
LVL 2

Expert Comment

by:WileyKat
ID: 1291382
Nice code.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now