Skip to content

Commit

Permalink
#74642 Refactored and changed root only for mobile
Browse files Browse the repository at this point in the history
  • Loading branch information
mkhamoyan authored and github-actions committed Sep 27, 2022
1 parent 7b12e0a commit 3021845
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@
<ItemGroup Condition="'$(TargetPlatformIdentifier)' != ''">
<Compile Include="System\IO\IsolatedStorage\IsolatedStorageException.cs" />
<Compile Include="System\IO\IsolatedStorage\IsolatedStorageFile.cs" />
<Compile Condition="'$(TargetPlatformIdentifier)' == 'Android' or '$(TargetPlatformIdentifier)' == 'iOS' or '$(TargetPlatformIdentifier)' == 'MacCatalyst' or '$(TargetPlatformIdentifier)' == 'tvOS'"
Include="System\IO\IsolatedStorage\IsolatedStorageFile.AnyMobile.cs" />
<Compile Condition="'$(TargetPlatformIdentifier)' != 'Android' and '$(TargetPlatformIdentifier)' != 'iOS' and '$(TargetPlatformIdentifier)' != 'MacCatalyst' and '$(TargetPlatformIdentifier)' != 'tvOS'"
Include="System\IO\IsolatedStorage\IsolatedStorageFile.NonMobile.cs" />
<Compile Include="System\IO\IsolatedStorage\IsolatedStorageFileStream.cs" />
<Compile Include="System\IO\IsolatedStorage\IsolatedStorage.cs" />
<Compile Include="System\IO\IsolatedStorage\IsolatedStorageScope.cs" />
<Compile Include="System\IO\IsolatedStorage\IsolatedStorageScope.cs" />
<Compile Include="System\IO\IsolatedStorage\Helper.cs" />
<Compile Include="System\IO\IsolatedStorage\Helper.Win32Unix.cs" />
<Compile Include="System\IO\IsolatedStorage\INormalizeForIsolatedStorage.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal static string GetDataDirectory(IsolatedStorageScope scope)

