Utils.hs 42.9 KB
Newer Older
1
{-# LANGUAGE CPP, ForeignFunctionInterface #-}
2
3
4
-----------------------------------------------------------------------------
-- |
-- Module      :  Distribution.Simple.Utils
ijones's avatar
ijones committed
5
-- Copyright   :  Isaac Jones, Simon Marlow 2003-2004
Duncan Coutts's avatar
Duncan Coutts committed
6
--                portions Copyright (c) 2007, Galois Inc.
7
--
Duncan Coutts's avatar
Duncan Coutts committed
8
-- Maintainer  :  cabal-devel@haskell.org
ijones's avatar
ijones committed
9
-- Portability :  portable
10
--
Duncan Coutts's avatar
Duncan Coutts committed
11
12
13
14
15
-- A large and somewhat miscellaneous collection of utility functions used
-- throughout the rest of the Cabal lib and in other tools that use the Cabal
-- lib like @cabal-install@. It has a very simple set of logging actions. It
-- has low level functions for running programs, a bunch of wrappers for
-- various directory and file functions that do extra logging.
16

ijones's avatar
ijones committed
17
{- All rights reserved.
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials provided
      with the distribution.

    * Neither the name of Isaac Jones nor the names of other
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -}

module Distribution.Simple.Utils (
48
        cabalVersion,
49
50

        -- * logging and errors
51
52
        die,
        dieWithLocation,
53
        topHandler, topHandlerWith,
54
        warn, notice, setupMessage, info, debug,
55
        debugNoWrap, chattyTry,
56
57

        -- * running programs
58
        rawSystemExit,
tibbe's avatar
tibbe committed
59
        rawSystemExitCode,
60
        rawSystemExitWithEnv,
61
        rawSystemStdout,
62
        rawSystemStdInOut,
63
        rawSystemIOWithEnv,
64
        maybeExit,
65
        xargs,
66
        findProgramLocation,
67
        findProgramVersion,
68
69

        -- * copying files
70
        smartCopySources,
71
        createDirectoryIfMissingVerbose,
72
        copyFileVerbose,
73
        copyDirectoryRecursiveVerbose,
74
        copyFiles,
refold's avatar
refold committed
75
        copyFileTo,
76

77
78
79
        -- * installing files
        installOrdinaryFile,
        installExecutableFile,
refold's avatar
refold committed
80
        installMaybeExecutableFile,
81
        installOrdinaryFiles,
refold's avatar
refold committed
82
83
        installExecutableFiles,
        installMaybeExecutableFiles,
84
85
        installDirectoryContents,

86
87
88
89
        -- * File permissions
        setFileOrdinary,
        setFileExecutable,

90
        -- * file names
ijones's avatar
ijones committed
91
        currentDir,
92
93

        -- * finding files
94
        findFile,
95
        findFirstFile,
96
97
        findFileWithExtension,
        findFileWithExtension',
98
99
100
        findModuleFile,
        findModuleFiles,
        getDirectoryContentsRecursive,
101

102
103
104
        -- * environment variables
        isInSearchPath,

105
106
        -- * simple file globbing
        matchFileGlob,
Ian Lynagh's avatar
Ian Lynagh committed
107
        matchDirFileGlob,
108
109
        parseFileGlob,
        FileGlob(..),
110

Duncan Coutts's avatar
Duncan Coutts committed
111
        -- * temp files and dirs
112
        withTempFile,
Duncan Coutts's avatar
Duncan Coutts committed
113
        withTempDirectory,
114
115

        -- * .cabal and .buildinfo files
116
117
        defaultPackageDesc,
        findPackageDesc,
118
119
        defaultHookedPackageDesc,
        findHookedPackageDesc,
120

121
122
123
        -- * reading and writing files safely
        withFileContents,
        writeFileAtomic,
124
        rewriteFile,
125

126
127
128
        -- * Unicode
        fromUTF8,
        toUTF8,
129
        readUTF8File,
130
        withUTF8FileContents,
131
        writeUTF8File,
132
        normaliseLineEndings,
133

134
135
136
137
138
139
140
        -- * generic utils
        equating,
        comparing,
        isInfixOf,
        intercalate,
        lowercase,
        wrapText,
141
        wrapLine,
142
143
  ) where

144
import Control.Monad
145
    ( when, unless, filterM )
146
147
import Control.Concurrent.MVar
    ( newEmptyMVar, putMVar, takeMVar )
148
import Data.List
149
  ( nub, unfoldr, isPrefixOf, tails, intercalate )
150
import Data.Char as Char
151
152
    ( toLower, chr, ord )
import Data.Bits
153
    ( Bits((.|.), (.&.), shiftL, shiftR) )
154
155
import qualified Data.ByteString.Lazy as BS
import qualified Data.ByteString.Lazy.Char8 as BS.Char8
156
157

import System.Directory
refold's avatar
refold committed
158
159
    ( Permissions(executable), getDirectoryContents, getPermissions
    , doesDirectoryExist, doesFileExist, removeFile, findExecutable )
160
161
162
163
164
import System.Environment
    ( getProgName )
import System.Exit
    ( exitWith, ExitCode(..) )
import System.FilePath
165
166
    ( normalise, (</>), (<.>)
    , getSearchPath, takeDirectory, splitFileName
167
    , splitExtension, splitExtensions, splitDirectories )
168
import System.Directory
169
    ( createDirectory, renameFile, removeDirectoryRecursive )
170
import System.IO
171
172
    ( Handle, openFile, openBinaryFile, openBinaryTempFile
    , IOMode(ReadMode), hSetBinaryMode
173
    , hGetContents, stderr, stdout, hPutStr, hFlush, hClose )
174
import System.IO.Error as IO.Error
175
176
    ( isDoesNotExistError, isAlreadyExistsError
    , ioeSetFileName, ioeGetFileName, ioeGetErrorString )
Duncan Coutts's avatar
Duncan Coutts committed
177
178
import System.IO.Error
    ( ioeSetLocation, ioeGetLocation )
179
180
import System.IO.Unsafe
    ( unsafeInterleaveIO )
181
import qualified Control.Exception as Exception
182

183
import Distribution.Text
184
    ( display, simpleParse )
185
import Distribution.Package
186
    ( PackageIdentifier )
187
188
import Distribution.ModuleName (ModuleName)
import qualified Distribution.ModuleName as ModuleName
189
190
import Distribution.Version
    (Version(..))
191

192
import Control.Exception (IOException, evaluate, throwIO)
193
import System.Process (rawSystem, runProcess)
Malcolm.Wallace's avatar
Malcolm.Wallace committed
194

195
196
import Control.Concurrent (forkIO)
import System.Process (runInteractiveProcess, waitForProcess)
197
#if __GLASGOW_HASKELL__ >= 702
198
199
import System.Process (showCommandForUser)
#endif
simonmar's avatar
simonmar committed
200

201
import Distribution.Compat.CopyFile
202
         ( copyFile, copyOrdinaryFile, copyExecutableFile
203
         , setFileOrdinary, setFileExecutable, setDirOrdinary )
204
import Distribution.Compat.TempFile
205
         ( openTempFile, createTempDirectory )
206
import Distribution.Compat.Exception
207
         ( tryIO, catchIO, catchExit )
208
import Distribution.Verbosity
209

210
211
#ifdef VERSION_base
import qualified Paths_Cabal (version)
212
213
#endif

214
215
-- We only get our own version number when we're building with ourselves
cabalVersion :: Version
216
#if defined(VERSION_base)
217
cabalVersion = Paths_Cabal.version
218
219
#elif defined(CABAL_VERSION)
cabalVersion = Version [CABAL_VERSION] []
220
#else
221
cabalVersion = Version [1,9999] []  --used when bootstrapping
222
223
#endif

224
225
-- ----------------------------------------------------------------------------
-- Exception and logging utils
ijones's avatar
ijones committed
226

227
228
dieWithLocation :: FilePath -> Maybe Int -> String -> IO a
dieWithLocation filename lineno msg =
229
230
231
232
233
234
  ioError . setLocation lineno
          . flip ioeSetFileName (normalise filename)
          $ userError msg
  where
    setLocation Nothing  err = err
    setLocation (Just n) err = ioeSetLocation err (show n)
simonmar's avatar
simonmar committed
235
236

die :: String -> IO a
237
238
die msg = ioError (userError msg)

239
240
topHandlerWith :: (Exception.IOException -> IO a) -> IO a -> IO a
topHandlerWith cont prog = catchIO prog handle
241
242
243
244
245
  where
    handle ioe = do
      hFlush stdout
      pname <- getProgName
      hPutStr stderr (mesage pname)
246
      cont ioe
247
248
249
250
251
252
253
254
255
      where
        mesage pname = wrapText (pname ++ ": " ++ file ++ detail)
        file         = case ioeGetFileName ioe of
                         Nothing   -> ""
                         Just path -> path ++ location ++ ": "
        location     = case ioeGetLocation ioe of
                         l@(n:_) | n >= '0' && n <= '9' -> ':' : l
                         _                              -> ""
        detail       = ioeGetErrorString ioe
simonmar's avatar
simonmar committed
256

257
258
259
topHandler :: IO a -> IO a
topHandler prog = topHandlerWith (const $ exitWith (ExitFailure 1)) prog

260
261
262
263
-- | Non fatal conditions that may be indicative of an error or problem.
--
-- We display these at the 'normal' verbosity level.
--
264
warn :: Verbosity -> String -> IO ()
265
warn verbosity msg =
266
267
  when (verbosity >= normal) $ do
    hFlush stdout
268
    hPutStr stderr (wrapText ("Warning: " ++ msg))
269
270
271
272
273
274
275
276
277
278
279

-- | Useful status messages.
--
-- We display these at the 'normal' verbosity level.
--
-- This is for the ordinary helpful status messages that users see. Just
-- enough information to know that things are working but not floods of detail.
--
notice :: Verbosity -> String -> IO ()
notice verbosity msg =
  when (verbosity >= normal) $
280
    putStr (wrapText msg)
281

282
283
setupMessage :: Verbosity -> String -> PackageIdentifier -> IO ()
setupMessage verbosity msg pkgid =
284
    notice verbosity (msg ++ ' ': display pkgid ++ "...")
285

286
-- | More detail on the operation of some action.
287
--
288
289
290
291
292
-- We display these messages when the verbosity level is 'verbose'
--
info :: Verbosity -> String -> IO ()
info verbosity msg =
  when (verbosity >= verbose) $
293
    putStr (wrapText msg)
294
295
296
297
298
299
300

-- | Detailed internal debugging information
--
-- We display these messages when the verbosity level is 'deafening'
--
debug :: Verbosity -> String -> IO ()
debug verbosity msg =
301
  when (verbosity >= deafening) $ do
302
    putStr (wrapText msg)
303
    hFlush stdout
304

305
306
307
308
309
310
311
312
-- | A variant of 'debug' that doesn't perform the automatic line
-- wrapping. Produces better output in some cases.
debugNoWrap :: Verbosity -> String -> IO ()
debugNoWrap verbosity msg =
  when (verbosity >= deafening) $ do
    putStrLn msg
    hFlush stdout

313
314
315
316
317
318
-- | Perform an IO action, catching any IO exceptions and printing an error
--   if one occurs.
chattyTry :: String  -- ^ a description of the action we were attempting
          -> IO ()   -- ^ the action itself
          -> IO ()
chattyTry desc action =
319
  catchIO action $ \exception ->
320
    putStrLn $ "Error while " ++ desc ++ ": " ++ show exception
321

322
323
324
-- -----------------------------------------------------------------------------
-- Helper functions

325
326
-- | Wraps text to the default line width. Existing newlines are preserved.
wrapText :: String -> String
327
wrapText = unlines
328
329
330
331
         . map (intercalate "\n"
              . map unwords
              . wrapLine 79
              . words)
332
         . lines
333
334
335
336

-- | Wraps a list of words to a list of lines of words of a particular width.
wrapLine :: Int -> [String] -> [[String]]
wrapLine width = wrap 0 []
337
338
339
340
341
342
343
344
345
346
347
348
349
  where wrap :: Int -> [String] -> [String] -> [[String]]
        wrap 0   []   (w:ws)
          | length w + 1 > width
          = wrap (length w) [w] ws
        wrap col line (w:ws)
          | col + length w + 1 > width
          = reverse line : wrap 0 [] (w:ws)
        wrap col line (w:ws)
          = let col' = col + length w + 1
             in wrap col' (w:line) ws
        wrap _ []   [] = []
        wrap _ line [] = [reverse line]

simonmar's avatar
simonmar committed
350
351
352
353
354
-- -----------------------------------------------------------------------------
-- rawSystem variants
maybeExit :: IO ExitCode -> IO ()
maybeExit cmd = do
  res <- cmd
355
  unless (res == ExitSuccess) $ exitWith res
simonmar's avatar
simonmar committed
356

Ian Lynagh's avatar
Ian Lynagh committed
357
358
359
printRawCommandAndArgs :: Verbosity -> FilePath -> [String] -> IO ()
printRawCommandAndArgs verbosity path args
 | verbosity >= deafening = print (path, args)
360
 | verbosity >= verbose   =
361
#if __GLASGOW_HASKELL__ >= 702
362
363
364
365
                            putStrLn $ showCommandForUser path args
#else
                            putStrLn $ unwords (path : args)
#endif
Ian Lynagh's avatar
Ian Lynagh committed
366
367
 | otherwise              = return ()

368
369
370
371
372
373
374
375
376
377
378
printRawCommandAndArgsAndEnv :: Verbosity
                             -> FilePath
                             -> [String]
                             -> [(String, String)]
                             -> IO ()
printRawCommandAndArgsAndEnv verbosity path args env
 | verbosity >= deafening = do putStrLn ("Environment: " ++ show env)
                               print (path, args)
 | verbosity >= verbose   = putStrLn $ unwords (path : args)
 | otherwise              = return ()

simonmar's avatar
simonmar committed
379
-- Exit with the same exitcode if the subcommand fails
380
381
rawSystemExit :: Verbosity -> FilePath -> [String] -> IO ()
rawSystemExit verbosity path args = do
Ian Lynagh's avatar
Ian Lynagh committed
382
  printRawCommandAndArgs verbosity path args
383
  hFlush stdout
384
385
386
387
  exitcode <- rawSystem path args
  unless (exitcode == ExitSuccess) $ do
    debug verbosity $ path ++ " returned " ++ show exitcode
    exitWith exitcode
simonmar's avatar
simonmar committed
388

tibbe's avatar
tibbe committed
389
390
391
392
393
394
395
396
397
rawSystemExitCode :: Verbosity -> FilePath -> [String] -> IO ExitCode
rawSystemExitCode verbosity path args = do
  printRawCommandAndArgs verbosity path args
  hFlush stdout
  exitcode <- rawSystem path args
  unless (exitcode == ExitSuccess) $ do
    debug verbosity $ path ++ " returned " ++ show exitcode
  return exitcode

398
399
400
401
402
403
404
405
406
407
408
409
410
411
rawSystemExitWithEnv :: Verbosity
                     -> FilePath
                     -> [String]
                     -> [(String, String)]
                     -> IO ()
rawSystemExitWithEnv verbosity path args env = do
    printRawCommandAndArgsAndEnv verbosity path args env
    hFlush stdout
    ph <- runProcess path args Nothing (Just env) Nothing Nothing Nothing
    exitcode <- waitForProcess ph
    unless (exitcode == ExitSuccess) $ do
        debug verbosity $ path ++ " returned " ++ show exitcode
        exitWith exitcode

412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
-- Closes the passed in handles before returning.
rawSystemIOWithEnv :: Verbosity
                   -> FilePath
                   -> [String]
                   -> [(String, String)]
                   -> Maybe Handle  -- ^ stdin
                   -> Maybe Handle  -- ^ stdout
                   -> Maybe Handle  -- ^ stderr
                   -> IO ExitCode
rawSystemIOWithEnv verbosity path args env inp out err = do
    printRawCommandAndArgsAndEnv verbosity path args env
    hFlush stdout
    ph <- runProcess path args Nothing (Just env) inp out err
    exitcode <- waitForProcess ph
    unless (exitcode == ExitSuccess) $ do
      debug verbosity $ path ++ " returned " ++ show exitcode
    return exitcode

430
431
-- | Run a command and return its output.
--
432
-- The output is assumed to be text in the locale encoding.
433
--
434
435
rawSystemStdout :: Verbosity -> FilePath -> [String] -> IO String
rawSystemStdout verbosity path args = do
436
437
438
439
  (output, errors, exitCode) <- rawSystemStdInOut verbosity path args
                                                  Nothing False
  when (exitCode /= ExitSuccess) $
    die errors
440
441
  return output

442
443
444
445
446
447
448
449
450
451
-- | Run a command and return its output, errors and exit status. Optionally
-- also supply some input. Also provides control over whether the binary/text
-- mode of the input and output.
--
rawSystemStdInOut :: Verbosity
                  -> FilePath -> [String]
                  -> Maybe (String, Bool) -- ^ input text and binary mode
                  -> Bool                 -- ^ output in binary mode
                  -> IO (String, String, ExitCode) -- ^ output, errors, exit
rawSystemStdInOut verbosity path args input outputBinary = do
Ian Lynagh's avatar
Ian Lynagh committed
452
  printRawCommandAndArgs verbosity path args
453

454
455
456
  Exception.bracket
     (runInteractiveProcess path args Nothing Nothing)
     (\(inh,outh,errh,_) -> hClose inh >> hClose outh >> hClose errh)
457
    $ \(inh,outh,errh,pid) -> do
458

459
460
461
462
      -- output mode depends on what the caller wants
      hSetBinaryMode outh outputBinary
      -- but the errors are always assumed to be text (in the current locale)
      hSetBinaryMode errh False
463

464
      -- fork off a couple threads to pull on the stderr and stdout
465
      -- so if the process writes to stderr we do not block.
466

467
      err <- hGetContents errh
468
      out <- hGetContents outh
469

470
      mv <- newEmptyMVar
471
      let force str = (evaluate (length str) >> return ())
472
            `Exception.finally` putMVar mv ()
473
          --TODO: handle exceptions like text decoding.
474
475
      _ <- forkIO $ force out
      _ <- forkIO $ force err
476
477
478
479
480
481
482
483
484
485
486
487
488

      -- push all the input, if any
      case input of
        Nothing -> return ()
        Just (inputStr, inputBinary) -> do
                -- input mode depends on what the caller wants
          hSetBinaryMode inh inputBinary
          hPutStr inh inputStr
          hClose inh
          --TODO: this probably fails if the process refuses to consume
          -- or if it closes stdin (eg if it exits)

      -- wait for both to finish, in either order
489
490
      takeMVar mv
      takeMVar mv
491
492
493

      -- wait for the program to terminate
      exitcode <- waitForProcess pid
494
495
496
497
      unless (exitcode == ExitSuccess) $
        debug verbosity $ path ++ " returned " ++ show exitcode
                       ++ if null err then "" else
                          " with error message:\n" ++ err
498
499
500
501
                       ++ case input of
                            Nothing       -> ""
                            Just ("",  _) -> ""
                            Just (inp, _) -> "\nstdin input:\n" ++ inp
502

503
      return (out, err, exitcode)
504
505
506


-- | Look for a program on the path.
507
508
findProgramLocation :: Verbosity -> FilePath -> IO (Maybe FilePath)
findProgramLocation verbosity prog = do
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
  debug verbosity $ "searching for " ++ prog ++ " in path."
  res <- findExecutable prog
  case res of
      Nothing   -> debug verbosity ("Cannot find " ++ prog ++ " on the path")
      Just path -> debug verbosity ("found " ++ prog ++ " at "++ path)
  return res


-- | Look for a program and try to find it's version number. It can accept
-- either an absolute path or the name of a program binary, in which case we
-- will look for the program on the path.
--
findProgramVersion :: String             -- ^ version args
                   -> (String -> String) -- ^ function to select version
                                         --   number from program output
                   -> Verbosity
                   -> FilePath           -- ^ location
                   -> IO (Maybe Version)
findProgramVersion versionArg selectVersion verbosity path = do
  str <- rawSystemStdout verbosity path [versionArg]
         `catchIO`   (\_ -> return "")
         `catchExit` (\_ -> return "")
  let version :: Maybe Version
      version = simpleParse (selectVersion str)
  case version of
      Nothing -> warn verbosity $ "cannot determine version of " ++ path
                               ++ " :\n" ++ show str
      Just v  -> debug verbosity $ path ++ " is version " ++ display v
  return version


540
541
542
543
544
-- | Like the unix xargs program. Useful for when we've got very long command
-- lines that might overflow an OS limit on command line length and so you
-- need to invoke a command multiple times to get all the args in.
--
-- Use it with either of the rawSystem variants above. For example:
545
--
546
-- > xargs (32*1024) (rawSystemExit verbosity) prog fixedArgs bigArgs
547
--
548
549
550
xargs :: Int -> ([String] -> IO ())
      -> [String] -> [String] -> IO ()
xargs maxSize rawSystemFun fixedArgs bigArgs =
551
  let fixedArgSize = sum (map length fixedArgs) + length fixedArgs
552
      chunkSize = maxSize - fixedArgSize
553
   in mapM_ (rawSystemFun . (fixedArgs ++)) (chunks chunkSize bigArgs)
554
555
556
557
558

  where chunks len = unfoldr $ \s ->
          if null s then Nothing
                    else Just (chunk [] len s)

Ian Lynagh's avatar
Ian Lynagh committed
559
        chunk acc _   []     = (reverse acc,[])
560
561
562
563
        chunk acc len (s:ss)
          | len' < len = chunk (s:acc) (len-len'-1) ss
          | otherwise  = (reverse acc, s:ss)
          where len' = length s
