2015-01-20 20:58:48 +00:00
|
|
|
{- File size.
|
|
|
|
-
|
|
|
|
- License: BSD-2-clause
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE CPP #-}
|
2019-11-22 16:49:41 +00:00
|
|
|
{-# OPTIONS_GHC -fno-warn-tabs #-}
|
2015-01-20 20:58:48 +00:00
|
|
|
|
2019-11-21 19:38:06 +00:00
|
|
|
module Utility.FileSize (
|
|
|
|
FileSize,
|
|
|
|
getFileSize,
|
|
|
|
getFileSize',
|
|
|
|
) where
|
2015-01-20 20:58:48 +00:00
|
|
|
|
|
|
|
import System.PosixCompat.Files
|
2015-01-20 21:27:42 +00:00
|
|
|
#ifdef mingw32_HOST_OS
|
2015-01-20 20:58:48 +00:00
|
|
|
import Control.Exception (bracket)
|
|
|
|
import System.IO
|
2015-01-20 21:27:42 +00:00
|
|
|
#endif
|
2015-01-20 20:58:48 +00:00
|
|
|
|
2016-01-25 20:16:18 +00:00
|
|
|
type FileSize = Integer
|
|
|
|
|
2015-01-20 20:58:48 +00:00
|
|
|
{- Gets the size of a file.
|
|
|
|
-
|
|
|
|
- This is better than using fileSize, because on Windows that returns a
|
|
|
|
- FileOffset which maxes out at 2 gb.
|
|
|
|
- See https://github.com/jystic/unix-compat/issues/16
|
|
|
|
-}
|
2016-01-25 20:16:18 +00:00
|
|
|
getFileSize :: FilePath -> IO FileSize
|
2015-01-20 20:58:48 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2015-01-20 21:27:42 +00:00
|
|
|
getFileSize f = fmap (fromIntegral . fileSize) (getFileStatus f)
|
2015-01-20 20:58:48 +00:00
|
|
|
#else
|
|
|
|
getFileSize f = bracket (openFile f ReadMode) hClose hFileSize
|
|
|
|
#endif
|
|
|
|
|
2019-12-06 18:44:42 +00:00
|
|
|
{- Gets the size of the file, when its FileStatus is already known.
|
|
|
|
-
|
|
|
|
- On windows, uses getFileSize. Otherwise, the FileStatus contains the
|
|
|
|
- size, so this does not do any work. -}
|
2016-01-25 20:16:18 +00:00
|
|
|
getFileSize' :: FilePath -> FileStatus -> IO FileSize
|
2015-01-20 20:58:48 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
|
|
|
getFileSize' _ s = return $ fromIntegral $ fileSize s
|
|
|
|
#else
|
|
|
|
getFileSize' f _ = getFileSize f
|
|
|
|
#endif
|