Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Method for removing a file with optional authorization.

  • Loading branch information...
commit cd93eb9098a2d687fc7114112fb15c778a562979 1 parent 4c765d8
@uliwitness uliwitness authored
Showing with 67 additions and 1 deletion.
  1. +2 −0  SUPlainInstallerInternals.h
  2. +65 −1 SUPlainInstallerInternals.m
View
2  SUPlainInstallerInternals.h
@@ -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
View
66 SUPlainInstallerInternals.m
@@ -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,
@@ -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);
Please sign in to comment.
Something went wrong with that request. Please try again.