<commit_before>-----------------------------------------------------------------------------
-- |
-- Module      : Data.EXT2.UsageBitmaps
-- Copyright   : (C) 2015 <NAME>,
--                   2015 <NAME>
-- License     : MIT (see LICENSE file)
-- Maintainer  : <NAME> <<EMAIL>>,
--               <NAME> <<EMAIL>>
-- Stability   : experimental
-- Portability : ghc
module Data.EXT2.UsageBitmaps
( BlockUsageBitmap(..)
, InodeUsageBitmap(..)
, blockUsageBool
, inodeUsageBool
, fetchUsageBitmaps
) where

import Control.Lens
import Control.Monad
import Data.Binary.Get
import Data.Bits.Bitwise (toListBE)
import qualified Data.ByteString.Lazy as LBS
import Data.EXT2.BlockGroupDescriptor
import Data.EXT2.Superblock
import Data.Functor
import Data.Word
import System.IO

data BlockUsageBitmap = BlockUsageBitmap Integer [Word8] deriving (Eq)
data InodeUsageBitmap = InodeUsageBitmap Integer [Word8] deriving (Eq)

lenUsageBitmaps :: Integral a => Superblock -> a
lenUsageBitmaps sb = sb ^. logBlockSize . to fromIntegral
{-# INLINE lenUsageBitmaps #-}

instance Show BlockUsageBitmap where
  show bm@(BlockUsageBitmap len _) =
    concatMap (\bool -> if bool then "1" else "0") (blockUsageBool bm) ++
    "(Len: " ++ show len ++ ")"

instance Show InodeUsageBitmap where
  show bm@(InodeUsageBitmap len _) =
    concatMap (\bool -> if bool then "1" else "0") (inodeUsageBool bm) ++
    "(Len: " ++ show len ++ ")"

blockUsageBool :: BlockUsageBitmap -> [Bool]
blockUsageBool (BlockUsageBitmap len words') =
  take (fromIntegral len) $ concatMap toListBE words'
{-# INLINE blockUsageBool #-}

inodeUsageBool :: InodeUsageBitmap -> [Bool]
inodeUsageBool (InodeUsageBitmap len words') =
  take (fromIntegral len) $ concatMap toListBE words'
{-# INLINE inodeUsageBool #-}

fetchUsageBitmaps :: Superblock -> BlockGroupDescriptor -> Handle ->
                     IO (BlockUsageBitmap, InodeUsageBitmap)
fetchUsageBitmaps sb bgd handle = do
  hSeek handle AbsoluteSeek $ blockOffset sb $ bgd ^. blockUsageAddr
  blockUsage <- runGet (getBlockUsageBitmap sb) <$>
                LBS.hGet handle (lenUsageBitmaps sb)
  hSeek handle AbsoluteSeek $ blockOffset sb $ bgd ^. inodeUsageAddr
  inodeUsage <- runGet (getInodeUsageBitmap sb) <$>
                LBS.hGet handle (lenUsageBitmaps sb)
  return (blockUsage, inodeUsage)

getBlockUsageBitmap :: Superblock -> Get BlockUsageBitmap
getBlockUsageBitmap sb =
  BlockUsageBitmap (sb ^. blocksCount) <$>
    replicateM (sb ^. logBlockSize . to fromIntegral) getWord8
{-# INLINE getBlockUsageBitmap #-}

getInodeUsageBitmap :: Superblock -> Get InodeUsageBitmap
getInodeUsageBitmap sb =
  InodeUsageBitmap (sb ^. inodesCount) <$>
    replicateM (sb ^. logBlockSize . to fromIntegral) getWord8
{-# INLINE getInodeUsageBitmap #-}<commit_msg>Usage bitmaps appeared to have the wrong size<commit_after>-----------------------------------------------------------------------------
-- |
-- Module      : Data.EXT2.UsageBitmaps
-- Copyright   : (C) 2015 <NAME>,
--                   2015 <NAME>
-- License     : MIT (see LICENSE file)
-- Maintainer  : <NAME> <<EMAIL>>,
--               <NAME> <<EMAIL>>
-- Stability   : experimental
-- Portability : ghc
module Data.EXT2.UsageBitmaps
( BlockUsageBitmap(..)
, InodeUsageBitmap(..)
, blockUsageBool
, inodeUsageBool
, fetchUsageBitmaps
) where

import Control.Lens
import Control.Monad
import Data.Binary.Get
import Data.Bits.Bitwise (toListBE)
import qualified Data.ByteString.Lazy as LBS
import Data.EXT2.BlockGroupDescriptor
import Data.EXT2.Superblock
import Data.Functor
import Data.Word
import System.IO

data BlockUsageBitmap = BlockUsageBitmap Integer [Word8] deriving (Eq)
data InodeUsageBitmap = InodeUsageBitmap Integer [Word8] deriving (Eq)

lenUsageBitmaps :: Integral a => Superblock -> a
lenUsageBitmaps sb = sb ^. logBlockSize . to fromIntegral
{-# INLINE lenUsageBitmaps #-}

instance Show BlockUsageBitmap where
  show bm@(BlockUsageBitmap len _) =
    concatMap (\bool -> if bool then "1" else "0") (blockUsageBool bm) ++
    "(Len: " ++ show len ++ ")"

instance Show InodeUsageBitmap where
  show bm@(InodeUsageBitmap len _) =
    concatMap (\bool -> if bool then "1" else "0") (inodeUsageBool bm) ++
    "(Len: " ++ show len ++ ")"

blockUsageBool :: BlockUsageBitmap -> [Bool]
blockUsageBool (BlockUsageBitmap len words') =
  take (fromIntegral len) $ concatMap toListBE words'
{-# INLINE blockUsageBool #-}

inodeUsageBool :: InodeUsageBitmap -> [Bool]
inodeUsageBool (InodeUsageBitmap len words') =
  take (fromIntegral len) $ concatMap toListBE words'
{-# INLINE inodeUsageBool #-}

fetchUsageBitmaps :: Superblock -> BlockGroupDescriptor -> Handle ->
                     IO (BlockUsageBitmap, InodeUsageBitmap)
fetchUsageBitmaps sb bgd handle = do
  hSeek handle AbsoluteSeek $ blockOffset sb $ bgd ^. blockUsageAddr
  blockUsage <- runGet (getBlockUsageBitmap sb) <$>
                LBS.hGet handle (lenUsageBitmaps sb)
  hSeek handle AbsoluteSeek $ blockOffset sb $ bgd ^. inodeUsageAddr
  inodeUsage <- runGet (getInodeUsageBitmap sb) <$>
                LBS.hGet handle (lenUsageBitmaps sb)
  return (blockUsage, inodeUsage)

getBlockUsageBitmap :: Superblock -> Get BlockUsageBitmap
getBlockUsageBitmap sb =
  BlockUsageBitmap (sb ^. blocksPerGroup) <$>
    replicateM (sb ^. logBlockSize . to fromIntegral) getWord8
{-# INLINE getBlockUsageBitmap #-}

getInodeUsageBitmap :: Superblock -> Get InodeUsageBitmap
getInodeUsageBitmap sb =
  InodeUsageBitmap (sb ^. inodesPerGroup) <$>
    replicateM (sb ^. logBlockSize . to fromIntegral) getWord8
{-# INLINE getInodeUsageBitmap #-}
{'fraction_non_alphanumeric': 0.13335716839470863, 'fraction_numerical': 0.007150518412584912, 'mean_word_length': 3.3985849056603774, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7633619', 'n_tokens_mistral': 1721, 'n_tokens_neox': 1679, 'n_words': 469}