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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
|
{-# LANGUAGE CPP #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
-- Copyright (C) 2011 John Millikin <jmillikin@gmail.com>
--
-- See license.txt for details
module FilesystemTests.Posix
( test_Posix
) where
import Prelude hiding (FilePath)
import Control.Exception (bracket)
import Control.Monad
import Control.Monad.IO.Class (liftIO)
import qualified Data.ByteString
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as Char8
import qualified Data.Text
import Data.Text (Text)
import qualified Data.Text.IO
import Data.Time.Clock (diffUTCTime, getCurrentTime)
import Foreign
import Foreign.C
import Test.Chell
#if MIN_VERSION_base(4,2,0)
import qualified GHC.IO.Exception as GHC
#else
import qualified GHC.IOBase as GHC
#endif
import qualified System.Posix.IO as PosixIO
import Filesystem
import Filesystem.Path
import qualified Filesystem.Path.Rules as Rules
import qualified Filesystem.Path.CurrentOS as CurrentOS
import FilesystemTests.Util (assertionsWithTemp, todo)
test_Posix :: Suite
test_Posix = suite "posix"
[ suite "isFile"
[ test_IsFile "ascii"
(decode "test.txt")
, test_IsFile "utf8"
(fromText "\xA1\xA2.txt")
, test_IsFile "iso8859"
(decode "\xA1\xA2\xA3.txt")
, suite "pipe"
[ test_PipeIsFile "ascii"
(decode "test.txt")
, test_PipeIsFile "utf8"
(fromText "\xA1\xA2.txt")
, test_PipeIsFile "iso8859"
(decode "\xA1\xA2\xA3.txt")
]
]
, suite "isDirectory"
[ test_IsDirectory "ascii"
(decode "test.d")
, test_IsDirectory "utf8"
(fromText "\xA1\xA2.d")
, test_IsDirectory "iso8859"
(decode "\xA1\xA2\xA3.d")
]
, suite "rename"
[ test_Rename "ascii"
(decode "old_test.txt")
(decode "new_test.txt")
, test_Rename "utf8"
(fromText "old_\xA1\xA2.txt")
(fromText "new_\xA1\xA2.txt")
, test_Rename "iso8859"
(decode "old_\xA1\xA2\xA3.txt")
(decode "new_\xA1\xA2\xA3.txt")
]
, suite "canonicalizePath"
[ test_CanonicalizePath "ascii"
(decode "test-a.txt")
(decode "test-b.txt")
, test_CanonicalizePath "utf8"
(fromText "\xA1\xA2-a.txt")
(fromText "\xA1\xA2-b.txt")
, test_CanonicalizePath "iso8859"
(decode "\xA1\xA2\xA3-a.txt")
#ifdef CABAL_OS_DARWIN
(decode "%A1%A2%A3-b.txt")
#else
(decode "\xA1\xA2\xA3-b.txt")
#endif
, test_CanonicalizePath_TrailingSlash
]
, suite "createDirectory"
[ test_CreateDirectory "ascii"
(decode "test.d")
, test_CreateDirectory "utf8"
(fromText "\xA1\xA2.d")
, test_CreateDirectory "iso8859"
(decode "\xA1\xA2\xA3.d")
, test_CreateDirectory_FailExists
, test_CreateDirectory_SucceedExists
, test_CreateDirectory_FailFileExists
]
, suite "createTree"
[ test_CreateTree "ascii"
(decode "test.d")
, test_CreateTree "ascii-slash"
(decode "test.d/")
, test_CreateTree "utf8"
(fromText "\xA1\xA2.d")
, test_CreateTree "utf8-slash"
(fromText "\xA1\xA2.d/")
, test_CreateTree "iso8859"
(decode "\xA1\xA2\xA3.d")
, test_CreateTree "iso8859-slash"
(decode "\xA1\xA2\xA3.d/")
]
, test_ListDirectory
, suite "removeFile"
[ test_RemoveFile "ascii"
(decode "test.txt")
, test_RemoveFile "utf8"
(fromText "\xA1\xA2.txt")
, test_RemoveFile "iso8859"
(decode "\xA1\xA2\xA3.txt")
]
, suite "removeDirectory"
[ test_RemoveDirectory "ascii"
(decode "test.d")
, test_RemoveDirectory "utf8"
(fromText "\xA1\xA2.d")
, test_RemoveDirectory "iso8859"
(decode "\xA1\xA2\xA3.d")
]
, suite "removeTree"
[ test_RemoveTree "ascii"
(decode "test.d")
, test_RemoveTree "utf8"
(fromText "\xA1\xA2.d")
, test_RemoveTree "iso8859"
(decode "\xA1\xA2\xA3.d")
]
, suite "getWorkingDirectory"
[ test_GetWorkingDirectory "ascii"
(decode "test.d")
, test_GetWorkingDirectory "utf8"
(fromText "\xA1\xA2.d")
, test_GetWorkingDirectory "iso8859"
(decode "\xA1\xA2\xA3.d")
]
, suite "setWorkingDirectory"
[ test_SetWorkingDirectory "ascii"
(decode "test.d")
, test_SetWorkingDirectory "utf8"
(fromText "\xA1\xA2.d")
, test_SetWorkingDirectory "iso8859"
(decode "\xA1\xA2\xA3.d")
]
, suite "getHomeDirectory"
[ test_GetHomeDirectory "ascii"
(decode "/home/test.d")
, test_GetHomeDirectory "utf8"
(decode "/home/\xA1\xA2.d")
, test_GetHomeDirectory "iso8859"
(decode "/home/\xA1\xA2\xA3.d")
]
, suite "getDesktopDirectory"
[ test_GetDesktopDirectory "ascii"
(decode "/desktop/test.d")
, test_GetDesktopDirectory "utf8"
(decode "/desktop/\xA1\xA2.d")
, test_GetDesktopDirectory "iso8859"
(decode "/desktop/\xA1\xA2\xA3.d")
]
, todo "getDocumentsDirectory"
, todo "getAppDataDirectory"
, todo "getAppCacheDirectory"
, todo "getAppConfigDirectory"
, suite "getModified"
[ test_GetModified "ascii"
(decode "test.txt")
, test_GetModified "utf8"
(fromText "\xA1\xA2.txt")
, test_GetModified "iso8859"
(decode "\xA1\xA2\xA3.txt")
]
, suite "getSize"
[ test_GetSize "ascii"
(decode "test.txt")
, test_GetSize "utf8"
(fromText "\xA1\xA2.txt")
, test_GetSize "iso8859"
(decode "\xA1\xA2\xA3.txt")
]
, suite "copyFile"
[ test_CopyFile "ascii"
(decode "old_test.txt")
(decode "new_test.txt")
, test_CopyFile "utf8"
(fromText "old_\xA1\xA2.txt")
(fromText "new_\xA1\xA2.txt")
, test_CopyFile "iso8859"
(decode "old_\xA1\xA2\xA3.txt")
(decode "new_\xA1\xA2\xA3.txt")
]
, todo "openFile"
, suite "withFile"
[ suite "read"
[ test_WithFile_Read "ascii"
(decode "test.txt")
, test_WithFile_Read "utf8"
(fromText "\xA1\xA2.txt")
, test_WithFile_Read "iso8859"
(decode "\xA1\xA2\xA3.txt")
]
, suite "write"
[ test_WithFile_Write "ascii"
(decode "test.txt")
, test_WithFile_Write "utf8"
(fromText "\xA1\xA2.txt")
, test_WithFile_Write "iso8859"
(decode "\xA1\xA2\xA3.txt")
]
]
, todo "readFile"
, todo "writeFile"
, todo "appendFile"
, todo "openTextFile"
, suite "withTextFile"
[ test_WithTextFile "ascii"
(decode "test.txt")
, test_WithTextFile "utf8"
(fromText "\xA1\xA2.txt")
, test_WithTextFile "iso8859"
(decode "\xA1\xA2\xA3.txt")
]
, todo "readTextFile"
, todo "writeTextFile"
, todo "appendTextFile"
, suite "regression-tests"
[ test_ListDirectoryLeaksFds
]
]
test_IsFile :: Text -> FilePath -> Suite
test_IsFile test_name file_name = assertionsWithTemp test_name $ \tmp -> do
let path = tmp </> file_name
before <- liftIO $ Filesystem.isFile path
$expect (not before)
touch_ffi path "contents\n"
after <- liftIO $ Filesystem.isFile path
$expect after
test_PipeIsFile :: Text -> FilePath -> Suite
test_PipeIsFile test_name file_name = assertionsWithTemp test_name $ \tmp -> do
let path = tmp </> file_name
before <- liftIO $ Filesystem.isFile path
$expect (not before)
mkfifo_ffi path
after <- liftIO $ Filesystem.isFile path
$expect after
test_IsDirectory :: Text -> FilePath -> Suite
test_IsDirectory test_name dir_name = assertionsWithTemp test_name $ \tmp -> do
let path = tmp </> dir_name
before <- liftIO $ Filesystem.isDirectory path
$expect (not before)
mkdir_ffi path
after <- liftIO $ Filesystem.isDirectory path
$expect after
test_Rename :: Text -> FilePath -> FilePath -> Suite
test_Rename test_name old_name new_name = assertionsWithTemp test_name $ \tmp -> do
let old_path = tmp </> old_name
let new_path = tmp </> new_name
touch_ffi old_path ""
old_before <- liftIO $ Filesystem.isFile old_path
new_before <- liftIO $ Filesystem.isFile new_path
$expect old_before
$expect (not new_before)
liftIO $ Filesystem.rename old_path new_path
old_after <- liftIO $ Filesystem.isFile old_path
new_after <- liftIO $ Filesystem.isFile new_path
$expect (not old_after)
$expect new_after
test_CopyFile :: Text -> FilePath -> FilePath -> Suite
test_CopyFile test_name old_name new_name = assertionsWithTemp test_name $ \tmp -> do
let old_path = tmp </> old_name
let new_path = tmp </> new_name
touch_ffi old_path ""
old_before <- liftIO $ Filesystem.isFile old_path
new_before <- liftIO $ Filesystem.isFile new_path
$expect old_before
$expect (not new_before)
liftIO $ Filesystem.copyFile old_path new_path
old_after <- liftIO $ Filesystem.isFile old_path
new_after <- liftIO $ Filesystem.isFile new_path
$expect old_after
$expect new_after
old_contents <- liftIO $
Filesystem.withTextFile old_path ReadMode $
Data.Text.IO.hGetContents
new_contents <- liftIO $
Filesystem.withTextFile new_path ReadMode $
Data.Text.IO.hGetContents
$expect (equalLines old_contents new_contents)
test_CanonicalizePath :: Text -> FilePath -> FilePath -> Suite
test_CanonicalizePath test_name src_name dst_name = assertionsWithTemp test_name $ \tmp -> do
let src_path = tmp </> src_name
let subdir = tmp </> "subdir"
-- canonicalize the directory first, to avoid false negatives if
-- it gets placed in a symlinked location.
mkdir_ffi subdir
canon_subdir <- liftIO (Filesystem.canonicalizePath subdir)
let dst_path = canon_subdir </> dst_name
touch_ffi dst_path ""
symlink_ffi dst_path src_path
canonicalized <- liftIO $ Filesystem.canonicalizePath src_path
$expect $ equal canonicalized dst_path
test_CanonicalizePath_TrailingSlash :: Suite
test_CanonicalizePath_TrailingSlash = assertionsWithTemp "trailing-slash" $ \tmp -> do
let src_path = tmp </> "src"
let subdir = tmp </> "subdir"
-- canonicalize the directory first, to avoid false negatives if
-- it gets placed in a symlinked location.
mkdir_ffi subdir
canon_subdir <- liftIO (Filesystem.canonicalizePath (tmp </> "subdir"))
let dst_path = canon_subdir </> "dst"
mkdir_ffi dst_path
symlink_ffi dst_path src_path
canonicalized <- liftIO (Filesystem.canonicalizePath (src_path </> empty))
$expect (equal canonicalized (dst_path </> empty))
test_CreateDirectory :: Text -> FilePath -> Suite
test_CreateDirectory test_name dir_name = assertionsWithTemp test_name $ \tmp -> do
let dir_path = tmp </> dir_name
exists_before <- liftIO $ Filesystem.isDirectory dir_path
$assert (not exists_before)
liftIO $ Filesystem.createDirectory False dir_path
exists_after <- liftIO $ Filesystem.isDirectory dir_path
$expect exists_after
test_CreateDirectory_FailExists :: Suite
test_CreateDirectory_FailExists = assertionsWithTemp "fail-if-exists" $ \tmp -> do
let dir_path = tmp </> "subdir"
mkdir_ffi dir_path
$expect $ throwsEq
(mkAlreadyExists "createDirectory" dir_path)
(Filesystem.createDirectory False dir_path)
test_CreateDirectory_SucceedExists :: Suite
test_CreateDirectory_SucceedExists = assertionsWithTemp "succeed-if-exists" $ \tmp -> do
let dir_path = tmp </> "subdir"
mkdir_ffi dir_path
liftIO $ Filesystem.createDirectory True dir_path
test_CreateDirectory_FailFileExists :: Suite
test_CreateDirectory_FailFileExists = assertionsWithTemp "fail-if-file-exists" $ \tmp -> do
let dir_path = tmp </> "subdir"
touch_ffi dir_path ""
$expect $ throwsEq
(mkAlreadyExists "createDirectory" dir_path)
(Filesystem.createDirectory False dir_path)
$expect $ throwsEq
(mkAlreadyExists "createDirectory" dir_path)
(Filesystem.createDirectory True dir_path)
mkAlreadyExists :: String -> FilePath -> GHC.IOError
mkAlreadyExists loc path = GHC.IOError Nothing GHC.AlreadyExists loc "File exists"
#if MIN_VERSION_base(4,2,0)
(Just (errnoCInt eEXIST))
#endif
(Just (CurrentOS.encodeString path))
test_CreateTree :: Text -> FilePath -> Suite
test_CreateTree test_name dir_name = assertionsWithTemp test_name $ \tmp -> do
let dir_path = tmp </> dir_name
let subdir = dir_path </> "subdir"
dir_exists_before <- liftIO $ Filesystem.isDirectory dir_path
subdir_exists_before <- liftIO $ Filesystem.isDirectory subdir
$assert (not dir_exists_before)
$assert (not subdir_exists_before)
liftIO $ Filesystem.createTree subdir
dir_exists_after <- liftIO $ Filesystem.isDirectory dir_path
subdir_exists_after <- liftIO $ Filesystem.isDirectory subdir
$expect dir_exists_after
$expect subdir_exists_after
test_ListDirectory :: Suite
test_ListDirectory = assertionsWithTemp "listDirectory" $ \tmp -> do
-- OSX replaces non-UTF8 filenames with http-style %XX escapes
let paths =
#ifdef CABAL_OS_DARWIN
[ tmp </> decode "%A1%A2%A3.txt"
, tmp </> decode "test.txt"
, tmp </> fromText "\xA1\xA2.txt"
]
#else
[ tmp </> decode "test.txt"
, tmp </> fromText "\xA1\xA2.txt"
, tmp </> decode "\xA1\xA2\xA3.txt"
]
#endif
forM_ paths (\path -> touch_ffi path "")
names <- liftIO $ Filesystem.listDirectory tmp
$expect $ sameItems paths names
test_RemoveFile :: Text -> FilePath -> Suite
test_RemoveFile test_name file_name = assertionsWithTemp test_name $ \tmp -> do
let file_path = tmp </> file_name
touch_ffi file_path "contents\n"
before <- liftIO $ Filesystem.isFile file_path
$assert before
liftIO $ Filesystem.removeFile file_path
after <- liftIO $ Filesystem.isFile file_path
$expect (not after)
test_RemoveDirectory :: Text -> FilePath -> Suite
test_RemoveDirectory test_name dir_name = assertionsWithTemp test_name $ \tmp -> do
let dir_path = tmp </> dir_name
mkdir_ffi dir_path
before <- liftIO $ Filesystem.isDirectory dir_path
$assert before
liftIO $ Filesystem.removeDirectory dir_path
after <- liftIO $ Filesystem.isDirectory dir_path
$expect (not after)
test_RemoveTree :: Text -> FilePath -> Suite
test_RemoveTree test_name dir_name = assertionsWithTemp test_name $ \tmp -> do
let dir_path = tmp </> dir_name
let subdir = dir_path </> "subdir"
mkdir_ffi dir_path
mkdir_ffi subdir
dir_before <- liftIO $ Filesystem.isDirectory dir_path
subdir_before <- liftIO $ Filesystem.isDirectory subdir
$assert dir_before
$assert subdir_before
liftIO $ Filesystem.removeTree dir_path
dir_after <- liftIO $ Filesystem.isDirectory dir_path
subdir_after <- liftIO $ Filesystem.isDirectory subdir
$expect (not dir_after)
$expect (not subdir_after)
test_GetWorkingDirectory :: Text -> FilePath -> Suite
test_GetWorkingDirectory test_name dir_name = assertionsWithTemp test_name $ \tmp -> do
-- canonicalize to avoid issues with symlinked temp dirs
canon_tmp <- liftIO (Filesystem.canonicalizePath tmp)
let dir_path = canon_tmp </> dir_name
mkdir_ffi dir_path
chdir_ffi dir_path
cwd <- liftIO $ Filesystem.getWorkingDirectory
$expect (equal cwd dir_path)
test_SetWorkingDirectory :: Text -> FilePath -> Suite
test_SetWorkingDirectory test_name dir_name = assertionsWithTemp test_name $ \tmp -> do
-- canonicalize to avoid issues with symlinked temp dirs
canon_tmp <- liftIO (Filesystem.canonicalizePath tmp)
let dir_path = canon_tmp </> dir_name
mkdir_ffi dir_path
liftIO $ Filesystem.setWorkingDirectory dir_path
cwd <- getcwd_ffi
$expect (equal cwd dir_path)
test_GetHomeDirectory :: Text -> FilePath -> Suite
test_GetHomeDirectory test_name dir_name = assertions test_name $ do
path <- liftIO $ withEnv "HOME" (Just dir_name) Filesystem.getHomeDirectory
$expect (equal path dir_name)
test_GetDesktopDirectory :: Text -> FilePath -> Suite
test_GetDesktopDirectory test_name dir_name = assertions test_name $ do
path <- liftIO $
withEnv "XDG_DESKTOP_DIR" (Just dir_name) $
Filesystem.getDesktopDirectory
$expect (equal path dir_name)
fallback <- liftIO $
withEnv "XDG_DESKTOP_DIR" Nothing $
withEnv "HOME" (Just dir_name) $
Filesystem.getDesktopDirectory
$expect (equal fallback (dir_name </> "Desktop"))
test_GetModified :: Text -> FilePath -> Suite
test_GetModified test_name file_name = assertionsWithTemp test_name $ \tmp -> do
let file_path = tmp </> file_name
touch_ffi file_path ""
now <- liftIO getCurrentTime
mtime <- liftIO $ Filesystem.getModified file_path
$expect (equalWithin (diffUTCTime mtime now) 0 2)
test_GetSize :: Text -> FilePath -> Suite
test_GetSize test_name file_name = assertionsWithTemp test_name $ \tmp -> do
let file_path = tmp </> file_name
let contents = "contents\n"
touch_ffi file_path contents
size <- liftIO $ Filesystem.getSize file_path
$expect (equal size (toInteger (Data.ByteString.length contents)))
test_WithFile_Read :: Text -> FilePath -> Suite
test_WithFile_Read test_name file_name = assertionsWithTemp test_name $ \tmp -> do
let file_path = tmp </> file_name
let contents = "contents\n"
touch_ffi file_path contents
read_contents <- liftIO $
Filesystem.withFile file_path ReadMode $
Data.ByteString.hGetContents
$expect (equalLines contents read_contents)
test_WithFile_Write :: Text -> FilePath -> Suite
test_WithFile_Write test_name file_name = assertionsWithTemp test_name $ \tmp -> do
let file_path = tmp </> file_name
let contents = "contents\n"
liftIO $
Filesystem.withFile file_path WriteMode $
(\h -> Data.ByteString.hPut h contents)
read_contents <- liftIO $
Filesystem.withFile file_path ReadMode $
Data.ByteString.hGetContents
$expect (equalLines contents read_contents)
test_WithTextFile :: Text -> FilePath -> Suite
test_WithTextFile test_name file_name = assertionsWithTemp test_name $ \tmp -> do
let file_path = tmp </> file_name
let contents = "contents\n"
touch_ffi file_path (Char8.pack contents)
read_contents <- liftIO $
Filesystem.withTextFile file_path ReadMode $
Data.Text.IO.hGetContents
$expect (equalLines (Data.Text.pack contents) read_contents)
test_ListDirectoryLeaksFds :: Suite
test_ListDirectoryLeaksFds = assertionsWithTemp "listDirectory-leaks-fds" $ \tmp -> do
-- Test that listDirectory doesn't leak file descriptors.
let dir_path = tmp </> "subdir"
mkdir_ffi dir_path
nullfd1 <- liftIO $ PosixIO.openFd "/dev/null" PosixIO.ReadOnly Nothing PosixIO.defaultFileFlags
liftIO $ PosixIO.closeFd nullfd1
subdirContents <- liftIO $ listDirectory dir_path
nullfd2 <- liftIO $ PosixIO.openFd "/dev/null" PosixIO.ReadOnly Nothing PosixIO.defaultFileFlags
liftIO $ PosixIO.closeFd nullfd2
$assert (equal nullfd1 nullfd2)
withPathCString :: FilePath -> (CString -> IO a) -> IO a
withPathCString p = Data.ByteString.useAsCString (encode p)
decode :: ByteString -> FilePath
decode = Rules.decode Rules.posix
encode :: FilePath -> ByteString
encode = Rules.encode Rules.posix
fromText :: Text -> FilePath
fromText = Rules.fromText Rules.posix
-- | Create a file using the raw POSIX API, via FFI
touch_ffi :: FilePath -> Data.ByteString.ByteString -> Assertions ()
touch_ffi path contents = do
fp <- liftIO $ withPathCString path $ \path_cstr ->
Foreign.C.withCString "wb" $ \mode_cstr ->
c_fopen path_cstr mode_cstr
$assert (fp /= nullPtr)
_ <- liftIO $ Data.ByteString.useAsCStringLen contents $ \(buf, len) ->
c_fwrite buf 1 (fromIntegral len) fp
_ <- liftIO $ c_fclose fp
return ()
-- | Create a directory using the raw POSIX API, via FFI
mkdir_ffi :: FilePath -> Assertions ()
mkdir_ffi path = do
ret <- liftIO $ withPathCString path $ \path_cstr ->
c_mkdir path_cstr 0o700
$assert (ret == 0)
-- | Create a symlink using the raw POSIX API, via FFI
symlink_ffi :: FilePath -> FilePath -> Assertions ()
symlink_ffi dst src = do
ret <- liftIO $
withPathCString dst $ \dst_p ->
withPathCString src $ \src_p ->
c_symlink dst_p src_p
$assert (ret == 0)
-- | Create a FIFO using the raw POSIX API, via FFI
mkfifo_ffi :: FilePath -> Assertions ()
mkfifo_ffi path = do
ret <- liftIO $ withPathCString path $ \path_cstr ->
c_mkfifo path_cstr 0o700
$assert (ret == 0)
getcwd_ffi :: Assertions FilePath
getcwd_ffi = do
buf <- liftIO $ c_getcwd nullPtr 0
$assert (buf /= nullPtr)
bytes <- liftIO $ Data.ByteString.packCString buf
liftIO $ c_free buf
return (decode bytes)
chdir_ffi :: FilePath -> Assertions ()
chdir_ffi path = do
ret <- liftIO $
withPathCString path $ \path_p ->
c_chdir path_p
$assert (ret == 0)
errnoCInt :: Errno -> CInt
errnoCInt (Errno x) = x
withEnv :: ByteString -> Maybe FilePath -> IO a -> IO a
withEnv name val io = bracket set unset (\_ -> io) where
set = do
old <- getEnv name
setEnv name (fmap encode val)
return old
unset = setEnv name
getEnv :: ByteString -> IO (Maybe ByteString)
getEnv name = Data.ByteString.useAsCString name $ \cName -> do
ret <- liftIO (c_getenv cName)
if ret == nullPtr
then return Nothing
else fmap Just (Data.ByteString.packCString ret)
setEnv :: ByteString -> Maybe ByteString -> IO ()
setEnv name Nothing = throwErrnoIfMinus1_ "setEnv" $
Data.ByteString.useAsCString name c_unsetenv
setEnv name (Just val) = throwErrnoIfMinus1_ "setEnv" $
Data.ByteString.useAsCString name $ \cName ->
Data.ByteString.useAsCString val $ \cVal ->
c_setenv cName cVal 1
foreign import ccall unsafe "fopen"
c_fopen :: CString -> CString -> IO (Ptr ())
foreign import ccall unsafe "fclose"
c_fclose :: Ptr () -> IO CInt
foreign import ccall unsafe "fwrite"
c_fwrite :: CString -> CSize -> CSize -> Ptr () -> IO CSize
foreign import ccall unsafe "mkdir"
c_mkdir :: CString -> CInt -> IO CInt
foreign import ccall unsafe "symlink"
c_symlink :: CString -> CString -> IO CInt
foreign import ccall unsafe "mkfifo"
c_mkfifo :: CString -> CInt -> IO CInt
foreign import ccall unsafe "getcwd"
c_getcwd :: CString -> CSize -> IO CString
foreign import ccall unsafe "chdir"
c_chdir :: CString -> IO CInt
foreign import ccall unsafe "free"
c_free :: Ptr a -> IO ()
foreign import ccall unsafe "getenv"
c_getenv :: CString -> IO CString
foreign import ccall unsafe "setenv"
c_setenv :: CString -> CString -> CInt -> IO CInt
foreign import ccall unsafe "unsetenv"
c_unsetenv :: CString -> IO CInt
|