git-annex/Utility/Env/Set.hs
Joey Hess 82448bdf39
fix a annex.pidlock issue
That made eg git-annex get of an unlocked file hang until the
annex.pidlocktimeout and then fail.

This fix should be fully thread safe no matter what else git-annex is
doing.

Only using runsGitAnnexChildProcess in the one place it's known to be a
problem. Could audit for all places where git-annex runs itself as a child
and add it to all of them, later.
2020-06-17 15:30:59 -04:00

49 lines
1 KiB
Haskell

{- portable environment variables
-
- Copyright 2013 Joey Hess <id@joeyh.name>
-
- License: BSD-2-clause
-}
{-# LANGUAGE CPP #-}
module Utility.Env.Set (
setEnv,
unsetEnv,
legalInEnvVar,
) where
#ifdef mingw32_HOST_OS
import qualified System.SetEnv
import Utility.Env
#else
import qualified System.Posix.Env as PE
#endif
import Data.Char
{- Sets an environment variable. To overwrite an existing variable,
- overwrite must be True.
-
- On Windows, setting a variable to "" unsets it. -}
setEnv :: String -> String -> Bool -> IO ()
#ifndef mingw32_HOST_OS
setEnv var val overwrite = PE.setEnv var val overwrite
#else
setEnv var val True = System.SetEnv.setEnv var val
setEnv var val False = do
r <- getEnv var
case r of
Nothing -> setEnv var val True
Just _ -> return ()
#endif
unsetEnv :: String -> IO ()
#ifndef mingw32_HOST_OS
unsetEnv = PE.unsetEnv
#else
unsetEnv = System.SetEnv.unsetEnv
#endif
legalInEnvVar :: Char -> Bool
legalInEnvVar '_' = True
legalInEnvVar c = isAsciiLower c || isAsciiUpper c || (isNumber c && isAscii c)