mirror of
https://github.com/morluto/rea.git
synced 2026-12-04 19:41:20 -05:00
refactor(ntfs): share buffered record slices (#15645)
This commit is contained in:
@@ -254,20 +254,6 @@ export const hasMftFileRecordHeader = (prefix: Uint8Array): boolean => {
|
||||
);
|
||||
};
|
||||
|
||||
/** Retain only identity/header facts outside the fixed inline detail window. */
|
||||
export const parseMftIndex = (
|
||||
bytes: Uint8Array,
|
||||
startRecord = 0,
|
||||
recordLimit = MFT_INLINE_HEADER_LIMIT,
|
||||
): Result<MftIndex, RegionDecodeFailure> => {
|
||||
const records: MftRecordHeader[] = [];
|
||||
const inventory = scanMft(bytes, (header) => records.push(header), {
|
||||
startRecord,
|
||||
recordLimit,
|
||||
});
|
||||
return inventory.ok ? ok({ ...inventory.value, records }) : inventory;
|
||||
};
|
||||
|
||||
/** Select bounded record details from the owning index and its unchanged acquisition. */
|
||||
export const selectMftMetadata = (
|
||||
bytes: Uint8Array,
|
||||
@@ -289,10 +275,16 @@ export const selectMftMetadata = (
|
||||
const recordHeaders: MftRecord[] = [];
|
||||
if (end > low) {
|
||||
const restored = Buffer.allocUnsafe(index.recordBytes);
|
||||
for (let position = low; position < end; position += 1)
|
||||
for (let position = low; position < end; position += 1) {
|
||||
const { fileOffset } = index.records[position]!;
|
||||
recordHeaders.push(
|
||||
readRecord(bytes, index.records[position]!.fileOffset, restored),
|
||||
readRecordSlice(
|
||||
bytes.subarray(fileOffset, fileOffset + index.recordBytes),
|
||||
fileOffset,
|
||||
restored,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
return {
|
||||
...index.metadata,
|
||||
@@ -307,11 +299,12 @@ export const selectMftMetadata = (
|
||||
};
|
||||
};
|
||||
|
||||
const scanMft = (
|
||||
/** Retain only identity/header facts outside the fixed inline detail window. */
|
||||
export const parseMftIndex = (
|
||||
bytes: Uint8Array,
|
||||
retainHeader?: (header: MftRecordHeader) => void,
|
||||
window = { startRecord: 0, recordLimit: MFT_INLINE_HEADER_LIMIT },
|
||||
): Result<MftInventory, RegionDecodeFailure> => {
|
||||
startRecord = 0,
|
||||
recordLimit = MFT_INLINE_HEADER_LIMIT,
|
||||
): Result<MftIndex, RegionDecodeFailure> => {
|
||||
if (bytes.length < NTFS_MFT_MIN_RECORD_BYTES)
|
||||
return err(
|
||||
failure(
|
||||
@@ -335,6 +328,7 @@ const scanMft = (
|
||||
);
|
||||
|
||||
const recordHeaders: MftRecord[] = [];
|
||||
const records: MftRecordHeader[] = [];
|
||||
let nextRecordHeaderIndex: number | null = null;
|
||||
let recordCount = 0;
|
||||
let inUseCount = 0;
|
||||
@@ -356,11 +350,8 @@ const scanMft = (
|
||||
offset + recordBytes <= bytes.length;
|
||||
offset += recordBytes
|
||||
) {
|
||||
if (
|
||||
!hasMftFileRecordSignature(
|
||||
bytes.subarray(offset, offset + MFT_FILE_RECORD_MAGIC_BYTES),
|
||||
)
|
||||
) {
|
||||
const slice = bytes.subarray(offset, offset + recordBytes);
|
||||
if (!hasMftFileRecordSignature(slice)) {
|
||||
invalidSlots += 1;
|
||||
firstInvalidOffset ??= offset;
|
||||
if (!hasDeclaredRecordSize) break;
|
||||
@@ -368,22 +359,19 @@ const scanMft = (
|
||||
}
|
||||
const slotIndex = offset / recordBytes;
|
||||
const inDetailWindow =
|
||||
slotIndex >= window.startRecord &&
|
||||
recordHeaders.length < window.recordLimit;
|
||||
slotIndex >= startRecord && recordHeaders.length < recordLimit;
|
||||
if (inDetailWindow) {
|
||||
const record = readRecord(bytes, offset, restored);
|
||||
if (retainHeader !== undefined) {
|
||||
const {
|
||||
fileNames: _names,
|
||||
fileNameCoverage: _nameCoverage,
|
||||
attributes: _attributes,
|
||||
attributeCoverage: _coverage,
|
||||
attributeIssue: _issue,
|
||||
attributeListResolution: _resolution,
|
||||
...header
|
||||
} = record;
|
||||
retainHeader(header);
|
||||
}
|
||||
const record = readRecordSlice(slice, offset, restored);
|
||||
const {
|
||||
fileNames: _names,
|
||||
fileNameCoverage: _nameCoverage,
|
||||
attributes: _attributes,
|
||||
attributeCoverage: _coverage,
|
||||
attributeIssue: _issue,
|
||||
attributeListResolution: _resolution,
|
||||
...header
|
||||
} = record;
|
||||
records.push(header);
|
||||
fileNameCount += record.fileNames.length;
|
||||
if (record.attributeCoverage === "incomplete") incompleteAttributes += 1;
|
||||
recordHeaders.push(record);
|
||||
@@ -405,17 +393,11 @@ const scanMft = (
|
||||
firstInvalidExtentOffset ??= offset;
|
||||
}
|
||||
} else {
|
||||
const summary = readRecordSummary(
|
||||
bytes,
|
||||
offset,
|
||||
restored,
|
||||
retainHeader !== undefined,
|
||||
);
|
||||
if (retainHeader !== undefined && summary.header !== undefined)
|
||||
retainHeader(summary.header);
|
||||
const summary = readRecordSlice(slice, offset, restored, "header");
|
||||
if (summary.header !== undefined) records.push(summary.header);
|
||||
fileNameCount += summary.fileNameCount;
|
||||
if (summary.attributeCoverage === "incomplete") incompleteAttributes += 1;
|
||||
if (slotIndex >= window.startRecord) nextRecordHeaderIndex ??= slotIndex;
|
||||
if (slotIndex >= startRecord) nextRecordHeaderIndex ??= slotIndex;
|
||||
recordCount += 1;
|
||||
if (summary.inUse) inUseCount += 1;
|
||||
if (summary.directory) directoryCount += 1;
|
||||
@@ -477,14 +459,14 @@ const scanMft = (
|
||||
truncated_tail_bytes: truncatedTailBytes,
|
||||
record_headers: resolveMftAttributeLists(recordHeaders, bytes, recordBytes),
|
||||
omitted_record_headers: recordCount - recordHeaders.length,
|
||||
record_header_start_index: window.startRecord,
|
||||
record_header_start_index: startRecord,
|
||||
next_record_header_index: nextRecordHeaderIndex,
|
||||
file_name_count: fileNameCount,
|
||||
file_name_incomplete_record_count: incompleteNames,
|
||||
attribute_incomplete_record_count: incompleteAttributes,
|
||||
};
|
||||
|
||||
return ok({ metadata, recordBytes, limitations });
|
||||
return ok({ metadata, recordBytes, limitations, records });
|
||||
};
|
||||
|
||||
/** Resolve only acquired attributes, with one bounded decoded-record cache per detail page. */
|
||||
@@ -670,17 +652,6 @@ function* resolveMftAttributeListSteps(
|
||||
return resolved;
|
||||
}
|
||||
|
||||
const readRecord = (
|
||||
bytes: Uint8Array,
|
||||
fileOffset: number,
|
||||
restored: Buffer,
|
||||
): MftRecord =>
|
||||
readRecordSlice(
|
||||
bytes.subarray(fileOffset, fileOffset + restored.length),
|
||||
fileOffset,
|
||||
restored,
|
||||
);
|
||||
|
||||
interface MftRecordScanSummary {
|
||||
readonly header?: MftRecordHeader;
|
||||
readonly fileNameCount: number;
|
||||
@@ -695,20 +666,6 @@ interface MftRecordScanSummary {
|
||||
readonly extentValid: boolean;
|
||||
}
|
||||
|
||||
/** Lightweight record accounting for slots outside the inline detail window. */
|
||||
const readRecordSummary = (
|
||||
bytes: Uint8Array,
|
||||
fileOffset: number,
|
||||
restored: Buffer,
|
||||
includeHeader: boolean,
|
||||
): MftRecordScanSummary =>
|
||||
readRecordSlice(
|
||||
bytes.subarray(fileOffset, fileOffset + restored.length),
|
||||
fileOffset,
|
||||
restored,
|
||||
includeHeader ? "header" : "summary",
|
||||
);
|
||||
|
||||
function readRecordSlice(
|
||||
record: Uint8Array,
|
||||
fileOffset: number,
|
||||
|
||||
Reference in New Issue
Block a user