Haskell removeDirectoryRecursive:Windows 上的权限被拒绝

Ruu*_*uud 5 windows haskell

当我removeDirectoryRecursive在 Windows 上使用时,IOException类型PermissionDenied,并显示消息 \xe2\x80\x9cremoveDirectoryRecursive:权限被拒绝\xe2\x80\x9d。我确实拥有删除该目录所需的权限。对于具有相同内容的目录,在 Linux 上不会出现此问题。

\n

Ruu*_*uud 4

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

\n\n

存在,但最近removePathForcibly才推出

\n\n

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

\n\n
import Control.Monad (forM_, when)\nimport System.FilePath ((</>))\nimport qualified System.Directory as FileSystem\n\n-- Recursively makes all files and directories in a directory writable.\n-- On Windows this is required to be able to recursively delete the directory.\nmakeWritableRecursive :: FilePath -> IO ()\nmakeWritableRecursive path = do\n  permissions <- FileSystem.getPermissions path\n  FileSystem.setPermissions path (FileSystem.setOwnerWritable True permissions)\n  isDirectory <- FileSystem.doesDirectoryExist path\n  when isDirectory $ do\n    contents <- FileSystem.listDirectory path\n    forM_ [path </> item | item <- contents] makeWritableRecursive\n
Run Code Online (Sandbox Code Playgroud)\n