Solved

Getting free disk space?

Posted on 1998-04-27
5
267 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

623 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