/core/externals/update-engine/Core/KSExistenceChecker.m
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 15#import "KSExistenceChecker.h" 16 17 18// Subclass that returns an existence checker that always says YES. 19@interface KSTrueExistenceChecker : KSExistenceChecker 20@end 21 22 23@implementation KSExistenceChecker 24 25+ (id)falseChecker { 26 return [[[self alloc] init] autorelease]; 27} 28 29+ (id)trueChecker { 30 return [[[KSTrueExistenceChecker alloc] init] autorelease]; 31} 32 33- (id)initWithCoder:(NSCoder *)coder { 34 return [super init]; 35} 36 37- (void)encodeWithCoder:(NSCoder *)coder { 38 // Nothing to encode, but declared to show that subclasses must support 39 // NSCoding. 40} 41 42// Subclasses should override. 43- (BOOL)exists { 44 return NO; 45} 46 47// Must be defined to make falseCheckers equal 48- (unsigned)hash { 49 return 0; 50} 51 52// Must be defined to make falseCheckers equal 53- (BOOL)isEqual:(id)other { 54 if (other == self) 55 return YES; 56 // must be an EXACT match; don't use isKindOfClass 57 if (!other || ([other class] != [self class])) 58 return NO; 59 return YES; 60} 61 62// Assumes all KSExistenceChecker objects are "false" 63- (NSString *)description { 64 return [NSString stringWithFormat: 65 @"<%@:%p false>", [self class], self]; 66} 67 68@end 69 70 71@implementation KSPathExistenceChecker 72 73+ (id)checkerWithPath:(NSString *)path { 74 return [[[self alloc] initWithPath:path] autorelease]; 75} 76 77- (id)init { 78 return [self initWithPath:nil]; 79} 80 81- (id)initWithPath:(NSString *)path { 82 if ((self = [super init])) { 83 path_ = [path copy]; 84 if (path_ == nil) { 85 [self release]; 86 return nil; 87 } 88 } 89 return self; 90} 91 92- (id)initWithCoder:(NSCoder *)coder { 93 if ((self = [super initWithCoder:coder])) { 94 path_ = [[coder decodeObjectForKey:@"path"] retain]; 95 } 96 return self; 97} 98 99- (void)dealloc { 100 [path_ release]; 101 [super dealloc]; 102} 103 104- (void)encodeWithCoder:(NSCoder *)coder { 105 [coder encodeObject:path_ forKey:@"path"]; 106} 107 108- (unsigned)hash { 109 return [path_ hash]; 110} 111 112- (BOOL)isEqual:(id)other { 113 if (other == self) 114 return YES; 115 if (!other || ![other isKindOfClass:[self class]]) 116 return NO; 117 return [path_ isEqualToString: 118 ((KSPathExistenceChecker *)other)->path_]; 119} 120 121- (BOOL)exists { 122 _GTMDevAssert(path_ != nil, @"path_ can't be nil"); 123 return [[NSFileManager defaultManager] fileExistsAtPath:path_]; 124} 125 126- (NSString *)description { 127 return [NSString stringWithFormat: 128 @"<%@:%p path=%@>", [self class], self, path_]; 129} 130 131- (NSString *)path { 132 return path_; 133} 134 135@end 136 137 138@implementation KSLaunchServicesExistenceChecker 139 140+ (id)checkerWithBundleID:(NSString *)bid { 141 return [[[self alloc] initWithBundleID:bid] autorelease]; 142} 143 144- (id)init { 145 return [self initWithBundleID:nil]; 146} 147 148- (id)initWithBundleID:(NSString *)bid { 149 if ((self = [super init])) { 150 bundleID_ = [bid copy]; 151 if (bundleID_ == nil) { 152 [self release]; 153 return nil; 154 } 155 } 156 return self; 157} 158 159- (id)initWithCoder:(NSCoder *)coder { 160 if ((self = [super initWithCoder:coder])) { 161 bundleID_ = [[coder decodeObjectForKey:@"bundle_id"] retain]; 162 } 163 return self; 164} 165 166- (void)dealloc { 167 [bundleID_ release]; 168 [super dealloc]; 169} 170 171- (void)encodeWithCoder:(NSCoder *)coder { 172 [coder encodeObject:bundleID_ forKey:@"bundle_id"]; 173} 174 175- (unsigned)hash { 176 return [bundleID_ hash]; 177} 178 179- (BOOL)isEqual:(id)other { 180 if (other == self) 181 return YES; 182 if (!other || ![other isKindOfClass:[self class]]) 183 return NO; 184 return [bundleID_ isEqualToString: 185 ((KSLaunchServicesExistenceChecker *)other)->bundleID_]; 186} 187 188- (BOOL)exists { 189 _GTMDevAssert(bundleID_ != nil, @"bundleID_ can't be nil"); 190 CFURLRef url = NULL; 191 OSStatus err = noErr; 192 // Lookup the URL of the application specified by bundleID_ in the LS DB 193 err = LSFindApplicationForInfo(kLSUnknownCreator, 194 (CFStringRef)bundleID_, 195 NULL, NULL, 196 &url); 197 if (url) CFRelease(url); 198 return err == noErr; 199} 200 201- (NSString *)description { 202 return [NSString stringWithFormat: 203 @"<%@:%p bundle=%@>", [self class], self, bundleID_]; 204} 205 206 207@end 208 209 210@implementation KSSpotlightExistenceChecker 211 212+ (id)checkerWithQuery:(NSString *)query { 213 return [[[self alloc] initWithQuery:query] autorelease]; 214} 215 216- (id)init { 217 return [self initWithQuery:nil]; 218} 219 220- (id)initWithQuery:(NSString *)query { 221 if ((self = [super init])) { 222 query_ = [query copy]; 223 if (query_ == nil) { 224 [self release]; 225 return nil; 226 } 227 } 228 return self; 229} 230 231- (void)dealloc { 232 [query_ release]; 233 [super dealloc]; 234} 235 236- (id)initWithCoder:(NSCoder *)coder { 237 if ((self = [super initWithCoder:coder])) { 238 query_ = [[coder decodeObjectForKey:@"query"] retain]; 239 } 240 return self; 241} 242 243- (void)encodeWithCoder:(NSCoder *)coder { 244 [coder encodeObject:query_ forKey:@"query"]; 245} 246 247- (unsigned)hash { 248 return [query_ hash]; 249} 250 251- (BOOL)isEqual:(id)other { 252 if (other == self) 253 return YES; 254 if (!other || ![other isKindOfClass:[self class]]) 255 return NO; 256 return [query_ isEqualToString: 257 ((KSSpotlightExistenceChecker *)other)->query_]; 258} 259 260- (NSString *)description { 261 return [NSString stringWithFormat: 262 @"<%@:%p query=%@>", [self class], self, query_]; 263} 264 265 266// Sends |query_| to Spotlight, and returns YES if any results are returned, 267// NO otherwise. 268- (BOOL)exists { 269 _GTMDevAssert(query_ != nil, @"query_ can't be nil"); 270 271 MDQueryRef query = MDQueryCreate(kCFAllocatorDefault, 272 (CFStringRef)query_, 273 NULL, NULL); 274 if (query == NULL) { 275 GTMLoggerInfo(@"Failed to create MDQuery from %@", query_); 276 return NO; 277 } 278 279 int count = 0; 280 // Create and execute the query synchronously. 281 Boolean ok = MDQueryExecute(query, kMDQuerySynchronous); 282 if (ok) { 283 count = MDQueryGetResultCount(query); 284 } else { 285 GTMLoggerInfo(@"failed to execute query\n"); // COV_NF_LINE 286 } 287 288 CFRelease(query); 289 290 // Returns YES if we got any results 291 return (count > 0); 292} 293 294@end 295 296 297@implementation KSTrueExistenceChecker 298 299- (BOOL)exists { 300 return YES; 301} 302 303- (NSString *)description { 304 return [NSString stringWithFormat: 305 @"<%@:%p true>", [self class], self]; 306} 307 308@end