From 2ecde4fea5aaeb926a513c689e2f2ca9995a5f2b Mon Sep 17 00:00:00 2001 From: Alexander Kobjolke Date: Fri, 1 Sep 2023 08:37:54 +0200 Subject: [PATCH] Implement getFileInfo to retrieve meta data getFileInfo returns a record containing meta data about the given file, such as permissions, last modification time and time. We'll use this action to fill a neat status bar. --- hcat.cabal | 9 +++++++++ package.yaml | 4 ++-- src/HCat/FileInfo.hs | 45 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 src/HCat/FileInfo.hs diff --git a/hcat.cabal b/hcat.cabal index 9c3766b..d0e182c 100644 --- a/hcat.cabal +++ b/hcat.cabal @@ -18,6 +18,7 @@ extra-source-files: library exposed-modules: HCat + HCat.FileInfo HCat.Internal other-modules: Paths_hcat @@ -30,8 +31,10 @@ library ghc-options: -Wall -fdefer-typed-holes build-depends: base >=4.13 && <5 + , directory , process , text + , time default-language: GHC2021 executable hcat @@ -47,7 +50,9 @@ executable hcat ghc-options: -Wall -fdefer-typed-holes build-depends: base >=4.13 && <5 + , directory , hcat + , time default-language: GHC2021 test-suite doctest @@ -66,7 +71,9 @@ test-suite doctest doctest:doctest build-depends: base >=4.13 && <5 + , directory , process + , time default-language: Haskell2010 test-suite spec @@ -88,7 +95,9 @@ test-suite spec build-depends: QuickCheck , base >=4.13 && <5 + , directory , hcat , hspec , quickcheck-instances + , time default-language: GHC2021 diff --git a/package.yaml b/package.yaml index d15b892..37763d1 100644 --- a/package.yaml +++ b/package.yaml @@ -11,9 +11,9 @@ extra-source-files: dependencies: - base >= 4.13 && < 5 + - time + - directory # - bytestring -# - time -# - directory ghc-options: - -Wall diff --git a/src/HCat/FileInfo.hs b/src/HCat/FileInfo.hs new file mode 100644 index 0000000..f1cc2d8 --- /dev/null +++ b/src/HCat/FileInfo.hs @@ -0,0 +1,45 @@ +{-# LANGUAGE RecordWildCards #-} + +-- | FileInfo defines a data type that captures important information about a +-- file such as timestamps and permissions. +module HCat.FileInfo (FileInfo, getFileInfo) where + +import Data.Text (Text) +import Data.Text qualified as T + +import Data.Time.Clock (UTCTime) + +import System.Directory (getFileSize, getModificationTime, getPermissions) +import System.Directory qualified as Directory + +data FileInfo = FileInfo + { filePath :: Text + , fileSize :: Integer + , fileMTime :: UTCTime + , fileReadable :: Bool + , fileWritable :: Bool + , fileExecutable :: Bool + } + deriving (Show) + +-- | @getFileInfo@ retrieves several meta data about the given file. +-- +-- >>> info <- getFileInfo "src/HCat/FileInfo.hs" +-- >>> filePath info +-- "src/HCat/FileInfo.hs" +-- >>> fileSize info > 0 +-- True +-- >>> fileReadable info +-- True +-- >>> fileExecutable info +-- False +getFileInfo :: FilePath -> IO FileInfo +getFileInfo path = do + perms <- getPermissions path + fileSize <- getFileSize path + fileMTime <- getModificationTime path + let fileReadable = Directory.readable perms + fileWritable = Directory.writable perms + fileExecutable = Directory.executable perms + filePath = T.pack path + return FileInfo{..}