Skip to content

Instantly share code, notes, and snippets.

@dradtke
Created October 16, 2012 20:01
Show Gist options
  • Select an option

  • Save dradtke/3901623 to your computer and use it in GitHub Desktop.

Select an option

Save dradtke/3901623 to your computer and use it in GitHub Desktop.
Recursively discover all files using Haskell and GIO
-- Given a File object, recursively discover all of its non-directory children
listFiles :: (FileClass f) => f -> IO ([File])
listFiles f
-- this file doesn't exist
| fileQueryExists f Nothing == False = return ([])
-- this file isn't a directory
| fileQueryFileType f [FileQueryInfoNone] Nothing /= FileTypeDirectory = return ([])
-- otherwise, create the enumerator and start looping
| otherwise = do e <- fileEnumerateChildren f "standard::*" [FileQueryInfoNone] Nothing
enumerate f e []
-- loop; takes the current file, its enumerator, and the list of results
-- it's not very pretty, but it gets the job done
where enumerate f e l = do
fileInfo <- fileEnumeratorNextFile e Nothing
case fileInfo of
Nothing -> return l
Just i -> do let fileName = fileInfoGetName i
case fileName of
Nothing -> return l
Just n -> do let c = fileGetChild f n
case (fileQueryFileType c [FileQueryInfoNone] Nothing) of
FileTypeDirectory -> (++) <$> listFiles c <*> enumerate f e l
_ -> enumerate f e $ c:l
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment