forked from haskell/directory
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement setModificationTime (and rewrite getModificationTime)
The unix package does not have a general setFileTimesHiRes that allowed setting mtime separately from atime. Therefore, we import the foreign utimensat function in the new Internal module (hsc), which also involves implementing struct timespec. Fixes haskell#13.
- Loading branch information
1 parent
b3d109e
commit 0317e68
Showing
4 changed files
with
178 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
module System.Directory.Internal where | ||
|
||
#ifndef mingw32_HOST_OS | ||
# include <HsUnixConfig.h> | ||
#endif | ||
|
||
#ifdef HAVE_UTIMENSAT | ||
# include <fcntl.h> | ||
# include <sys/stat.h> | ||
import Data.Time.Clock.POSIX (POSIXTime) | ||
import Foreign | ||
import Foreign.C | ||
import System.Posix.Types | ||
#endif | ||
|
||
#ifdef HAVE_UTIMENSAT | ||
|
||
data CTimeSpec = CTimeSpec EpochTime CLong | ||
|
||
instance Storable CTimeSpec where | ||
sizeOf _ = #size struct timespec | ||
alignment _ = alignment (undefined :: CInt) | ||
poke p (CTimeSpec sec nsec) = do | ||
(#poke struct timespec, tv_sec ) p sec | ||
(#poke struct timespec, tv_nsec) p nsec | ||
peek p = do | ||
sec <- #{peek struct timespec, tv_sec } p | ||
nsec <- #{peek struct timespec, tv_nsec} p | ||
return (CTimeSpec sec nsec) | ||
|
||
c_AT_FDCWD :: Integral a => a | ||
c_AT_FDCWD = (#const AT_FDCWD) | ||
|
||
utimeOmit :: CTimeSpec | ||
utimeOmit = CTimeSpec (CTime 0) (#const UTIME_OMIT) | ||
|
||
toCTimeSpec :: POSIXTime -> CTimeSpec | ||
toCTimeSpec t = CTimeSpec (CTime sec) (truncate $ 10 ^ (9 :: Int) * frac) | ||
where | ||
(sec, frac) = if frac' < 0 then (sec' - 1, frac' + 1) else (sec', frac') | ||
(sec', frac') = properFraction (toRational t) | ||
|
||
foreign import ccall unsafe "utimensat" c_utimensat | ||
:: CInt -> CString -> Ptr CTimeSpec -> CInt -> IO CInt | ||
|
||
#endif // HAVE_UTIMENSAT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters