]>
git.saurik.com Git - wxWidgets.git/blob - src/mac/morefile/FullPath.c
4 Contains: Routines for dealing with full pathnames... if you really must.
8 Copyright: © 1995-2001 by Apple Computer, Inc., all rights reserved.
10 You may incorporate this sample code into your applications without
11 restriction, though the sample code has been provided "AS IS" and the
12 responsibility for its operation is 100% yours. However, what you are
13 not permitted to do is to redistribute the source as "DSC Sample Code"
14 after having made changes. If you're going to re-distribute the source,
15 we require that you make it clear in the source that the code was
16 descended from Apple Sample Code, but that you've made changes.
20 DRI: Apple Macintosh Developer Technical Support
22 Other Contact: Apple Macintosh Developer Technical Support
23 <http://developer.apple.com/bugreporter/>
25 Technology: DTS Sample Code
31 Change History (most recent first):
33 <2> 2/7/01 JL Added standard header. Updated names of includes.
34 <1> 12/06/99 JL MoreFiles 1.5.
38 #include <MacErrors.h>
39 #include <MacMemory.h>
41 #include <TextUtils.h>
44 #define __COMPILINGMOREFILES
46 #include "FSpCompat.h"
52 The use of full pathnames is strongly discouraged. Full pathnames are
53 particularly unreliable as a means of identifying files, directories
54 or volumes within your application, for two primary reasons:
56 ¥ The user can change the name of any element in the path at virtually
58 ¥ Volume names on the Macintosh are *not* unique. Multiple
59 mounted volumes can have the same name. For this reason, the use of
60 a full pathname to identify a specific volume may not produce the
61 results you expect. If more than one volume has the same name and
62 a full pathname is used, the File Manager currently uses the first
63 mounted volume it finds with a matching name in the volume queue.
65 In general, you should use a fileÕs name, parent directory ID, and
66 volume reference number to identify a file you want to open, delete,
67 or otherwise manipulate.
69 If you need to remember the location of a particular file across
70 subsequent system boots, use the Alias Manager to create an alias record
71 describing the file. If the Alias Manager is not available, you can save
72 the fileÕs name, its parent directory ID, and the name of the volume on
73 which itÕs located. Although none of these methods is foolproof, they are
74 much more reliable than using full pathnames to identify files.
76 Nonetheless, it is sometimes useful to display a fileÕs full pathname to
77 the user. For example, a backup utility might display a list of full
78 pathnames of files as it copies them onto the backup medium. Or, a
79 utility might want to display a dialog box showing the full pathname of
80 a file when it needs the userÕs confirmation to delete the file. No
81 matter how unreliable full pathnames may be from a file-specification
82 viewpoint, users understand them more readily than volume reference
83 numbers or directory IDs. (Hint: Use the TruncString function from
84 TextUtils.h with truncMiddle as the truncWhere argument to shorten
85 full pathnames to a displayable length.)
87 The following technique for constructing the full pathname of a file is
88 intended for display purposes only. Applications that depend on any
89 particular structure of a full pathname are likely to fail on alternate
90 foreign file systems or under future system software versions.
93 /*****************************************************************************/
95 pascal OSErr
GetFullPath(short vRefNum
,
97 ConstStr255Param name
,
98 short *fullPathLength
,
107 result
= FSMakeFSSpecCompat(vRefNum
, dirID
, name
, &spec
);
108 if ( (result
== noErr
) || (result
== fnfErr
) )
110 result
= FSpGetFullPath(&spec
, fullPathLength
, fullPath
);
116 /*****************************************************************************/
118 pascal OSErr
FSpGetFullPath(const FSSpec
*spec
,
119 short *fullPathLength
,
131 /* Default to noErr */
132 realResult
= result
= noErr
;
134 /* work around Nav Services "bug" (it returns invalid FSSpecs with empty names) */
135 if ( spec
->name
[0] == 0 )
137 result
= FSMakeFSSpecCompat(spec
->vRefNum
, spec
->parID
, spec
->name
, &tempSpec
);
141 /* Make a copy of the input FSSpec that can be modified */
142 BlockMoveData(spec
, &tempSpec
, sizeof(FSSpec
));
145 if ( result
== noErr
)
147 if ( tempSpec
.parID
== fsRtParID
)
149 /* The object is a volume */
151 /* Add a colon to make it a full pathname */
153 tempSpec
.name
[tempSpec
.name
[0]] = ':';
156 result
= PtrToHand(&tempSpec
.name
[1], fullPath
, tempSpec
.name
[0]);
160 /* The object isn't a volume */
162 /* Is the object a file or a directory? */
163 pb
.dirInfo
.ioNamePtr
= tempSpec
.name
;
164 pb
.dirInfo
.ioVRefNum
= tempSpec
.vRefNum
;
165 pb
.dirInfo
.ioDrDirID
= tempSpec
.parID
;
166 pb
.dirInfo
.ioFDirIndex
= 0;
167 result
= PBGetCatInfoSync(&pb
);
168 // Allow file/directory name at end of path to not exist.
170 if ( (result
== noErr
) || (result
== fnfErr
) )
172 /* if the object is a directory, append a colon so full pathname ends with colon */
173 if ( (result
== noErr
) && (pb
.hFileInfo
.ioFlAttrib
& kioFlAttribDirMask
) != 0 )
176 tempSpec
.name
[tempSpec
.name
[0]] = ':';
179 /* Put the object name in first */
180 result
= PtrToHand(&tempSpec
.name
[1], fullPath
, tempSpec
.name
[0]);
181 if ( result
== noErr
)
183 /* Get the ancestor directory names */
184 pb
.dirInfo
.ioNamePtr
= tempSpec
.name
;
185 pb
.dirInfo
.ioVRefNum
= tempSpec
.vRefNum
;
186 pb
.dirInfo
.ioDrParID
= tempSpec
.parID
;
187 do /* loop until we have an error or find the root directory */
189 pb
.dirInfo
.ioFDirIndex
= -1;
190 pb
.dirInfo
.ioDrDirID
= pb
.dirInfo
.ioDrParID
;
191 result
= PBGetCatInfoSync(&pb
);
192 if ( result
== noErr
)
194 /* Append colon to directory name */
196 tempSpec
.name
[tempSpec
.name
[0]] = ':';
198 /* Add directory name to beginning of fullPath */
199 (void) Munger(*fullPath
, 0, NULL
, 0, &tempSpec
.name
[1], tempSpec
.name
[0]);
202 } while ( (result
== noErr
) && (pb
.dirInfo
.ioDrDirID
!= fsRtDirID
) );
208 if ( result
== noErr
)
210 /* Return the length */
211 *fullPathLength
= GetHandleSize(*fullPath
);
212 result
= realResult
; // return realResult in case it was fnfErr
216 /* Dispose of the handle and return NULL and zero length */
217 if ( *fullPath
!= NULL
)
219 DisposeHandle(*fullPath
);
228 /*****************************************************************************/
230 pascal OSErr
FSpLocationFromFullPath(short fullPathLength
,
231 const void *fullPath
,
239 /* Create a minimal alias from the full pathname */
240 nullString
[0] = 0; /* null string to indicate no zone or server name */
241 result
= NewAliasMinimalFromFullPath(fullPathLength
, fullPath
, nullString
, nullString
, &alias
);
242 if ( result
== noErr
)
244 /* Let the Alias Manager resolve the alias. */
245 result
= ResolveAlias(NULL
, alias
, spec
, &wasChanged
);
247 /* work around Alias Mgr sloppy volume matching bug */
248 if ( spec
->vRefNum
== 0 )
250 /* invalidate wrong FSSpec */
255 DisposeHandle((Handle
)alias
); /* Free up memory used */
260 /*****************************************************************************/
262 pascal OSErr
LocationFromFullPath(short fullPathLength
,
263 const void *fullPath
,
271 result
= FSpLocationFromFullPath(fullPathLength
, fullPath
, &spec
);
272 if ( result
== noErr
)
274 *vRefNum
= spec
.vRefNum
;
276 BlockMoveData(&spec
.name
[0], &name
[0], spec
.name
[0] + 1);
281 /*****************************************************************************/