Changeset 1258

Show
Ignore:
Timestamp:
01/31/10 18:42:24 (1 month ago)
Author:
astrange
Message:

Cleanup install/remove methods.

- Remove malloc and copy of a constant string.
- Use NSFileManager instead of stat().
- Properly unset some variables.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/CPFPerianPrefPaneController.m

    r1257 r1258  
    2121#import "CPFPerianPrefPaneController.h" 
    2222#import "UpdateCheckerAppDelegate.h" 
    23 #include <sys/stat.h> 
    2423 
    2524#define AC3DynamicRangeKey CFSTR("dynamicRange") 
     
    455454#pragma mark Install/Uninstall 
    456455 
    457 /* Shamelessly ripped from Sparkle */ 
     456/* Shamelessly ripped from Sparkle (and now different) */ 
    458457- (BOOL)_extractArchivePath:archivePath toDestination:(NSString *)destination finalPath:(NSString *)finalPath 
    459458{ 
    460         BOOL ret = NO, oldExist = NO; 
    461         struct stat sb; 
    462          
    463         if(stat([finalPath fileSystemRepresentation], &sb) == 0) 
    464                 oldExist = YES; 
    465          
    466         char *buf = NULL; 
     459        BOOL ret = NO, oldExist; 
     460        NSFileManager *defaultFileManager = [NSFileManager defaultManager]; 
     461        char *cmd; 
     462         
     463        oldExist = [defaultFileManager fileExistsAtPath:finalPath]; 
     464         
    467465        if(oldExist) 
    468                 asprintf(&buf, 
    469                                  "mv -f \"$DST_COMPONENT\" \"$TMP_PATH\" && " 
    470                                  "ditto -x -k --rsrc \"$SRC_ARCHIVE\" \"$DST_PATH\" && " 
    471                                  "rm -rf \"$TMP_PATH\""); 
    472         else 
    473                 asprintf(&buf, 
    474                                  "mkdir -p \"$DST_PATH\" && " 
    475                                  "ditto -x -k --rsrc \"$SRC_ARCHIVE\" \"$DST_PATH\""); 
    476         if(!buf) 
    477         { 
    478                 [errorString appendFormat:NSLocalizedString(@"Could not allocate memory for extraction command\n", @"")]; 
    479                 return FALSE; 
    480         } 
     466                cmd = "mv -f \"$DST_COMPONENT\" \"$TMP_PATH\" && " 
     467                "ditto -x -k --rsrc \"$SRC_ARCHIVE\" \"$DST_PATH\" && " 
     468                "rm -rf \"$TMP_PATH\""; 
     469        else 
     470                cmd = "mkdir -p \"$DST_PATH\" && " 
     471                "ditto -x -k --rsrc \"$SRC_ARCHIVE\" \"$DST_PATH\""; 
    481472         
    482473        setenv("SRC_ARCHIVE", [archivePath fileSystemRepresentation], 1); 
     
    484475        setenv("DST_COMPONENT", [finalPath fileSystemRepresentation], 1); 
    485476        setenv("TMP_PATH", [[finalPath stringByAppendingPathExtension:@"old"] fileSystemRepresentation], 1); 
    486  
    487         int status = system(buf); 
     477         
     478        int status = system(cmd); 
    488479        if(WIFEXITED(status) && WEXITSTATUS(status) == 0) 
    489480                ret = YES; 
    490481        else 
    491482                [errorString appendFormat:NSLocalizedString(@"Extraction for %@ failed\n", @""), archivePath]; 
    492  
    493         free(buf); 
     483         
    494484        unsetenv("SRC_ARCHIVE"); 
    495         unsetenv("$DST_COMPONENT"); 
     485        unsetenv("DST_COMPONENT"); 
    496486        unsetenv("TMP_PATH"); 
    497487        unsetenv("DST_PATH"); 
     
    501491- (BOOL)_authenticatedExtractArchivePath:(NSString *)archivePath toDestination:(NSString *)destination finalPath:(NSString *)finalPath 
    502492{ 
    503         BOOL ret = NO, oldExist = NO
    504         struct stat sb
    505         if(stat([finalPath fileSystemRepresentation], &sb) == 0) 
    506                oldExist = YES; 
    507          
    508         char *buf = NULL; 
     493        BOOL ret = NO, oldExist
     494        NSFileManager *defaultFileManager = [NSFileManager defaultManager]
     495        char *cmd; 
     496         
     497        oldExist = [defaultFileManager fileExistsAtPath:finalPath]; 
     498         
    509499        if(oldExist) 
    510                 asprintf(&buf, 
    511                                  "mv -f \"$DST_COMPONENT\" \"$TMP_PATH\" && " 
    512                                  "ditto -x -k --rsrc \"$SRC_ARCHIVE\" \"$DST_PATH\" && " 
    513                                  "rm -rf \"$TMP_PATH\" && " 
    514                                  "chown -R root:admin \"$DST_COMPONENT\""); 
    515         else 
    516                 asprintf(&buf, 
    517                                  "mkdir -p \"$DST_PATH\" && " 
    518                                  "ditto -x -k --rsrc \"$SRC_ARCHIVE\" \"$DST_PATH\" && " 
    519                                  "chown -R root:admin \"$DST_COMPONENT\""); 
    520         if(!buf) 
    521         { 
    522                 [errorString appendFormat:NSLocalizedString(@"Could not allocate memory for extraction command\n", @"")]; 
    523                 return FALSE; 
    524         } 
     500                cmd = "mv -f \"$DST_COMPONENT\" \"$TMP_PATH\" && " 
     501                "ditto -x -k --rsrc \"$SRC_ARCHIVE\" \"$DST_PATH\" && " 
     502                "rm -rf \"$TMP_PATH\" && " 
     503                "chown -R root:admin \"$DST_COMPONENT\""; 
     504        else 
     505                cmd = "mkdir -p \"$DST_PATH\" && " 
     506                "ditto -x -k --rsrc \"$SRC_ARCHIVE\" \"$DST_PATH\" && " 
     507                "chown -R root:admin \"$DST_COMPONENT\""; 
    525508         
    526509        setenv("SRC_ARCHIVE", [archivePath fileSystemRepresentation], 1); 
     
    529512        setenv("DST_PATH", [destination fileSystemRepresentation], 1); 
    530513         
    531         char* arguments[] = { "-c", buf, NULL }; 
     514        char* const arguments[] = { "-c", cmd, NULL }; 
    532515        if(AuthorizationExecuteWithPrivileges(auth, "/bin/sh", kAuthorizationFlagDefaults, arguments, NULL) == errAuthorizationSuccess) 
    533516        { 
     
    542525                [errorString appendFormat:NSLocalizedString(@"Authentication failed for extraction for %@\n", @""), archivePath]; 
    543526         
    544         free(buf); 
    545527        unsetenv("SRC_ARCHIVE"); 
    546         unsetenv("$DST_COMPONENT"); 
     528        unsetenv("DST_COMPONENT"); 
    547529        unsetenv("TMP_PATH"); 
    548530        unsetenv("DST_PATH"); 
     
    553535{ 
    554536        BOOL ret = NO; 
    555         struct stat sb; 
    556         if(stat([componentPath fileSystemRepresentation], &sb) != 0) 
    557                 /* No error, just forget it */ 
     537        NSFileManager *defaultFileManager = [NSFileManager defaultManager]; 
     538         
     539        if(![defaultFileManager fileExistsAtPath:componentPath]) 
     540        /* No error, just forget it */ 
    558541                return FALSE; 
    559542         
    560         char *buf = NULL; 
    561         asprintf(&buf, 
    562                          "rm -rf \"$COMP_PATH\""); 
    563         if(!buf) 
    564         { 
    565                 [errorString appendFormat:NSLocalizedString(@"Could not allocate memory for removal command\n", @"")]; 
    566                 return FALSE; 
    567         } 
     543        char *cmd = "rm -rf \"$COMP_PATH\""; 
    568544         
    569545        setenv("COMP_PATH", [componentPath fileSystemRepresentation], 1); 
    570546         
    571         char* arguments[] = { "-c", buf, NULL }; 
     547        char* const arguments[] = { "-c", cmd, NULL }; 
    572548        if(AuthorizationExecuteWithPrivileges(auth, "/bin/sh", kAuthorizationFlagDefaults, arguments, NULL) == errAuthorizationSuccess) 
    573549        { 
     
    581557        else 
    582558                [errorString appendFormat:NSLocalizedString(@"Authentication failed for removal for %@\n", @""), componentPath]; 
    583         free(buf); 
     559         
    584560        unsetenv("COMP_PATH"); 
    585561        return ret; 
    586562} 
    587  
    588563 
    589564- (BOOL)installArchive:(NSString *)archivePath forPiece:(NSString *)component type:(ComponentType)type withMyVersion:(NSString *)myVersion