PageRenderTime 100ms CodeModel.GetById 77ms app.highlight 20ms RepoModel.GetById 2ms app.codeStats 0ms

/core/externals/update-engine/Samples/EngineRunner/ERSelfUpdateCommand.m

http://macfuse.googlecode.com/
Objective C | 111 lines | 62 code | 29 blank | 20 comment | 11 complexity | e8a7d6f25b1e0f7aed51ac5a36cf38de 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 "ERSelfUpdateCommand.h"
 16
 17#import <sys/param.h>
 18#import <unistd.h>
 19
 20#import "ERUtilities.h"
 21#import "KSUpdateEngine.h"
 22
 23static NSString *kSelfProductID = @"EngineRunner";
 24static NSString *kSelfUpdateURL = @"http://update-engine.googlecode.com/svn/site/enginerunner.plist";
 25static NSString *kSelfVersion =
 26  CONVERT_SYMBOL_TO_NSSTRING(UPDATE_ENGINE_VERSION);
 27
 28
 29@interface ERSelfUpdateCommand (PrivateMethods)
 30
 31// Returns the path the currently running executable.
 32- (NSString *)executablePath;
 33
 34@end  // PrivateMethods
 35
 36
 37@implementation ERSelfUpdateCommand
 38
 39- (NSString *)name {
 40  return @"selfupdate";
 41}  // name
 42
 43
 44- (NSString *)blurb {
 45  return @"Update EngineRunner";
 46}  // blurb
 47
 48
 49- (NSDictionary *)optionalArguments {
 50  return [NSDictionary dictionaryWithObjectsAndKeys:
 51                       @"Version to claim that we are", @"version",
 52                       @"ProductID to claim that we are", @"productid",
 53                       @"Server URL", @"url",
 54                       @"Existence checker path", @"xcpath",
 55                       nil];
 56}  // requiredArguments
 57
 58
 59- (NSString *)executablePath {
 60  // NSProcessInfo's zeroth argument is a full path to the executable,
 61  // but it's not documented as actually doing that.  For now, assume
 62  // that it works.  If the behavior changes in the future, you should
 63  // be able to get the executable's directory by getting the current
 64  // working directory (since EngineRunner doesn't change it) and
 65  // then attaching argv[0].
 66
 67  NSProcessInfo *processInfo = [NSProcessInfo processInfo];
 68  NSString *command = [[processInfo arguments] objectAtIndex:0];
 69
 70  return command;
 71
 72}  // executablePath
 73
 74
 75- (BOOL)runWithArguments:(NSDictionary *)args {
 76  NSString *productID = [args valueForKey:@"productid"];
 77  NSString *version = [args valueForKey:@"version"];
 78  NSString *urlstring = [args valueForKey:@"url"];
 79  NSString *xcpath = [args valueForKey:@"xcpath"];
 80
 81  if (productID == nil) productID = kSelfProductID;
 82  if (version == nil) version = kSelfVersion;
 83  if (urlstring == nil) urlstring = kSelfUpdateURL;
 84  if (xcpath == nil) xcpath = [self executablePath];
 85
 86  NSArray *argv = [[NSProcessInfo processInfo] arguments];
 87  NSString *me = [argv objectAtIndex:0];
 88
 89  NSArray *arguments = [NSArray arrayWithObjects:
 90                                @"run",
 91                                @"-productid", productID,
 92                                @"-version", version,
 93                                @"-url", urlstring,
 94                                @"-xcpath", xcpath,
 95                                nil];
 96
 97  NSTask *task = [NSTask launchedTaskWithLaunchPath:me
 98                                          arguments:arguments];
 99  [task waitUntilExit];
100
101  if ([task terminationStatus] != 0) {
102    fprintf(stdout, "Could not perform self-update.  Check out the log at\n");
103    fprintf(stdout, "~/Library/Logs/EngineRunner.log for more information.\n");
104    return NO;
105  } else {
106    return YES;
107  }
108
109}  // runWithArguments
110
111@end  // ERSelfUpdateCommand