Does not go through symbolic links and may get stuck in a cycle if there is a cycle in the file system.
last_error
is updated with the first encountered error, or null on success.
The method does not stop on the first error and tries to remove the most files and directories.
var path = "/does/not/exists/".to_path
path.rmdir
assert path.last_error != null
path = "/tmp/path/to/create".to_path
path.to_s.mkdir
assert path.exists
path.rmdir
assert path.last_error == null
# Recursively delete a directory and all of its content
#
# Does not go through symbolic links and may get stuck in a cycle if there
# is a cycle in the file system.
#
# `last_error` is updated with the first encountered error, or null on success.
# The method does not stop on the first error and tries to remove the most files and directories.
#
# ~~~
# var path = "/does/not/exists/".to_path
# path.rmdir
# assert path.last_error != null
#
# path = "/tmp/path/to/create".to_path
# path.to_s.mkdir
# assert path.exists
# path.rmdir
# assert path.last_error == null
# ~~~
fun rmdir
do
var first_error = null
for file in self.files do
var stat = file.link_stat
if stat == null then
if first_error == null then first_error = file.last_error
continue
end
if stat.is_dir then
# Recursively rmdir
file.rmdir
else
file.delete
end
if first_error == null then first_error = file.last_error
end
# Delete the directory itself if things are fine
if first_error == null then
if not path.to_cstring.rmdir then
first_error = new IOError("Cannot remove `{self}`: {sys.errno.strerror}")
end
end
self.last_error = first_error
end
lib/core/file.nit:710,2--754,4