[UnconditionalSuppressMessage("SingleFile", "IL3000:Avoid accessing Assembly file path when publishing as a single file",
Justification = "Code handles single-file deployment by using the information of the .exe file")]
internal static void GetDefaultIdentityAndHash(out object identity, char separator)
internal static void GetDefaultIdentityAndHash(out object identity, out string hash, char separator)
{
// In .NET Framework IsolatedStorage uses identity from System.Security.Policy.Evidence to build
// the folder structure on disk. It would use the "best" available evidence in this order:
Expand All @@ -56,9 +56,10 @@ internal static void GetDefaultIdentityAndHash(out object identity, char separat
{
AssemblyName assemblyName = assembly.GetName();

string hash = IdentityHelper.GetNormalizedStrongNameHash(assemblyName)!;
hash = IdentityHelper.GetNormalizedStrongNameHash(assemblyName)!;
if (hash != null)
{
hash = "StrongName" + separator + hash;
identity = assemblyName;
return;
}
Expand All @@ -73,7 +74,9 @@ internal static void GetDefaultIdentityAndHash(out object identity, char separat
location = Environment.ProcessPath;
if (string.IsNullOrEmpty(location))
throw new IsolatedStorageException(SR.IsolatedStorage_Init);
identity = new Uri(location);
Uri locationUri = new Uri(location);
hash = "Url" + separator + IdentityHelper.GetNormalizedUriHash(locationUri);
identity = locationUri;
}

internal static string GetRandomDirectory(string rootDirectory, IsolatedStorageScope scope)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ public virtual bool IncreaseQuotaTo(long newQuotaSize)

public abstract void Remove();

internal string? IdentityHash
{
get; private set;
}

protected void InitStore(IsolatedStorageScope scope, Type appEvidenceType)
{
InitStore(scope, null, appEvidenceType);
Expand All @@ -137,7 +142,7 @@ protected void InitStore(IsolatedStorageScope scope, Type? domainEvidenceType, T
VerifyScope(scope);
Scope = scope;

Helper.GetDefaultIdentityAndHash(out object identity, SeparatorInternal);
Helper.GetDefaultIdentityAndHash(out object identity, out string hash, SeparatorInternal);

if (Helper.IsApplication(scope))
{
Expand All @@ -148,10 +153,13 @@ protected void InitStore(IsolatedStorageScope scope, Type? domainEvidenceType, T
if (Helper.IsDomain(scope))
{
_domainIdentity = identity;
hash = string.Create(null, stackalloc char[128], $"{hash}{SeparatorExternal}{hash}");
}

_assemblyIdentity = identity;
}

IdentityHash = hash;
}

private static void VerifyScope(IsolatedStorageScope scope)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.IO.IsolatedStorage
{
public sealed partial class IsolatedStorageFile : IsolatedStorage, IDisposable
{
private string GetIsolatesStorageRoot()
{
return Helper.GetRootDirectory(Scope);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text;

namespace System.IO.IsolatedStorage
{
public sealed partial class IsolatedStorageFile : IsolatedStorage, IDisposable
{
private string GetIsolatesStorageRoot()
{
StringBuilder root = new StringBuilder(Helper.GetRootDirectory(Scope));
root.Append(SeparatorExternal);
root.Append(IdentityHash);

return root.ToString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ internal IsolatedStorageFile(IsolatedStorageScope scope)
// Evidence isn't currently available: https://github.com/dotnet/runtime/issues/18208
// public static IsolatedStorageFile GetStore(IsolatedStorageScope scope, Evidence domainEvidence, Type domainEvidenceType, Evidence assemblyEvidence, Type assemblyEvidenceType) { return default(IsolatedStorageFile); }

// InitStore will set up the IdentityHash
InitStore(scope, null, null);

StringBuilder sb = new StringBuilder(Helper.GetRootDirectory(scope));
StringBuilder sb = new StringBuilder(GetIsolatesStorageRoot());
sb.Append(SeparatorExternal);

if (Helper.IsApplication(scope))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
<Compile Include="System\IO\IsolatedStorage\MoveFileTests.cs" />
<Compile Include="System\IO\IsolatedStorage\OpenFileTests.cs" />
<Compile Include="System\IO\IsolatedStorage\TestHelper.cs" />
<Compile Condition="'$(TargetPlatformIdentifier)' == 'Android' or '$(TargetPlatformIdentifier)' == 'iOS' or '$(TargetPlatformIdentifier)' == 'MacCatalyst' or '$(TargetPlatformIdentifier)' == 'tvOS'"
Include="System\IO\IsolatedStorage\TestHelper.AnyMobile.cs" />
<Compile Condition="'$(TargetPlatformIdentifier)' != 'Android' and '$(TargetPlatformIdentifier)' != 'iOS' and '$(TargetPlatformIdentifier)' != 'MacCatalyst' and '$(TargetPlatformIdentifier)' != 'tvOS'"
Include="System\IO\IsolatedStorage\TestHelper.NonMobile.cs" />
<Compile Include="System\IO\IsolatedStorage\RemoveTests.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,23 @@ public partial class HelperTests
public void GetDefaultIdentityAndHash()
{
object identity;
Helper.GetDefaultIdentityAndHash(out identity, '.');
string hash;
Helper.GetDefaultIdentityAndHash(out identity, out hash, '.');

Assert.NotNull(identity);
Assert.NotNull(hash);

// We lie about the identity type when creating the folder structure as we're emulating the Evidence types
// we don't have available in .NET Standard. We don't serialize the actual identity object, so the desktop
// implementation will work with locations built off the hash.
if (identity.GetType() != typeof(Uri))
if (identity.GetType() == typeof(Uri))
{
Assert.StartsWith(@"Url.", hash);
}
else if (identity.GetType() != typeof(Uri))
{
Assert.IsType<AssemblyName>(identity);
Assert.StartsWith(@"StrongName.", hash);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Reflection;
using System.Collections.Generic;

namespace System.IO.IsolatedStorage
{
public static partial class TestHelper
{
static TestHelper()
{
s_rootDirectoryProperty = typeof(IsolatedStorageFile).GetProperty("RootDirectory", BindingFlags.NonPublic | BindingFlags.Instance);

s_roots = new List<string>();

string hash;
object identity;
Helper.GetDefaultIdentityAndHash(out identity, out hash, '.');

string userRoot = Helper.GetDataDirectory(IsolatedStorageScope.User);
string randomUserRoot = Helper.GetRandomDirectory(userRoot, IsolatedStorageScope.User);
s_roots.Add(randomUserRoot);

// Application scope doesn't go under a random dir
s_roots.Add(userRoot);

// https://github.com/dotnet/runtime/issues/2092
// https://github.com/dotnet/runtime/issues/21742
if (OperatingSystem.IsWindows()
&& !PlatformDetection.IsInAppContainer)
{
s_roots.Add(Helper.GetDataDirectory(IsolatedStorageScope.Machine));
}

// We don't expose Roaming yet
// Helper.GetDataDirectory(IsolatedStorageScope.Roaming);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Reflection;
using System.Collections.Generic;

namespace System.IO.IsolatedStorage
{
public static partial class TestHelper
{
static TestHelper()
{
s_rootDirectoryProperty = typeof(IsolatedStorageFile).GetProperty("RootDirectory", BindingFlags.NonPublic | BindingFlags.Instance);

s_roots = new List<string>();

string hash;
object identity;
Helper.GetDefaultIdentityAndHash(out identity, out hash, '.');

string userRoot = Helper.GetDataDirectory(IsolatedStorageScope.User);
string randomUserRoot = Helper.GetRandomDirectory(userRoot, IsolatedStorageScope.User);
s_roots.Add(Path.Combine(randomUserRoot, hash));
s_roots.Add(randomUserRoot);

// Application scope doesn't go under a random dir
s_roots.Add(Path.Combine(userRoot, hash));
s_roots.Add(userRoot);

// https://github.com/dotnet/runtime/issues/2092
// https://github.com/dotnet/runtime/issues/21742
if (OperatingSystem.IsWindows()
&& !PlatformDetection.IsInAppContainer)
{
s_roots.Add(Helper.GetDataDirectory(IsolatedStorageScope.Machine));
}

// We don't expose Roaming yet
// Helper.GetDataDirectory(IsolatedStorageScope.Roaming);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,11 @@

namespace System.IO.IsolatedStorage
{
public static class TestHelper
public static partial class TestHelper
{
private static PropertyInfo s_rootDirectoryProperty;
private static List<string> s_roots;

static TestHelper()
{
s_rootDirectoryProperty = typeof(IsolatedStorageFile).GetProperty("RootDirectory", BindingFlags.NonPublic | BindingFlags.Instance);

s_roots = new List<string>();

object identity;
Helper.GetDefaultIdentityAndHash(out identity, '.');

string userRoot = Helper.GetDataDirectory(IsolatedStorageScope.User);
string randomUserRoot = Helper.GetRandomDirectory(userRoot, IsolatedStorageScope.User);
s_roots.Add(randomUserRoot);

// Application scope doesn't go under a random dir
s_roots.Add(userRoot);

// https://github.com/dotnet/runtime/issues/2092
// https://github.com/dotnet/runtime/issues/21742
if (OperatingSystem.IsWindows()
&& !PlatformDetection.IsInAppContainer)
{
s_roots.Add(Helper.GetDataDirectory(IsolatedStorageScope.Machine));
}

// We don't expose Roaming yet
// Helper.GetDataDirectory(IsolatedStorageScope.Roaming);
}

/// <summary>
/// Where the user's files go
/// </summary>
Expand Down

0 comments on commit 3021845

Please sign in to comment.