ijones's avatar
ijones committed
564
565
566
567
568

-- ------------------------------------------------------------
-- * File Utilities
-- ------------------------------------------------------------

569
570
571
572
573
----------------
-- Finding files

-- | Find a file by looking in a search path. The file path must match exactly.
--
ijones's avatar
ijones committed
574
575
576
findFile :: [FilePath]    -- ^search locations
         -> FilePath      -- ^File Name
         -> IO FilePath
577
578
579
580
581
582
findFile searchPath fileName =
  findFirstFile id
    [ path </> fileName
    | path <- nub searchPath]
  >>= maybe (die $ fileName ++ " doesn't exist") return

583
584
585
586
-- | Find a file by looking in a search path with one of a list of possible
-- file extensions. The file base name should be given and it will be tried
-- with each of the extensions in each element of the search path.
--
587
588
589
590
591
592
593
594
595
596
findFileWithExtension :: [String]
                      -> [FilePath]
                      -> FilePath
                      -> IO (Maybe FilePath)
findFileWithExtension extensions searchPath baseName =
  findFirstFile id
    [ path </> baseName <.> ext
    | path <- nub searchPath
    , ext <- nub extensions ]

597
598
599
-- | Like 'findFileWithExtension' but returns which element of the search path
-- the file was found in, and the file path relative to that base directory.
--
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
findFileWithExtension' :: [String]
                       -> [FilePath]
                       -> FilePath
                       -> IO (Maybe (FilePath, FilePath))
