rename TempFileCleaner to TempFileManager; add global path config for temp files
I did this in a funny way (sets the environment for the process). The idea is that any code which sloppily used Path.GetTempDirectory (etc.) would now have its pathing changed. It is a little dangerous to allow this to be changed on the fly (I do allow it) since something may expect it to be stable, but I think it's OK. So anyway. keep your eyes peeled for problems. It's possible I could do this differently and only affect a subset of safely managed things. should fix #1252
This commit is contained in:
parent
faaf4d2f18
commit
ba4b32f619
|
@ -140,7 +140,7 @@ namespace BizHawk.Client.Common
|
|||
//handling of initial .. was removed (Path.GetFullPath can handle it)
|
||||
//handling of file:// or file:\\ was removed (can Path.GetFullPath handle it? not sure)
|
||||
|
||||
// all pad paths default to EXE
|
||||
// all bad paths default to EXE
|
||||
return GetExeDirectoryAbsolute();
|
||||
}
|
||||
|
||||
|
@ -461,5 +461,19 @@ namespace BizHawk.Client.Common
|
|||
|
||||
return entry;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Puts the currently configured temp path into the environment for use as actual temp directory
|
||||
/// </summary>
|
||||
public static void RefreshTempPath()
|
||||
{
|
||||
if (Global.Config.PathEntries.TempFilesFragment != "")
|
||||
{
|
||||
//TODO - BUG - needs to route through PathManager.MakeAbsolutePath or something similar, but how?
|
||||
string target = Global.Config.PathEntries.TempFilesFragment;
|
||||
BizHawk.Common.TempFileManager.HelperSetTempPath(target);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -37,6 +37,7 @@ namespace BizHawk.Client.Common
|
|||
{
|
||||
PathEntries.ResolveWithDefaults();
|
||||
HotkeyBindings.ResolveWithDefaults();
|
||||
PathManager.RefreshTempPath();
|
||||
}
|
||||
|
||||
// Core preference for generic file extension, key: file extension, value: a systemID or empty if no preference
|
||||
|
|
|
@ -165,6 +165,8 @@ namespace BizHawk.Client.Common
|
|||
|
||||
public string GlobalRomFragment => Global.Config.PathEntries["Global", "ROM"].Path;
|
||||
|
||||
public string TempFilesFragment => Global.Config.PathEntries["Global", "Temp Files"].Path;
|
||||
|
||||
// this one is special
|
||||
public string GlobalBaseFragment => Global.Config.PathEntries["Global", "Base"].Path;
|
||||
|
||||
|
@ -184,6 +186,7 @@ namespace BizHawk.Client.Common
|
|||
new PathEntry { System = "Global_NULL", SystemDisplayName = "Global", Type = "TAStudio states", Path = Path.Combine(".", "Movies", "TAStudio states"), Ordinal = 12 },
|
||||
new PathEntry { System = "Global_NULL", SystemDisplayName = "Global", Type = "Multi-Disk Bundles", Path = Path.Combine(".", ""), Ordinal = 13 },
|
||||
new PathEntry { System = "Global_NULL", SystemDisplayName = "Global", Type = "External Tools", Path = Path.Combine(".", "ExternalTools"), Ordinal = 14 },
|
||||
new PathEntry { System = "Global_NULL", SystemDisplayName = "Global", Type = "Temp Files", Path = "", Ordinal = 15 },
|
||||
|
||||
new PathEntry { System = "INTV", SystemDisplayName = "Intellivision", Type = "Base", Path = Path.Combine(".", "Intellivision"), Ordinal = 0 },
|
||||
new PathEntry { System = "INTV", SystemDisplayName = "Intellivision", Type = "ROM", Path = ".", Ordinal = 1 },
|
||||
|
|
|
@ -75,7 +75,7 @@ namespace BizHawk.Client.Common
|
|||
_mDisk = disk;
|
||||
if (disk)
|
||||
{
|
||||
var path = TempFileCleaner.GetTempFilename("movieOnDisk");
|
||||
var path = TempFileManager.GetTempFilename("movieOnDisk");
|
||||
stream = new FileStream(path, FileMode.Create, System.Security.AccessControl.FileSystemRights.FullControl, FileShare.None, 4 * 1024, FileOptions.DeleteOnClose);
|
||||
}
|
||||
else
|
||||
|
|
|
@ -27,7 +27,7 @@ namespace BizHawk.Client.Common
|
|||
_mCapacity = capacity;
|
||||
if (onDisk)
|
||||
{
|
||||
var path = TempFileCleaner.GetTempFilename("rewindbuf");
|
||||
var path = TempFileManager.GetTempFilename("rewindbuf");
|
||||
|
||||
// I checked the DeleteOnClose operation to make sure it cleans up when the process is aborted, and it seems to.
|
||||
// Otherwise we would have a more complex tempfile management problem here.
|
||||
|
|
|
@ -115,7 +115,7 @@ namespace BizHawk.Client.EmuHawk
|
|||
}
|
||||
}
|
||||
|
||||
BizHawk.Common.TempFileCleaner.Start();
|
||||
BizHawk.Common.TempFileManager.Start();
|
||||
|
||||
|
||||
HawkFile.ArchiveHandlerFactory = new SevenZipSharpArchiveHandler();
|
||||
|
|
|
@ -308,6 +308,9 @@ namespace BizHawk.Client.EmuHawk
|
|||
private void Ok_Click(object sender, EventArgs e)
|
||||
{
|
||||
SaveSettings();
|
||||
|
||||
PathManager.RefreshTempPath();
|
||||
|
||||
GlobalWin.OSD.AddMessage("Path settings saved");
|
||||
Close();
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ namespace BizHawk.Common
|
|||
public InstanceDll(string dllPath)
|
||||
{
|
||||
// copy the dll to a temp directory
|
||||
var path = TempFileCleaner.GetTempFilename(string.Format("{0}", Path.GetFileNameWithoutExtension(dllPath)),".dll",false);
|
||||
var path = TempFileManager.GetTempFilename(string.Format("{0}", Path.GetFileNameWithoutExtension(dllPath)),".dll",false);
|
||||
using (var stream = new FileStream(path, FileMode.Create, System.Security.AccessControl.FileSystemRights.FullControl, FileShare.ReadWrite | FileShare.Delete, 4 * 1024, FileOptions.None))
|
||||
using (var sdll = File.OpenRead(dllPath))
|
||||
sdll.CopyTo(stream);
|
||||
|
@ -32,7 +32,7 @@ namespace BizHawk.Common
|
|||
var lastError = GetLastError();
|
||||
throw new InvalidOperationException($"Failed to load plugin {path}, error code: 0x{lastError:X}");
|
||||
}
|
||||
var newfname = TempFileCleaner.RenameTempFilenameForDelete(path);
|
||||
var newfname = TempFileManager.RenameTempFilenameForDelete(path);
|
||||
File.Move(path, newfname);
|
||||
}
|
||||
finally
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
|
@ -10,7 +12,7 @@ namespace BizHawk.Common
|
|||
/// Files shouldn't be named that unless they're safe to delete, but notably, they may stil be in use. That won't hurt this component.
|
||||
/// When they're no longer in use, this component will then be able to delete them.
|
||||
/// </summary>
|
||||
public static class TempFileCleaner
|
||||
public static class TempFileManager
|
||||
{
|
||||
// TODO - manage paths other than %temp%, make not static, or allow adding multiple paths to static instance
|
||||
|
||||
|
@ -41,7 +43,7 @@ namespace BizHawk.Common
|
|||
|
||||
public static void Start()
|
||||
{
|
||||
lock (typeof(TempFileCleaner))
|
||||
lock (typeof(TempFileManager))
|
||||
{
|
||||
if (thread != null)
|
||||
{
|
||||
|
@ -64,27 +66,49 @@ namespace BizHawk.Common
|
|||
|
||||
static void ThreadProc()
|
||||
{
|
||||
var di = new DirectoryInfo(Path.GetTempPath());
|
||||
//squirrely logic, trying not to create garbage
|
||||
HashSet<string> knownTempDirs = new HashSet<string>();
|
||||
List<DirectoryInfo> dis = new List<DirectoryInfo>();
|
||||
for (;;)
|
||||
{
|
||||
var fis = di.GetFiles("bizdelete-*");
|
||||
foreach (var fi in fis)
|
||||
lock (typeof(TempFileManager))
|
||||
{
|
||||
knownTempDirs.Add(Path.GetTempPath());
|
||||
if (dis.Count != knownTempDirs.Count)
|
||||
dis = knownTempDirs.Select(x => new DirectoryInfo(x)).ToList();
|
||||
}
|
||||
|
||||
foreach(var di in dis)
|
||||
{
|
||||
FileInfo[] fis = null;
|
||||
try
|
||||
{
|
||||
// SHUT. UP. THE. EXCEPTIONS.
|
||||
#if WINDOWS
|
||||
DeleteFileW(fi.FullName);
|
||||
#else
|
||||
fi.Delete();
|
||||
#endif
|
||||
fis = di.GetFiles("bizdelete-*");
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
if(fis != null)
|
||||
{
|
||||
foreach (var fi in fis)
|
||||
{
|
||||
try
|
||||
{
|
||||
// SHUT. UP. THE. EXCEPTIONS.
|
||||
#if WINDOWS
|
||||
DeleteFileW(fi.FullName);
|
||||
#else
|
||||
fi.Delete();
|
||||
#endif
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
|
||||
// try not to do more than one thing per frame
|
||||
Thread.Sleep(100);
|
||||
// try not to do more than one thing per frame
|
||||
Thread.Sleep(100);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// try not to slam the filesystem too hard, we dont want this to cause any hiccups
|
||||
|
@ -97,5 +121,13 @@ namespace BizHawk.Common
|
|||
}
|
||||
|
||||
static Thread thread;
|
||||
|
||||
public static void HelperSetTempPath(string path)
|
||||
{
|
||||
//yes... this is how we're doing it, for now, until it's proven to be troublesome
|
||||
Directory.CreateDirectory(path);
|
||||
Environment.SetEnvironmentVariable("TMP", path);
|
||||
Environment.SetEnvironmentVariable("TEMP", path);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue