Skip to content
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

Move Environment.iOS.cs under libraries #70226

Merged
merged 1 commit into from
Jun 4, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -2242,7 +2242,7 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Environment.FreeBSD.cs" Condition="'$(TargetsFreeBSD)' == 'true'" />
<Compile Include="$(MSBuildThisFileDirectory)System\Environment.Linux.cs" Condition="'$(TargetsLinux)' == 'true'" />
<Compile Include="$(MSBuildThisFileDirectory)System\Environment.OSX.cs" Condition="'$(TargetsOSX)' == 'true' or '$(TargetsMacCatalyst)' == 'true'" />
<Compile Include="$(MSBuildThisFileDirectory)System\Environment.iOS.cs" Condition="'$(TargetsiOS)' == 'true' or '$(TargetstvOS)' == 'true'" />
<Compile Include="$(MSBuildThisFileDirectory)System\Environment.iOS.cs" Condition="'$(IsiOSLike)' == 'true'" />
<Compile Include="$(MSBuildThisFileDirectory)System\Environment.OSVersion.Unix.cs" Condition="'$(IsOSXLike)' != 'true'" />
<Compile Include="$(MSBuildThisFileDirectory)System\Environment.SunOS.cs" Condition="'$(Targetsillumos)' == 'true' or '$(TargetsSolaris)' == 'true'" />
<Compile Include="$(MSBuildThisFileDirectory)System\TimeZoneInfo.FullGlobalizationData.Unix.cs" Condition="'$(UseMinimalGlobalizationData)' != 'true'" />
Expand Down Expand Up @@ -2274,6 +2274,14 @@
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)System\IO\FileStatus.SetTimes.OSX.cs" />
</ItemGroup>
<ItemGroup Condition="'$(IsiOSLike)' == 'true'">
<Compile Include="$(CommonPath)Interop\OSX\System.Native\Interop.SearchPath.cs">
<Link>Common\Interop\OSX\Interop.SearchPath.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\OSX\System.Native\Interop.SearchPathTempDirectory.cs">
<Link>Common\Interop\OSX\Interop.SearchPathTempDirectory.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup Condition="'$(TargetsMacCatalyst)' == 'true'">
<Compile Include="$(CommonPath)Interop\OSX\System.Native\Interop.iOSSupportVersion.cs" Link="Common\Interop\OSX\System.Native\Interop.iOSSupportVersion.cs" />
</ItemGroup>
Expand Down
111 changes: 111 additions & 0 deletions src/libraries/System.Private.CoreLib/src/System/Environment.iOS.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,124 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.IO;
using System.Threading;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using NSSearchPathDirectory = Interop.Sys.NSSearchPathDirectory;

namespace System
{
public static partial class Environment
{
#if !TARGET_MACCATALYST
// iOS/tvOS aren't allowed to call libproc APIs so return 0 here, this also matches what we returned in earlier releases
public static long WorkingSet => 0;
#endif

private static Dictionary<SpecialFolder, string>? s_specialFolders;

private static string GetFolderPathCore(SpecialFolder folder, SpecialFolderOption option)
{
if (s_specialFolders == null)
{
Interlocked.CompareExchange(ref s_specialFolders, new Dictionary<SpecialFolder, string>(), null);
}

string? path;
lock (s_specialFolders)
{
if (!s_specialFolders.TryGetValue(folder, out path))
{
path = GetSpecialFolder(folder) ?? string.Empty;
s_specialFolders[folder] = path;
}
}
return path;
}

private static string? GetSpecialFolder(SpecialFolder folder)
{
switch (folder)
{
case SpecialFolder.Personal:
case SpecialFolder.LocalApplicationData:
return CombineDocumentDirectory(string.Empty);

case SpecialFolder.ApplicationData:
// note: at first glance that looked like a good place to return NSLibraryDirectory
// but it would break isolated storage for existing applications
return CombineDocumentDirectory(".config");

case SpecialFolder.Resources:
return Interop.Sys.SearchPath(NSSearchPathDirectory.NSLibraryDirectory); // older (8.2 and previous) would return String.Empty

case SpecialFolder.Desktop:
case SpecialFolder.DesktopDirectory:
return Path.Combine(GetFolderPathCore(SpecialFolder.Personal, SpecialFolderOption.None), "Desktop");

case SpecialFolder.MyMusic:
return Path.Combine(GetFolderPathCore(SpecialFolder.Personal, SpecialFolderOption.None), "Music");

case SpecialFolder.MyPictures:
return Path.Combine(GetFolderPathCore(SpecialFolder.Personal, SpecialFolderOption.None), "Pictures");

case SpecialFolder.Templates:
return CombineDocumentDirectory("Templates");

case SpecialFolder.MyVideos:
return Path.Combine(GetFolderPathCore(SpecialFolder.Personal, SpecialFolderOption.None), "Videos");

case SpecialFolder.CommonTemplates:
return "/usr/share/templates";

case SpecialFolder.Fonts:
return CombineDocumentDirectory(".fonts");

case SpecialFolder.Favorites:
return CombineSearchPath(NSSearchPathDirectory.NSLibraryDirectory, "Favorites");

case SpecialFolder.ProgramFiles:
return Interop.Sys.SearchPath(NSSearchPathDirectory.NSApplicationDirectory);

case SpecialFolder.InternetCache:
return Interop.Sys.SearchPath(NSSearchPathDirectory.NSCachesDirectory);

case SpecialFolder.UserProfile:
return GetEnvironmentVariable("HOME");

case SpecialFolder.CommonApplicationData:
return "/usr/share";

default:
return string.Empty;
}

static string CombineSearchPath(NSSearchPathDirectory searchPath, string subdirectory)
{
string? path = Interop.Sys.SearchPath(searchPath);
return path != null ?
Path.Combine(path, subdirectory) :
string.Empty;
}

static string CombineDocumentDirectory(string subdirectory)
{
#if TARGET_TVOS
string? path = CombineSearchPath(NSSearchPathDirectory.NSLibraryDirectory, Path.Combine("Caches", "Documents"));
// Special version of CombineSearchPath which creates the path if needed.
// This isn't needed for "real" search paths which always exist, but on tvOS
// the base path is really a subdirectory we define rather than an OS directory.
// In order to not treat Directory.Exists(SpecialFolder.ApplicationData) differently
// on tvOS, guarantee that it exists by creating it here
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
path = Path.Combine(path, subdirectory);
#else
string? path = CombineSearchPath(NSSearchPathDirectory.NSDocumentDirectory, subdirectory);
#endif
return path;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -279,15 +279,6 @@
<Link>Common\Interop\Unix\System.Native\Interop.GetEnviron.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup Condition="'$(TargetsMacCatalyst)' == 'true' or '$(TargetsiOS)' == 'true' or '$(TargetstvOS)' == 'true'">
<Compile Include="$(BclSourcesRoot)\System\Environment.iOS.cs" />
<Compile Include="$(CommonPath)Interop\OSX\System.Native\Interop.SearchPath.cs">
<Link>Common\Interop\OSX\Interop.SearchPath.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\OSX\System.Native\Interop.SearchPath.iOS.cs">
<Link>Common\Interop\OSX\Interop.SearchPath.iOS.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup Condition="'$(TargetsBrowser)' == 'true'">
<Compile Include="$(BclSourcesRoot)\System\Threading\TimerQueue.Browser.Mono.cs" />
</ItemGroup>
Expand Down
119 changes: 0 additions & 119 deletions src/mono/System.Private.CoreLib/src/System/Environment.iOS.cs

This file was deleted.