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