Haskell removeDirectoryRecursive:Windows上的权限被拒绝

时间:2016-08-12 21:46:07

标签: windows haskell

当我在Windows上使用removeDirectoryRecursive时,会抛出IOException类型为PermissionDenied的消息“removeDirectoryRecursive:permission denied”。我确实拥有删除目录所需的权限。对于具有相同内容的目录,在Linux上不会发生此问题。

1 个答案:

答案 0 :(得分:3)

如果要删除的目录包含只读文件,那么在Windows上删除这些文件会失败,但在Linux上则不会。

存在removePathForcibly,但只引入了very recently

所以这里有一个小辅助函数来递归地使文件可写。它可以在调用removeDirectoryRecursive之前使用。

import Control.Monad (forM_, when)
import System.FilePath ((</>))
import qualified System.Directory as FileSystem

-- Recursively makes all files and directories in a directory writable.
-- On Windows this is required to be able to recursively delete the directory.
makeWritableRecursive :: FilePath -> IO ()
makeWritableRecursive path = do
  permissions <- FileSystem.getPermissions path
  FileSystem.setPermissions path (FileSystem.setOwnerWritable True permissions)
  isDirectory <- FileSystem.doesDirectoryExist path
  when isDirectory $ do
    contents <- FileSystem.listDirectory path
    forM_ [path </> item | item <- contents] makeWritableRecursive