findFileWithExtension' extensions searchPath baseName =
  findFirstFile (uncurry (</>))
    [ (path, baseName <.> ext)
    | path <- nub searchPath
    , ext <- nub extensions ]

findFirstFile :: (a -> FilePath) -> [a] -> IO (Maybe a)
findFirstFile file = findFirst
  where findFirst []     = return Nothing
        findFirst (x:xs) = do exists <- doesFileExist (file x)
                              if exists
                                then return (Just x)
                                else findFirst xs
ijones's avatar
ijones committed
617

618
619
620
621
-- | Finds the files corresponding to a list of Haskell module names.
--
-- As 'findModuleFile' but for a list of module names.
--
622
623
624
625
626
627
628
findModuleFiles :: [FilePath]   -- ^ build prefix (location of objects)
                -> [String]     -- ^ search suffixes
                -> [ModuleName] -- ^ modules
                -> IO [(FilePath, FilePath)]
findModuleFiles searchPath extensions moduleNames =
  mapM (findModuleFile searchPath extensions) moduleNames

629
630
631
632
633
-- | Find the file corresponding to a Haskell module name.
--
-- This is similar to 'findFileWithExtension'' but specialised to a module
-- name. The function fails if the file corresponding to the module is missing.
--
634
635
636
637
638
639
640
641
642
643
644
645
646
findModuleFile :: [FilePath]  -- ^ build prefix (location of objects)
               -> [String]    -- ^ search suffixes
               -> ModuleName  -- ^ module
               -> IO (FilePath, FilePath)
