/core/externals/google-toolbox-for-mac/Foundation/GTMNSFileManager+Carbon.m

http://macfuse.googlecode.com/ · Objective C · 116 lines · 80 code · 17 blank · 19 comment · 6 complexity · b6960467ad845f45f97b3be1ba273e6d MD5 · raw file

  1. //
  2. // GTMNSFileManager+Carbon.m
  3. //
  4. // Copyright 2008 Google Inc.
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7. // use this file except in compliance with the License. You may obtain a copy
  8. // of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15. // License for the specific language governing permissions and limitations under
  16. // the License.
  17. //
  18. #import "GTMNSFileManager+Carbon.h"
  19. #import <CoreServices/CoreServices.h>
  20. #import <sys/param.h>
  21. #import "GTMDefines.h"
  22. @implementation NSFileManager (GTMFileManagerCarbonAdditions)
  23. - (NSData *)gtm_aliasDataForPath:(NSString *)path {
  24. NSData *data = nil;
  25. FSRef ref;
  26. AliasHandle alias = NULL;
  27. require_quiet([path length], CantUseParams);
  28. require_noerr(FSPathMakeRef((UInt8 *)[path fileSystemRepresentation],
  29. &ref, NULL), CantMakeRef);
  30. require_noerr(FSNewAlias(NULL, &ref, &alias), CantMakeAlias);
  31. Size length = GetAliasSize(alias);
  32. data = [NSData dataWithBytes:*alias length:length];
  33. DisposeHandle((Handle)alias);
  34. CantMakeAlias:
  35. CantMakeRef:
  36. CantUseParams:
  37. return data;
  38. }
  39. - (NSString *)gtm_pathFromAliasData:(NSData *)data {
  40. return [self gtm_pathFromAliasData:data resolve:YES withUI:YES];
  41. }
  42. - (NSString *)gtm_pathFromAliasData:(NSData *)data
  43. resolve:(BOOL)resolve
  44. withUI:(BOOL)withUI {
  45. NSString *path = nil;
  46. require_quiet(data, CantUseParams);
  47. AliasHandle alias;
  48. const void *bytes = [data bytes];
  49. NSUInteger length = [data length];
  50. require_noerr(PtrToHand(bytes, (Handle *)&alias, length), CantMakeHandle);
  51. FSRef ref;
  52. Boolean wasChanged;
  53. // we don't use a require here because it is quite legitimate for an alias
  54. // resolve to fail.
  55. if (resolve) {
  56. OSStatus err
  57. = FSResolveAliasWithMountFlags(NULL, alias, &ref, &wasChanged,
  58. withUI ? kResolveAliasFileNoUI : 0);
  59. if (err == noErr) {
  60. path = [self gtm_pathFromFSRef:&ref];
  61. }
  62. } else {
  63. OSStatus err
  64. = FSCopyAliasInfo(alias, NULL, NULL, (CFStringRef *)(&path), NULL, NULL);
  65. if (err != noErr) path = nil;
  66. GTMCFAutorelease(path);
  67. }
  68. DisposeHandle((Handle)alias);
  69. CantMakeHandle:
  70. CantUseParams:
  71. return path;
  72. }
  73. - (FSRef *)gtm_FSRefForPath:(NSString *)path {
  74. FSRef* fsRef = NULL;
  75. require_quiet([path length], CantUseParams);
  76. NSMutableData *fsRefData = [NSMutableData dataWithLength:sizeof(FSRef)];
  77. require(fsRefData, CantAllocateFSRef);
  78. fsRef = (FSRef*)[fsRefData mutableBytes];
  79. Boolean isDir = FALSE;
  80. const UInt8 *filePath = (const UInt8 *)[path fileSystemRepresentation];
  81. require_noerr_action(FSPathMakeRef(filePath, fsRef, &isDir),
  82. CantMakeRef, fsRef = NULL);
  83. CantMakeRef:
  84. CantAllocateFSRef:
  85. CantUseParams:
  86. return fsRef;
  87. }
  88. - (NSString *)gtm_pathFromFSRef:(FSRef *)fsRef {
  89. NSString *nsPath = nil;
  90. require_quiet(fsRef, CantUseParams);
  91. char path[MAXPATHLEN];
  92. require_noerr(FSRefMakePath(fsRef, (UInt8 *)path, MAXPATHLEN), CantMakePath);
  93. nsPath = [self stringWithFileSystemRepresentation:path length:strlen(path)];
  94. nsPath = [nsPath stringByStandardizingPath];
  95. CantMakePath:
  96. CantUseParams:
  97. return nsPath;
  98. }
  99. @end