home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / mac / MoreFiles / FullPath.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  7.6 KB  |  245 lines

  1. /*
  2. **    Apple Macintosh Developer Technical Support
  3. **
  4. **    Routines for dealing with full pathnames... if you really must.
  5. **
  6. **    by Jim Luther, Apple Developer Technical Support Emeritus
  7. **
  8. **    File:        FullPath.c
  9. **
  10. **    Copyright ⌐ 1995-1996 Apple Computer, Inc.
  11. **    All rights reserved.
  12. **
  13. **    You may incorporate this sample code into your applications without
  14. **    restriction, though the sample code has been provided "AS IS" and the
  15. **    responsibility for its operation is 100% yours.  However, what you are
  16. **    not permitted to do is to redistribute the source as "DSC Sample Code"
  17. **    after having made changes. If you're going to re-distribute the source,
  18. **    we require that you make it clear in the source that the code was
  19. **    descended from Apple Sample Code, but that you've made changes.
  20. */
  21.  
  22. /* 
  23.  * This code, which was decended from Apple Sample Code, has been modified by 
  24.  * Netscape.
  25.  */
  26.  
  27. #include <Types.h>
  28. #include <Errors.h>
  29. #include <Memory.h>
  30. #include <Files.h>
  31. #include <TextUtils.h>
  32. #include <Aliases.h>
  33.  
  34. #define    __COMPILINGMOREFILES
  35.  
  36. #include "FSpCompat.h"
  37. #include "FullPath.h"
  38.  
  39. /*
  40.     IMPORTANT NOTE:
  41.     
  42.     The use of full pathnames is strongly discouraged. Full pathnames are
  43.     particularly unreliable as a means of identifying files, directories
  44.     or volumes within your application, for two primary reasons:
  45.     
  46.     Ñ     The user can change the name of any element in the path at virtually
  47.         any time.
  48.     Ñ    Volume names on the Macintosh are *not* unique. Multiple
  49.         mounted volumes can have the same name. For this reason, the use of
  50.         a full pathname to identify a specific volume may not produce the
  51.         results you expect. If more than one volume has the same name and
  52.         a full pathname is used, the File Manager currently uses the first
  53.         mounted volume it finds with a matching name in the volume queue.
  54.     
  55.     In general, you should use a file╒s name, parent directory ID, and
  56.     volume reference number to identify a file you want to open, delete,
  57.     or otherwise manipulate.
  58.     
  59.     If you need to remember the location of a particular file across
  60.     subsequent system boots, use the Alias Manager to create an alias record
  61.     describing the file. If the Alias Manager is not available, you can save
  62.     the file╒s name, its parent directory ID, and the name of the volume on
  63.     which it╒s located. Although none of these methods is foolproof, they are
  64.     much more reliable than using full pathnames to identify files.
  65.     
  66.     Nonetheless, it is sometimes useful to display a file╒s full pathname to
  67.     the user. For example, a backup utility might display a list of full
  68.     pathnames of files as it copies them onto the backup medium. Or, a
  69.     utility might want to display a dialog box showing the full pathname of
  70.     a file when it needs the user╒s confirmation to delete the file. No
  71.     matter how unreliable full pathnames may be from a file-specification
  72.     viewpoint, users understand them more readily than volume reference
  73.     numbers or directory IDs. (Hint: Use the TruncString function from
  74.     TextUtils.h with truncMiddle as the truncWhere argument to shorten
  75.     full pathnames to a displayable length.)
  76.     
  77.     The following technique for constructing the full pathname of a file is
  78.     intended for display purposes only. Applications that depend on any
  79.     particular structure of a full pathname are likely to fail on alternate
  80.     foreign file systems or under future system software versions.
  81. */
  82.  
  83. /*****************************************************************************/
  84.  
  85. pascal    OSErr    GetFullPath(short vRefNum,
  86.                             long dirID,
  87.                             StringPtr name,
  88.                             short *fullPathLength,
  89.                             Handle *fullPath)
  90. {
  91.     OSErr        result;
  92.     FSSpec        spec;
  93.     
  94.     *fullPathLength = 0;
  95.     *fullPath = NULL;
  96.     
  97.     result = FSMakeFSSpecCompat(vRefNum, dirID, name, &spec);
  98.     if ( result == noErr )
  99.     {
  100.         result = FSpGetFullPath(&spec, fullPathLength, fullPath);
  101.     }
  102.     
  103.     return ( result );
  104. }
  105.  
  106. /*****************************************************************************/
  107.  
  108. pascal    OSErr    FSpGetFullPath(const FSSpec *spec,
  109.                                short *fullPathLength,
  110.                                Handle *fullPath)
  111. {
  112.     OSErr        result;
  113.     FSSpec        tempSpec;
  114.     CInfoPBRec    pb;
  115.     
  116.     *fullPathLength = 0;
  117.     *fullPath = NULL;
  118.     
  119.     /* Make a copy of the input FSSpec that can be modified */
  120.     BlockMoveData(spec, &tempSpec, sizeof(FSSpec));
  121.     
  122.     if ( tempSpec.parID == fsRtParID )
  123.     {
  124.         /* The object is a volume */
  125.         
  126.         /* Add a colon to make it a full pathname */
  127.         ++tempSpec.name[0];
  128.         tempSpec.name[tempSpec.name[0]] = ':';
  129.         
  130.         /* We're done */
  131.         result = PtrToHand(&tempSpec.name[1], fullPath, tempSpec.name[0]);
  132.     }
  133.     else
  134.     {
  135.         /* The object isn't a volume */
  136.         
  137.         /* Is the object a file or a directory? */
  138.         pb.dirInfo.ioNamePtr = tempSpec.name;
  139.         pb.dirInfo.ioVRefNum = tempSpec.vRefNum;
  140.         pb.dirInfo.ioDrDirID = tempSpec.parID;
  141.         pb.dirInfo.ioFDirIndex = 0;
  142.         result = PBGetCatInfoSync(&pb);
  143.         if ( result == noErr )
  144.         {
  145.             /* if the object is a directory, append a colon so full pathname ends with colon */
  146.             if ( (pb.hFileInfo.ioFlAttrib & ioDirMask) != 0 )
  147.             {
  148.                 ++tempSpec.name[0];
  149.                 tempSpec.name[tempSpec.name[0]] = ':';
  150.             }
  151.             
  152.             /* Put the object name in first */
  153.             result = PtrToHand(&tempSpec.name[1], fullPath, tempSpec.name[0]);
  154.             if ( result == noErr )
  155.             {
  156.                 /* Get the ancestor directory names */
  157.                 pb.dirInfo.ioNamePtr = tempSpec.name;
  158.                 pb.dirInfo.ioVRefNum = tempSpec.vRefNum;
  159.                 pb.dirInfo.ioDrParID = tempSpec.parID;
  160.                 do    /* loop until we have an error or find the root directory */
  161.                 {
  162.                     pb.dirInfo.ioFDirIndex = -1;
  163.                     pb.dirInfo.ioDrDirID = pb.dirInfo.ioDrParID;
  164.                     result = PBGetCatInfoSync(&pb);
  165.                     if ( result == noErr )
  166.                     {
  167.                         /* Append colon to directory name */
  168.                         ++tempSpec.name[0];
  169.                         tempSpec.name[tempSpec.name[0]] = ':';
  170.                         
  171.                         /* Add directory name to beginning of fullPath */
  172.                         (void) Munger(*fullPath, 0, NULL, 0, &tempSpec.name[1], tempSpec.name[0]);
  173.                         result = MemError();
  174.                     }
  175.                 } while ( (result == noErr) && (pb.dirInfo.ioDrDirID != fsRtDirID) );
  176.             }
  177.         }
  178.     }
  179.     if ( result == noErr )
  180.     {
  181.         /* Return the length */
  182.         *fullPathLength = GetHandleSize(*fullPath);
  183.     }
  184.     else
  185.     {
  186.         /* Dispose of the handle and return NULL and zero length */
  187.         if ( *fullPath != NULL )
  188.         {
  189.             DisposeHandle(*fullPath);
  190.         }
  191.         *fullPath = NULL;
  192.         *fullPathLength = 0;
  193.     }
  194.     
  195.     return ( result );
  196. }
  197.  
  198. /*****************************************************************************/
  199.  
  200. pascal OSErr FSpLocationFromFullPath(short fullPathLength,
  201.                                      const void *fullPath,
  202.                                      FSSpec *spec)
  203. {
  204.     AliasHandle    alias;
  205.     OSErr        result;
  206.     Boolean        wasChanged;
  207.     Str32        nullString;
  208.     
  209.     /* Create a minimal alias from the full pathname */
  210.     nullString[0] = 0;    /* null string to indicate no zone or server name */
  211.     result = NewAliasMinimalFromFullPath(fullPathLength, fullPath, nullString, nullString, &alias);
  212.     if ( result == noErr )
  213.     {
  214.         /* Let the Alias Manager resolve the alias. */
  215.         result = ResolveAlias(NULL, alias, spec, &wasChanged);
  216.         
  217.         DisposeHandle((Handle)alias);    /* Free up memory used */
  218.     }
  219.     return ( result );
  220. }
  221.  
  222. /*****************************************************************************/
  223.  
  224. pascal OSErr LocationFromFullPath(short fullPathLength,
  225.                                   const void *fullPath,
  226.                                   short *vRefNum,
  227.                                   long *parID,
  228.                                   Str31 name)
  229. {
  230.     OSErr    result;
  231.     FSSpec    spec;
  232.     
  233.     result = FSpLocationFromFullPath(fullPathLength, fullPath, &spec);
  234.     if ( result == noErr )
  235.     {
  236.         *vRefNum = spec.vRefNum;
  237.         *parID = spec.parID;
  238.         BlockMoveData(&spec.name[0], &name[0], spec.name[0] + 1);
  239.     }
  240.     return ( result );
  241. }
  242.  
  243. /*****************************************************************************/
  244.  
  245.