[longomatch] Save serialzed objects in a safer way
- From: Andoni Morales Alastruey <amorales src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [longomatch] Save serialzed objects in a safer way
- Date: Fri, 12 Dec 2014 18:04:58 +0000 (UTC)
commit 2128aed6bb9052562597c0d0ef77f4a7e3646a60
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date: Fri Dec 12 19:03:25 2014 +0100
Save serialzed objects in a safer way
Save to a temporary file without replacing the original one
and move to the final destination if the serialization
succeed
LongoMatch.Core/Common/Serializer.cs | 16 +++++++++++-----
1 files changed, 11 insertions(+), 5 deletions(-)
---
diff --git a/LongoMatch.Core/Common/Serializer.cs b/LongoMatch.Core/Common/Serializer.cs
index 15d8949..dcc3f15 100644
--- a/LongoMatch.Core/Common/Serializer.cs
+++ b/LongoMatch.Core/Common/Serializer.cs
@@ -55,12 +55,18 @@ namespace LongoMatch.Core.Common
}
}
- public static void Save<T>(T obj, string filepath,
- SerializationType type=SerializationType.Json) {
- Stream stream = new FileStream(filepath, FileMode.Create, FileAccess.Write,
FileShare.None);
- using (stream) {
+ public static void Save<T> (T obj, string filepath,
+ SerializationType type=SerializationType.Json)
+ {
+ string tmpPath = filepath + ".tmp";
+ using (Stream stream = new FileStream(tmpPath, FileMode.Create,
+ FileAccess.Write, FileShare.None)) {
Save<T> (obj, stream, type);
- stream.Close();
+ }
+ if (File.Exists (filepath)) {
+ File.Replace (tmpPath, filepath, null);
+ } else {
+ File.Move (tmpPath, filepath);
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]