/core/externals/update-engine/Core/KSExistenceChecker.m

http://macfuse.googlecode.com/ · Objective C · 308 lines · 217 code · 65 blank · 26 comment · 38 complexity · dbaeddcc97040e9cbc5ffc0e36dc1a92 MD5 · raw file

  1. // Copyright 2008 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "KSExistenceChecker.h"
  15. // Subclass that returns an existence checker that always says YES.
  16. @interface KSTrueExistenceChecker : KSExistenceChecker
  17. @end
  18. @implementation KSExistenceChecker
  19. + (id)falseChecker {
  20. return [[[self alloc] init] autorelease];
  21. }
  22. + (id)trueChecker {
  23. return [[[KSTrueExistenceChecker alloc] init] autorelease];
  24. }
  25. - (id)initWithCoder:(NSCoder *)coder {
  26. return [super init];
  27. }
  28. - (void)encodeWithCoder:(NSCoder *)coder {
  29. // Nothing to encode, but declared to show that subclasses must support
  30. // NSCoding.
  31. }
  32. // Subclasses should override.
  33. - (BOOL)exists {
  34. return NO;
  35. }
  36. // Must be defined to make falseCheckers equal
  37. - (unsigned)hash {
  38. return 0;
  39. }
  40. // Must be defined to make falseCheckers equal
  41. - (BOOL)isEqual:(id)other {
  42. if (other == self)
  43. return YES;
  44. // must be an EXACT match; don't use isKindOfClass
  45. if (!other || ([other class] != [self class]))
  46. return NO;
  47. return YES;
  48. }
  49. // Assumes all KSExistenceChecker objects are "false"
  50. - (NSString *)description {
  51. return [NSString stringWithFormat:
  52. @"<%@:%p false>", [self class], self];
  53. }
  54. @end
  55. @implementation KSPathExistenceChecker
  56. + (id)checkerWithPath:(NSString *)path {
  57. return [[[self alloc] initWithPath:path] autorelease];
  58. }
  59. - (id)init {
  60. return [self initWithPath:nil];
  61. }
  62. - (id)initWithPath:(NSString *)path {
  63. if ((self = [super init])) {
  64. path_ = [path copy];
  65. if (path_ == nil) {
  66. [self release];
  67. return nil;
  68. }
  69. }
  70. return self;
  71. }
  72. - (id)initWithCoder:(NSCoder *)coder {
  73. if ((self = [super initWithCoder:coder])) {
  74. path_ = [[coder decodeObjectForKey:@"path"] retain];
  75. }
  76. return self;
  77. }
  78. - (void)dealloc {
  79. [path_ release];
  80. [super dealloc];
  81. }
  82. - (void)encodeWithCoder:(NSCoder *)coder {
  83. [coder encodeObject:path_ forKey:@"path"];
  84. }
  85. - (unsigned)hash {
  86. return [path_ hash];
  87. }
  88. - (BOOL)isEqual:(id)other {
  89. if (other == self)
  90. return YES;
  91. if (!other || ![other isKindOfClass:[self class]])
  92. return NO;
  93. return [path_ isEqualToString:
  94. ((KSPathExistenceChecker *)other)->path_];
  95. }
  96. - (BOOL)exists {
  97. _GTMDevAssert(path_ != nil, @"path_ can't be nil");
  98. return [[NSFileManager defaultManager] fileExistsAtPath:path_];
  99. }
  100. - (NSString *)description {
  101. return [NSString stringWithFormat:
  102. @"<%@:%p path=%@>", [self class], self, path_];
  103. }
  104. - (NSString *)path {
  105. return path_;
  106. }
  107. @end
  108. @implementation KSLaunchServicesExistenceChecker
  109. + (id)checkerWithBundleID:(NSString *)bid {
  110. return [[[self alloc] initWithBundleID:bid] autorelease];
  111. }
  112. - (id)init {
  113. return [self initWithBundleID:nil];
  114. }
  115. - (id)initWithBundleID:(NSString *)bid {
  116. if ((self = [super init])) {
  117. bundleID_ = [bid copy];
  118. if (bundleID_ == nil) {
  119. [self release];
  120. return nil;
  121. }
  122. }
  123. return self;
  124. }
  125. - (id)initWithCoder:(NSCoder *)coder {
  126. if ((self = [super initWithCoder:coder])) {
  127. bundleID_ = [[coder decodeObjectForKey:@"bundle_id"] retain];
  128. }
  129. return self;
  130. }
  131. - (void)dealloc {
  132. [bundleID_ release];
  133. [super dealloc];
  134. }
  135. - (void)encodeWithCoder:(NSCoder *)coder {
  136. [coder encodeObject:bundleID_ forKey:@"bundle_id"];
  137. }
  138. - (unsigned)hash {
  139. return [bundleID_ hash];
  140. }
  141. - (BOOL)isEqual:(id)other {
  142. if (other == self)
  143. return YES;
  144. if (!other || ![other isKindOfClass:[self class]])
  145. return NO;
  146. return [bundleID_ isEqualToString:
  147. ((KSLaunchServicesExistenceChecker *)other)->bundleID_];
  148. }
  149. - (BOOL)exists {
  150. _GTMDevAssert(bundleID_ != nil, @"bundleID_ can't be nil");
  151. CFURLRef url = NULL;
  152. OSStatus err = noErr;
  153. // Lookup the URL of the application specified by bundleID_ in the LS DB
  154. err = LSFindApplicationForInfo(kLSUnknownCreator,
  155. (CFStringRef)bundleID_,
  156. NULL, NULL,
  157. &url);
  158. if (url) CFRelease(url);
  159. return err == noErr;
  160. }
  161. - (NSString *)description {
  162. return [NSString stringWithFormat:
  163. @"<%@:%p bundle=%@>", [self class], self, bundleID_];
  164. }
  165. @end
  166. @implementation KSSpotlightExistenceChecker
  167. + (id)checkerWithQuery:(NSString *)query {
  168. return [[[self alloc] initWithQuery:query] autorelease];
  169. }
  170. - (id)init {
  171. return [self initWithQuery:nil];
  172. }
  173. - (id)initWithQuery:(NSString *)query {
  174. if ((self = [super init])) {
  175. query_ = [query copy];
  176. if (query_ == nil) {
  177. [self release];
  178. return nil;
  179. }
  180. }
  181. return self;
  182. }
  183. - (void)dealloc {
  184. [query_ release];
  185. [super dealloc];
  186. }
  187. - (id)initWithCoder:(NSCoder *)coder {
  188. if ((self = [super initWithCoder:coder])) {
  189. query_ = [[coder decodeObjectForKey:@"query"] retain];
  190. }
  191. return self;
  192. }
  193. - (void)encodeWithCoder:(NSCoder *)coder {
  194. [coder encodeObject:query_ forKey:@"query"];
  195. }
  196. - (unsigned)hash {
  197. return [query_ hash];
  198. }
  199. - (BOOL)isEqual:(id)other {
  200. if (other == self)
  201. return YES;
  202. if (!other || ![other isKindOfClass:[self class]])
  203. return NO;
  204. return [query_ isEqualToString:
  205. ((KSSpotlightExistenceChecker *)other)->query_];
  206. }
  207. - (NSString *)description {
  208. return [NSString stringWithFormat:
  209. @"<%@:%p query=%@>", [self class], self, query_];
  210. }
  211. // Sends |query_| to Spotlight, and returns YES if any results are returned,
  212. // NO otherwise.
  213. - (BOOL)exists {
  214. _GTMDevAssert(query_ != nil, @"query_ can't be nil");
  215. MDQueryRef query = MDQueryCreate(kCFAllocatorDefault,
  216. (CFStringRef)query_,
  217. NULL, NULL);
  218. if (query == NULL) {
  219. GTMLoggerInfo(@"Failed to create MDQuery from %@", query_);
  220. return NO;
  221. }
  222. int count = 0;
  223. // Create and execute the query synchronously.
  224. Boolean ok = MDQueryExecute(query, kMDQuerySynchronous);
  225. if (ok) {
  226. count = MDQueryGetResultCount(query);
  227. } else {
  228. GTMLoggerInfo(@"failed to execute query\n"); // COV_NF_LINE
  229. }
  230. CFRelease(query);
  231. // Returns YES if we got any results
  232. return (count > 0);
  233. }
  234. @end
  235. @implementation KSTrueExistenceChecker
  236. - (BOOL)exists {
  237. return YES;
  238. }
  239. - (NSString *)description {
  240. return [NSString stringWithFormat:
  241. @"<%@:%p true>", [self class], self];
  242. }
  243. @end