~ubuntu-branches/ubuntu/raring/gitit/raring

« back to all changes in this revision

Viewing changes to Network/Gitit/Util.hs

  • Committer: Package Import Robot
  • Author(s): Colin Watson
  • Date: 2013-04-15 17:35:07 UTC
  • mfrom: (15.1.3 raring-proposed)
  • Revision ID: package-import@ubuntu.com-20130415173507-83npbivi5qqezt1y
Tags: 0.10.3.1-2ubuntu1
* Resynchronise with Debian experimental.  Remaining changes:
  - Undo port to filestore 0.6 - we'd ideally like to keep 0.5 for Raring.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
{-# LANGUAGE CPP #-}
 
1
{-# LANGUAGE CPP, ScopedTypeVariables #-}
2
2
{-
3
3
Copyright (C) 2009 John MacFarlane <jgm@berkeley.edu>
4
4
This program is free software; you can redistribute it and/or modify
33
33
import System.IO.Error (isAlreadyExistsError)
34
34
import Control.Monad.Trans (liftIO)
35
35
import Data.Char (toLower)
36
 
import Data.ByteString.Lazy.UTF8 (toString)
37
 
import qualified Data.ByteString.Lazy as B
38
36
import Network.Gitit.Types
39
 
import Control.Monad (liftM)
40
 
#if MIN_VERSION_base(4,5,0)
41
 
#else
42
 
import Codec.Binary.UTF8.String (encodeString)
43
 
#endif
 
37
import qualified Control.Exception as E
 
38
import qualified Text.Pandoc.UTF8 as UTF8
44
39
 
45
40
-- | Read file as UTF-8 string.  Encode filename as UTF-8.
46
41
readFileUTF8 :: FilePath -> IO String
47
 
#if MIN_VERSION_base(4,5,0)
48
 
readFileUTF8 f = liftM toString $ B.readFile f
49
 
#else
50
 
readFileUTF8 f = liftM toString $ B.readFile $ encodeString f
51
 
#endif
 
42
readFileUTF8 = UTF8.readFile
52
43
 
53
44
-- | Perform a function a directory and return to working directory.
54
45
inDir :: FilePath -> IO a -> IO a
61
52
 
62
53
-- | Perform a function in a temporary directory and clean up.
63
54
withTempDir :: FilePath -> (FilePath -> IO a) -> IO a
64
 
withTempDir baseName = bracket (createTempDir 0 baseName) removeDirectoryRecursive
 
55
withTempDir baseName f = do
 
56
  oldDir <- getCurrentDirectory
 
57
  bracket (createTempDir 0 baseName)
 
58
          (\tmp -> setCurrentDirectory oldDir >> removeDirectoryRecursive tmp)
 
59
          f
65
60
 
66
61
-- | Create a temporary directory with a unique name.
67
62
createTempDir :: Integer -> FilePath -> IO FilePath
68
63
createTempDir num baseName = do
69
64
  sysTempDir <- getTemporaryDirectory
70
65
  let dirName = sysTempDir </> baseName <.> show num
71
 
  liftIO $ catch (createDirectory dirName >> return dirName) $
 
66
  liftIO $ E.catch (createDirectory dirName >> return dirName) $
72
67
      \e -> if isAlreadyExistsError e
73
68
               then createTempDir (num + 1) baseName
74
69
               else ioError e