PageRenderTime 139ms CodeModel.GetById 121ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 19#import "GTMNSFileManager+Carbon.h"
 20#import <CoreServices/CoreServices.h>
 21#import <sys/param.h>
 22#import "GTMDefines.h"
 23
 24@implementation NSFileManager (GTMFileManagerCarbonAdditions)
 25
 26- (NSData *)gtm_aliasDataForPath:(NSString *)path { 
 27  NSData *data = nil;
 28  FSRef ref;
 29  AliasHandle alias = NULL;
 30
 31  require_quiet([path length], CantUseParams);
 32  require_noerr(FSPathMakeRef((UInt8 *)[path fileSystemRepresentation], 
 33                              &ref, NULL), CantMakeRef); 
 34  require_noerr(FSNewAlias(NULL, &ref, &alias), CantMakeAlias);
 35
 36  Size length = GetAliasSize(alias);
 37  data = [NSData dataWithBytes:*alias length:length];
 38  
 39  DisposeHandle((Handle)alias);
 40
 41CantMakeAlias:  
 42CantMakeRef:
 43CantUseParams:
 44  return data; 
 45} 
 46
 47- (NSString *)gtm_pathFromAliasData:(NSData *)data {
 48  return [self gtm_pathFromAliasData:data resolve:YES withUI:YES];
 49} 
 50
 51- (NSString *)gtm_pathFromAliasData:(NSData *)data 
 52                            resolve:(BOOL)resolve 
 53                             withUI:(BOOL)withUI {
 54  NSString *path = nil;
 55  require_quiet(data, CantUseParams);
 56  
 57  AliasHandle alias;
 58  const void *bytes = [data bytes];
 59  NSUInteger length = [data length];
 60  require_noerr(PtrToHand(bytes, (Handle *)&alias, length), CantMakeHandle);
 61  
 62  FSRef ref;
 63  Boolean wasChanged;
 64  // we don't use a require here because it is quite legitimate for an alias
 65  // resolve to fail.
 66
 67  if (resolve) { 
 68    OSStatus err
 69      = FSResolveAliasWithMountFlags(NULL, alias, &ref, &wasChanged,
 70                                     withUI ? kResolveAliasFileNoUI : 0);
 71    if (err == noErr) {
 72      path = [self gtm_pathFromFSRef:&ref];
 73    }
 74  } else {
 75    OSStatus err
 76      = FSCopyAliasInfo(alias, NULL, NULL, (CFStringRef *)(&path), NULL, NULL);
 77    if (err != noErr) path = nil;
 78    GTMCFAutorelease(path);
 79  }
 80  DisposeHandle((Handle)alias);
 81CantMakeHandle:
 82CantUseParams:
 83  return path;
 84}
 85
 86- (FSRef *)gtm_FSRefForPath:(NSString *)path {
 87  FSRef* fsRef = NULL;
 88  require_quiet([path length], CantUseParams);
 89  NSMutableData *fsRefData = [NSMutableData dataWithLength:sizeof(FSRef)];
 90  require(fsRefData, CantAllocateFSRef);
 91  fsRef = (FSRef*)[fsRefData mutableBytes];
 92  Boolean isDir = FALSE;
 93  const UInt8 *filePath = (const UInt8 *)[path fileSystemRepresentation];
 94  require_noerr_action(FSPathMakeRef(filePath, fsRef, &isDir), 
 95                       CantMakeRef, fsRef = NULL);
 96CantMakeRef:
 97CantAllocateFSRef:
 98CantUseParams:
 99  return fsRef;
100}
101
102- (NSString *)gtm_pathFromFSRef:(FSRef *)fsRef {
103  NSString *nsPath = nil;
104  require_quiet(fsRef, CantUseParams);
105  
106  char path[MAXPATHLEN];
107  require_noerr(FSRefMakePath(fsRef, (UInt8 *)path, MAXPATHLEN), CantMakePath);
108  nsPath = [self stringWithFileSystemRepresentation:path length:strlen(path)];
109  nsPath = [nsPath stringByStandardizingPath];
110  
111CantMakePath:
112CantUseParams:
113  return nsPath;
114}  
115
116@end