summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2011-03-20 14:04:39 -0400
committerGravatar Joey Hess <joey@kitenet.net>2011-03-20 14:09:24 -0400
commit6634b6a6b84a924f6f6059b5bea61f449d056eee (patch)
treea212611adb819bd3908165a391aa312ad403ce2c
parent140a351fc535235ae5714122f0c98174cbdb19ce (diff)
imcomplete attempt at supporting lutimes(3) for BSD compat
-rw-r--r--Command/Add.hs2
-rw-r--r--Touch.hsc84
2 files changed, 58 insertions, 28 deletions
diff --git a/Command/Add.hs b/Command/Add.hs
index f6ccf0fb8..da98bffa4 100644
--- a/Command/Add.hs
+++ b/Command/Add.hs
@@ -58,7 +58,7 @@ cleanup file key = do
-- touch the symlink to have the same mtime as the file it points to
s <- liftIO $ getFileStatus file
let mtime = modificationTime s
- liftIO $ touch file (TimeSpec mtime 0) False
+ liftIO $ touch file (TimeSpec mtime) False
Annex.queue "add" [Param "--"] file
return True
diff --git a/Touch.hsc b/Touch.hsc
index 456175182..8a3e31b57 100644
--- a/Touch.hsc
+++ b/Touch.hsc
@@ -9,8 +9,6 @@
module Touch (
TimeSpec(..),
- nowTime,
- omitTime,
touchBoth,
touch
) where
@@ -18,23 +16,23 @@ module Touch (
import Foreign
import Foreign.C
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-
-#ifndef _POSIX_C_SOURCE
-#define _POSIX_C_SOURCE >= 200809L
-#endif
+data TimeSpec = TimeSpec CTime
-data TimeSpec = TimeSpec CTime CLong
+{- 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 ()
-touch file mtime follow = touchBoth file omitTime mtime follow
+touch file mtime follow = touchBoth file mtime mtime follow
-touchBoth :: FilePath -> TimeSpec -> TimeSpec -> Bool -> IO ()
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <sys/time.h>
-omitTime :: TimeSpec
-nowTime :: TimeSpec
+#ifndef _BSD_SOURCE
+#define _BSD_SOURCE
+#endif
#if (defined UTIME_OMIT && defined UTIME_NOW && defined AT_FDCWD && defined AT_SYMLINK_NOFOLLOW)
@@ -44,9 +42,6 @@ at_fdcwd = #const AT_FDCWD
at_symlink_nofollow :: CInt
at_symlink_nofollow = #const AT_SYMLINK_NOFOLLOW
-omitTime = TimeSpec 0 #const UTIME_OMIT
-nowTime = TimeSpec 0 #const UTIME_NOW
-
instance Storable TimeSpec where
-- use the larger alignment of the two types in the struct
alignment _ = max sec_alignment nsec_alignment
@@ -56,19 +51,16 @@ instance Storable TimeSpec where
sizeOf _ = #{size struct timespec}
peek ptr = do
sec <- #{peek struct timespec, tv_sec} ptr
- nsec <- #{peek struct timespec, tv_nsec} ptr
- return $ TimeSpec sec nsec
- poke ptr (TimeSpec sec nsec) = do
+ return $ TimeSpec sec
+ poke ptr (TimeSpec sec) = do
#{poke struct timespec, tv_sec} ptr sec
- #{poke struct timespec, tv_nsec} ptr nsec
+ #{poke struct timespec, tv_nsec} ptr (0 :: CLong)
{- While its interface is beastly, utimensat is in recent
- POSIX standards, unlike futimes. -}
+ POSIX standards, unlike lutimes. -}
foreign import ccall "utimensat"
c_utimensat :: CInt -> CString -> Ptr TimeSpec -> CInt -> IO CInt
-{- Changes the access and/or modification times of an existing file.
- Can follow symlinks, or not. Throws IO error on failure. -}
touchBoth file atime mtime follow =
allocaArray 2 $ \ptr ->
withCString file $ \f -> do
@@ -83,8 +75,46 @@ touchBoth file atime mtime follow =
else at_symlink_nofollow
#else
-#warning "utimensat not available; building without symlink timestamp preservation support"
-omitTime = TimeSpec 0 (-1)
-nowTime = TimeSpec 0 (-2)
+#if 0
+{- Using lutimes is needed for BSD.
+ -
+ - TODO: test if lutimes is available. May have to do it in configure.
+ - TODO: TimeSpec uses a CTime, while tv_sec is a CLong. It is implementation
+ - dependent whether these are the same; need to find a cast that works.
+ - (The cast below fails.. without the cast it works on linux i386, but
+ - maybe not elsewhere.)
+ -}
+
+instance Storable TimeSpec where
+ alignment _ = alignment (undefined::CLong)
+ sizeOf _ = #{size struct timeval}
+ peek ptr = do
+ sec <- #{peek struct timeval, tv_sec} ptr
+ return $ TimeSpec sec
+ poke ptr (TimeSpec sec) = do
+ #{poke struct timeval, tv_sec} ptr (sec :: CLong)
+ #{poke struct timeval, tv_usec} ptr (0 :: CLong)
+
+foreign import ccall "utimes"
+ c_utimes :: CString -> Ptr TimeSpec -> IO CInt
+foreign import ccall "lutimes"
+ c_lutimes :: CString -> Ptr TimeSpec -> IO CInt
+
+touchBoth file atime mtime follow =
+ allocaArray 2 $ \ptr ->
+ withCString file $ \f -> do
+ pokeArray ptr [atime, mtime]
+ r <- syscall f ptr
+ if (r /= 0)
+ then throwErrno "touchBoth"
+ else return ()
+ where
+ syscall = if follow
+ then c_lutimes
+ else c_utimes
+
+#else
+warning "utimensat and lutimes not available; building without symlink timestamp preservation support"
touchBoth _ _ _ _ = return ()
#endif
+#endif