Skip to content

Instantly share code, notes, and snippets.

@kig
Created November 26, 2008 03:45
Show Gist options
  • Save kig/29264 to your computer and use it in GitHub Desktop.
Save kig/29264 to your computer and use it in GitHub Desktop.
import System.Directory
import System.FilePath
import System.Posix.Files
import System.Posix.Types
import System.Posix.User
import Data.List (partition)
data DirInfo = DirInfo {
info :: FileInfo,
size :: Integer,
count :: Integer,
subTreeSize :: Integer,
subTreeCount :: Integer,
files :: [FileInfo],
dirs :: [FileInfo]
}
data FileInfo = FileInfo {
path :: String,
stat :: FileStatus
}
getUserName uid = do
u <- getUserEntryForID uid
return $ userName u
getGroupName gid = do
g <- getGroupEntryForID gid
return $ groupName g
getFileInfo s = do
fullPath <- canonicalizePath s
stat <- getFileStatus fullPath
return $ FileInfo {
path = fullPath,
stat = stat
}
dirFromInfo info = do
rentries <- getDirectoryContents (path info)
let entries = filter (\s -> s /= "." && s /= "..") rentries
entryInfos <- mapM (\s -> getFileInfo $ joinPath [path info, s]) entries
let (dirs, files) = partition (isDirectory.stat) entryInfos
return $ DirInfo {
info = info,
size = sum $ map (fromIntegral.fileSize.stat) files,
count = fromIntegral $ length entryInfos,
subTreeSize = 0,
subTreeCount = 0,
files = files,
dirs = dirs
}
dirFromPath s = dirFromInfo =<< getFileInfo s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment