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