ntfs 0.1.0.2 → 0.1.1.0
raw patch · 10 files changed
+506/−32 lines, 10 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ NTFS.Attribute.FileName: parseFileNameValue :: ByteString -> Either ParseError FileName
+ NTFS.Index: IndexEntry :: MFTRef -> FileName -> Maybe VCN -> IndexEntry
+ NTFS.Index: [indexChildVCN] :: IndexEntry -> Maybe VCN
+ NTFS.Index: [indexFileName] :: IndexEntry -> FileName
+ NTFS.Index: [indexFileReference] :: IndexEntry -> MFTRef
+ NTFS.Index: attributeTypeIndexAllocation :: Word32
+ NTFS.Index: attributeTypeIndexRoot :: Word32
+ NTFS.Index: data IndexEntry
+ NTFS.Index: findDirectoryEntry :: Volume -> MFTRecord -> String -> IO (Either ParseError (Maybe IndexEntry))
+ NTFS.Index: instance GHC.Classes.Eq NTFS.Index.IndexEntry
+ NTFS.Index: instance GHC.Internal.Show.Show NTFS.Index.IndexEntry
+ NTFS.Index: parseIndexRecord :: Word16 -> ByteString -> Either ParseError [IndexEntry]
+ NTFS.Index: parseIndexRoot :: Attribute -> Either ParseError [IndexEntry]
+ NTFS.Index: readDirectoryEntries :: Volume -> MFTRecord -> IO (Either ParseError [IndexEntry])
+ NTFS.MFT.Record: fixupUpdateSequence :: Word16 -> ByteString -> Either ParseError ByteString
+ NTFS.Path: readEntryPath :: Volume -> FileEntry -> IO (Either ParseError [String])
+ NTFS.Path: readRecordPath :: Volume -> MFTRef -> IO (Either ParseError [String])
+ NTFS.Path: rootDirectoryRef :: MFTRef
Files
- CHANGELOG.md +14/−0
- README.md +4/−2
- ntfs.cabal +3/−1
- src/NTFS.hs +4/−0
- src/NTFS/Attribute/FileName.hs +29/−23
- src/NTFS/Index.hs +191/−0
- src/NTFS/MFT/Record.hs +12/−3
- src/NTFS/Path.hs +73/−0
- src/NTFS/Volume.hs +2/−2
- test/Spec.hs +174/−1
CHANGELOG.md view
@@ -8,6 +8,20 @@ ## [Unreleased] +## [0.1.1.0] - 2026-11-27++### Added++- `NTFS.Index` with `$INDEX_ROOT` and `$INDEX_ALLOCATION` entry parsing,+ directory enumeration and exact-name lookup.+- `parseFileNameValue` for file name values without an attribute header.+- `fixupUpdateSequence` for applying an update sequence array to sector records.+- `NTFS.Path` with path components resolved through parent MFT references.++### Fixed++- `readMFTRecordBytes` now ignores the sequence number in MFT references.+ ## [0.1.0.2] - 2026-11-27 ### Added
README.md view
@@ -16,9 +16,11 @@ - `NTFS.Attribute.Data`, `NTFS.Attribute.StandardInformation` and `NTFS.Attribute.FileName` for common attribute values. - `NTFS.File` for the metadata and data of one record.+- `NTFS.Index` for directory entries and exact-name lookup.+- `NTFS.Path` for path components resolved through parent MFT references. -Directory index parsing and path lookup are not available yet.-The current Hackage release is `0.1.0.2`.+NTFS name collation is not available yet.+The current Hackage release is `0.1.1.0`. ## Building
ntfs.cabal view
@@ -1,6 +1,6 @@ cabal-version: 2.4 name: ntfs-version: 0.1.0.2+version: 0.1.1.0 synopsis: Pure Haskell library for reading NTFS disk images description: Parsers for NTFS volume geometry, MFT records, attributes and runlists, with@@ -28,9 +28,11 @@ NTFS.Attribute.StandardInformation NTFS.BootSector NTFS.File+ NTFS.Index NTFS.MFT NTFS.MFT.Attribute NTFS.MFT.Record+ NTFS.Path NTFS.Runlist NTFS.Types NTFS.Volume
src/NTFS.hs view
@@ -6,9 +6,11 @@ , module NTFS.Attribute.StandardInformation , module NTFS.BootSector , module NTFS.File+ , module NTFS.Index , module NTFS.MFT , module NTFS.MFT.Attribute , module NTFS.MFT.Record+ , module NTFS.Path , module NTFS.Runlist , module NTFS.Volume ) where@@ -18,9 +20,11 @@ import NTFS.Attribute.StandardInformation import NTFS.BootSector import NTFS.File+import NTFS.Index import NTFS.MFT import NTFS.MFT.Attribute import NTFS.MFT.Record+import NTFS.Path import NTFS.Runlist import NTFS.Types import NTFS.Volume
src/NTFS/Attribute/FileName.hs view
@@ -7,6 +7,7 @@ , fileNameNamespaceWin32DOS , decodeUTF16LE , parseFileName+ , parseFileNameValue ) where import Data.Bits (shiftL, (.|.))@@ -62,29 +63,34 @@ case attributeValue attr of NonResidentValue{} -> Left (InvalidValue "$FILE_NAME is not resident")- ResidentValue bytes- | BS.length bytes < 0x42 ->- Left (UnexpectedEndOfData "$FILE_NAME value is truncated")- | 0x42 + 2 * fromIntegral nameLength > BS.length bytes ->- Left (UnexpectedEndOfData "$FILE_NAME name is truncated")- | otherwise ->- Right- FileName- { fileNameParent = MFTRef (word64At bytes 0x00)- , fileNameCreationTime = word64At bytes 0x08- , fileNameFileAlterTime = word64At bytes 0x10- , fileNameMFTChangeTime = word64At bytes 0x18- , fileNameFileReadTime = word64At bytes 0x20- , fileNameAllocatedSize = word64At bytes 0x28- , fileNameRealSize = word64At bytes 0x30- , fileNameFlags = word32At bytes 0x38- , fileNameReparse = word32At bytes 0x3c- , fileNameNameLength = nameLength- , fileNameNamespace = BS.index bytes 0x41- , fileNameName = decodeUTF16LE (BS.take (2 * fromIntegral nameLength) (BS.drop 0x42 bytes))- }- where- nameLength = BS.index bytes 0x40+ ResidentValue bytes -> parseFileNameValue bytes++-- | Interpret the value bytes of a @$FILE_NAME@ attribute. Index entries hold+-- this value directly, without an attribute header.+parseFileNameValue :: BS.ByteString -> Either ParseError FileName+parseFileNameValue bytes+ | BS.length bytes < 0x42 =+ Left (UnexpectedEndOfData "$FILE_NAME value is truncated")+ | 0x42 + 2 * fromIntegral nameLength > BS.length bytes =+ Left (UnexpectedEndOfData "$FILE_NAME name is truncated")+ | otherwise =+ Right+ FileName+ { fileNameParent = MFTRef (word64At bytes 0x00)+ , fileNameCreationTime = word64At bytes 0x08+ , fileNameFileAlterTime = word64At bytes 0x10+ , fileNameMFTChangeTime = word64At bytes 0x18+ , fileNameFileReadTime = word64At bytes 0x20+ , fileNameAllocatedSize = word64At bytes 0x28+ , fileNameRealSize = word64At bytes 0x30+ , fileNameFlags = word32At bytes 0x38+ , fileNameReparse = word32At bytes 0x3c+ , fileNameNameLength = nameLength+ , fileNameNamespace = BS.index bytes 0x41+ , fileNameName = decodeUTF16LE (BS.take (2 * fromIntegral nameLength) (BS.drop 0x42 bytes))+ }+ where+ nameLength = BS.index bytes 0x40 word32At :: BS.ByteString -> Int -> Word32 word32At bytes offset =
+ src/NTFS/Index.hs view
@@ -0,0 +1,191 @@+-- | Directory index entries from @$INDEX_ROOT@ and @$INDEX_ALLOCATION@.+module NTFS.Index+ ( IndexEntry (..)+ , attributeTypeIndexRoot+ , attributeTypeIndexAllocation+ , parseIndexRoot+ , parseIndexRecord+ , readDirectoryEntries+ , findDirectoryEntry+ ) where++import Data.Bits (shiftL, (.&.), (.|.))+import qualified Data.ByteString as BS+import Data.Word (Word16, Word32, Word64)++import NTFS.Attribute.Data (FileData (..))+import NTFS.Attribute.FileName (FileName (..), parseFileNameValue)+import NTFS.BootSector+import NTFS.MFT.Attribute+import NTFS.MFT.Record (MFTRecord (..), RawAttribute (..),+ fixupUpdateSequence)+import NTFS.Runlist (DataRun (..), parseRunlist)+import NTFS.Types+import NTFS.Volume++-- | Type code of the @$INDEX_ROOT@ attribute.+attributeTypeIndexRoot :: Word32+attributeTypeIndexRoot = 0x90++-- | Type code of the @$INDEX_ALLOCATION@ attribute.+attributeTypeIndexAllocation :: Word32+attributeTypeIndexAllocation = 0xa0++-- | One entry in a directory index. Entries pointing only to a child node are+-- omitted because they carry no file name.+data IndexEntry = IndexEntry+ { indexFileReference :: MFTRef+ , indexFileName :: FileName+ , indexChildVCN :: Maybe VCN+ } deriving (Eq, Show)++-- | Parse the resident @$INDEX_ROOT@ of a directory record.+parseIndexRoot :: Attribute -> Either ParseError [IndexEntry]+parseIndexRoot attr+ | attributeTypeCode attr /= attributeTypeIndexRoot =+ Left (InvalidValue "attribute is not $INDEX_ROOT")+ | otherwise =+ case attributeValue attr of+ NonResidentValue{} -> Left (InvalidValue "$INDEX_ROOT is not resident")+ ResidentValue bytes+ | BS.length bytes < 0x20 ->+ Left (UnexpectedEndOfData "$INDEX_ROOT is truncated")+ | indexedType /= attributeTypeFileName ->+ Left (InvalidValue "$INDEX_ROOT does not index $FILE_NAME")+ | entryEnd > BS.length bytes ->+ Left (UnexpectedEndOfData "$INDEX_ROOT entries are truncated")+ | otherwise -> parseEntries entryStart entryEnd bytes+ where+ indexedType = word32At bytes 0x00+ entryStart = 0x10 + fromIntegral (word32At bytes 0x10)+ entryEnd = 0x10 + fromIntegral (word32At bytes 0x14)++-- | Parse one @INDX@ block from @$INDEX_ALLOCATION@, applying its fix-up.+parseIndexRecord :: Word16 -> BS.ByteString -> Either ParseError [IndexEntry]+parseIndexRecord bytesPerSector bytes+ | BS.length bytes < 0x28 =+ Left (UnexpectedEndOfData "index record is truncated")+ | BS.take 4 bytes /= indexSignature =+ Left (InvalidValue "index record signature is not INDX")+ | otherwise = do+ fixed <- fixupUpdateSequence bytesPerSector bytes+ let entryStart = 0x18 + fromIntegral (word32At fixed 0x18)+ entryEnd = 0x18 + fromIntegral (word32At fixed 0x1c)+ if entryEnd > BS.length fixed+ then Left (UnexpectedEndOfData "index record entries are truncated")+ else parseEntries entryStart entryEnd fixed++-- | Read all named entries of a directory record, including @$INDEX_ALLOCATION@.+readDirectoryEntries ::+ Volume -> MFTRecord -> IO (Either ParseError [IndexEntry])+readDirectoryEntries volume record =+ case traverse parseAttribute (filter isIndex (recordAttributes record)) of+ Left err -> pure (Left err)+ Right attrs -> do+ let roots = filter ((== attributeTypeIndexRoot) . attributeTypeCode) attrs+ allocations = filter ((== attributeTypeIndexAllocation) . attributeTypeCode) attrs+ case traverse parseIndexRoot roots of+ Left err -> pure (Left err)+ Right rootEntries -> do+ blocks <- traverse (readIndexAllocation volume) allocations+ pure $ fmap (\groups -> concat rootEntries ++ concat groups) (sequence blocks)+ where+ isIndex raw =+ rawAttributeType raw == attributeTypeIndexRoot+ || rawAttributeType raw == attributeTypeIndexAllocation++-- | Find an entry by exact file name. NTFS name collation is not applied.+findDirectoryEntry ::+ Volume -> MFTRecord -> String -> IO (Either ParseError (Maybe IndexEntry))+findDirectoryEntry volume record name = do+ entries <- readDirectoryEntries volume record+ pure $ fmap (findNamed name) entries++findNamed :: String -> [IndexEntry] -> Maybe IndexEntry+findNamed name entries =+ case filter ((== name) . fileNameName . indexFileName) entries of+ entry:_ -> Just entry+ [] -> Nothing++readIndexAllocation :: Volume -> Attribute -> IO (Either ParseError [IndexEntry])+readIndexAllocation volume attr+ | attributeTypeCode attr /= attributeTypeIndexAllocation =+ pure (Left (InvalidValue "attribute is not $INDEX_ALLOCATION"))+ | otherwise =+ case attributeValue attr of+ ResidentValue _ -> pure (Left (InvalidValue "$INDEX_ALLOCATION is not non-resident"))+ NonResidentValue _ lastVCN runlistBytes allocated real initialized ->+ case parseRunlist runlistBytes of+ Left err -> pure (Left err)+ Right runs+ | null runs -> pure (Left (InvalidValue "$INDEX_ALLOCATION runlist is empty"))+ | sum (map runLength runs) <= fromIntegral (vcnNumber lastVCN) ->+ pure (Left (InvalidValue "$INDEX_ALLOCATION runlist does not cover its VCN range"))+ | otherwise -> do+ bytes <- readData volume (NonResidentData runs allocated real initialized)+ pure $ do+ content <- bytes+ ClusterSize blockSize <- indexRecordSize (volumeBootSector volume)+ let blocks = chunks (fromIntegral blockSize) content+ sectorBytes = bsBytesPerSector (volumeBootSector volume)+ concat <$> traverse (parseIndexRecord sectorBytes) blocks++parseEntries :: Int -> Int -> BS.ByteString -> Either ParseError [IndexEntry]+parseEntries start end bytes = go start []+ where+ go offset acc+ | offset >= end = Right (reverse acc)+ | offset + 0x10 > end =+ Left (UnexpectedEndOfData "index entry header is truncated")+ | entryLength < 0x10 = Left (InvalidValue "index entry length is too small")+ | offset + entryLength > end =+ Left (UnexpectedEndOfData "index entry extends past index area")+ | keyLength == 0 = go (offset + entryLength) acc+ | keyEnd > offset + entryLength =+ Left (UnexpectedEndOfData "index entry key is truncated")+ | otherwise = do+ name <- parseFileNameValue (BS.take keyLength (BS.drop keyStart bytes))+ child <- parseChild flags keyEnd (offset + entryLength)+ let next = IndexEntry (MFTRef fileRefValue) name child : acc+ if flags .&. 0x01 /= 0+ then Right (reverse next)+ else go (offset + entryLength) next+ where+ fileRefValue = word64At bytes offset+ entryLength = fromIntegral (word16At bytes (offset + 0x08))+ keyLength = fromIntegral (word16At bytes (offset + 0x0a))+ flags = word16At bytes (offset + 0x0c)+ keyStart = offset + 0x10+ keyEnd = keyStart + keyLength++ parseChild flags position limit+ | flags .&. 0x02 == 0 = Right Nothing+ | position + 8 > limit =+ Left (UnexpectedEndOfData "index entry child VCN is truncated")+ | otherwise = Right (Just (VCN (fromIntegral (word64At bytes position))))++indexSignature :: BS.ByteString+indexSignature = BS.pack [0x49, 0x4e, 0x44, 0x58]++vcnNumber :: VCN -> Integer+vcnNumber (VCN value) = toInteger value++chunks :: Int -> BS.ByteString -> [BS.ByteString]+chunks size bytes+ | BS.null bytes = []+ | otherwise = BS.take size bytes : chunks size (BS.drop size bytes)++word16At :: BS.ByteString -> Int -> Word16+word16At bytes offset =+ fromIntegral (BS.index bytes offset)+ .|. (fromIntegral (BS.index bytes (offset + 1)) `shiftL` 8)++word32At :: BS.ByteString -> Int -> Word32+word32At bytes offset =+ fromIntegral (word16At bytes offset)+ .|. (fromIntegral (word16At bytes (offset + 2)) `shiftL` 16)++word64At :: BS.ByteString -> Int -> Word64+word64At bytes offset =+ fromIntegral (word32At bytes offset)+ .|. (fromIntegral (word32At bytes (offset + 4)) `shiftL` 32)
src/NTFS/MFT/Record.hs view
@@ -5,6 +5,7 @@ , parseMFTRecord , isRecordInUse , isDirectoryRecord+ , fixupUpdateSequence ) where import Data.Bits (shiftL, shiftR, (.&.), (.|.))@@ -60,14 +61,22 @@ | BS.take 4 bytes /= fileSignature = Left (InvalidValue "MFT record signature is not FILE") | bytesPerSector < 2 = Left (InvalidValue "bytes per sector is too small")+ | otherwise = fixupUpdateSequence bytesPerSector bytes++-- | Restore the sector trailers listed in a record's update sequence array.+-- The caller is responsible for checking the record signature.+fixupUpdateSequence :: Word16 -> BS.ByteString -> Either ParseError BS.ByteString+fixupUpdateSequence bytesPerSector bytes+ | BS.length bytes < 8 = Left (UnexpectedEndOfData "record header is truncated")+ | bytesPerSector < 2 = Left (InvalidValue "bytes per sector is too small") | usaCount == 0 = Left (InvalidValue "update sequence array is empty")- | usaOffset < 0x30 = Left (InvalidValue "update sequence array offset is too small")+ | usaOffset < 0x08 = Left (InvalidValue "update sequence array offset is too small") | usaOffset + 2 * usaCount > BS.length bytes = Left (UnexpectedEndOfData "update sequence array is truncated") | any outOfRange positions =- Left (UnexpectedEndOfData "MFT record sector trailer is truncated")+ Left (UnexpectedEndOfData "record sector trailer is truncated") | any trailerMismatch positions =- Left (InvalidValue "MFT record update sequence number mismatch")+ Left (InvalidValue "record update sequence number mismatch") | otherwise = Right (foldl replace bytes (zip positions replacements)) where usaOffset = fromIntegral (word16At bytes 0x04)
+ src/NTFS/Path.hs view
@@ -0,0 +1,73 @@+-- | Path components resolved by following parent MFT references.+module NTFS.Path+ ( rootDirectoryRef+ , readRecordPath+ , readEntryPath+ ) where++import NTFS.Attribute.FileName (FileName (..), fileNameNamespaceDOS,+ fileNameNamespacePosix,+ fileNameNamespaceWin32,+ fileNameNamespaceWin32DOS)+import NTFS.BootSector (bsBytesPerSector)+import NTFS.File (FileEntry (..), isEntryInUse,+ parseFileEntry)+import NTFS.MFT.Record (parseMFTRecord)+import NTFS.Types+import NTFS.Volume (Volume (..), readMFTRecordBytes)++-- | MFT reference of the root directory.+rootDirectoryRef :: MFTRef+rootDirectoryRef = MFTRef 5++-- | Resolve a record to its path components, starting at the root.+-- The root directory itself contributes no component.+readRecordPath :: Volume -> MFTRef -> IO (Either ParseError [String])+readRecordPath volume start = go start [] []+ where+ sectorBytes = bsBytesPerSector (volumeBootSector volume)++ go ref acc visited+ | mftRefNumber ref `elem` visited =+ pure (Left (InvalidValue "cycle in MFT parent chain"))+ | otherwise = do+ bytes <- readMFTRecordBytes volume ref+ case bytes >>= parseMFTRecord sectorBytes >>= parseFileEntry of+ Left err -> pure (Left err)+ Right entry+ | not (isEntryInUse entry) ->+ pure (Left (InvalidValue "MFT record is not in use"))+ | otherwise ->+ case preferredName (entryFileNames entry) of+ Nothing -> pure (Left (InvalidValue "MFT record has no $FILE_NAME"))+ Just name -> stepCurrent name+ where+ stepCurrent name+ | component == "." = pure (Right acc)+ | mftRefNumber parent == mftRefNumber ref =+ pure (Right (component : acc))+ | otherwise = go parent (component : acc) (mftRefNumber ref : visited)+ where+ parent = fileNameParent name+ component = fileNameName name++-- | Resolve a combined file entry to its path components.+readEntryPath :: Volume -> FileEntry -> IO (Either ParseError [String])+readEntryPath volume entry =+ readRecordPath volume (MFTRef (fromIntegral (entryRecordNumber entry)))++preferredName :: [FileName] -> Maybe FileName+preferredName names =+ case+ [ name+ | namespace <-+ [ fileNameNamespaceWin32+ , fileNameNamespaceWin32DOS+ , fileNameNamespaceDOS+ , fileNameNamespacePosix+ ]+ , name <- names+ , fileNameNamespace name == namespace+ ] of+ name:_ -> Just name+ [] -> Nothing
src/NTFS/Volume.hs view
@@ -42,11 +42,11 @@ -- | Read the bytes of one MFT record. readMFTRecordBytes :: Volume -> MFTRef -> IO (Either ParseError BS.ByteString)-readMFTRecordBytes volume (MFTRef ref) =+readMFTRecordBytes volume ref = case mftRecordSize (volumeBootSector volume) of Left err -> pure (Left err) Right (ClusterSize size) -> do- let offset = mftOffset volume + fromIntegral ref * fromIntegral size+ let offset = mftOffset volume + fromIntegral (mftRefNumber ref) * fromIntegral size hSeek (volumeHandle volume) AbsoluteSeek offset bytes <- BS.hGet (volumeHandle volume) (fromIntegral size) if fromIntegral (BS.length bytes) == size
test/Spec.hs view
@@ -18,12 +18,15 @@ import NTFS.Attribute.StandardInformation import NTFS.BootSector import NTFS.File+import NTFS.Index import NTFS.MFT import NTFS.MFT.Attribute import NTFS.MFT.Record+import NTFS.Path import NTFS.Runlist import NTFS.Types (ClusterSize (..), LCN (..),- MFTRef (..), VCN (..),+ MFTRef (..),+ ParseError (..), VCN (..), mftRefNumber, mftRefSequence) import NTFS.Volume@@ -55,6 +58,10 @@ , ("file entry from MFT record", pure testFileEntry) , ("read resident and non-resident data", testReadData) , ("enumerate MFT records", testReadAllMFTRecords)+ , ("index root entries", pure testIndexRoot)+ , ("index record entries", pure testIndexRecord)+ , ("directory entry lookup", testDirectoryEntries)+ , ("resolve record path", testRecordPath) ] unless (and results) exitFailure @@ -362,6 +369,88 @@ ) check `finally` (hClose handle >> removeFile path) +testIndexRoot :: Bool+testIndexRoot =+ case parseAttribute (residentAttributeOf attributeTypeIndexRoot indexRootFixture)+ >>= parseIndexRoot of+ Right [entry] ->+ mftRefNumber (indexFileReference entry) == 9+ && fileNameName (indexFileName entry) == "AB"+ && indexChildVCN entry == Just (VCN 3)+ _ -> False++testIndexRecord :: Bool+testIndexRecord =+ case parseIndexRecord 512 indxFixture of+ Right [entry] ->+ mftRefNumber (indexFileReference entry) == 9+ && fileNameName (indexFileName entry) == "AB"+ && indexChildVCN entry == Nothing+ _ -> False++testDirectoryEntries :: IO Bool+testDirectoryEntries = do+ tmpDir <- getTemporaryDirectory+ (path, handle) <- openBinaryTempFile tmpDir "ntfs-index-test.img"+ let check = do+ BS.hPut handle bootSector+ hFlush handle+ opened <- openVolume handle+ case opened of+ Left _ -> pure False+ Right volume ->+ case parseMFTRecord 512 mftRecordFixture of+ Left _ -> pure False+ Right base -> do+ let record =+ base+ { recordAttributes =+ [residentAttributeOf attributeTypeIndexRoot indexRootFixture]+ }+ entries <- readDirectoryEntries volume record+ found <- findDirectoryEntry volume record "AB"+ missing <- findDirectoryEntry volume record "ZZ"+ pure+ ( fmap (map (fileNameName . indexFileName)) entries == Right ["AB"]+ && fmap (fmap (fileNameName . indexFileName)) found == Right (Just "AB")+ && fmap (fmap (fileNameName . indexFileName)) missing == Right Nothing+ )+ check `finally` (hClose handle >> removeFile path)++testRecordPath :: IO Bool+testRecordPath = do+ tmpDir <- getTemporaryDirectory+ (path, handle) <- openBinaryTempFile tmpDir "ntfs-path-test.img"+ let mftStart = 16384+ recordSize = 1024+ writeRecord number bytes =+ hSeek handle AbsoluteSeek (mftStart + fromIntegral number * recordSize)+ >> BS.hPut handle bytes+ check = do+ BS.hPut handle bootSector+ writeRecord 5 (recordWithFileName 5 5 ".")+ writeRecord 7 (recordWithFileName 7 8 "X")+ writeRecord 8 (recordWithFileName 8 7 "Y")+ writeRecord 9 (recordWithFileName 9 5 "AB")+ writeRecord 10 (recordWithFileName 10 5 "DIR")+ writeRecord 11 (recordWithFileName 11 10 "FILE")+ hFlush handle+ opened <- openVolume handle+ case opened of+ Left _ -> pure False+ Right volume -> do+ child <- readRecordPath volume (MFTRef 9)+ root <- readRecordPath volume rootDirectoryRef+ cycle <- readRecordPath volume (MFTRef 7)+ nested <- readRecordPath volume (MFTRef 11)+ pure+ ( child == Right ["AB"]+ && root == Right []+ && cycle == Left (InvalidValue "cycle in MFT parent chain")+ && nested == Right ["DIR", "FILE"]+ )+ check `finally` (hClose handle >> removeFile path)+ bootSector :: BS.ByteString bootSector = build@@ -531,6 +620,90 @@ , (0x40, [0x02]) , (0x41, [0x01]) , (0x42, [0x41, 0x00, 0x42, 0x00])+ ]++fileNameValueOf :: Word64 -> String -> BS.ByteString+fileNameValueOf ref name =+ build+ (BS.replicate (0x42 + 2 * length name) 0)+ [ (0x00, word64Bytes ref)+ , (0x40, [fromIntegral (length name)])+ , (0x41, [fileNameNamespaceWin32])+ , (0x42, concatMap (\c -> word16Bytes (fromIntegral (fromEnum c))) name)+ ]++recordWithFileName :: Word32 -> Word64 -> String -> BS.ByteString+recordWithFileName number parent name =+ build+ (BS.replicate 1024 0)+ [ (0x00, [0x46, 0x49, 0x4c, 0x45])+ , (0x04, word16Bytes 0x30)+ , (0x06, word16Bytes 3)+ , (0x10, word16Bytes 1)+ , (0x12, word16Bytes 1)+ , (0x14, word16Bytes 0x38)+ , (0x16, word16Bytes 3)+ , (0x18, word32Bytes (fromIntegral used))+ , (0x1c, word32Bytes 0x400)+ , (0x20, word64Bytes 0)+ , (0x28, word16Bytes 1)+ , (0x2c, word32Bytes number)+ , (0x30, word16Bytes 0xaa55)+ , (0x32, word16Bytes 0x1111)+ , (0x34, word16Bytes 0x2222)+ , (0x38, BS.unpack attribute)+ , (endOffset, word32Bytes 0xffffffff)+ , (0x1fe, word16Bytes 0xaa55)+ , (0x3fe, word16Bytes 0xaa55)+ ]+ where+ attribute =+ rawAttributeBytes+ (residentAttributeOf attributeTypeFileName (fileNameValueOf parent name))+ endOffset = 0x38 + BS.length attribute+ used = endOffset + 8++indexRootFixture :: BS.ByteString+indexRootFixture =+ build+ (BS.replicate 0x80 0)+ [ (0x00, word32Bytes 0x30)+ , (0x04, word32Bytes 1)+ , (0x08, word32Bytes 0x1000)+ , (0x0c, [0x01, 0x00, 0x00, 0x00])+ , (0x10, word32Bytes 0x10)+ , (0x14, word32Bytes 0x6e)+ , (0x18, word32Bytes 0x70)+ , (0x1c, word32Bytes 0)+ , (0x20, word64Bytes 0x0002000000000009)+ , (0x28, word16Bytes 0x5e)+ , (0x2a, word16Bytes 0x46)+ , (0x2c, word16Bytes 3)+ , (0x30, BS.unpack fileNameFixture)+ , (0x76, word64Bytes 3)+ ]++indxFixture :: BS.ByteString+indxFixture =+ build+ (BS.replicate 1024 0)+ [ (0x00, [0x49, 0x4e, 0x44, 0x58])+ , (0x04, word16Bytes 0x28)+ , (0x06, word16Bytes 3)+ , (0x18, word32Bytes 0x28)+ , (0x1c, word32Bytes 0x7e)+ , (0x20, word32Bytes 0x3f8)+ , (0x24, word32Bytes 0)+ , (0x28, word16Bytes 0xabcd)+ , (0x2a, word16Bytes 0x1111)+ , (0x2c, word16Bytes 0x2222)+ , (0x40, word64Bytes 0x0002000000000009)+ , (0x48, word16Bytes 0x56)+ , (0x4a, word16Bytes 0x46)+ , (0x4c, word16Bytes 1)+ , (0x50, BS.unpack fileNameFixture)+ , (0x1fe, word16Bytes 0xabcd)+ , (0x3fe, word16Bytes 0xabcd) ] residentAttributeOf :: Word32 -> BS.ByteString -> RawAttribute