System.Directory.removeDirectoryRecursive and symlinks

Gracjan Polak gracjanpolak at gmail.com
Tue Jun 10 13:42:22 UTC 2014


Hi all,

A crime scene:

Prelude System.Directory> :!mkdir a-directory
Prelude System.Directory> :!touch a-directory/a-file.txt
Prelude System.Directory> :!ln -s "a-directory" "a-symlink-to-a-directory"
Prelude System.Directory> :!ls a-directory
a-file.txt
Prelude System.Directory> :!ls a-symlink-to-a-directory
a-file.txt
Prelude System.Directory> removeDirectoryRecursive
                             "a-symlink-to-a-directory"
*** Exception: a-symlink-to-a-directory: removeDirectory: 
    inappropriate type (Not a directory)
Prelude System.Directory> :!ls a-symlink-to-a-directory
Prelude System.Directory> :!ls a-directory
Prelude System.Directory> :!ls -a a-directory
.	..
Prelude System.Directory> :!ls -a a-symlink-to-a-directory
.	..
Prelude System.Directory> 

removeDirectoryRecursive is removing all contents *of the directory linked*
but is unable to remove the symlink itself.

This behavior is surprizing if not dangerous. I understand that this mimics
behavior of unlink/rmdir and DeleteFile/RemoveDirectory. but let me quote
relevant manuals:

man rm:
The rm utility removes symbolic links, not the files referenced by the links.

DeleteFile docs:
If the path points to a symbolic link, the symbolic link is deleted, not the
target. To delete a target, you must call CreateFile and specify
FILE_FLAG_DELETE_ON_CLOSE.

RemoveDirectory removes a directory junction, even if the contents of the
target are not empty; the function removes directory junctions regardless of
the state of the target object.

Note: doesDirectoryExist and doesFileExist follow symlinks so they add more
surprize to the scenario.

What can we do about this?

-- 
Gracjan




More information about the Libraries mailing list