Bug 37993 - NSFileManager.DefaultManager.GetAttributes always returns null
Summary: NSFileManager.DefaultManager.GetAttributes always returns null
Status: RESOLVED FIXED
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: XI 9.4 (iOS 9.2)
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-01-25 14:07 UTC by Adam Hartley [MSFT]
Modified: 2016-01-28 19:00 UTC (History)
5 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Sample (37.42 KB, application/zip)
2016-01-25 14:07 UTC, Adam Hartley [MSFT]
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Adam Hartley [MSFT] 2016-01-25 14:07:02 UTC
Created attachment 14717 [details]
Sample

## Steps to reproduce

Attached is a C# sample project and an Objective-C sample project that I translated.

1. Build and run the C# project (tested on the iPhone 6 simulator)
2. Tap the button

## Expected result

No exception should be thrown

## Actual result

Exception thrown as NSFileManager.DefaultManager.GetAttributes returns null

Additionally, NSFileManager.DefaultManager.SetAttributes appears to set the attributes but it always returns success (even if the attribute is invalid or the file path is invalid) and the out parameter is always null.

## Notes

This does not happen in the Objective-C sample

=== Xamarin Studio ===

Version 5.10.1 (build 6)
Installation UUID: 11a26d86-0d21-407a-8da9-8c197ecc0ad1
Runtime:
	Mono 4.2.1 (explicit/6dd2d0d)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 402010102

=== Xamarin.Profiler ===

Not Installed

=== Apple Developer Tools ===

Xcode 7.2 (9548)
Build 7C68

=== Xamarin.iOS ===

Version: 9.4.0.0 (Business Edition)
Hash: 7322991
Branch: master
Build date: 2015-12-08 16:20:29-0500

=== Xamarin.Android ===

Version: 6.0.0.34 (Business Edition)
Android SDK: /Users/Adam/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		4.0.3 (API level 15)
		4.4   (API level 19)
		5.0   (API level 21)
		5.1   (API level 22)
		6.0   (API level 23)

SDK Tools Version: 24.4.1
SDK Platform Tools Version: 23.1
SDK Build Tools Version: 23.0.1

Java SDK: /usr
java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)

=== Xamarin Android Player ===

Version: 0.6.5
Location: /Applications/Xamarin Android Player.app

=== Xamarin.Mac ===

Version: 2.4.0.109 (Business Edition)

=== Build Information ===

Release ID: 510010006
Git revision: 0b60eecdb531933734519c13257d16a780274aab
Build date: 2015-12-04 20:28:20-05
Xamarin addins: 9876fd7c9837977178411ec7375b4352c0a0d6af
Build lane: monodevelop-lion-cycle6-baseline

=== Operating System ===

Mac OS X 10.11.3
Darwin Adams-Retina-MacBook-Pro.local 15.3.0 Darwin Kernel Version 15.3.0
    Thu Dec 10 18:40:58 PST 2015
    root:xnu-3248.30.4~1/RELEASE_X86_64 x86_64
Comment 1 Rolf Bjarne Kvinge [MSFT] 2016-01-28 19:00:40 UTC
The reason GetAttributes returns null is because you're passing an URI, filePath is:

    "file:///Users/rolf/Library/Developer/CoreSimulator/Devices/526B6624-F1E7-4005-BF1E-28585DE6567F/data/Containers/Bundle/Application/42CECFAF-76A2-4250-8F56-2A8AE724F680/249114.app/TestFile.rtf"

which is because you're calling ToString on the NSUrl:

    NSBundle.MainBundle.GetUrlForResource("TestFile", "rtf").ToString()

if you instead call Path:

    NSBundle.MainBundle.GetUrlForResource("TestFile", "rtf").Path

your sample works.

In fact you're calling "path" in the ObjC sample:

    [[[NSBundle mainBundle] URLForResource:@"TestFile" withExtension:@"rtf"] path]

which is why it works there.

SetAttributes also returns failure if I pass a bogus path:

    result = NSFileManager.DefaultManager.SetAttributes(attributes, filePath + "x", out error);

However I did find one issue: we're not persisting the ProtectionKey value correctly (when converting from the strongly typed NSFileAttributes instance to the NSDictionary that the native API accepts, we don't handle ProtectionKey correctly). This is now fixed (maccore/master: 831d8e291ff7db0a044f081db4835caf857c622c).