Skip to content

Commit

Permalink
Avoid overwritting by moving old file to backup
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmed-shariff committed Dec 12, 2022
1 parent 3872e72 commit 444a627
Showing 1 changed file with 57 additions and 5 deletions.
62 changes: 57 additions & 5 deletions Assets/UXF/Scripts/DataHandling/FileSaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,58 @@ void Worker()
Utilities.UXFDebugLog("Finished worker thread");
}

/// <summary>
/// Move `path` to a back up path.
/// Backup is the path file name with its LastWriteTime as a suffix.
/// </summary>
protected virtual void MoveToBackup(string path)
{
string fileName = Path.GetFileNameWithoutExtension(path);
string suffix = File.GetLastWriteTime(path).ToString("dd-MM-yyyy-HH-mm-FF");
string ext = Path.GetExtension(path);
string newPath = Path.Combine(Path.GetDirectoryName(path), $"{fileName}_{suffix}.{ext}");
File.Move(path, newPath);
}

/// <summary>
/// Same as File.WriteAllText, but makes sure files are not overwritten.
/// If file file exists, the old file will be renamed with a suffix with its LastWriteTime.
/// </summary>
protected virtual void SafeFileWriteAllText(string path, string content)
{
if (File.Exists(path))
{
MoveToBackup(path);
}
File.WriteAllText(path, content);
}

/// <summary>
/// Same as File.WriteAllLines, but makes sure files are not overwritten.
/// If file file exists, the old file will be renamed with a suffix with its LastWriteTime.
/// </summary>
protected virtual void SafeFileWriteAllLines(string path, string[] content)
{
if (File.Exists(path))
{
MoveToBackup(path);
}
File.WriteAllLines(path, content);
}

/// <summary>
/// Same as File.WriteAllBytes, but makes sure files are not overwritten.
/// If file file exists, the old file will be renamed with a suffix with its LastWriteTime.
/// </summary>
protected virtual void SafeFileWriteAllBytes(string path, byte[] content)
{
if (File.Exists(path))
{
MoveToBackup(path);
}
File.WriteAllBytes(path, content);
}

/// <summary>
/// Returns true if there may be a risk of overwriting data.
/// </summary>
Expand Down Expand Up @@ -165,7 +217,7 @@ public override string HandleDataTable(UXFDataTable table, string experiment, st

if (verboseDebug) Utilities.UXFDebugLogFormat("Queuing save of file: {0}", savePath);

ManageInWorker(() => { File.WriteAllLines(savePath, lines); });
ManageInWorker(() => { SafeFileWriteAllLines(savePath, lines); });
return GetRelativePath(StoragePath, savePath);
}

Expand All @@ -186,7 +238,7 @@ public override string HandleJSONSerializableObject(List<object> serializableObj

if (verboseDebug) Utilities.UXFDebugLogFormat("Queuing save of file: {0}", savePath);

ManageInWorker(() => { File.WriteAllText(savePath, text); });
ManageInWorker(() => { SafeFileWriteAllText(savePath, text); });
return GetRelativePath(StoragePath, savePath);;
}

Expand All @@ -207,7 +259,7 @@ public override string HandleJSONSerializableObject(Dictionary<string, object> s

if (verboseDebug) Utilities.UXFDebugLogFormat("Queuing save of file: {0}", savePath);

ManageInWorker(() => { File.WriteAllText(savePath, text); });
ManageInWorker(() => { SafeFileWriteAllText(savePath, text); });
return GetRelativePath(StoragePath, savePath);;
}

Expand All @@ -227,7 +279,7 @@ public override string HandleText(string text, string experiment, string ppid, i

if (verboseDebug) Utilities.UXFDebugLogFormat("Queuing save of file: {0}", savePath);

ManageInWorker(() => { File.WriteAllText(savePath, text); });
ManageInWorker(() => { SafeFileWriteAllText(savePath, text); });
return GetRelativePath(StoragePath, savePath);;
}

Expand All @@ -247,7 +299,7 @@ public override string HandleBytes(byte[] bytes, string experiment, string ppid,

if (verboseDebug) Utilities.UXFDebugLogFormat("Queuing save of file: {0}", savePath);

ManageInWorker(() => { File.WriteAllBytes(savePath, bytes); });
ManageInWorker(() => { SafeFileWriteAllBytes(savePath, bytes); });
return GetRelativePath(StoragePath, savePath);
}

Expand Down

0 comments on commit 444a627

Please sign in to comment.