Previous Book Contents Book Index Next

Inside Macintosh: Files /
Chapter 2 - File Manager / Using the File Manager


Manipulating the Default Volume and Directory

When your application is running, the File Manager maintains a default volume and a default directory for it. The default directory is used in File Manager routines whenever you don't explicitly specify some directory. The default volume is the volume containing the default directory.

If you pass 0 as the volume specification with routines that operate on a volume (such as mounting or ejecting routines), the File Manager assumes that you want to perform the operation on the default volume. Initially, the volume used to start up the application is set as the default volume, but your application can designate any mounted volume as the default volume.

With routines that access files or directories, if you don't specify a directory and you pass a volume specification of 0, the File Manager assumes that the file or directory is located in the default directory. Initially, the default directory is set to the root directory of the default volume, but your application can designate any directory as the default directory.

Note
Don't confuse the default directory and volume maintained by the
File Manager with the current directory and volume maintained by
the Standard File Package. Although the default volume and current volume are initially the same, they can differ whenever your application resets one of them. See the chapter "Standard File Package" in this book for more information about the current directory and volume.
The provision of a default volume was originally intended as a convenient way for
you to limit all File Manager calls to a particular volume. The default directory was introduced along with HFS as an analog to the default volume. In general, however, it
is safest to specify both a volume and a directory explicitly in all File Manager calls. In particular, the introduction of file system specification records has rendered default volumes and directories largely obsolete. As a result, you should avoid relying on them.

In some cases, however, you might want to set the default volume or directory explicitly. You can determine the default volume and directory by calling the GetVol or HGetVol function. You can explicitly set the default directory and volume by calling the SetVol or HSetVol function. For reasons explained later, however, the use of HSetVol and its low-level equivalent PBHSetVol is discouraged.

To set the default volume only, you can call SetVol, passing it the volume reference number of the volume you want to establish as the default volume, as in this example:

myErr := SetVol(NIL, myVRefNum);
You can instead specify the volume by name, but because volume names might not be unique, it is best to use the volume reference number.

To set both the default directory and the default volume, you could call HSetVol, passing it the appropriate volume reference number and directory ID, as in this example:

myErr := HSetVol(NIL, myVRefNum, myDirID);
However, using HSetVol can lead to problems in certain circumstances. When you call HSetVol (or its low-level version PBHSetVol) and pass a working directory reference number in the vRefNum parameter, the File Manager stores the encoded volume reference number and directory ID separately. If you later call GetVol (or its low-level version PBGetVol), the File Manager returns that volume reference number, not the working directory reference number you passed to HSetVol. The net result is that any code using the results of the GetVol call will access the root directory of the default volume, not the actual default directory.

It is important to realize that calling HSetVol is perfectly safe if all the code executing in your application's partition always calls HGetVol instead of GetVol. This is because HGetVol returns a working directory reference number whenever the previous call to HSetVol passed one in. Calling HSetVol can create problems only if your application is running under a system software version prior to version 7.0. In that case, a desk accessory might be opened in your application's partition, thereby inheriting your application's default volume and directory. If that desk accessory calls GetVol instead of HGetVol, it might receive a volume reference number when it expects a working directory reference number, as described in the previous paragraph. To avoid this problem, you can simply use SetVol (or PBSetVol) instead of HSetVol, as in this example:

myErr := SetVol(NIL, myVRefNum);
In this case, the myVRefNum parameter should contain a working directory
reference number.


Previous Book Contents Book Index Next

© Apple Computer, Inc.
2 JUL 1996