PageRenderTime 41ms CodeModel.GetById 11ms app.highlight 27ms RepoModel.GetById 1ms app.codeStats 0ms

/core/externals/google-toolbox-for-mac/Foundation/GTMPath.m

http://macfuse.googlecode.com/
Objective C | 175 lines | 119 code | 31 blank | 25 comment | 20 complexity | 2453c177ad00b3ac29988095fd9ca1d7 MD5 | raw file
  1//
  2//  GTMPath.m
  3//
  4//  Copyright 2007-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 "GTMPath.h"
 20#import "GTMDefines.h"
 21
 22#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
 23// NSFileManager has improved substantially in Leopard and beyond, so GTMPath
 24// is now deprecated.
 25
 26@implementation GTMPath
 27
 28+ (id)pathWithFullPath:(NSString *)fullPath {
 29  return [[[self alloc] initWithFullPath:fullPath] autorelease];
 30}
 31
 32- (id)init {
 33  return [self initWithFullPath:nil];
 34}
 35
 36- (id)initWithFullPath:(NSString *)fullPath {
 37  if ((self = [super init])) {
 38    fullPath_ = [[fullPath stringByResolvingSymlinksInPath] copy];
 39    if (![fullPath_ isAbsolutePath] || [self attributes] == nil) {
 40      [self release];
 41      return nil;
 42    }
 43  }
 44
 45  return self;
 46}
 47
 48- (void)dealloc {
 49  [fullPath_ release];
 50  [super dealloc];
 51}
 52
 53- (NSString *)description {
 54  return [self fullPath];
 55}
 56
 57- (NSString *)name {
 58  return [fullPath_ lastPathComponent];
 59}
 60
 61- (GTMPath *)parent {
 62  if ([self isRoot]) return nil;
 63  NSString *parentPath = [fullPath_ stringByDeletingLastPathComponent];
 64  return [[self class] pathWithFullPath:parentPath];
 65}
 66
 67- (BOOL)isDirectory {
 68  BOOL isDir = NO;
 69  BOOL exists = [[NSFileManager defaultManager]
 70                 fileExistsAtPath:fullPath_ isDirectory:&isDir];
 71  return exists && isDir;
 72}
 73
 74- (BOOL)isRoot {
 75  return [fullPath_ isEqualToString:@"/"];
 76}
 77
 78- (NSDictionary *)attributes {
 79  NSFileManager *mgr = [NSFileManager defaultManager];
 80#if GTM_MACOS_SDK && (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5)
 81  NSDictionary *attributes = [mgr fileAttributesAtPath:fullPath_
 82                                          traverseLink:NO];
 83#else
 84  NSDictionary *attributes = [mgr attributesOfItemAtPath:fullPath_
 85                                                   error:NULL];
 86#endif
 87  return attributes;
 88}
 89
 90- (NSString *)fullPath {
 91  return [[fullPath_ copy] autorelease];
 92}
 93
 94@end
 95
 96
 97@implementation GTMPath (GTMPathGeneration)
 98
 99- (GTMPath *)createDirectoryName:(NSString *)name mode:(mode_t)mode {
100  NSDictionary *attributes =
101    [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:mode]
102                                forKey:NSFilePosixPermissions];
103  return [self createDirectoryName:name attributes:attributes];
104}
105
106- (GTMPath *)createDirectoryName:(NSString *)name
107                      attributes:(NSDictionary *)attributes {
108  if ([name length] == 0) return nil;
109
110  // We first check to see if the requested directory alread exists by trying
111  // to create a GTMPath from the desired new path string. Only if the path
112  // doesn't already exist do we attempt to create it. If the path already
113  // exists, we will end up returning a GTMPath for the pre-existing path.
114  NSString *newPath = [fullPath_ stringByAppendingPathComponent:name];
115  GTMPath *nascentPath = [GTMPath pathWithFullPath:newPath];
116  if (nascentPath && ![nascentPath isDirectory]) {
117    return nil;  // Return nil because the path exists, but it's not a dir
118  }
119
120  if (!nascentPath) {
121    NSFileManager *mgr = [NSFileManager defaultManager];
122#if GTM_IPHONE_SDK || (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5)
123    NSError *error = nil;
124    BOOL created = [mgr createDirectoryAtPath:newPath
125                  withIntermediateDirectories:YES
126                                   attributes:attributes
127                                        error:&error];
128#else
129    BOOL created = [mgr createDirectoryAtPath:newPath attributes:attributes];
130#endif  // MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
131    nascentPath = created ? [GTMPath pathWithFullPath:newPath] : nil;
132  }
133
134  return nascentPath;
135}
136
137- (GTMPath *)createFileName:(NSString *)name mode:(mode_t)mode {
138  NSDictionary *attributes =
139    [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:mode]
140                                forKey:NSFilePosixPermissions];
141  return [self createFileName:name attributes:attributes];
142}
143
144- (GTMPath *)createFileName:(NSString *)name
145                 attributes:(NSDictionary *)attributes {
146  return [self createFileName:name attributes:attributes data:[NSData data]];
147}
148
149- (GTMPath *)createFileName:(NSString *)name
150                 attributes:(NSDictionary *)attributes
151                       data:(NSData *)data {
152  if ([name length] == 0) return nil;
153
154  // See createDirectoryName:attribute: for some high-level notes about what and
155  // why this method does what it does.
156  NSString *newPath = [fullPath_ stringByAppendingPathComponent:name];
157  GTMPath *nascentPath = [GTMPath pathWithFullPath:newPath];
158  if (nascentPath != nil && [nascentPath isDirectory]) {
159    return nil;  // Return nil because the path exists, but it's a dir
160  }
161
162  if (nascentPath == nil) {
163    BOOL created = [[NSFileManager defaultManager]
164                    createFileAtPath:newPath
165                            contents:data
166                          attributes:attributes];
167    nascentPath = created ? [GTMPath pathWithFullPath:newPath] : nil;
168  }
169
170  return nascentPath;
171}
172
173@end
174
175#endif //  MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5