+{- |The 'getPermissions' operation returns the
+permissions for the file or directory.
+
+The operation may fail with:
+
+* 'isPermissionError' if the user is not permitted to access
+ the permissions; or
+
+* 'isDoesNotExistError' if the file or directory does not exist.
+
+-}
+
+getPermissions :: FilePath -> IO Permissions
+getPermissions name = do
+#ifdef mingw32_HOST_OS
+ withFilePath name $ \s -> do
+ -- stat() does a better job of guessing the permissions on Windows
+ -- than access() does. e.g. for execute permission, it looks at the
+ -- filename extension :-)
+ --
+ -- I tried for a while to do this properly, using the Windows security API,
+ -- and eventually gave up. getPermissions is a flawed API anyway. -- SimonM
+ allocaBytes sizeof_stat $ \ p_stat -> do
+ throwErrnoIfMinus1_ "getPermissions" $ c_stat s p_stat
+ mode <- st_mode p_stat
+ let usr_read = mode .&. s_IRUSR
+ let usr_write = mode .&. s_IWUSR
+ let usr_exec = mode .&. s_IXUSR
+ let is_dir = mode .&. s_IFDIR
+ return (
+ Permissions {
+ readable = usr_read /= 0,
+ writable = usr_write /= 0,
+ executable = is_dir == 0 && usr_exec /= 0,
+ searchable = is_dir /= 0 && usr_exec /= 0
+ }
+ )
+#else
+ read_ok <- Posix.fileAccess name True False False
+ write_ok <- Posix.fileAccess name False True False
+ exec_ok <- Posix.fileAccess name False False True
+ stat <- Posix.getFileStatus name
+ let is_dir = Posix.fileMode stat .&. Posix.directoryMode /= 0
+ return (
+ Permissions {
+ readable = read_ok,
+ writable = write_ok,
+ executable = not is_dir && exec_ok,
+ searchable = is_dir && exec_ok
+ }
+ )
+#endif
+
+{- |The 'setPermissions' operation sets the
+permissions for the file or directory.
+
+The operation may fail with:
+
+* 'isPermissionError' if the user is not permitted to set
+ the permissions; or
+
+* 'isDoesNotExistError' if the file or directory does not exist.
+
+-}
+
+setPermissions :: FilePath -> Permissions -> IO ()
+setPermissions name (Permissions r w e s) = do
+#ifdef mingw32_HOST_OS
+ allocaBytes sizeof_stat $ \ p_stat -> do
+ withFilePath name $ \p_name -> do
+ throwErrnoIfMinus1_ "setPermissions" $ do
+ c_stat p_name p_stat
+ mode <- st_mode p_stat
+ let mode1 = modifyBit r mode s_IRUSR
+ let mode2 = modifyBit w mode1 s_IWUSR
+ let mode3 = modifyBit (e || s) mode2 s_IXUSR
+ c_wchmod p_name mode3
+ where
+ modifyBit :: Bool -> CMode -> CMode -> CMode
+ modifyBit False m b = m .&. (complement b)
+ modifyBit True m b = m .|. b
+#else
+ stat <- Posix.getFileStatus name
+ let mode = Posix.fileMode stat
+ let mode1 = modifyBit r mode Posix.ownerReadMode
+ let mode2 = modifyBit w mode1 Posix.ownerWriteMode
+ let mode3 = modifyBit (e || s) mode2 Posix.ownerExecuteMode
+ Posix.setFileMode name mode3
+ where
+ modifyBit :: Bool -> Posix.FileMode -> Posix.FileMode -> Posix.FileMode
+ modifyBit False m b = m .&. (complement b)
+ modifyBit True m b = m .|. b
+#endif
+
+#ifdef mingw32_HOST_OS
+foreign import ccall unsafe "_wchmod"
+ c_wchmod :: CWString -> CMode -> IO CInt
+#endif
+
+copyPermissions :: FilePath -> FilePath -> IO ()
+copyPermissions source dest = do
+#ifdef mingw32_HOST_OS
+ allocaBytes sizeof_stat $ \ p_stat -> do
+ withFilePath source $ \p_source -> do
+ withFilePath dest $ \p_dest -> do
+ throwErrnoIfMinus1_ "copyPermissions" $ c_stat p_source p_stat
+ mode <- st_mode p_stat
+ throwErrnoIfMinus1_ "copyPermissions" $ c_wchmod p_dest mode
+#else
+ stat <- Posix.getFileStatus source
+ let mode = Posix.fileMode stat
+ Posix.setFileMode dest mode
+#endif
+