summaryrefslogtreecommitdiff
path: root/Utility/Touch.hsc
diff options
context:
space:
mode:
authorGravatar Joey Hess <joeyh@joeyh.name>2015-10-21 02:22:18 -0400
committerGravatar Joey Hess <joeyh@joeyh.name>2015-10-21 02:22:18 -0400
commita2c7c5c16be3874114fb0304d6ec3353a09628ea (patch)
tree40ed684a2cea22df8fa8616c6952ce4ffb91c953 /Utility/Touch.hsc
parent2ef94e9c027493a71eee4050594d587754af0752 (diff)
Symlink timestamp preservation code uses functions from unix-2.7.0 when available, which should be more portable.
Diffstat (limited to 'Utility/Touch.hsc')
-rw-r--r--Utility/Touch.hsc26
1 files changed, 23 insertions, 3 deletions
diff --git a/Utility/Touch.hsc b/Utility/Touch.hsc
index c3318e6da..e1b1e887e 100644
--- a/Utility/Touch.hsc
+++ b/Utility/Touch.hsc
@@ -5,7 +5,7 @@
- License: BSD-2-clause
-}
-{-# LANGUAGE ForeignFunctionInterface #-}
+{-# LANGUAGE ForeignFunctionInterface, CPP #-}
module Utility.Touch (
TimeSpec(..),
@@ -13,6 +13,26 @@ module Utility.Touch (
touch
) where
+#if MIN_VERSION_unix(2,7,0)
+
+import System.Posix.Files
+import System.Posix.Types
+
+newtype TimeSpec = TimeSpec EpochTime
+
+{- Changes the access and modification times of an existing file.
+ Can follow symlinks, or not. Throws IO error on failure. -}
+touchBoth :: FilePath -> TimeSpec -> TimeSpec -> Bool -> IO ()
+touchBoth file (TimeSpec atime) (TimeSpec mtime) follow
+ | follow = setFileTimes file atime mtime
+ | otherwise = setSymbolicLinkTimesHiRes file (realToFrac atime) (realToFrac mtime)
+
+touch :: FilePath -> TimeSpec -> Bool -> IO ()
+touch file mtime = touchBoth file mtime mtime
+
+#else
+{- Compatability interface for old version of unix, to be removed eventally. -}
+
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
@@ -35,8 +55,6 @@ import Foreign.C
newtype TimeSpec = TimeSpec CTime
-{- Changes the access and modification times of an existing file.
- Can follow symlinks, or not. Throws IO error on failure. -}
touchBoth :: FilePath -> TimeSpec -> TimeSpec -> Bool -> IO ()
touch :: FilePath -> TimeSpec -> Bool -> IO ()
@@ -123,3 +141,5 @@ touchBoth file atime mtime follow =
touchBoth _ _ _ _ = return ()
#endif
#endif
+
+#endif