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.
The provision of a default volume was originally intended as a convenient way for
- 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.
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
orHGetVol
function. You can explicitly set the default directory and volume by calling theSetVol
orHSetVol
function. For reasons explained later, however, the use ofHSetVol
and its low-level equivalentPBHSetVol
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, usingHSetVol
can lead to problems in certain circumstances. When you callHSetVol
(or its low-level versionPBHSetVol
) and pass a working directory reference number in thevRefNum
parameter, the File Manager stores the encoded volume reference number and directory ID separately. If you later callGetVol
(or its low-level versionPBGetVol
), the File Manager returns that volume reference number, not the working directory reference number you passed toHSetVol
. The net result is that any code using the results of theGetVol
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 callsHGetVol
instead ofGetVol
. This is becauseHGetVol
returns a working directory reference number whenever the previous call toHSetVol
passed one in. CallingHSetVol
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 callsGetVol
instead ofHGetVol
, 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 useSetVol
(orPBSetVol
) instead ofHSetVol
, as in this example:
myErr := SetVol(NIL, myVRefNum);In this case, themyVRefNum
parameter should contain a working directory
reference number.