From af563e5dd5aca9e7aac6c7a2764261ac7db9c90c Mon Sep 17 00:00:00 2001 From: Mostafa Berg Date: Mon, 27 Feb 2017 14:19:42 +0100 Subject: [PATCH] +Fixed issue #74 by defaulting to permission 644 if no permissions or invalid permissions are set +Also added test case to verify resolution with a zip file created on WinRAR for Windows --- Zip.xcodeproj/project.pbxproj | 4 ++++ Zip/Zip.swift | 15 ++++++++++----- ZipTests/ZipTests.swift | 18 ++++++++++++++++++ ZipTests/unsupported_permissions.zip | Bin 0 -> 287 bytes 4 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 ZipTests/unsupported_permissions.zip diff --git a/Zip.xcodeproj/project.pbxproj b/Zip.xcodeproj/project.pbxproj index 6dfd208..1b15319 100644 --- a/Zip.xcodeproj/project.pbxproj +++ b/Zip.xcodeproj/project.pbxproj @@ -8,6 +8,7 @@ /* Begin PBXBuildFile section */ 303B4F6E1E4CBE5000DC1633 /* permissions.zip in Resources */ = {isa = PBXBuildFile; fileRef = 303B4F6D1E4CBE5000DC1633 /* permissions.zip */; }; + 305237181E64595700CA46D1 /* unsupported_permissions.zip in Resources */ = {isa = PBXBuildFile; fileRef = 305237171E64595700CA46D1 /* unsupported_permissions.zip */; }; 342545901CE525B200336074 /* Zip.swift in Sources */ = {isa = PBXBuildFile; fileRef = 347E3AD71C1E04C900A11FD3 /* Zip.swift */; }; 342545921CE525B200336074 /* unzip.c in Sources */ = {isa = PBXBuildFile; fileRef = 342FC0EB1C5044DC0023A3C3 /* unzip.c */; }; 342545941CE525B200336074 /* QuickZip.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3443A3F51C4AB8A3004AD173 /* QuickZip.swift */; }; @@ -71,6 +72,7 @@ /* Begin PBXFileReference section */ 303B4F6D1E4CBE5000DC1633 /* permissions.zip */ = {isa = PBXFileReference; lastKnownFileType = archive.zip; path = permissions.zip; sourceTree = ""; }; + 305237171E64595700CA46D1 /* unsupported_permissions.zip */ = {isa = PBXFileReference; lastKnownFileType = archive.zip; path = unsupported_permissions.zip; sourceTree = ""; }; 342545B51CE525B200336074 /* Zip.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Zip.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 342FC0E71C5044DC0023A3C3 /* crypt.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = crypt.h; sourceTree = ""; }; 342FC0E81C5044DC0023A3C3 /* ioapi.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = ioapi.c; sourceTree = ""; }; @@ -196,6 +198,7 @@ 347E3A821C1DFFB500A11FD3 /* ZipTests */ = { isa = PBXGroup; children = ( + 305237171E64595700CA46D1 /* unsupported_permissions.zip */, 303B4F6D1E4CBE5000DC1633 /* permissions.zip */, 34940A221C58876200D41574 /* 3crBXeO.gif */, 34940A231C58876200D41574 /* kYkLkPf.gif */, @@ -418,6 +421,7 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + 305237181E64595700CA46D1 /* unsupported_permissions.zip in Resources */, 303B4F6E1E4CBE5000DC1633 /* permissions.zip in Resources */, 34940A251C58876200D41574 /* kYkLkPf.gif in Resources */, 3443A3FD1C4AD199004AD173 /* bb8.zip in Resources */, diff --git a/Zip/Zip.swift b/Zip/Zip.swift index c806b0d..80722dc 100644 --- a/Zip/Zip.swift +++ b/Zip/Zip.swift @@ -199,11 +199,16 @@ public class Zip { } //Set file permissions from current fileInfo - let permissions = (fileInfo.external_fa >> 16) & 0x1FF - do { - try fileManager.setAttributes([.posixPermissions : permissions], ofItemAtPath: fullPath) - } catch let error { - print("Failed to set permissions to file \(fullPath)") + if fileInfo.external_fa != 0 { + let permissions = (fileInfo.external_fa >> 16) & 0x1FF + //We will devifne a valid permission range between Owner read only to full access + if permissions >= 0o400 && permissions <= 0o777 { + do { + try fileManager.setAttributes([.posixPermissions : permissions], ofItemAtPath: fullPath) + } catch { + print("Failed to set permissions to file \(fullPath), error: \(error)") + } + } } ret = unzGoToNextFile(zip) diff --git a/ZipTests/ZipTests.swift b/ZipTests/ZipTests.swift index e26fe8c..282a3e3 100644 --- a/ZipTests/ZipTests.swift +++ b/ZipTests/ZipTests.swift @@ -217,6 +217,24 @@ class ZipTests: XCTestCase { } } + func testUnzipWithUnsupportedPermissions() { + do { + let permissionsURL = Bundle(for: ZipTests.self).url(forResource: "unsupported_permissions", withExtension: "zip")! + let unzipDestination = try Zip.quickUnzipFile(permissionsURL) + print(unzipDestination) + let fileManager = FileManager.default + let permission644 = unzipDestination.appendingPathComponent("unsupported_permission").appendingPathExtension("txt") + do { + let attributes644 = try fileManager.attributesOfItem(atPath: permission644.path) + XCTAssertEqual(attributes644[.posixPermissions] as? Int, 0o644) + } catch { + XCTFail("Failed to get file attributes \(error)") + } + } catch { + XCTFail("Failed extract unsupported_permissions.zip") + } + } + func testUnzipPermissions() { do { let permissionsURL = Bundle(for: ZipTests.self).url(forResource: "permissions", withExtension: "zip")! diff --git a/ZipTests/unsupported_permissions.zip b/ZipTests/unsupported_permissions.zip new file mode 100644 index 0000000000000000000000000000000000000000..5d3dbda8c22b0b3452eba9a946cd332ee44266b1 GIT binary patch literal 287 zcmWIWW@Zs#U|`^2D9DTUx?(0hF%`(G2VyB8F3l@0Ehxw@DoIU=FGwxQ%`7g?%+J#+ zsVI>>eUbOD0*~v((0dGl{}<_9V!e~l5_+KHIY;X6Wq)mVeGlz95hm)|u|4bf!(1O# z(}UL2zCP)eUZnA9%CghX*>&o!u~;6S-69+GnlPfONN2)Pt$LG` zp!~`j#sF_dCV2*2&Qk$85Cjy!BtFM-LDeuaNHCbKE427A;S%GoY3t;X=>Tt5HjqJ# MK$ruh=YmZI0Q3G~<^TWy literal 0 HcmV?d00001 -- 2.26.2