Skip to content

Commit bb33665

Browse files
Merge pull request #117 from GridProtectionAlliance/RemoveOldestFilesBeforeFullUpdate
Updates to `RemoveOldestFilesBeforeFull` operation
2 parents 062e0e5 + 2090666 commit bb33665

1 file changed

Lines changed: 41 additions & 10 deletions

File tree

Source/Libraries/Adapters/openHistorian.Adapters/LocalOutputAdapter.cs

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -884,10 +884,24 @@ private void RemoveOldestFilesBeforeFull()
884884
// accommodate high volume data archiving in very low disk space environments.
885885
long neededSpace = m_archiveInfo.DesiredRemainingSpace + 3 * m_archiveInfo.TargetFileSize;
886886

887+
string[] archiveDirectories = m_archiveDirectories ?? [WorkingDirectory];
888+
889+
// Normalize archive directories: absolute, trimmed, with trailing separator
890+
for (int i = 0; i < archiveDirectories.Length; i++)
891+
{
892+
string normalizedDirectory = Path.GetFullPath(archiveDirectories[i]).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
893+
894+
// Add trailing separator to ensure only subdirectory, not prefix match which might be a file
895+
if (!normalizedDirectory.EndsWith(Path.DirectorySeparatorChar.ToString()))
896+
normalizedDirectory += Path.DirectorySeparatorChar;
897+
898+
archiveDirectories[i] = normalizedDirectory;
899+
}
900+
887901
try
888902
{
889903
// Check if any target archive destination has enough disk space
890-
foreach (string path in m_archiveDirectories ?? [WorkingDirectory])
904+
foreach (string path in archiveDirectories)
891905
{
892906
FilePath.GetAvailableFreeSpace(path, out long freeSpace, out _);
893907

@@ -912,15 +926,19 @@ private void RemoveOldestFilesBeforeFull()
912926

913927
long fileSizeSum = 0;
914928

915-
// Find oldest archive files until we have reached target disk space. End time is preferred over start
916-
// time for sorting since devices with an inaccurate GPS clock can provide bad start times when out
917-
// of range timestamps are not configured to be filtered and you don't want to accidentally delete a
918-
// file with otherwise in-range data.
919-
ArchiveDetails[] filesToDelete = database.GetAllAttachedFiles().OrderBy(file => file.EndTime).TakeWhile(item =>
920-
{
921-
fileSizeSum += item.FileSize;
922-
return fileSizeSum < neededSpace;
923-
}).ToArray();
929+
// Find the oldest archive files until we have reached target disk space. End time is preferred over
930+
// start time for sorting since devices with an inaccurate GPS clock can provide bad start times when
931+
// out of range timestamps are not configured to be filtered, and you don't want to accidentally delete
932+
// a file with otherwise in-range data.
933+
ArchiveDetails[] filesToDelete = database.GetAllAttachedFiles()
934+
.Where(filePathTargetsArchiveDirectories)
935+
.OrderBy(file => file.EndTime)
936+
.TakeWhile(item =>
937+
{
938+
fileSizeSum += item.FileSize;
939+
return fileSizeSum < neededSpace;
940+
})
941+
.ToArray();
924942

925943
database.DeleteFiles(filesToDelete.Select(file => file.Id).ToList());
926944

@@ -930,6 +948,19 @@ private void RemoveOldestFilesBeforeFull()
930948
{
931949
OnProcessException(MessageLevel.Error, new InvalidOperationException($"Failed while attempting to delete oldest archive files in order to free disk space: {ex.Message}", ex));
932950
}
951+
952+
return;
953+
954+
bool filePathTargetsArchiveDirectories(ArchiveDetails file)
955+
{
956+
// Normalize file path: absolute, trimmed
957+
string filePath = Path.GetFullPath(file.FileName);
958+
959+
// Case-insensitive compare on Windows, case-sensitive on Posix environments
960+
StringComparison comparison = Common.IsPosixEnvironment ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;
961+
962+
return archiveDirectories.Any(directory => filePath.StartsWith(directory, comparison));
963+
}
933964
}
934965

935966
private void DownsampleArchiveFiles()

0 commit comments

Comments
 (0)