Important: The information in this document is obsolete and should not be used for new development.
Constructing Full Pathnames
As indicated in "Names and Pathnames" on page 2-27, the use of full or partial pathnames is strongly discouraged. Full pathnames are particularly unreliable as a means of identifying files or directories within your application, largely because the user can change the name of any element in the path at virtually any time. In general, you should use a file's name, parent directory ID, and volume reference number to identify a file you want to open, delete, or otherwise manipulate.If you need to remember the location of a particular file across subsequent system boots, use the Alias Manager to create an alias record describing the file. If the Alias Manager is not available, you can save the file's name, its parent directory ID, and the name of the volume on which it's located. Although none of these methods is foolproof, they are much more reliable than using full pathnames to identify files.
Nonetheless, it is sometimes useful to display a file's full pathname to the user. For example, a backup utility might display a list of full pathnames of files as it copies them onto the backup medium. Or, a utility might want to display a dialog box showing the full pathname of a file when it needs the user's confirmation to delete the file. No matter how unreliable full pathnames may be from a file-specification viewpoint, users understand them more readily than volume reference numbers or directory IDs.
Listing 2-5 shows one way to define a function,
- Note
- The following technique for constructing the full pathname of a file is intended for display purposes only. Applications that depend on any particular structure of a full pathname are likely to fail on alternate foreign file systems or under future system software versions.
GetFullPath
, that accepts a directory ID and a filename as parameters and returns the full pathname of the corresponding file (if any). TheGetFullPath
function calls the low-level functionPBGetCatInfo
for the specified directory to determine the name and directory ID of that directory's parent directory. It then performs the same operation on the parent directory's parent, continuing until it finds a parent directory with ID fsRtDirID. Under HFS, this is always the ID of a volume's root directory.Listing 2-5 Constructing the full pathname of a file
FUNCTION GetFullPath (DirID: LongInt; vRefnum: Integer): Str255; VAR myPB: CInfoPBRec; {parameter block for PBGetCatInfo} dirName: Str255; {a directory name} fullPath: Str255; {full pathname being constructed} myErr: OSErr; BEGIN fullPath := ''; {initialize full pathname} myPB.ioNamePtr := @dirName; myPB.ioVRefNum := vRefNum; {indicate target volume} myPB.ioDrParID := DirId; {initialize parent directory ID} myPB.ioFDirIndex := -1; {get info about a directory} {Get name of each parent directory, up to root directory.} REPEAT myPB.ioDrDirID := myPB.ioDrParID; myErr := PBGetCatInfo(@myPB, FALSE); IF gHaveAUX THEN BEGIN IF dirName[1] <> '/' THEN dirName := concat(dirName, '/'); END ELSE dirName := concat(dirName, ':'); fullPath := concat(dirName, fullPath); UNTIL myPB.ioDrDirID = fsRtDirID; GetFullPath := fullPath; {return full pathname} END;Note thatGetFullPath
uses either a slash (/) or a colon (:) to separate names in the full path, depending on whether A/UX is running or not. TheGetFullPath
function reads the value of the global variable gHaveAUX to determine whether A/UX is running; your application must initialize this variable (preferably by calling theGestalt
function) before it callsGetFullPath
.The
GetFullPath
function defined in Listing 2-5 returns a result of typeStr255
, which limits the full pathname to 255 characters. An actual full pathname, however, might exceed 255 characters. A volume name can be up to 27 characters, and each directory name can be up to 31 characters. If the average volume and directory name is about 20 characters long,GetFullPath
can handle files located only about 12 levels deep. If the length of the average directory name is closer to the maximum,GetFullPath
provides a full pathname for files located only about 8 levels deep. If necessary, you can overcome this limitation by rewritingGetFullPath
to return a handle to the full pathname; the algorithm for ascending the directory hierarchy usingPBGetCatInfo
will still work, however.