--------------------------------------------------------------------------------
-- Utility functions

newDirHandle :: HalfsCapable b t r l m =>
                InodeRef
             -> HalfsM b r l m (DirHandle r l)
newDirHandle inr = do
  rawDirBytes <- readStream inr 0 Nothing
  dirEnts     <- if BS.null rawDirBytes
                 then do return []
                 else case decode rawDirBytes of
                   Left msg -> throwError $ HE_DecodeFail_Directory msg
                   Right x  -> return x
  DirHandle
    `fmap` newRef (Just inr)
    `ap`   newRef (M.fromList $ map deName dirEnts `zip` dirEnts)
    `ap`   newRef Clean
    `ap`   newLock

-- Get directory handle's inode reference...
newDirHandle :: HalfsCapable b t r l m =>
                InodeRef
             -> HalfsM b r l m (DirHandle r l)
newDirHandle inr = do
  rawDirBytes <- readStream inr 0 Nothing
  dirEnts     <- if BS.null rawDirBytes
                 then do return []
                 else case decode rawDirBytes of
                   Left msg -> throwError $ HE_DecodeFail_Directory msg
                   Right x  -> return x
  DirHandle
    `fmap` newRef (Just inr)
    `ap`   newRef (M.fromList $ map deName dirEnts `zip` dirEnts)
    `ap`   newRef Clean
    `ap`   newLock

-- Get directory handle's inode reference...
newDirHandle inr = do
  rawDirBytes <- readStream inr 0 Nothing
  dirEnts     <- if BS.null rawDirBytes
                 then do return []
                 else case decode rawDirBytes of
                   Left msg -> throwError $ HE_DecodeFail_Directory msg
                   Right x  -> return x
  DirHandle
    `fmap` newRef (Just inr)
    `ap`   newRef (M.fromList $ map deName dirEnts `zip` dirEnts)
    `ap`   newRef Clean
    `ap`   newLock

-- Get directory handle's inode reference...