You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// We have room for this message. No need to truncate.
118
-
truncateAtOffset =nil
119
-
}else{
115
+
if !cacheHasSpaceForNewMessageBeforeEndOfFile {
120
116
// This message can't be written without exceeding our maximum file length.
121
117
// We'll need to start writing the file from the beginning of the file.
122
118
123
119
// Trim the file to the current writer position to remove soon-to-be-abandoned data from the file.
124
-
truncateAtOffset = writer.offsetInFile
120
+
try writer.truncate(at: writer.offsetInFile)
121
+
122
+
// Update the offsetInFileAtEndOfNewestMessage in our header and reader such that we will ignore the now-deleted data in the file.
123
+
// If the application crashes between writing this header and writing the next message data, we'll have only lost the messages we were about to delete.
124
+
// If `writer.offsetInFile == FileHeader.expectedEndOfHeaderInFile`, then crashing before we update the header would lead to this now-empty file being viewed as corrupted.
@@ -144,12 +146,6 @@ public final class CacheAdvance<T: Codable> {
144
146
// If the application crashes between writing the header and writing the message data, we'll have lost the messages between the previous offsetInFileOfOldestMessage and the new offsetInFileOfOldestMessage.
Copy file name to clipboardExpand all lines: Sources/CacheAdvance/CacheReader.swift
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -63,8 +63,8 @@ final class CacheReader {
63
63
guard !previousReadWasEmpty else{
64
64
// If the previous read was also empty, then the file has been corrupted.
65
65
// Two empty reads in a row means that offsetInFileAtEndOfNewestMessage is incorrect.
66
-
// This inconsistency is likely due to a crash occurring during a message write.
67
-
// This issue will not occur in a current version of the library, but an earlier version was capable of creating this corruption.
66
+
// This inconsistency is likely due to a crash occurring during a message write, between truncating the file and updating the header values to reflect that the file was truncated.
67
+
// This file is actually empty, despite what the header tells us.
68
68
throwCacheAdvanceError.fileCorrupted
69
69
}
70
70
// We know the next message is at the end of the file header. Let's seek to it.
// Make sure the header data is persisted before we read it as part of the `messages()` call below.
117
-
try header.synchronizeHeaderData()
118
-
// Our file is empty. Make the file corrupted by setting the offset at end of newest message to be further in the file.
119
-
// This should never happen, but past versions of this repo could lead to a file having this kind of inconsistency if a crash occurred at the wrong time.
// Make sure the header data is persisted before we read it as part of the `messages()` call below.
117
+
try header.synchronizeHeaderData()
118
+
// Our file is empty. Make the file corrupted by setting the offset at end of newest message to be further in the file.
119
+
// This should never happen, but past versions of this repo could lead to a file having this kind of inconsistency if a crash occurred at the wrong time.
0 commit comments