Skip to content

Commit

Permalink
Method for removing a file with optional authorization.
Browse files Browse the repository at this point in the history
  • Loading branch information
uliwitness committed Dec 14, 2009
1 parent 4c765d8 commit cd93eb9
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
2 changes: 2 additions & 0 deletions SUPlainInstallerInternals.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
+ (NSString *)temporaryNameForPath:(NSString *)path;
+ (BOOL)copyPathWithAuthentication:(NSString *)src overPath:(NSString *)dst temporaryName:(NSString *)tmp error:(NSError **)error;
+ (void)_movePathToTrash:(NSString *)path;
+ (BOOL)_removeFileAtPath:(NSString *)path error: (NSError**)error;
+ (BOOL)_removeFileAtPathWithForcedAuthentication:(NSString *)src error:(NSError **)error;
@end

#endif
66 changes: 65 additions & 1 deletion SUPlainInstallerInternals.m
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ + (BOOL)_movePathWithForcedAuthentication:(NSString *)src toPath:(NSString *)dst

AuthorizationRef auth = NULL;
OSStatus authStat = errAuthorizationDenied;
while (authStat == errAuthorizationDenied) {
while( authStat == errAuthorizationDenied )
{
authStat = AuthorizationCreate(NULL,
kAuthorizationEmptyEnvironment,
kAuthorizationFlagDefaults,
Expand Down Expand Up @@ -321,6 +322,69 @@ + (BOOL)_movePathWithForcedAuthentication:(NSString *)src toPath:(NSString *)dst
return res;
}


+ (BOOL)_removeFileAtPathWithForcedAuthentication:(NSString *)src error:(NSError **)error
{
// *** MUST BE SAFE TO CALL ON NON-MAIN THREAD!

const char* srcPath = [src fileSystemRepresentation];

AuthorizationRef auth = NULL;
OSStatus authStat = errAuthorizationDenied;
while( authStat == errAuthorizationDenied )
{
authStat = AuthorizationCreate(NULL,
kAuthorizationEmptyEnvironment,
kAuthorizationFlagDefaults,
&auth);
}

BOOL res = NO;
if (authStat == errAuthorizationSuccess)
{
res = YES;

if( res ) // If there's something at our tmp path (previous failed update or whatever) delete that first.
{
const char* rmParams[] = { "-rf", srcPath, NULL };
res = AuthorizationExecuteWithPrivilegesAndWait( auth, "/bin/rm", kAuthorizationFlagDefaults, rmParams );
if( !res )
SULog(@"Can't remove destination file");
}

AuthorizationFree(auth, 0);

if (!res)
{
// Something went wrong somewhere along the way, but we're not sure exactly where.
NSString *errorMessage = [NSString stringWithFormat:@"Authenticated file remove from %@ failed.", src];
if (error != NULL)
*error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUAuthenticationFailure userInfo:[NSDictionary dictionaryWithObject:errorMessage forKey:NSLocalizedDescriptionKey]];
}
}
else
{
if (error != NULL)
*error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUAuthenticationFailure userInfo:[NSDictionary dictionaryWithObject:@"Couldn't get permission to authenticate." forKey:NSLocalizedDescriptionKey]];
}
return res;
}

+ (BOOL)_removeFileAtPath:(NSString *)path error: (NSError**)error
{
BOOL success = YES;
#if MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4
if( ![[NSFileManager defaultManager] removeFileAtPath: path handler: nil] )
#else
if( ![[NSFileManager defaultManager] removeItemAtPath: path error: NULL] )
#endif
{
success = [self _removeFileAtPathWithForcedAuthentication: path error: error];
}

return success;
}

+ (void)_movePathToTrash:(NSString *)path
{
//SULog(@"Moving %@ to the trash.", path);
Expand Down

0 comments on commit cd93eb9

Please sign in to comment.