Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create output file atomically #221

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions alex.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ executable alex
, array
, containers
, directory
, filepath

default-language: Haskell98
default-extensions: CPP
Expand Down
47 changes: 38 additions & 9 deletions src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,20 @@ import Paths_alex ( version, getDataDir )
import Control.Exception as Exception ( block, unblock, catch, throw )
#endif
#if __GLASGOW_HASKELL__ >= 610
import Control.Exception ( bracketOnError )
import Control.Exception ( bracketOnError, handleJust )
#endif
import Control.Monad ( when, liftM )
import System.IO.Error (isDoesNotExistError)
import Control.Monad ( guard, when, liftM )
import Data.Char ( chr )
import Data.List ( isSuffixOf, nub )
import Data.Version ( showVersion )
import System.Console.GetOpt ( getOpt, usageInfo, ArgOrder(..), OptDescr(..), ArgDescr(..) )
import System.Directory ( removeFile )
import System.Directory ( renameFile, removeFile )
import System.Environment ( getProgName, getArgs )
import System.FilePath (splitFileName)
import System.Exit ( ExitCode(..), exitWith )
import System.IO ( stderr, Handle, IOMode(..), openFile, hClose, hPutStr, hPutStrLn )
import qualified System.IO as IO ( openTempFileWithDefaultPermissions )
#if __GLASGOW_HASKELL__ >= 612
import System.IO ( hGetContents, hSetEncoding, utf8 )
#endif
Expand Down Expand Up @@ -66,6 +69,37 @@ alexOpenFile file mode = do
alexOpenFile = openFile
#endif

openTempFileWithDefaultPermissions :: FilePath -> String -> IO (FilePath, Handle)
#if __GLASGOW_HASKELL__ >= 612
openTempFileWithDefaultPermissions dir template = do
(file, h) <- IO.openTempFileWithDefaultPermissions dir template
hSetEncoding h utf8
return (file, h)
#else
openTempFileWithDefaultPermissions = IO.openTempFileWithDefaultPermissions
#endif

tryRemoveFile :: FilePath -> IO ()
tryRemoveFile name = handleJust (guard . isDoesNotExistError) return (removeFile name)

createAtomically :: FilePath -> (Handle -> IO a) -> IO a
createAtomically o_file action = bracketOnError open cleanup $ \ (o_file_tmp, h) -> do
r <- action h
hClose h
renameFile o_file_tmp o_file
return r
where
open :: IO (FilePath, Handle)
open = openTempFileWithDefaultPermissions dir template
where
(dir, file) = splitFileName o_file
template = file ++ "~"

cleanup :: (FilePath, Handle) -> IO ()
cleanup (o_file_tmp, h) = do
hClose h
tryRemoveFile o_file_tmp

-- `main' decodes the command line arguments and calls `alex'.

main:: IO ()
Expand Down Expand Up @@ -169,11 +203,7 @@ alex cli file basename script = do

scheme <- getScheme directives

-- open the output file; remove it if we encounter an error
bracketOnError
(alexOpenFile o_file WriteMode)
(\h -> do hClose h; removeFile o_file)
$ \out_h -> do
createAtomically o_file $ \ out_h -> do

let scanner2, scanner_final :: Scanner
scs :: [StartCode]
Expand Down Expand Up @@ -242,7 +272,6 @@ alex cli file basename script = do
tmplt <- alexReadFile $ template_dir ++ "/AlexTemplate.hs"
hPutStr out_h tmplt

hClose out_h
finish_info

getScheme :: [Directive] -> IO Scheme
Expand Down