Filesystem: restrict getversionedfilename to 100 tries

This commit is contained in:
Jonathan Jenne 2023-04-03 16:33:14 +02:00
parent 1c49054844
commit 2d2c09bdf4

View File

@ -39,6 +39,9 @@ Public Class File
' Source: https://docs.microsoft.com/de-de/windows/win32/fileio/naming-a-file?redirectedfrom=MSDN#maximum-path-length-limitation
Private Const MAX_FILE_PATH_LENGTH = 250
' This prevents an infinite loop when no file can be created in a location
Private Const MAX_FILE_VERSION = 100
Private Const FILE_NAME_ACCESS_TEST = "accessTest.txt"
Public Sub New(LogConfig As LogConfig)
@ -151,8 +154,13 @@ Public Class File
oFileNameWithoutExtension = oNewFileNameWithoutExtension
End If
' while file exists, increment version
' while file exists, increment version.
' version cannot go above MAX_FILE_VERSION, to prevent infinite loop
Do
If oFileVersion >= MAX_FILE_VERSION Then
Throw New OverflowException($"Tried '{MAX_FILE_VERSION}' times to version filename before giving up. Sorry.")
End If
oFinalFileName = Path.Combine(oDestinationDir, GetFilenameWithVersion(oFileNameWithoutExtension, oVersionSeparator, oFileVersion, oExtension))
_Logger.Debug("Intermediate Filename is {0}", oFinalFileName)
_Logger.Debug("File version: {0}", oFileVersion)