aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/AppBase/ConfigurationHelper.cs
diff options
context:
space:
mode:
authorBrian J. Murrell <brian@interlinx.bc.ca>2021-11-29 17:53:26 -0500
committerBrian J. Murrell <brian@interlinx.bc.ca>2021-11-29 17:53:26 -0500
commit757970bfc17b0eb1566b45fbe700dcb16423b190 (patch)
tree63fbc171621f5ec7ae156f341d9b1df37643deac /Emby.Server.Implementations/AppBase/ConfigurationHelper.cs
parenta3a4689af22693b535e80b98624831866fda2a61 (diff)
parentc677b4f6b7f7e874097aa2cee866d9ed1e574178 (diff)
Merge remote-tracking branch 'origin/master' into HEAD
Diffstat (limited to 'Emby.Server.Implementations/AppBase/ConfigurationHelper.cs')
-rw-r--r--Emby.Server.Implementations/AppBase/ConfigurationHelper.cs14
1 files changed, 6 insertions, 8 deletions
diff --git a/Emby.Server.Implementations/AppBase/ConfigurationHelper.cs b/Emby.Server.Implementations/AppBase/ConfigurationHelper.cs
index 29bac6634..3a916e5c0 100644
--- a/Emby.Server.Implementations/AppBase/ConfigurationHelper.cs
+++ b/Emby.Server.Implementations/AppBase/ConfigurationHelper.cs
@@ -1,5 +1,3 @@
-#nullable enable
-
using System;
using System.IO;
using System.Linq;
@@ -35,27 +33,27 @@ namespace Emby.Server.Implementations.AppBase
}
catch (Exception)
{
- configuration = Activator.CreateInstance(type) ?? throw new ArgumentException($"Provided path ({type}) is not valid.", nameof(type));
+ // Note: CreateInstance returns null for Nullable<T>, e.g. CreateInstance(typeof(int?)) returns null.
+ configuration = Activator.CreateInstance(type)!;
}
using var stream = new MemoryStream(buffer?.Length ?? 0);
xmlSerializer.SerializeToStream(configuration, stream);
// Take the object we just got and serialize it back to bytes
- byte[] newBytes = stream.GetBuffer();
- int newBytesLen = (int)stream.Length;
+ Span<byte> newBytes = stream.GetBuffer().AsSpan(0, (int)stream.Length);
// If the file didn't exist before, or if something has changed, re-save
- if (buffer == null || !newBytes.AsSpan(0, newBytesLen).SequenceEqual(buffer))
+ if (buffer == null || !newBytes.SequenceEqual(buffer))
{
var directory = Path.GetDirectoryName(path) ?? throw new ArgumentException($"Provided path ({path}) is not valid.", nameof(path));
Directory.CreateDirectory(directory);
+
// Save it after load in case we got new items
- // use FileShare.None as this bypasses dotnet bug dotnet/runtime#42790 .
using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
{
- fs.Write(newBytes, 0, newBytesLen);
+ fs.Write(newBytes);
}
}