2011-03-15 03:00:23 +00:00
|
|
|
{- More control over touching a file.
|
|
|
|
-
|
|
|
|
- Copyright 2011 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE ForeignFunctionInterface #-}
|
|
|
|
|
2011-08-20 20:11:42 +00:00
|
|
|
module Utility.Touch (
|
2011-03-15 03:00:23 +00:00
|
|
|
TimeSpec(..),
|
|
|
|
touchBoth,
|
|
|
|
touch
|
|
|
|
) where
|
|
|
|
|
2012-03-09 23:08:10 +00:00
|
|
|
import Utility.FileSystemEncoding
|
|
|
|
|
2011-03-15 03:00:23 +00:00
|
|
|
import Foreign
|
|
|
|
import Foreign.C
|
2011-07-15 16:47:14 +00:00
|
|
|
import Control.Monad (when)
|
2012-02-04 18:30:28 +00:00
|
|
|
|
2011-05-21 15:07:08 +00:00
|
|
|
newtype TimeSpec = TimeSpec CTime
|
2011-03-16 16:06:32 +00:00
|
|
|
|
2011-03-20 18:04:39 +00:00
|
|
|
{- 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 ()
|
2011-03-15 03:00:23 +00:00
|
|
|
|
2011-03-16 17:46:08 +00:00
|
|
|
touch :: FilePath -> TimeSpec -> Bool -> IO ()
|
2011-09-21 03:24:48 +00:00
|
|
|
touch file mtime = touchBoth file mtime mtime
|
2011-03-16 17:46:08 +00:00
|
|
|
|
2011-03-20 18:04:39 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/time.h>
|
2011-03-16 17:46:08 +00:00
|
|
|
|
2011-03-20 18:04:39 +00:00
|
|
|
#ifndef _BSD_SOURCE
|
|
|
|
#define _BSD_SOURCE
|
|
|
|
#endif
|
2011-03-16 17:46:08 +00:00
|
|
|
|
|
|
|
#if (defined UTIME_OMIT && defined UTIME_NOW && defined AT_FDCWD && defined AT_SYMLINK_NOFOLLOW)
|
|
|
|
|
|
|
|
at_fdcwd :: CInt
|
|
|
|
at_fdcwd = #const AT_FDCWD
|
|
|
|
|
|
|
|
at_symlink_nofollow :: CInt
|
|
|
|
at_symlink_nofollow = #const AT_SYMLINK_NOFOLLOW
|
|
|
|
|
2011-03-15 03:00:23 +00:00
|
|
|
instance Storable TimeSpec where
|
2011-03-15 05:16:27 +00:00
|
|
|
-- use the larger alignment of the two types in the struct
|
|
|
|
alignment _ = max sec_alignment nsec_alignment
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
sec_alignment = alignment (undefined::CTime)
|
|
|
|
nsec_alignment = alignment (undefined::CLong)
|
2011-03-15 03:00:23 +00:00
|
|
|
sizeOf _ = #{size struct timespec}
|
|
|
|
peek ptr = do
|
|
|
|
sec <- #{peek struct timespec, tv_sec} ptr
|
2011-03-20 18:04:39 +00:00
|
|
|
return $ TimeSpec sec
|
|
|
|
poke ptr (TimeSpec sec) = do
|
2011-03-15 03:00:23 +00:00
|
|
|
#{poke struct timespec, tv_sec} ptr sec
|
2011-03-20 18:04:39 +00:00
|
|
|
#{poke struct timespec, tv_nsec} ptr (0 :: CLong)
|
2011-03-15 03:00:23 +00:00
|
|
|
|
|
|
|
{- While its interface is beastly, utimensat is in recent
|
2011-03-20 18:04:39 +00:00
|
|
|
POSIX standards, unlike lutimes. -}
|
2011-03-15 03:00:23 +00:00
|
|
|
foreign import ccall "utimensat"
|
|
|
|
c_utimensat :: CInt -> CString -> Ptr TimeSpec -> CInt -> IO CInt
|
|
|
|
|
|
|
|
touchBoth file atime mtime follow =
|
|
|
|
allocaArray 2 $ \ptr ->
|
2012-02-04 18:30:28 +00:00
|
|
|
withFilePath file $ \f -> do
|
2011-03-15 03:00:23 +00:00
|
|
|
pokeArray ptr [atime, mtime]
|
|
|
|
r <- c_utimensat at_fdcwd f ptr flags
|
2011-07-15 16:47:14 +00:00
|
|
|
when (r /= 0) $ throwErrno "touchBoth"
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
flags
|
|
|
|
| follow = 0
|
|
|
|
| otherwise = at_symlink_nofollow
|
2011-03-15 03:00:23 +00:00
|
|
|
|
2011-03-16 17:46:08 +00:00
|
|
|
#else
|
2011-03-20 18:04:39 +00:00
|
|
|
#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.
|
2011-03-20 22:06:06 +00:00
|
|
|
- (Without the cast it works on linux i386, but
|
2011-03-20 18:04:39 +00:00
|
|
|
- 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
|
2011-03-20 22:06:06 +00:00
|
|
|
#{poke struct timeval, tv_sec} ptr sec
|
2011-03-20 18:04:39 +00:00
|
|
|
#{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 ->
|
2012-02-04 18:30:28 +00:00
|
|
|
withFilePath file $ \f -> do
|
2011-03-20 18:04:39 +00:00
|
|
|
pokeArray ptr [atime, mtime]
|
|
|
|
r <- syscall f ptr
|
2012-04-22 03:32:33 +00:00
|
|
|
when (r /= 0) $
|
|
|
|
throwErrno "touchBoth"
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
syscall
|
|
|
|
| follow = c_lutimes
|
|
|
|
| otherwise = c_utimes
|
2011-03-20 18:04:39 +00:00
|
|
|
|
|
|
|
#else
|
2011-03-20 22:06:06 +00:00
|
|
|
#warning "utimensat and lutimes not available; building without symlink timestamp preservation support"
|
2012-04-22 15:21:23 +00:00
|
|
|
touchBoth _ _ _ _ = return ()
|
2011-03-16 17:46:08 +00:00
|
|
|
#endif
|
2011-03-20 18:04:39 +00:00
|
|
|
#endif
|