File: CopyFileWithMetadata.hs

package info (click to toggle)
ghc 8.0.1-17
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 55,080 kB
  • ctags: 9,332
  • sloc: haskell: 363,120; ansic: 54,900; sh: 4,782; makefile: 974; perl: 542; asm: 315; python: 306; xml: 154; lisp: 7
file content (46 lines) | stat: -rw-r--r-- 1,343 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
{-# LANGUAGE CPP #-}
module CopyFileWithMetadata where
#include "util.inl"
import System.Directory
import Control.Exception (finally)
import Data.Foldable (for_)
import Data.List (sort)
import System.IO.Error (catchIOError)

main :: TestEnv -> IO ()
main _t = (`finally` cleanup) $ do

  -- prepare source file
  writeFile "a" contents
  writeFile "b" "To be replaced\n"
  setModificationTime "a" mtime
  modifyWritable False "a"
  perm <- getPermissions "a"

  -- sanity check
  T(expectEq) () ["a", "b"] . sort =<< listDirectory "."

  -- copy file
  copyFileWithMetadata "a" "b"
  copyFileWithMetadata "a" "c"

  -- make sure we got the right results
  T(expectEq) () ["a", "b", "c"] . sort =<< listDirectory "."
  for_ ["b", "c"] $ \ f -> do
    T(expectEq) f perm =<< getPermissions f
    T(expectEq) f mtime =<< getModificationTime f
    T(expectEq) f contents =<< readFile f

  where
    contents = "This is the data\n"
    mtime = read "2000-01-01 00:00:00"

    cleanup = do
      -- needed to ensure the test runner can clean up our mess
      modifyWritable True "a" `catchIOError` \ _ -> return ()
      modifyWritable True "b" `catchIOError` \ _ -> return ()
      modifyWritable True "c" `catchIOError` \ _ -> return ()

    modifyWritable b f = do
      perm <- getPermissions f
      setPermissions f (setOwnerWritable b perm)