summaryrefslogtreecommitdiff
path: root/compiler/GHC/Utils/Touch.hs
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/GHC/Utils/Touch.hs')
-rw-r--r--compiler/GHC/Utils/Touch.hs29
1 files changed, 29 insertions, 0 deletions
diff --git a/compiler/GHC/Utils/Touch.hs b/compiler/GHC/Utils/Touch.hs
new file mode 100644
index 0000000000..b6181584f0
--- /dev/null
+++ b/compiler/GHC/Utils/Touch.hs
@@ -0,0 +1,29 @@
+{-# 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 file = do
+#if defined(mingw32_HOST_OS)
+ hdl <- createFile file gENERIC_WRITE fILE_SHARE_NONE Nothing oPEN_ALWAYS fILE_ATTRIBUTE_NORMAL Nothing
+ t <- getSystemTimeAsFileTime
+ setFileTime hdl Nothing Nothing (Just t)
+ closeHandle hdl
+#else
+ let oflags = defaultFileFlags { noctty = True }
+ fd <- openFd file WriteOnly (Just 0o666) oflags
+ touchFd fd
+ closeFd fd
+#endif
+