findModuleFile searchPath extensions moduleName =
      maybe notFound return
  =<< findFileWithExtension' extensions searchPath
                             (ModuleName.toFilePath moduleName)
  where
    notFound = die $ "Error: Could not find module: " ++ display moduleName
                  ++ " with any suffix: " ++ show extensions
                  ++ " in the search path: " ++ show searchPath

647
648
649
650
651
652
-- | List all the files in a directory and all subdirectories.
--
-- The order places files in sub-directories after all the files in their
-- parent directories. The list is generated lazily so is not well defined if
-- the source directory structure changes before the list is used.
--
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
getDirectoryContentsRecursive :: FilePath -> IO [FilePath]
getDirectoryContentsRecursive topdir = recurseDirectories [""]
  where
    recurseDirectories :: [FilePath] -> IO [FilePath]
    recurseDirectories []         = return []
    recurseDirectories (dir:dirs) = unsafeInterleaveIO $ do
      (files, dirs') <- collect [] [] =<< getDirectoryContents (topdir </> dir)
      files' <- recurseDirectories (dirs' ++ dirs)
      return (files ++ files')

      where
        collect files dirs' []              = return (reverse files, reverse dirs')
        collect files dirs' (entry:entries) | ignore entry
                                            = collect files dirs' entries
        collect files dirs' (entry:entries) = do
          let dirEntry = dir </> entry
          isDirectory <- doesDirectoryExist (topdir </> dirEntry)
          if isDirectory
            then collect files (dirEntry:dirs') entries
            else collect (dirEntry:files) dirs' entries

        ignore ['.']      = True
        ignore ['.', '.'] = True
        ignore _          = False

678
679
680
681
682
683
684
------------------------
-- Environment variables

-- | Is this directory in the system search path?
isInSearchPath :: FilePath -> IO Bool
isInSearchPath path = fmap (elem path) getSearchPath

685
686
687
----------------
-- File globbing

688
689
690
691
data FileGlob
   -- | No glob at all, just an ordinary file
   = NoGlob FilePath

Duncan Coutts's avatar
Duncan Coutts committed
692
   -- | dir prefix and extension, like @\"foo\/bar\/\*.baz\"@ corresponds to
693
   --    @FileGlob \"foo\/bar\" \".baz\"@
694
695
696
697
698
699
   | FileGlob FilePath String

parseFileGlob :: FilePath -> Maybe FileGlob
parseFileGlob filepath = case splitExtensions filepath of
  (filepath', ext) -> case splitFileName filepath' of
    (dir, "*") | '*' `elem` dir
700
701
              || '*' `elem` ext
              || null ext            -> Nothing
702
703
704
705
706
707
               | null dir            -> Just (FileGlob "." ext)
               | otherwise           -> Just (FileGlob dir ext)
    _          | '*' `elem` filepath -> Nothing
               | otherwise           -> Just (NoGlob filepath)

matchFileGlob :: FilePath -> IO [FilePath]
708
matchFileGlob = matchDirFileGlob "."
709

Ian Lynagh's avatar
Ian Lynagh committed
710
711
matchDirFileGlob :: FilePath -> FilePath -> IO [FilePath]
matchDirFileGlob dir filepath = case parseFileGlob filepath of
712
  Nothing -> die $ "invalid file glob '" ++ filepath
Ian Lynagh's avatar
Ian Lynagh committed
713
714
                ++ "'. Wildcards '*' are only allowed in place of the file"
                ++ " name, not in the directory name or file extension."
715
                ++ " If a wildcard is used it must be with an file extension."
Ian Lynagh's avatar
Ian Lynagh committed
716
717
718
  Just (NoGlob filepath') -> return [filepath']
  Just (FileGlob dir' ext) -> do
    files <- getDirectoryContents (dir </> dir')
719
    case   [ dir' </> file
720
721
           | file <- files
           , let (name, ext') = splitExtensions file
722
723
724
725
           , not (null name) && ext' == ext ] of
      []      -> die $ "filepath wildcard '" ++ filepath
                    ++ "' does not match any files."
      matches -> return matches
Ian Lynagh's avatar
Ian Lynagh committed
726

727
728
----------------------------------------
-- Copying and installing files and dirs
ijones's avatar
ijones committed
729

730
731
-- | Same as 'createDirectoryIfMissing' but logs at higher verbosity levels.
--
732
733
734
735
736
737
738
739
740
741
742
createDirectoryIfMissingVerbose :: Verbosity
                                -> Bool     -- ^ Create its parents too?
                                -> FilePath
                                -> IO ()
createDirectoryIfMissingVerbose verbosity create_parents path0
  | create_parents = createDirs (parents path0)
  | otherwise      = createDirs (take 1 (parents path0))
  where
    parents = reverse . scanl1 (</>) . splitDirectories . normalise

    createDirs []         = return ()
743
    createDirs (dir:[])   = createDir dir throwIO
744
745
746
    createDirs (dir:dirs) =
      createDir dir $ \_ -> do
        createDirs dirs
747
        createDir dir throwIO
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765

    createDir :: FilePath -> (IOException -> IO ()) -> IO ()
    createDir dir notExistHandler = do
      r <- tryIO $ createDirectoryVerbose verbosity dir
      case (r :: Either IOException ()) of
        Right ()                   -> return ()
        Left  e
          | isDoesNotExistError  e -> notExistHandler e
          -- createDirectory (and indeed POSIX mkdir) does not distinguish
          -- between a dir already existing and a file already existing. So we
          -- check for it here. Unfortunately there is a slight race condition
          -- here, but we think it is benign. It could report an exeption in
          -- the case that the dir did exist but another process deletes the
          -- directory and creates a file in its place before we can check
          -- that the directory did indeed exist.
          | isAlreadyExistsError e -> (do
              isDir <- doesDirectoryExist dir
              if isDir then return ()
766
                       else throwIO e
Duncan Coutts's avatar
Duncan Coutts committed
767
              ) `catchIO` ((\_ -> return ()) :: IOException -> IO ())
768
          | otherwise              -> throwIO e
769
770
771
772
773
774

createDirectoryVerbose :: Verbosity -> FilePath -> IO ()
createDirectoryVerbose verbosity dir = do
  info verbosity $ "creating " ++ dir
  createDirectory dir
  setDirOrdinary dir
775

776
777
778
779
780
-- | Copies a file without copying file permissions. The target file is created
-- with default permissions. Any existing target file is replaced.
--
-- At higher verbosity levels it logs an info message.
--
781
782
copyFileVerbose :: Verbosity -> FilePath -> FilePath -> IO ()
copyFileVerbose verbosity src dest = do
783
  info verbosity ("copy " ++ src ++ " to " ++ dest)
784
785
  copyFile src dest

786
787
788
789
-- | Install an ordinary file. This is like a file copy but the permissions
-- are set appropriately for an installed file. On Unix it is \"-rw-r--r--\"
-- while on Windows it uses the default permissions for the target directory.
--
790
791
792
793
794
installOrdinaryFile :: Verbosity -> FilePath -> FilePath -> IO ()
installOrdinaryFile verbosity src dest = do
  info verbosity ("Installing " ++ src ++ " to " ++ dest)
  copyOrdinaryFile src dest

795
796
797
798
-- | Install an executable file. This is like a file copy but the permissions
-- are set appropriately for an installed file. On Unix it is \"-rwxr-xr-x\"
-- while on Windows it uses the default permissions for the target directory.
--
799
800
801
802
803
installExecutableFile :: Verbosity -> FilePath -> FilePath -> IO ()
installExecutableFile verbosity src dest = do
  info verbosity ("Installing executable " ++ src ++ " to " ++ dest)
  copyExecutableFile src dest

refold's avatar
refold committed
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
-- | Install a file that may or not be executable, preserving permissions.
installMaybeExecutableFile :: Verbosity -> FilePath -> FilePath -> IO ()
installMaybeExecutableFile verbosity src dest = do
  perms <- getPermissions src
  if (executable perms) --only checks user x bit
    then installExecutableFile verbosity src dest
    else installOrdinaryFile   verbosity src dest

-- | Given a relative path to a file, copy it to the given directory, preserving
-- the relative path and creating the parent directories if needed.
copyFileTo :: Verbosity -> FilePath -> FilePath -> IO ()
copyFileTo verbosity dir file = do
  let targetFile = dir </> file
  createDirectoryIfMissingVerbose verbosity True (takeDirectory targetFile)
  installOrdinaryFile verbosity file targetFile

-- | Common implementation of 'copyFiles', 'installOrdinaryFiles',
-- 'installExecutableFiles' and 'installMaybeExecutableFiles'.
copyFilesWith :: (Verbosity -> FilePath -> FilePath -> IO ())
              -> Verbosity -> FilePath -> [(FilePath, FilePath)] -> IO ()
copyFilesWith doCopy verbosity targetDir srcFiles = do

  -- Create parent directories for everything
  let dirs = map (targetDir </>) . nub . map (takeDirectory . snd) $ srcFiles
  mapM_ (createDirectoryIfMissingVerbose verbosity True) dirs

  -- Copy all the files
  sequence_ [ let src  = srcBase   </> srcFile
                  dest = targetDir </> srcFile
               in doCopy verbosity src dest
            | (srcBase, srcFile) <- srcFiles ]

836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
-- | Copies a bunch of files to a target directory, preserving the directory
-- structure in the target location. The target directories are created if they
-- do not exist.
--
-- The files are identified by a pair of base directory and a path relative to
-- that base. It is only the relative part that is preserved in the
-- destination.
--
-- For example:
--
-- > copyFiles normal "dist/src"
-- >    [("", "src/Foo.hs"), ("dist/build/", "src/Bar.hs")]
--
-- This would copy \"src\/Foo.hs\" to \"dist\/src\/src\/Foo.hs\" and
-- copy \"dist\/build\/src\/Bar.hs\" to \"dist\/src\/src\/Bar.hs\".
--
-- This operation is not atomic. Any IO failure during the copy (including any
-- missing source files) leaves the target in an unknown state so it is best to
-- use it with a freshly created directory so that it can be simply deleted if
-- anything goes wrong.
--
copyFiles :: Verbosity -> FilePath -> [(FilePath, FilePath)] -> IO ()
refold's avatar
refold committed
858
copyFiles = copyFilesWith copyFileVerbose
859

860
861
-- | This is like 'copyFiles' but uses 'installOrdinaryFile'.
--
862
installOrdinaryFiles :: Verbosity -> FilePath -> [(FilePath, FilePath)] -> IO ()
refold's avatar
refold committed
863
installOrdinaryFiles = copyFilesWith installOrdinaryFile
864

refold's avatar
refold committed
865
866
867
868
869
-- | This is like 'copyFiles' but uses 'installExecutableFile'.
--
installExecutableFiles :: Verbosity -> FilePath -> [(FilePath, FilePath)]
                          -> IO ()
installExecutableFiles = copyFilesWith installExecutableFile
870

refold's avatar
refold committed
871
872
873
874
875
-- | This is like 'copyFiles' but uses 'installMaybeExecutableFile'.
--
installMaybeExecutableFiles :: Verbosity -> FilePath -> [(FilePath, FilePath)]
                               -> IO ()
installMaybeExecutableFiles = copyFilesWith installMaybeExecutableFile
876

877
878
879
880
-- | This installs all the files in a directory to a target location,
-- preserving the directory layout. All the files are assumed to be ordinary
-- rather than executable files.
--
881
882
883
884
885
886
installDirectoryContents :: Verbosity -> FilePath -> FilePath -> IO ()
installDirectoryContents verbosity srcDir destDir = do
  info verbosity ("copy directory '" ++ srcDir ++ "' to '" ++ destDir ++ "'.")
  srcFiles <- getDirectoryContentsRecursive srcDir
  installOrdinaryFiles verbosity destDir [ (srcDir, f) | f <- srcFiles ]

887
888
889
890
891
892
893
894
895
896
897
---------------------------------
-- Deprecated file copy functions

{-# DEPRECATED smartCopySources
      "Use findModuleFiles and copyFiles or installOrdinaryFiles" #-}
smartCopySources :: Verbosity -> [FilePath] -> FilePath
                 -> [ModuleName] -> [String] -> IO ()
smartCopySources verbosity searchPath targetDir moduleNames extensions =
      findModuleFiles searchPath extensions moduleNames
  >>= copyFiles verbosity targetDir

898
899
{-# DEPRECATED copyDirectoryRecursiveVerbose
      "You probably want installDirectoryContents instead" #-}
900
901
copyDirectoryRecursiveVerbose :: Verbosity -> FilePath -> FilePath -> IO ()
copyDirectoryRecursiveVerbose verbosity srcDir destDir = do
902
  info verbosity ("copy directory '" ++ srcDir ++ "' to '" ++ destDir ++ "'.")
903
904
  srcFiles <- getDirectoryContentsRecursive srcDir
  copyFiles verbosity destDir [ (srcDir, f) | f <- srcFiles ]
905

906
907
908
---------------------------
-- Temporary files and dirs

909
910
-- | Use a temporary filename that doesn't already exist.
--
911
912
withTempFile :: Bool     -- ^ Keep temporary files?
             -> FilePath -- ^ Temp dir to create the file in
913
914
             -> String   -- ^ File name template. See 'openTempFile'.
             -> (FilePath -> Handle -> IO a) -> IO a
915
withTempFile keepTempFiles tmpDir template action =
916
917
  Exception.bracket
    (openTempFile tmpDir template)
918
919
    (\(name, handle) -> do hClose handle
                           unless keepTempFiles $ removeFile name)
920
921
    (uncurry action)

922
-- | Create and use a temporary directory.
Duncan Coutts's avatar
Duncan Coutts committed
923
--
924
925
926
927
928
929
930
-- Creates a new temporary directory inside the given directory, making use
-- of the template. The temp directory is deleted after use. For example:
--
-- > withTempDirectory verbosity "src" "sdist." $ \tmpDir -> do ...
--
-- The @tmpDir@ will be a new subdirectory of the given directory, e.g.
-- @src/sdist.342@.
Duncan Coutts's avatar
Duncan Coutts committed
931
--
932
933
934
935
withTempDirectory :: Verbosity
                  -> Bool     -- ^ Keep temporary files?
                  -> FilePath -> String -> (FilePath -> IO a) -> IO a
withTempDirectory _verbosity keepTempFiles targetDir template =
936
937
  Exception.bracket
    (createTempDirectory targetDir template)
938
    (unless keepTempFiles . removeDirectoryRecursive)
Duncan Coutts's avatar
Duncan Coutts committed
939

940
941
942
-----------------------------------
-- Safely reading and writing files

943
944
945
946
947
948
949
950
951
952
-- | Gets the contents of a file, but guarantee that it gets closed.
--
-- The file is read lazily but if it is not fully consumed by the action then
-- the remaining input is truncated and the file is closed.
--
withFileContents :: FilePath -> (String -> IO a) -> IO a
withFileContents name action =
  Exception.bracket (openFile name ReadMode) hClose
                    (\hnd -> hGetContents hnd >>= action)

953
954
955
956
957
958
959
960
-- | Writes a file atomically.
--
-- The file is either written sucessfully or an IO exception is raised and
-- the original file is left unchanged.
--
-- On windows it is not possible to delete a file that is open by a process.
-- This case will give an IO exception but the atomic property is not affected.
--
961
962
963
964
965
966
967
968
969
970
writeFileAtomic :: FilePath -> BS.ByteString -> IO ()
writeFileAtomic targetPath content = do
  let (targetDir, targetFile) = splitFileName targetPath
  Exception.bracketOnError
    (openBinaryTempFile targetDir $ targetFile <.> "tmp")
    (\(tmpPath, handle) -> hClose handle >> removeFile tmpPath)
    (\(tmpPath, handle) -> do
        BS.hPut handle content
        hClose handle
        renameFile tmpPath targetPath)
971

972
973
974
975
976
977
-- | Write a file but only if it would have new content. If we would be writing
-- the same as the existing content then leave the file as is so that we do not
-- update the file's modification time.
--
rewriteFile :: FilePath -> String -> IO ()
rewriteFile path newContent =
978
  flip catchIO mightNotExist $ do
979
    existingContent <- readFile path
980
    _ <- evaluate (length existingContent)
981
    unless (existingContent == newContent) $
982
      writeFileAtomic path (BS.Char8.pack newContent)
983
  where
984
985
    mightNotExist e | isDoesNotExistError e = writeFileAtomic path
                                              (BS.Char8.pack newContent)
986
                    | otherwise             = ioError e
987
988
989
990

-- | The path name that represents the current directory.
-- In Unix, it's @\".\"@, but this is system-specific.
-- (E.g. AmigaOS uses the empty string @\"\"@ for the current directory.)
ijones's avatar
ijones committed
991
992
currentDir :: FilePath
currentDir = "."
993

994
995
996
997
-- ------------------------------------------------------------
-- * Finding the description file
-- ------------------------------------------------------------

ijones's avatar
ijones committed
998
-- |Package description file (/pkgname/@.cabal@)
999
defaultPackageDesc :: Verbosity -> IO FilePath
1000
defaultPackageDesc _verbosity = findPackageDesc currentDir
1001
1002

-- |Find a package description file in the given directory.  Looks for
ijones's avatar
ijones committed
1003
-- @.cabal@ files.
1004
findPackageDesc :: FilePath    -- ^Where to look
1005
                -> IO FilePath -- ^<pkgname>.cabal
1006
findPackageDesc dir
1007
 = do files <- getDirectoryContents dir
1008
1009
1010
1011
1012
1013
1014
1015
      -- to make sure we do not mistake a ~/.cabal/ dir for a <pkgname>.cabal
      -- file we filter to exclude dirs and null base file names:
      cabalFiles <- filterM doesFileExist
                       [ dir </> file
                       | file <- files
                       , let (name, ext) = splitExtension file
                       , not (null name) && ext == ".cabal" ]
      case cabalFiles of
1016
        []          -> noDesc
1017
        [cabalFile] -> return cabalFile
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
        multiple    -> multiDesc multiple

  where
    noDesc :: IO a
    noDesc = die $ "No cabal file found.\n"
                ++ "Please create a package description file <pkgname>.cabal"

    multiDesc :: [String] -> IO a
    multiDesc l = die $ "Multiple cabal files found.\n"
                    ++ "Please use only one of: "
1028
                    ++ intercalate ", " l
1029

1030
1031
-- |Optional auxiliary package information file (/pkgname/@.buildinfo@)
defaultHookedPackageDesc :: IO (Maybe FilePath)
1032
defaultHookedPackageDesc = findHookedPackageDesc currentDir
1033
1034
1035
1036

-- |Find auxiliary package information in the given directory.
-- Looks for @.buildinfo@ files.
findHookedPackageDesc
1037
1038
    :: FilePath                 -- ^Directory to search
    -> IO (Maybe FilePath)      -- ^/dir/@\/@/pkgname/@.buildinfo@, if present
1039
findHookedPackageDesc dir = do
1040
1041
1042
1043
1044
1045
1046
    files <- getDirectoryContents dir
    buildInfoFiles <- filterM doesFileExist
                        [ dir </> file
                        | file <- files
                        , let (name, ext) = splitExtension file
                        , not (null name) && ext == buildInfoExt ]
    case buildInfoFiles of
1047
1048
1049
        [] -> return Nothing
        [f] -> return (Just f)
        _ -> die ("Multiple files with extension " ++ buildInfoExt)
1050

1051
1052
1053
buildInfoExt  :: String
buildInfoExt = ".buildinfo"

1054
-- ------------------------------------------------------------
1055
-- * Unicode stuff
1056
1057
-- ------------------------------------------------------------

Duncan Coutts's avatar
Duncan Coutts committed
1058
1059
-- This is a modification of the UTF8 code from gtk2hs and the
-- utf8-string package.
1060
1061
1062
1063
1064
1065

fromUTF8 :: String -> String
fromUTF8 []     = []
fromUTF8 (c:cs)
  | c <= '\x7F' = c : fromUTF8 cs
  | c <= '\xBF' = replacementChar : fromUTF8 cs
Duncan Coutts's avatar
Duncan Coutts committed
1066
1067
1068
1069
1070
  | c <= '\xDF' = twoBytes c cs
  | c <= '\xEF' = moreBytes 3 0x800     cs (ord c .&. 0xF)
  | c <= '\xF7' = moreBytes 4 0x10000   cs (ord c .&. 0x7)
  | c <= '\xFB' = moreBytes 5 0x200000  cs (ord c .&. 0x3)
  | c <= '\xFD' = moreBytes 6 0x4000000 cs (ord c .&. 0x1)
1071
1072
  | otherwise   = replacementChar : fromUTF8 cs
  where
Duncan Coutts's avatar
Duncan Coutts committed
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
    twoBytes c0 (c1:cs')
      | ord c1 .&. 0xC0 == 0x80
      = let d = ((ord c0 .&. 0x1F) `shiftL` 6)
             .|. (ord c1 .&. 0x3F)
         in if d >= 0x80
               then  chr d           : fromUTF8 cs'
               else  replacementChar : fromUTF8 cs'
    twoBytes _ cs' = replacementChar : fromUTF8 cs'

    moreBytes :: Int -> Int -> [Char] -> Int -> [Char]
    moreBytes 1 overlong cs' acc
      | overlong <= acc && acc <= 0x10FFFF
     && (acc < 0xD800 || 0xDFFF < acc)
     && (acc < 0xFFFE || 0xFFFF < acc)
      = chr acc : fromUTF8 cs'

      | otherwise
      = replacementChar : fromUTF8 cs'

    moreBytes byteCount overlong (cn:cs') acc
      | ord cn .&. 0xC0 == 0x80
      = moreBytes (byteCount-1) overlong cs'
          ((acc `shiftL` 6) .|. ord cn .&. 0x3F)

    moreBytes _ _ cs' _
      = replacementChar : fromUTF8 cs'
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109

    replacementChar = '\xfffd'

toUTF8 :: String -> String
toUTF8 []        = []
toUTF8 (c:cs)
  | c <= '\x07F' = c
                 : toUTF8 cs
  | c <= '\x7FF' = chr (0xC0 .|. (w `shiftR` 6))
                 : chr (0x80 .|. (w .&. 0x3F))
                 : toUTF8 cs
Duncan Coutts's avatar
Duncan Coutts committed
1110
1111
1112
1113
1114
1115
  | c <= '\xFFFF'= chr (0xE0 .|.  (w `shiftR` 12))
                 : chr (0x80 .|. ((w `shiftR` 6)  .&. 0x3F))
                 : chr (0x80 .|.  (w .&. 0x3F))
                 : toUTF8 cs
  | otherwise    = chr (0xf0 .|.  (w `shiftR` 18))
                 : chr (0x80 .|. ((w `shiftR` 12)  .&. 0x3F))
1116
1117
1118
1119
1120
                 : chr (0x80 .|. ((w `shiftR` 6)  .&. 0x3F))
                 : chr (0x80 .|.  (w .&. 0x3F))
                 : toUTF8 cs
  where w = ord c

1121
1122
1123
1124
1125
1126
-- | Ignore a Unicode byte order mark (BOM) at the beginning of the input
--
ignoreBOM :: String -> String
ignoreBOM ('\xFEFF':string) = string
ignoreBOM string            = string

1127
1128
1129
1130
-- | Reads a UTF8 encoded text file as a Unicode String
--
-- Reads lazily using ordinary 'readFile'.
--
1131
readUTF8File :: FilePath -> IO String
1132
1133
readUTF8File f = fmap (ignoreBOM . fromUTF8)
               . hGetContents =<< openBinaryFile f ReadMode
1134

1135
1136
1137
1138
1139
1140
-- | Reads a UTF8 encoded text file as a Unicode String
--
-- Same behaviour as 'withFileContents'.
--
withUTF8FileContents :: FilePath -> (String -> IO a) -> IO a
withUTF8FileContents name action =
1141
1142
1143
1144
  Exception.bracket
    (openBinaryFile name ReadMode)
    hClose
    (\hnd -> hGetContents hnd >>= action . ignoreBOM . fromUTF8)
1145

1146
1147
1148
1149
-- | Writes a Unicode String as a UTF8 encoded text file.
--
-- Uses 'writeFileAtomic', so provides the same guarantees.
--
1150
writeUTF8File :: FilePath -> String -> IO ()
1151
writeUTF8File path = writeFileAtomic path . BS.Char8.pack . toUTF8
1152

1153
1154
1155
1156
1157
1158
1159
-- | Fix different systems silly line ending conventions
normaliseLineEndings :: String -> String
normaliseLineEndings [] = []
normaliseLineEndings ('\r':'\n':s) = '\n' : normaliseLineEndings s -- windows
normaliseLineEndings ('\r':s)      = '\n' : normaliseLineEndings s -- old osx
normaliseLineEndings (  c :s)      =   c  : normaliseLineEndings s

1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
-- ------------------------------------------------------------
-- * Common utils
-- ------------------------------------------------------------

equating :: Eq a => (b -> a) -> b -> b -> Bool
equating p x y = p x == p y

comparing :: Ord a =>