@@ -134,6 +134,58 @@ void Worker()
134
134
Utilities . UXFDebugLog ( "Finished worker thread" ) ;
135
135
}
136
136
137
+ /// <summary>
138
+ /// Move `path` to a back up path.
139
+ /// Backup is the path file name with its LastWriteTime as a suffix.
140
+ /// </summary>
141
+ protected virtual void MoveToBackup ( string path )
142
+ {
143
+ string fileName = Path . GetFileNameWithoutExtension ( path ) ;
144
+ string suffix = File . GetLastWriteTime ( path ) . ToString ( "dd-MM-yyyy-HH-mm-FF" ) ;
145
+ string ext = Path . GetExtension ( path ) ;
146
+ string newPath = Path . Combine ( Path . GetDirectoryName ( path ) , $ "{ fileName } _{ suffix } { ext } ") ;
147
+ File . Move ( path , newPath ) ;
148
+ }
149
+
150
+ /// <summary>
151
+ /// Same as File.WriteAllText, but makes sure files are not overwritten.
152
+ /// If file file exists, the old file will be renamed with a suffix with its LastWriteTime.
153
+ /// </summary>
154
+ protected virtual void SafeFileWriteAllText ( string path , string content )
155
+ {
156
+ if ( File . Exists ( path ) )
157
+ {
158
+ MoveToBackup ( path ) ;
159
+ }
160
+ File . WriteAllText ( path , content ) ;
161
+ }
162
+
163
+ /// <summary>
164
+ /// Same as File.WriteAllLines, but makes sure files are not overwritten.
165
+ /// If file file exists, the old file will be renamed with a suffix with its LastWriteTime.
166
+ /// </summary>
167
+ protected virtual void SafeFileWriteAllLines ( string path , string [ ] content )
168
+ {
169
+ if ( File . Exists ( path ) )
170
+ {
171
+ MoveToBackup ( path ) ;
172
+ }
173
+ File . WriteAllLines ( path , content ) ;
174
+ }
175
+
176
+ /// <summary>
177
+ /// Same as File.WriteAllBytes, but makes sure files are not overwritten.
178
+ /// If file file exists, the old file will be renamed with a suffix with its LastWriteTime.
179
+ /// </summary>
180
+ protected virtual void SafeFileWriteAllBytes ( string path , byte [ ] content )
181
+ {
182
+ if ( File . Exists ( path ) )
183
+ {
184
+ MoveToBackup ( path ) ;
185
+ }
186
+ File . WriteAllBytes ( path , content ) ;
187
+ }
188
+
137
189
/// <summary>
138
190
/// Returns true if there may be a risk of overwriting data.
139
191
/// </summary>
@@ -165,7 +217,7 @@ public override string HandleDataTable(UXFDataTable table, string experiment, st
165
217
166
218
if ( verboseDebug ) Utilities . UXFDebugLogFormat ( "Queuing save of file: {0}" , savePath ) ;
167
219
168
- ManageInWorker ( ( ) => { File . WriteAllLines ( savePath , lines ) ; } ) ;
220
+ ManageInWorker ( ( ) => { SafeFileWriteAllLines ( savePath , lines ) ; } ) ;
169
221
return GetRelativePath ( StoragePath , savePath ) ;
170
222
}
171
223
@@ -186,7 +238,7 @@ public override string HandleJSONSerializableObject(List<object> serializableObj
186
238
187
239
if ( verboseDebug ) Utilities . UXFDebugLogFormat ( "Queuing save of file: {0}" , savePath ) ;
188
240
189
- ManageInWorker ( ( ) => { File . WriteAllText ( savePath , text ) ; } ) ;
241
+ ManageInWorker ( ( ) => { SafeFileWriteAllText ( savePath , text ) ; } ) ;
190
242
return GetRelativePath ( StoragePath , savePath ) ; ;
191
243
}
192
244
@@ -207,7 +259,7 @@ public override string HandleJSONSerializableObject(Dictionary<string, object> s
207
259
208
260
if ( verboseDebug ) Utilities . UXFDebugLogFormat ( "Queuing save of file: {0}" , savePath ) ;
209
261
210
- ManageInWorker ( ( ) => { File . WriteAllText ( savePath , text ) ; } ) ;
262
+ ManageInWorker ( ( ) => { SafeFileWriteAllText ( savePath , text ) ; } ) ;
211
263
return GetRelativePath ( StoragePath , savePath ) ; ;
212
264
}
213
265
@@ -227,7 +279,7 @@ public override string HandleText(string text, string experiment, string ppid, i
227
279
228
280
if ( verboseDebug ) Utilities . UXFDebugLogFormat ( "Queuing save of file: {0}" , savePath ) ;
229
281
230
- ManageInWorker ( ( ) => { File . WriteAllText ( savePath , text ) ; } ) ;
282
+ ManageInWorker ( ( ) => { SafeFileWriteAllText ( savePath , text ) ; } ) ;
231
283
return GetRelativePath ( StoragePath , savePath ) ; ;
232
284
}
233
285
@@ -247,7 +299,7 @@ public override string HandleBytes(byte[] bytes, string experiment, string ppid,
247
299
248
300
if ( verboseDebug ) Utilities . UXFDebugLogFormat ( "Queuing save of file: {0}" , savePath ) ;
249
301
250
- ManageInWorker ( ( ) => { File . WriteAllBytes ( savePath , bytes ) ; } ) ;
302
+ ManageInWorker ( ( ) => { SafeFileWriteAllBytes ( savePath , bytes ) ; } ) ;
251
303
return GetRelativePath ( StoragePath , savePath ) ;
252
304
}
253
305
0 commit comments