Replace any potential usage of Environment.CurrentDirectory with a new RuntimeInfo.StartupDirectory

Using `Environment.CurrentDirectory` for storing / reading files is dangerous as the current directory is mutable and can be changed when performing a certain operation (like opening solutions in roslyn type reference builder for example).
This commit is contained in:
Salman Ahmed 2020-05-31 02:18:07 +03:00
parent e64e44ecf9
commit 82fe99cf4a
No known key found for this signature in database
GPG Key ID: ED81FD33FD9B58BC
5 changed files with 17 additions and 16 deletions

View File

@ -33,13 +33,11 @@ public static int Main(string[] args)
if (args.Length > 0 && args[0].Contains('.')) // easy way to check for a file import in args
{
var importer = new ArchiveImportIPCChannel(host);
// Restore the cwd so relative paths given at the command line work correctly
Directory.SetCurrentDirectory(cwd);
foreach (var file in args)
{
Console.WriteLine(@"Importing {0}", file);
if (!importer.ImportAsync(Path.GetFullPath(file)).Wait(3000))
if (!importer.ImportAsync(Path.GetFullPath(file, cwd)).Wait(3000))
throw new TimeoutException(@"IPC took too long to send");
}

View File

@ -7,6 +7,7 @@
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
using osu.Framework;
using osu.Framework.Allocation;
using osu.Framework.Configuration;
using osu.Framework.Platform;
@ -35,8 +36,7 @@ public void TestDefaultDirectory()
var osu = loadOsu(host);
var storage = osu.Dependencies.Get<Storage>();
string defaultStorageLocation = Path.Combine(Environment.CurrentDirectory, "headless", nameof(TestDefaultDirectory));
string defaultStorageLocation = RuntimeInfo.StartupStorage.GetFullPath(Path.Combine("headless", nameof(TestDefaultDirectory)));
Assert.That(storage.GetFullPath("."), Is.EqualTo(defaultStorageLocation));
}
finally
@ -46,17 +46,17 @@ public void TestDefaultDirectory()
}
}
private string customPath => Path.Combine(Environment.CurrentDirectory, "custom-path");
private string customPath { get; } = RuntimeInfo.StartupStorage.GetFullPath("custom-path");
[Test]
public void TestCustomDirectory()
{
using (var host = new HeadlessGameHost(nameof(TestCustomDirectory)))
{
string headlessPrefix = Path.Combine("headless", nameof(TestCustomDirectory));
string defaultStorageLocation = RuntimeInfo.StartupStorage.GetFullPath(Path.Combine("headless", nameof(TestCustomDirectory)));
// need access before the game has constructed its own storage yet.
Storage storage = new DesktopStorage(headlessPrefix, host);
Storage storage = new DesktopStorage(defaultStorageLocation, host);
// manual cleaning so we can prepare a config file.
storage.DeleteDirectory(string.Empty);
@ -84,10 +84,10 @@ public void TestSubDirectoryLookup()
{
using (var host = new HeadlessGameHost(nameof(TestSubDirectoryLookup)))
{
string headlessPrefix = Path.Combine("headless", nameof(TestSubDirectoryLookup));
string defaultStorageLocation = RuntimeInfo.StartupStorage.GetFullPath(Path.Combine("headless", nameof(TestSubDirectoryLookup)));
// need access before the game has constructed its own storage yet.
Storage storage = new DesktopStorage(headlessPrefix, host);
Storage storage = new DesktopStorage(defaultStorageLocation, host);
// manual cleaning so we can prepare a config file.
storage.DeleteDirectory(string.Empty);
@ -136,7 +136,7 @@ public void TestMigration()
// for testing nested files are not ignored (only top level)
host.Storage.GetStorageForDirectory("test-nested").GetStorageForDirectory("cache");
string defaultStorageLocation = Path.Combine(Environment.CurrentDirectory, "headless", nameof(TestMigration));
string defaultStorageLocation = RuntimeInfo.StartupStorage.GetFullPath(Path.Combine("headless", nameof(TestMigration)));
Assert.That(storage.GetFullPath("."), Is.EqualTo(defaultStorageLocation));

View File

@ -3,6 +3,7 @@
using System.IO;
using NUnit.Framework;
using osu.Framework;
using osu.Framework.IO.Stores;
namespace osu.Game.Tests.Resources
@ -20,10 +21,10 @@ public static string GetTestBeatmapForImport(bool virtualTrack = false)
var temp = Path.GetTempFileName() + ".osz";
using (var stream = GetTestBeatmapStream(virtualTrack))
using (var newFile = File.Create(temp))
using (var newFile = RuntimeInfo.StartupStorage.GetStream(temp, FileAccess.Write))
stream.CopyTo(newFile);
Assert.IsTrue(File.Exists(temp));
Assert.IsTrue(RuntimeInfo.StartupStorage.Exists(temp));
return temp;
}
}

View File

@ -6,6 +6,7 @@
using System.IO;
using System.Linq;
using System.Reflection;
using osu.Framework;
using osu.Framework.Logging;
using osu.Framework.Platform;
using osu.Game.Database;
@ -153,14 +154,14 @@ private void loadFromDisk()
{
try
{
string[] files = Directory.GetFiles(Environment.CurrentDirectory, $"{ruleset_library_prefix}.*.dll");
var files = RuntimeInfo.StartupStorage.GetFiles($"{ruleset_library_prefix}.*.dll");
foreach (string file in files.Where(f => !Path.GetFileName(f).Contains("Tests")))
loadRulesetFromFile(file);
}
catch (Exception e)
{
Logger.Error(e, $"Could not load rulesets from directory {Environment.CurrentDirectory}");
Logger.Error(e, $"Could not load rulesets from directory {RuntimeInfo.StartupDirectory}");
}
}

View File

@ -6,6 +6,7 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using osu.Framework;
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Track;
@ -118,7 +119,7 @@ public virtual void RecycleLocalStorage()
}
}
localStorage = new Lazy<Storage>(() => new NativeStorage($"{GetType().Name}-{Guid.NewGuid()}"));
localStorage = new Lazy<Storage>(() => RuntimeInfo.StartupStorage.GetStorageForDirectory($"{GetType().Name}-{Guid.NewGuid()}"));
}
[Resolved]