You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[mono] Implement Environment.GetFolderPath for Android #35490
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Globalization;
public static class Program
{
public static int Main(string[] args)
{
int result = 42;
var values = Enum.GetValues(typeof(global::System.Environment.SpecialFolder));
Console.WriteLine($"Start logging ==========");
foreach (var sf in values)
{
var path = global::System.Environment.GetFolderPath((global::System.Environment.SpecialFolder)sf);
if (!string.IsNullOrEmpty(path))
{
Console.WriteLine($"{sf} = {path};");
}
}
Console.WriteLine($"Finish logging ==========");
return result;
}
}
I get the following output in the log:
07-26 12:22:13.823 7529 7548 I DOTNET : Start logging ==========
07-26 12:22:13.862 7529 7548 I DOTNET : MyDocuments = /data/user/0/net.dot.Android.Device_Emulator.EnvironmentGetFolderPath.Test/files;
07-26 12:22:13.862 7529 7548 I DOTNET : MyDocuments = /data/user/0/net.dot.Android.Device_Emulator.EnvironmentGetFolderPath.Test/files;
07-26 12:22:13.866 7529 7548 I DOTNET : UserProfile = /data/user/0/net.dot.Android.Device_Emulator.EnvironmentGetFolderPath.Test/files;
07-26 12:22:13.866 7529 7548 I DOTNET : Finish logging ==========
caseSpecialFolder.MyDocuments:// same value as Personal
returnhome;
which returns PersistedFiles.GetHomeDirectory();)
HOME is the only case that's already fine, I don't think it matters whether that's fed by $HOME or by getpwuid_r. The issue is CommonApplicationData and friends
Looks like src/libraries/System.Private.CoreLib/src/System/Environment.GetFolderPathCore.Unix.cs is relying on XDG (Linux desktop spec) directory discovery on non-OSX
Okay, even with the XDG code paths, there are a few entries which should be specified but aren't on Android, e.g.:
case SpecialFolder.CommonApplicationData: return "/usr/share";
case SpecialFolder.CommonTemplates: return "/usr/share/templates";
Which has me thinking the problem is GetFolderPathCore is called, but failing on Android (i.e. because SpecialFolderOption.None is being used, which means in the event that the folder doesn't already exist then return string.Empty)
What is the correct behaviour for Android? Unilaterally create the directories, or simply don't try to validate them? What did Legacy MonoDroid do? @jonpryor/@jonathanpeppers?
Unilaterally create the directories, or simply don't try to validate them?
I definitely wouldn't try to create the directories. The value for CommonTemplates (/usr/share/templates) does not exist in my running Android emulator.
Activity
MaximLipnin commentedon Jul 26, 2021
When running the following as a functional test
I get the following output in the log:
It seems to be expected behavior.
MaximLipnin commentedon Jul 26, 2021
On the other hand, looking at the entire list of special folder, it still consists mostly of empty values:
EgorBo commentedon Jul 26, 2021
Related PR but for iOS: #34022
directhex commentedon Aug 20, 2021
Is HOME set on Android? It would explain why UserProfile and MyDocuments are set, but nothing else is
1 remaining item
directhex commentedon Aug 23, 2021
It would also explain why CI never caught this - the only SpecialFolders we validate in CI are "is MyDocuments equal to $HOME"
am11 commentedon Aug 23, 2021
Maybe we can use:
runtime/src/libraries/System.Private.CoreLib/src/System/IO/PersistedFiles.Unix.cs
Line 82 in 57bfe47
$HOMEenv var directly for Android?it does fallback to
getpwuid_rfor$HOME-less case:runtime/src/libraries/System.Private.CoreLib/src/System/IO/PersistedFiles.Unix.cs
Line 125 in 57bfe47
directhex commentedon Aug 23, 2021
As per #35490 (comment) the only case which is already fine is HOME (i.e. MyDocuments and UserProfile share a code path in
runtime/src/libraries/System.Private.CoreLib/src/System/Environment.GetFolderPathCore.Unix.cs
Lines 87 to 89 in 39135a4
PersistedFiles.GetHomeDirectory();)HOME is the only case that's already fine, I don't think it matters whether that's fed by
$HOMEor bygetpwuid_r. The issue is CommonApplicationData and friendsam11 commentedon Aug 23, 2021
Aha, okay! I understood that the other cases are broken because
$HOMEis not set, but apparently it's not the case.directhex commentedon Aug 23, 2021
Looks like src/libraries/System.Private.CoreLib/src/System/Environment.GetFolderPathCore.Unix.cs is relying on XDG (Linux desktop spec) directory discovery on non-OSX
directhex commentedon Aug 23, 2021
Okay, even with the XDG code paths, there are a few entries which should be specified but aren't on Android, e.g.:
Which has me thinking the problem is GetFolderPathCore is called, but failing on Android (i.e. because
SpecialFolderOption.Noneis being used, which means in the event that the folder doesn't already exist then return string.Empty)What is the correct behaviour for Android? Unilaterally create the directories, or simply don't try to validate them? What did Legacy MonoDroid do? @jonpryor/@jonathanpeppers?
jonathanpeppers commentedon Aug 23, 2021
The code sample above, legacy Xamarin.Android prints:
I definitely wouldn't try to create the directories. The value for
CommonTemplates(/usr/share/templates) does not exist in my running Android emulator.directhex commentedon Aug 23, 2021
So how about on Android, we switch from
SpecialFolderOption.NonetoSpecialFolderOption.DoNotVerify?Don't blank out SpecialFolders on Android if they don't already exist (…