perf(ntfs): skip long zero runs in offline USN journals (#12274)

Co-authored-by: ShuaixinHuang <197200901+ShuaixinHuang@users.noreply.github.com>
This commit is contained in:
ShuaixinHuangandShuaixinHuang authored and GitHub committed 2026-11-19 06:09:30 -05:00
1 parent a4bf59f661
commit a3a7922243
2 files changed
+64 -4

No files matched your search

+42
View File
@@ -44,6 +44,48 @@ it("preserves full references, flags, offsets and UTF-16 names after sparse padd
]);
});
it.each([2 * 64 * 1024, 2 * 64 * 1024 + 8, 2 * 64 * 1024 + 24])(
"keeps the exact record boundary after a %i-byte zero run on a resumed page",
(paddingBytes) => {
const startFileOffset = 72;
const recordOffset = startFileOffset + paddingBytes;
const bytes = Buffer.concat([
journalRecord(),
Buffer.alloc(paddingBytes),
journalRecord(),
Buffer.alloc(3),
]);
expect([...readUsnJournal(bytes, startFileOffset)]).toMatchObject([
{
ok: false,
error: {
kind: "padding",
fileOffset: startFileOffset,
bytes: paddingBytes,
},
},
{ ok: true, value: { fileOffset: recordOffset, name: "a😃b" } },
{
ok: false,
error: { kind: "truncated", fileOffset: recordOffset + 72 },
},
]);
},
);
it("does not skip a malformed header with a zero length and nonzero version", () => {
const paddingBytes = 2 * 64 * 1024 + 24;
const bytes = Buffer.alloc(paddingBytes + 8);
bytes.writeUInt16LE(2, paddingBytes + 4);
expect([...readUsnJournal(bytes)]).toEqual([
{
ok: false,
error: { kind: "padding", fileOffset: 0, bytes: paddingBytes },
},
{ ok: false, error: { kind: "malformed", fileOffset: paddingBytes } },
]);
});
it("uses a validated record length to skip unsupported and malformed records", () => {
const unsupported = journalRecord();
unsupported.writeUInt16LE(9, 4);
+22 -4
View File
@@ -2,6 +2,7 @@ import { BinaryReader } from "../binaryContainer.js";
import { err, ok, type Result } from "../result.js";
const COMMON_HEADER_BYTES = 8;
const ZERO_PADDING_BLOCK = Buffer.alloc(64 * 1024);
/** Bound extent objects in each inline event independently of source record length. */
export const USN_INLINE_EXTENT_LIMIT = 16;
@@ -55,12 +56,29 @@ export function* readUsnJournal(
const length = journal.u32(fileOffset);
if (length === 0 && journal.u32(fileOffset + 4) === 0) {
const start = fileOffset;
do {
fileOffset += COMMON_HEADER_BYTES;
} while (
fileOffset += COMMON_HEADER_BYTES;
if (
fileOffset + COMMON_HEADER_BYTES <= bytes.length &&
journal.word(fileOffset, 64) === 0n
);
) {
fileOffset += COMMON_HEADER_BYTES;
// Compare long zero runs in native code, then resolve the exact end
// on record boundaries without consuming a short trailing header.
while (
ZERO_PADDING_BLOCK.length <= bytes.length - fileOffset &&
ZERO_PADDING_BLOCK.compare(
bytes,
fileOffset,
fileOffset + ZERO_PADDING_BLOCK.length,
) === 0
)
fileOffset += ZERO_PADDING_BLOCK.length;
while (
fileOffset + COMMON_HEADER_BYTES <= bytes.length &&
journal.word(fileOffset, 64) === 0n
)
fileOffset += COMMON_HEADER_BYTES;
}
yield err({
kind: "padding",
fileOffset: start,