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

Change IsolatedStorageFile path for mobile #83380

Merged
merged 15 commits into from
May 11, 2023
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ namespace System.IO.IsolatedStorage
{
internal static partial class Helper
{
// we're using a different directory name for compatibility with legacy Xamarin
public const string IsolatedStorageDirectoryName = ".isolated-storage";

internal static string GetRandomDirectory(string rootDirectory, IsolatedStorageScope scope)
{
// In legacy Xamarin we didn't have a random directory inside of the isolated storage root for each app,
// we tried to preserve that in https://github.com/dotnet/runtime/pull/75541 but the fix wasn't complete enough
// and we still created random directories when not using the Roaming scope.
//
// Since we shipped that behavior as part of .NET 7 we can't change this now or upgraded apps wouldn't find their files anymore.
// We need to look for an existing random directory first before using the plain root directory.
return GetExistingRandomDirectory(rootDirectory) ?? rootDirectory;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +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.Threading;

namespace System.IO.IsolatedStorage
{
internal static partial class Helper
{
public const string IsolatedStorageDirectoryName = "IsolatedStorage";

internal static string GetRandomDirectory(string rootDirectory, IsolatedStorageScope scope)
{
string? randomDirectory = GetExistingRandomDirectory(rootDirectory);
if (string.IsNullOrEmpty(randomDirectory))
{
using (Mutex m = CreateMutexNotOwned(rootDirectory))
{
if (!m.WaitOne())
{
throw new IsolatedStorageException(SR.IsolatedStorage_Init);
}

try
{
randomDirectory = GetExistingRandomDirectory(rootDirectory);
if (string.IsNullOrEmpty(randomDirectory))
{
// Someone else hasn't created the directory before we took the lock
randomDirectory = Path.Combine(rootDirectory, Path.GetRandomFileName(), Path.GetRandomFileName());
CreateDirectory(randomDirectory, scope);
}
}
finally
{
m.ReleaseMutex();
}
}
}

return randomDirectory;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,38 +79,6 @@ internal static void GetDefaultIdentityAndHash(out object identity, out string h
identity = locationUri;
}

internal static string GetRandomDirectory(string rootDirectory, IsolatedStorageScope scope)
{
string? randomDirectory = GetExistingRandomDirectory(rootDirectory);
if (string.IsNullOrEmpty(randomDirectory))
{
using (Mutex m = CreateMutexNotOwned(rootDirectory))
{
if (!m.WaitOne())
{
throw new IsolatedStorageException(SR.IsolatedStorage_Init);
}

try
{
randomDirectory = GetExistingRandomDirectory(rootDirectory);
if (string.IsNullOrEmpty(randomDirectory))
{
// Someone else hasn't created the directory before we took the lock
randomDirectory = Path.Combine(rootDirectory, Path.GetRandomFileName(), Path.GetRandomFileName());
CreateDirectory(randomDirectory, scope);
}
}
finally
{
m.ReleaseMutex();
}
}
}

return randomDirectory;
}

internal static string? GetExistingRandomDirectory(string rootDirectory)
{
// Look for an existing random directory at the given root
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ internal static partial class Helper

/// <summary>
/// The full root directory is the relevant special folder from Environment.GetFolderPath() plus IsolatedStorageDirectoryName
/// and a set of random directory names if not roaming. (The random directories aren't created for WinRT as
/// the FolderPath locations for WinRT are app isolated already.)
/// and a set of random directory names if not roaming. (The random directories aren't created for Android/iOS as
/// the FolderPath locations for Android/iOS are app isolated already.)
///
/// Examples:
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'android' or '$(TargetPlatformIdentifier)' == 'ios' or '$(TargetPlatformIdentifier)' == 'maccatalyst' or '$(TargetPlatformIdentifier)' == 'tvos'">
<Compile Include="..\src\System\IO\IsolatedStorage\Helper.AnyMobile.cs" />
<Compile Include="System\IO\IsolatedStorage\TestHelper.AnyMobile.cs" />
<Compile Include="System\IO\IsolatedStorage\HelperTests.AnyMobile.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetPlatformIdentifier)' != 'android' and '$(TargetPlatformIdentifier)' != 'ios' and '$(TargetPlatformIdentifier)' != 'maccatalyst' and '$(TargetPlatformIdentifier)' != 'tvos' and '$(TargetPlatformIdentifier)' != ''">
<Compile Include="..\src\System\IO\IsolatedStorage\Helper.NonMobile.cs" />
<Compile Include="System\IO\IsolatedStorage\TestHelper.NonMobile.cs" />
<Compile Include="System\IO\IsolatedStorage\HelperTests.NonMobile.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(LibrariesProjectRoot)System.DirectoryServices\src\System.DirectoryServices.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Xunit;

namespace System.IO.IsolatedStorage
{
public partial class HelperTests
{
[Theory, InlineData(IsolatedStorageScope.User)]
public void GetRandomDirectory(IsolatedStorageScope scope)
{
using (var temp = new TempDirectory())
{
string randomDir = Helper.GetRandomDirectory(temp.Path, scope);
Assert.True(Directory.Exists(randomDir.Replace(Helper.IsolatedStorageDirectoryName, "")));
}
}

[Theory,
InlineData(IsolatedStorageScope.Assembly),
InlineData(IsolatedStorageScope.Assembly | IsolatedStorageScope.Roaming),
InlineData(IsolatedStorageScope.User)
]
public void GetRandomDirectoryWithExistingDir(IsolatedStorageScope scope)
{
using (var temp = new TempDirectory())
{
Assert.Null(Helper.GetExistingRandomDirectory(temp.Path));

string randomPath = Path.Combine(temp.Path, Path.GetRandomFileName(), Path.GetRandomFileName());
Directory.CreateDirectory(randomPath);
Assert.Equal(randomPath, Helper.GetExistingRandomDirectory(temp.Path));
Assert.Equal(Helper.GetRandomDirectory(temp.Path, scope), Helper.GetExistingRandomDirectory(temp.Path));
}
}

[Theory,
InlineData(IsolatedStorageScope.Assembly),
InlineData(IsolatedStorageScope.Assembly | IsolatedStorageScope.Roaming),
InlineData(IsolatedStorageScope.User)
]
public void GetRandomDirectoryWithNotExistingDir(IsolatedStorageScope scope)
{
using (var temp = new TempDirectory())
{
Assert.Null(Helper.GetExistingRandomDirectory(temp.Path));
Assert.Equal(Helper.GetRandomDirectory(temp.Path, scope), Helper.GetDataDirectory(scope));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Xunit;

namespace System.IO.IsolatedStorage
{
public partial class HelperTests
{
[Theory,
InlineData(IsolatedStorageScope.User),
InlineData(IsolatedStorageScope.Machine),
]
public void GetRandomDirectory(IsolatedStorageScope scope)
{
using (var temp = new TempDirectory())
{
string randomDir = Helper.GetRandomDirectory(temp.Path, scope);
Assert.True(Directory.Exists(randomDir));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,5 @@ public void GetExistingRandomDirectory()
Assert.Equal(randomPath, Helper.GetExistingRandomDirectory(temp.Path));
}
}

[Theory,
InlineData(IsolatedStorageScope.User),
InlineData(IsolatedStorageScope.Machine),
]
public void GetRandomDirectory(IsolatedStorageScope scope)
{
using (var temp = new TempDirectory())
{
string randomDir = Helper.GetRandomDirectory(temp.Path, scope);
Assert.True(Directory.Exists(randomDir));
}
}
}
}