Skip to content

Commit f14dc4f

Browse files
committed
FileSaver backup session dir if session dir already exists
1 parent 6f43243 commit f14dc4f

File tree

1 file changed

+42
-5
lines changed

1 file changed

+42
-5
lines changed

Assets/UXF/Scripts/DataHandling/FileSaver.cs

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
using System.Collections;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using UnityEngine;
4-
using UnityEngine.Events;
53
using System;
64
using System.IO;
75
using System.Threading;
8-
using System.Linq;
96
using System.Globalization;
107

118
namespace UXF
@@ -32,6 +29,16 @@ public class FileSaver : LocalFileDataHander
3229
[Tooltip("Enable to print debug messages to the console.")]
3330
public bool verboseDebug = false;
3431

32+
/// <summary>
33+
/// Enable backing up the session directory if it already exists when a session starts. If
34+
/// not true, this will overwrite the is the UXF runs with the same experiment name, ppid,
35+
/// and session number again.
36+
/// </summary>
37+
[Tooltip("Enable backing up the session directory if it already exists when a session starts. If" +
38+
"not true, this will overwrite the is the UXF runs with the same experiment name, ppid," +
39+
"and session number again.")]
40+
public bool backupSessionIfExists = true;
41+
3542
/// <summary>
3643
/// An action which does nothing.
3744
/// </summary>
@@ -46,7 +53,6 @@ public class FileSaver : LocalFileDataHander
4653

4754
bool quitting = false;
4855

49-
5056
/// <summary>
5157
/// Starts the FileSaver Worker thread.
5258
/// </summary>
@@ -59,6 +65,37 @@ public override void SetUp()
5965

6066
quitting = false;
6167
Directory.CreateDirectory(base.StoragePath);
68+
string sessionDirectory = GetSessionPath(session.experimentName, session.ppid, session.number);
69+
if (Directory.Exists(sessionDirectory))
70+
{
71+
Utilities.UXFDebugLogWarning($"Session directory {sessionDirectory} already exists.");
72+
if (backupSessionIfExists)
73+
{
74+
string suffix = Directory.GetLastWriteTime(sessionDirectory).ToString("dd-MM-yyyy-HH-mm-FF");
75+
string backupSessionDirectory = $"{sessionDirectory}_{suffix}";
76+
77+
int idx = 1;
78+
while (Directory.Exists(backupSessionDirectory))
79+
{
80+
backupSessionDirectory = $"{backupSessionDirectory}_{idx}";
81+
}
82+
83+
try
84+
{
85+
Directory.Move(sessionDirectory, backupSessionDirectory);
86+
Utilities.UXFDebugLogWarning($"Trying to backup {sessionDirectory} to {backupSessionDirectory}.");
87+
}
88+
catch(Exception e)
89+
{
90+
Utilities.UXFDebugLogError($"Failed to backup {sessionDirectory} to {backupSessionDirectory} with exception ({e}): {e.Message}");
91+
throw e;
92+
}
93+
}
94+
else
95+
{
96+
Utilities.UXFDebugLogWarning($"backupSessionIfExists is False. Content in {sessionDirectory} may be overwritten.");
97+
}
98+
}
6299

63100
if (!IsActive)
64101
{

0 commit comments

Comments
 (0)