Skip to content

Fix quarantineProperty in URLResourceValues #5125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions stdlib/public/SDK/Foundation/URL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,22 @@ public struct URLResourceValues {
/// The quarantine properties as defined in LSQuarantine.h. To remove quarantine information from a file, pass `nil` as the value when setting this property.
@available(OSX 10.10, *)
public var quarantineProperties: [String : Any]? {
get { return _get(.quarantinePropertiesKey) }
set { _set(.quarantinePropertiesKey, newValue: newValue as NSObject?) }
get {
// If a caller has caused us to stash NSNull in the dictionary (via set), make sure to return nil instead of NSNull
if let isNull = _values[.quarantinePropertiesKey] as? NSNull {
return nil
} else {
return _values[.quarantinePropertiesKey] as? [String : Any]
}
}
set {
if let v = newValue {
_set(.quarantinePropertiesKey, newValue: newValue as NSObject?)
} else {
// Set to NSNull, a special case for deleting quarantine properties
_set(.quarantinePropertiesKey, newValue: NSNull())
}
}
}
#endif

Expand Down
63 changes: 61 additions & 2 deletions test/stdlib/TestURL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class TestURL : TestURLSuper {
expectTrue(false, "Unable to write data")
}

// Modify an existing resource values
// Modify an existing resource value
do {
var resourceValues = try file.resourceValues(forKeys: [.nameKey])
expectNotNil(resourceValues.name)
Expand All @@ -64,9 +64,65 @@ class TestURL : TestURLSuper {
try file.setResourceValues(resourceValues)
} catch {
expectTrue(false, "Unable to set resources")
}
}
}

#if os(OSX)
func testQuarantineProperties() {
// Test the quarantine stuff; it has special logic
if #available(OSX 10.11, iOS 9.0, *) {
// Create a temporary file
var file = URL(fileURLWithPath: NSTemporaryDirectory())
let name = "my_great_file" + UUID().uuidString
file.appendPathComponent(name)
let data = Data(bytes: [1, 2, 3, 4, 5])
do {
try data.write(to: file)
} catch {
expectTrue(false, "Unable to write data")
}

// Set the quarantine info on a file
do {
var resourceValues = URLResourceValues()
resourceValues.quarantineProperties = ["LSQuarantineAgentName" : "TestURL"]
try file.setResourceValues(resourceValues)
} catch {
expectTrue(false, "Unable to set quarantine info")
}

// Get the quarantine info back
do {
var resourceValues = try file.resourceValues(forKeys: [.quarantinePropertiesKey])
expectEqual(resourceValues.quarantineProperties?["LSQuarantineAgentName"] as? String, "TestURL")
} catch {
expectTrue(false, "Unable to get quarantine info")
}

// Clear the quarantine info
do {
var resourceValues = URLResourceValues()
resourceValues.quarantineProperties = nil // this effectively sets a flag
try file.setResourceValues(resourceValues)

// Make sure that the resourceValues property returns nil
expectNil(resourceValues.quarantineProperties)
} catch {
expectTrue(false, "Unable to clear quarantine info")
}

// Get the quarantine info back again
do {
var resourceValues = try file.resourceValues(forKeys: [.quarantinePropertiesKey])
expectNil(resourceValues.quarantineProperties)
} catch {
expectTrue(false, "Unable to get quarantine info after clearing")
}

}
}
#endif

func testMoreSetProperties() {
// Create a temporary file
var file = URL(fileURLWithPath: NSTemporaryDirectory())
Expand Down Expand Up @@ -327,6 +383,9 @@ var URLTests = TestSuite("TestURL")
URLTests.test("testBasics") { TestURL().testBasics() }
URLTests.test("testProperties") { TestURL().testProperties() }
URLTests.test("testSetProperties") { TestURL().testSetProperties() }
#if os(OSX)
URLTests.test("testQuarantineProperties") { TestURL().testQuarantineProperties() }
#endif
URLTests.test("testMoreSetProperties") { TestURL().testMoreSetProperties() }
URLTests.test("testURLComponents") { TestURL().testURLComponents() }
URLTests.test("testURLResourceValues") { TestURL().testURLResourceValues() }
Expand Down