Solved

Getting free disk space?

Posted on 1998-04-27
5
247 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
string initialization in java 11 114
post4 challenge 28 102
withoutTen challenge 14 125
x-bar in Google Sheets 2 55
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

825 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