From a8e9cf5af995942c7e0fe350e29b8531545cf340 Mon Sep 17 00:00:00 2001 From: a5ob7r <12132068+a5ob7r@users.noreply.github.com> Date: Fri, 16 Sep 2022 23:06:17 +0900 Subject: [PATCH 1/2] Fix compatibilities with unix >=2.8 package Since version 2.8, the unix package changed `UserEntry` and `GroupEntry` into the `ByteString` based implementation instead of `String` as a breaking change. We can compile this package with unix >=2.8 if just re-exports the two of new data types, but it's probably uncomfortable for users because types of fields of the two isn't the same between prior 2.8 and since 2.8. So we need to define and export compatible data types for `UserEntry` and `GroupEntry` to resolve this problem. --- src/System/PosixCompat/User.hsc | 76 ++++++++++++++++++++++++++++++++- unix-compat.cabal | 10 ++++- 2 files changed, 84 insertions(+), 2 deletions(-) diff --git a/src/System/PosixCompat/User.hsc b/src/System/PosixCompat/User.hsc index b5b07e2..8def65f 100644 --- a/src/System/PosixCompat/User.hsc +++ b/src/System/PosixCompat/User.hsc @@ -3,7 +3,10 @@ {-| This module makes the operations exported by @System.Posix.User@ available on all platforms. On POSIX systems it re-exports operations from -@System.Posix.User@. On other platforms it provides dummy implementations. +@System.Posix.User@. And if using this module with unix package since version +@2.8@ on POSIX systems, it redefines some data type and functions for +compatibilities with unix package prior to version @2.8@. On other platforms it +provides dummy implementations. -} module System.PosixCompat.User ( -- * User environment @@ -37,7 +40,78 @@ module System.PosixCompat.User ( #include "HsUnixCompat.h" +#ifdef UNIX_2_8 +import System.Posix.Types (GroupID, UserID) import System.Posix.User + ( getRealUserID + , getRealGroupID + , getEffectiveUserID + , getEffectiveGroupID + , getGroups + , getLoginName + , getEffectiveUserName + , setUserID + , setGroupID + ) +import qualified System.Posix.User as User +import qualified System.Posix.User.ByteString as BUser + +data GroupEntry = GroupEntry + { groupName :: String + , groupPassword :: String + , groupID :: GroupID + , groupMembers :: [String] + } deriving (Show, Read, Eq) + +toCompatGroupEntry :: BUser.GroupEntry -> GroupEntry +toCompatGroupEntry entry = GroupEntry + { groupName = User.groupName entry + , groupPassword = User.groupPassword entry + , groupID = User.groupID entry + , groupMembers = User.groupMembers entry + } + +getGroupEntryForID :: GroupID -> IO GroupEntry +getGroupEntryForID = fmap toCompatGroupEntry . User.getGroupEntryForID + +getGroupEntryForName :: String -> IO GroupEntry +getGroupEntryForName = fmap toCompatGroupEntry . User.getGroupEntryForName + +getAllGroupEntries :: IO [GroupEntry] +getAllGroupEntries = fmap (map toCompatGroupEntry) User.getAllGroupEntries + +data UserEntry = UserEntry + { userName :: String + , userPassword :: String + , userID :: UserID + , userGroupID :: GroupID + , userGecos :: String + , homeDirectory :: String + , userShell :: String + } deriving (Show, Read, Eq) + +toCompatUserEntry :: BUser.UserEntry -> UserEntry +toCompatUserEntry entry = UserEntry + { userName = User.userName entry + , userPassword = User.userPassword entry + , userID = User.userID entry + , userGroupID = User.userGroupID entry + , userGecos = User.userGecos entry + , homeDirectory = User.homeDirectory entry + , userShell = User.userShell entry + } + +getUserEntryForID :: UserID -> IO UserEntry +getUserEntryForID = fmap toCompatUserEntry . User.getUserEntryForID + +getUserEntryForName :: String -> IO UserEntry +getUserEntryForName = fmap toCompatUserEntry . User.getUserEntryForName + +getAllUserEntries :: IO [UserEntry] +getAllUserEntries = fmap (map toCompatUserEntry) User.getAllUserEntries +#else +import System.Posix.User +#endif #if __GLASGOW_HASKELL__<605 getAllGroupEntries :: IO [GroupEntry] diff --git a/unix-compat.cabal b/unix-compat.cabal index 8879404..516d118 100644 --- a/unix-compat.cabal +++ b/unix-compat.cabal @@ -26,6 +26,9 @@ flag old-time description: build against old-time package default: False +flag unix28 + description: build against unix >= 2.8 package + Library default-language: Haskell2010 hs-source-dirs: src @@ -68,7 +71,12 @@ Library System.PosixCompat.Internal.Time else - build-depends: unix >= 2.6 && < 2.9 + if flag(unix28) + build-depends: unix >= 2.8 && < 2.9 + cpp-options: -DUNIX_2_8 + else + build-depends: unix >= 2.6 && < 2.8 + include-dirs: include includes: HsUnixCompat.h install-includes: HsUnixCompat.h From 3f6bd688cb56224955e77245a2649ba99ea32fff Mon Sep 17 00:00:00 2001 From: a5ob7r <12132068+a5ob7r@users.noreply.github.com> Date: Mon, 3 Oct 2022 09:47:33 +0900 Subject: [PATCH 2/2] Remove a unnecessary flag Use MIN_VERSION* macro to branch code by versions of unix package. --- src/System/PosixCompat/User.hsc | 2 +- unix-compat.cabal | 10 +--------- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/src/System/PosixCompat/User.hsc b/src/System/PosixCompat/User.hsc index 8def65f..f4c86c4 100644 --- a/src/System/PosixCompat/User.hsc +++ b/src/System/PosixCompat/User.hsc @@ -40,7 +40,7 @@ module System.PosixCompat.User ( #include "HsUnixCompat.h" -#ifdef UNIX_2_8 +#if MIN_VERSION_unix(2, 8, 0) import System.Posix.Types (GroupID, UserID) import System.Posix.User ( getRealUserID diff --git a/unix-compat.cabal b/unix-compat.cabal index 516d118..8879404 100644 --- a/unix-compat.cabal +++ b/unix-compat.cabal @@ -26,9 +26,6 @@ flag old-time description: build against old-time package default: False -flag unix28 - description: build against unix >= 2.8 package - Library default-language: Haskell2010 hs-source-dirs: src @@ -71,12 +68,7 @@ Library System.PosixCompat.Internal.Time else - if flag(unix28) - build-depends: unix >= 2.8 && < 2.9 - cpp-options: -DUNIX_2_8 - else - build-depends: unix >= 2.6 && < 2.8 - + build-depends: unix >= 2.6 && < 2.9 include-dirs: include includes: HsUnixCompat.h install-includes: HsUnixCompat.h