{-# LANGUAGE CPP #-}

module GHC.Utils.Touch (touch) where

import GHC.Prelude

#if defined(mingw32_HOST_OS)
import System.Win32.File
import System.Win32.Time
#else
import System.Posix.Files
import System.Posix.IO
#endif

-- | Set the mtime of the given file to the current time.
touch :: FilePath -> IO ()
touch :: FilePath -> IO ()
touch FilePath
file = do
#if defined(mingw32_HOST_OS)
    hdl <- FilePath
-> AccessMode
-> AccessMode
-> Maybe LPSECURITY_ATTRIBUTES
-> AccessMode
-> AccessMode
-> Maybe HANDLE
-> IO HANDLE
createFile FilePath
file AccessMode
gENERIC_WRITE AccessMode
fILE_SHARE_NONE Maybe LPSECURITY_ATTRIBUTES
forall a. Maybe a
Nothing AccessMode
oPEN_ALWAYS AccessMode
fILE_ATTRIBUTE_NORMAL Maybe HANDLE
forall a. Maybe a
Nothing
    t <- getSystemTimeAsFileTime
    setFileTime hdl Nothing Nothing (Just t)
    closeHandle hdl
#else
#if MIN_VERSION_unix(2,8,0)
  let oflags = defaultFileFlags { noctty = True, creat = Just 0o666 }
  fd <- openFd file WriteOnly oflags
#else
  let oflags = defaultFileFlags { noctty = True }
  fd <- openFd file WriteOnly (Just 0o666) oflags
#endif
  touchFd fd
  closeFd fd
#endif