@@ -134,6 +134,58 @@ void Worker()
134134 Utilities . UXFDebugLog ( "Finished worker thread" ) ;
135135 }
136136
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+
137189 /// <summary>
138190 /// Returns true if there may be a risk of overwriting data.
139191 /// </summary>
@@ -165,7 +217,7 @@ public override string HandleDataTable(UXFDataTable table, string experiment, st
165217
166218 if ( verboseDebug ) Utilities . UXFDebugLogFormat ( "Queuing save of file: {0}" , savePath ) ;
167219
168- ManageInWorker ( ( ) => { File . WriteAllLines ( savePath , lines ) ; } ) ;
220+ ManageInWorker ( ( ) => { SafeFileWriteAllLines ( savePath , lines ) ; } ) ;
169221 return GetRelativePath ( StoragePath , savePath ) ;
170222 }
171223
@@ -186,7 +238,7 @@ public override string HandleJSONSerializableObject(List<object> serializableObj
186238
187239 if ( verboseDebug ) Utilities . UXFDebugLogFormat ( "Queuing save of file: {0}" , savePath ) ;
188240
189- ManageInWorker ( ( ) => { File . WriteAllText ( savePath , text ) ; } ) ;
241+ ManageInWorker ( ( ) => { SafeFileWriteAllText ( savePath , text ) ; } ) ;
190242 return GetRelativePath ( StoragePath , savePath ) ; ;
191243 }
192244
@@ -207,7 +259,7 @@ public override string HandleJSONSerializableObject(Dictionary<string, object> s
207259
208260 if ( verboseDebug ) Utilities . UXFDebugLogFormat ( "Queuing save of file: {0}" , savePath ) ;
209261
210- ManageInWorker ( ( ) => { File . WriteAllText ( savePath , text ) ; } ) ;
262+ ManageInWorker ( ( ) => { SafeFileWriteAllText ( savePath , text ) ; } ) ;
211263 return GetRelativePath ( StoragePath , savePath ) ; ;
212264 }
213265
@@ -227,7 +279,7 @@ public override string HandleText(string text, string experiment, string ppid, i
227279
228280 if ( verboseDebug ) Utilities . UXFDebugLogFormat ( "Queuing save of file: {0}" , savePath ) ;
229281
230- ManageInWorker ( ( ) => { File . WriteAllText ( savePath , text ) ; } ) ;
282+ ManageInWorker ( ( ) => { SafeFileWriteAllText ( savePath , text ) ; } ) ;
231283 return GetRelativePath ( StoragePath , savePath ) ; ;
232284 }
233285
@@ -247,7 +299,7 @@ public override string HandleBytes(byte[] bytes, string experiment, string ppid,
247299
248300 if ( verboseDebug ) Utilities . UXFDebugLogFormat ( "Queuing save of file: {0}" , savePath ) ;
249301
250- ManageInWorker ( ( ) => { File . WriteAllBytes ( savePath , bytes ) ; } ) ;
302+ ManageInWorker ( ( ) => { SafeFileWriteAllBytes ( savePath , bytes ) ; } ) ;
251303 return GetRelativePath ( StoragePath , savePath ) ;
252304 }
253305
0 commit comments