From 1ff099811e9800945a560d96df063b74a76ef5f5 Mon Sep 17 00:00:00 2001 From: Paulo Fierro Date: Thu, 22 Jun 2017 12:57:32 -0500 Subject: [PATCH] Added support for overwriting existing symlinks Also added errors if these operations fail. --- SSZipArchive/SSZipArchive.m | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/SSZipArchive/SSZipArchive.m b/SSZipArchive/SSZipArchive.m index 578a724..ea0d75a 100755 --- a/SSZipArchive/SSZipArchive.m +++ b/SSZipArchive/SSZipArchive.m @@ -464,13 +464,34 @@ NSString *const SSZipArchiveErrorDomain = @"SSZipArchiveErrorDomain"; [destinationPath appendString:@((const char*)buffer)]; } + // Check if the symlink exists and delete it if we're overwriting + if (overwrite) + { + if ([fileManager fileExistsAtPath:fullPath]) + { + NSError *error = nil; + BOOL success = [fileManager removeItemAtPath:fullPath error:&error]; + if (!success) + { + NSString *message = [NSString stringWithFormat:@"Failed to delete existing symbolic link at \"%@\"", error.localizedDescription]; + NSLog(@"[SSZipArchive] %@", message); + success = NO; + unzippingError = [NSError errorWithDomain:SSZipArchiveErrorDomain code:error.code userInfo:@{NSLocalizedDescriptionKey: message}]; + } + } + } + // Create the symbolic link (making sure it stays relative if it was relative before) int symlinkError = symlink([destinationPath cStringUsingEncoding:NSUTF8StringEncoding], [fullPath cStringUsingEncoding:NSUTF8StringEncoding]); if (symlinkError != 0) { - NSLog(@"Failed to create symbolic link at \"%@\" to \"%@\". symlink() error code: %d", fullPath, destinationPath, errno); + // Bubble the error up to the completion handler + NSString *message = [NSString stringWithFormat:@"Failed to create symbolic link at \"%@\" to \"%@\" - symlink() error code: %d", fullPath, destinationPath, errno]; + NSLog(@"[SSZipArchive] %@", message); + success = NO; + unzippingError = [NSError errorWithDomain:NSPOSIXErrorDomain code:symlinkError userInfo:@{NSLocalizedDescriptionKey: message}]; } }