Skip to content

Commit

Permalink
Adapt to avoid crashes in new environments
Browse files Browse the repository at this point in the history
Adapt to environment discovered at https://forum.botengine.org/t/farm-manager-tribal-wars-2-farmbot/3038/169

Adapt to what we learned from this runtime exception:
----
run v2020-08-24 failed with exception: System.UnauthorizedAccessException: Access to the path 'C:\Users\B\AppData\Local\Temp\ccyj3guo.0bp\name-used-to-execute-file.exe' is denied. at System.IO.FileSystem.DeleteFile(String fullPath) at System.IO.File.Delete(String path) at Kalmit.ExecutableFile.ExecuteFileWithArguments(IImmutableList`1 environmentFiles, Byte[] executableFile, String arguments, IDictionary`2 environmentStrings) at Kalmit.ProcessFromElm019Code.CompileElm(IImmutableDictionary`2 elmCodeFiles, IImmutableList`1 pathToFileWithElmEntryPoint, String outputFileName, String elmMakeCommandAppendix) at Kalmit.ProcessFromElm019Code.CompileElmToJavascript(IImmutableDictionary`2 elmCodeFiles, IImmutableList`1 pathToFileWithElmEntryPoint, String elmMakeCommandAppendix)
[...]
  • Loading branch information
Viir committed Oct 17, 2020
1 parent 511d4f1 commit b0d1087
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,26 @@ static public (ProcessOutput processOutput, IReadOnlyCollection<(string name, II
var exitCode = process.ExitCode;
process.Close();

File.Delete(executableFilePath);
var createdFiles =
Filesystem.GetFilesFromDirectory(
directoryPath: workingDirectory,
filterByRelativeName: path => path != executableFileName);

var resultFiles =
Filesystem.GetAllFilesFromDirectory(workingDirectory);

Directory.Delete(workingDirectory, true);
try
{
Directory.Delete(path: workingDirectory, recursive: true);
}
// Avoid crash in scenario like https://forum.botengine.org/t/farm-manager-tribal-wars-2-farmbot/3038/170
catch (UnauthorizedAccessException)
{
}

return (new ProcessOutput
{
ExitCode = exitCode,
StandardError = standardError,
StandardOutput = standardOutput,
}, resultFiles);
}, createdFiles);
}

// Helper for Linux platform. Thank you Kyle Spearrin!
Expand Down
13 changes: 11 additions & 2 deletions implement/PersistentProcess/PersistentProcess.Common/Filesystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,18 @@ public class Filesystem
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "LOCALAPPDATA" : "HOME"),
"kalmit", ".cache");

static public IReadOnlyCollection<(string name, IImmutableList<byte> content)> GetAllFilesFromDirectory(string directoryPath) =>
static public IReadOnlyCollection<(string path, IImmutableList<byte> content)> GetAllFilesFromDirectory(string directoryPath) =>
GetFilesFromDirectory(
directoryPath: directoryPath,
filterByRelativeName: _ => true);

static public IReadOnlyCollection<(string path, IImmutableList<byte> content)> GetFilesFromDirectory(
string directoryPath,
Func<string, bool> filterByRelativeName) =>
Directory.GetFiles(directoryPath, "*", SearchOption.AllDirectories)
.Select(filePath => (GetRelativePath(directoryPath, filePath), (IImmutableList<byte>)File.ReadAllBytes(filePath).ToImmutableList()))
.Select(filePath => (absolutePath: filePath, relativePath: GetRelativePath(directoryPath, filePath)))
.Where(filePath => filterByRelativeName(filePath.relativePath))
.Select(filePath => (filePath.relativePath, (IImmutableList<byte>)File.ReadAllBytes(filePath.absolutePath).ToImmutableList()))
.ToList();

static public string GetRelativePath(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ static public Composition.TreeComponent LoadSortedTreeFromPath(string path)

var blobs =
Filesystem.GetAllFilesFromDirectory(path)
.Select(file => (path: (System.Collections.Immutable.IImmutableList<string>)file.name.Split('/', '\\').ToImmutableList(), content: file.content))
.Select(file => (path: (System.Collections.Immutable.IImmutableList<string>)file.path.Split('/', '\\').ToImmutableList(), content: file.content))
.ToImmutableList();

return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ namespace Kalmit.PersistentProcess.WebHost
{
public class Program
{
static public string AppVersionId => "2020-10-11";
static public string AppVersionId => "2020-10-17";
}
}
4 changes: 2 additions & 2 deletions implement/elm-fullstack/elm-fullstack.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>elm_fullstack</RootNamespace>
<AssemblyName>elm-fullstack</AssemblyName>
<AssemblyVersion>2020.1011.0.0</AssemblyVersion>
<FileVersion>2020.1011.0.0</FileVersion>
<AssemblyVersion>2020.1017.0.0</AssemblyVersion>
<FileVersion>2020.1017.0.0</FileVersion>
</PropertyGroup>

<PropertyGroup>
Expand Down
4 changes: 2 additions & 2 deletions implement/test-elm-fullstack/TestSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ static public IImmutableDictionary<IImmutableList<string>, IImmutableList<byte>>
string directoryPath) =>
ElmApp.ToFlatDictionaryWithPathComparer(
Filesystem.GetAllFilesFromDirectory(directoryPath)
.OrderBy(file => file.name)
.Select(filePathAndContent => ((IImmutableList<string>)filePathAndContent.name.Split(new[] { '/', '\\' }).ToImmutableList(), filePathAndContent.content)));
.OrderBy(file => file.path)
.Select(filePathAndContent => ((IImmutableList<string>)filePathAndContent.path.Split(new[] { '/', '\\' }).ToImmutableList(), filePathAndContent.content)));

static public IImmutableDictionary<IImmutableList<string>, IImmutableList<byte>> AsLoweredElmApp(
IImmutableDictionary<IImmutableList<string>, IImmutableList<byte>> originalAppFiles) =>
Expand Down

0 comments on commit b0d1087

Please sign in to comment.