Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Applying remarks from blog post comments

Implementation of a strict readFile (from Mike Tolly).
Using the nicer notation from Erik Hesselink.
  • Loading branch information...
commit 1eb0296f22f3c45bd8bc70ddfc7ca4efdde7bf69 1 parent 3c35879
authored

Showing 2 changed files with 18 additions and 6 deletions. Show diff stats Hide diff stats

  1. +6 4 Puppet/Interpreter/Functions.hs
  2. +12 2 Puppet/Utils.hs
10 Puppet/Interpreter/Functions.hs
@@ -12,12 +12,13 @@ module Puppet.Interpreter.Functions
12 12 ,pdbresourcequery
13 13 ) where
14 14
15   -import Prelude hiding (catch)
16   -import Control.Exception
17 15 import PuppetDB.Rest
18 16 import Puppet.Printers
19 17 import Puppet.Interpreter.Types
  18 +import Puppet.Utils
20 19
  20 +import Prelude hiding (catch)
  21 +import Control.Exception
21 22 import Data.Hash.MD5
22 23 import qualified Crypto.Hash.SHA1 as SHA1
23 24 import qualified Data.ByteString.Char8 as BS
@@ -25,7 +26,6 @@ import Data.String.Utils (join,replace)
25 26 import Text.RegexPR
26 27 import Text.Regex.PCRE.String
27 28 import Control.Monad.Error
28   -import System.IO
29 29 import qualified Data.ByteString.Base16 as B16
30 30 import SafeProcess
31 31 import Data.Either (lefts, rights)
@@ -88,7 +88,9 @@ versioncmp a b | a > b = 1
88 88 file :: [String] -> IO (Maybe String)
89 89 file [] = return Nothing
90 90 -- this is bad, is should be rewritten as a ByteString
91   -file (x:xs) = catch (fmap Just (withFile x ReadMode (\fh -> do { y <- hGetContents fh; evaluate (length y); return y }))) ((\_ -> file xs) :: SomeException -> IO (Maybe String))
  91 +file (x:xs) = catch
  92 + (fmap Just (readFile' x))
  93 + (\SomeException{} -> file xs)
92 94
93 95 puppetSplit :: String -> String -> IO (Either String [String])
94 96 puppetSplit str reg = do
14 Puppet/Utils.hs
... ... @@ -1,12 +1,15 @@
1 1 {-# LANGUAGE CPP, ForeignFunctionInterface #-}
2 2
3   -module Puppet.Utils (mGetExecutablePath) where
  3 +module Puppet.Utils (mGetExecutablePath, readFile', readSymbolicLink) where
4 4
5 5 -- copy pasted from base 4.6.0.0
6   -
  6 +import Prelude hiding (catch)
7 7 import Foreign.C
8 8 import Foreign.Marshal.Array
9 9 import System.Posix.Internals
  10 +import System.IO
  11 +import Control.Exception
  12 +
10 13
11 14 foreign import ccall unsafe "readlink" c_readlink :: CString -> CString -> CSize -> IO CInt
12 15
@@ -26,3 +29,10 @@ readSymbolicLink file =
26 29 mGetExecutablePath :: IO FilePath
27 30 mGetExecutablePath = readSymbolicLink $ "/proc/self/exe"
28 31
  32 +-- | Strict readFile
  33 +readFile' f = do
  34 + h <- openFile f ReadMode
  35 + s <- hGetContents h
  36 + evaluate (length s)
  37 + return s
  38 +

0 comments on commit 1eb0296

Please sign in to comment.
Something went wrong with that request. Please try again.