end
# Delete the directory itself
- if ok then path.to_cstring.rmdir
+ if ok then ok = path.to_cstring.rmdir and ok
return ok
end
end
# Create a directory (and all intermediate directories if needed)
- fun mkdir
+ #
+ # Return an error object in case of error.
+ #
+ # assert "/etc/".mkdir != null
+ fun mkdir: nullable Error
do
var dirs = self.split_with("/")
var path = new FlatBuffer
- if dirs.is_empty then return
+ if dirs.is_empty then return null
if dirs[0].is_empty then
# it was a starting /
path.add('/')
end
+ var error: nullable Error = null
for d in dirs do
if d.is_empty then continue
path.append(d)
path.add('/')
- path.to_s.to_cstring.file_mkdir
+ var res = path.to_s.to_cstring.file_mkdir
+ if not res and error == null then
+ error = new IOError("Cannot create directory `{path}`: {sys.errno.strerror}")
+ end
end
+ return error
end
# Delete a directory and all of its content, return `true` on success
#
# Does not go through symbolic links and may get stuck in a cycle if there
# is a cycle in the filesystem.
- fun rmdir: Bool do return to_path.rmdir
+ #
+ # Return an error object in case of error.
+ #
+ # assert "/fail/does not/exist".rmdir != null
+ fun rmdir: nullable Error
+ do
+ var res = to_path.rmdir
+ if res then return null
+ var error = new IOError("Cannot change remove `{self}`: {sys.errno.strerror}")
+ return error
+ end
# Change the current working directory
#
# "..".chdir
# assert getcwd == "/"
#
- # TODO: errno
- fun chdir do to_cstring.file_chdir
+ # Return an error object in case of error.
+ #
+ # assert "/etc".chdir == null
+ # assert "/fail/does no/exist".chdir != null
+ # assert getcwd == "/etc" # unchanger
+ fun chdir: nullable Error
+ do
+ var res = to_cstring.file_chdir
+ if res then return null
+ var error = new IOError("Cannot change directory to `{self}`: {sys.errno.strerror}")
+ return error
+ end
# Return right-most extension (without the dot)
#
end
end
- # returns files contained within the directory represented by self
+ # Returns entries contained within the directory represented by self.
+ #
+ # var files = "/etc".files
+ # assert files.has("issue")
+ #
+ # Returns an empty array in case of error
+ #
+ # files = "/etc/issue".files
+ # assert files.is_empty
+ #
+ # TODO find a better way to handle errors and to give them back to the user.
fun files: Array[String] is extern import Array[String], Array[String].add, NativeString.to_s, String.to_cstring `{
char *dir_path;
DIR *dir;
dir_path = String_to_cstring( recv );
if ((dir = opendir(dir_path)) == NULL)
{
- perror( dir_path );
- exit( 1 );
+ //perror( dir_path );
+ //exit( 1 );
+ Array_of_String results;
+ results = new_Array_of_String();
+ return results;
}
else
{
return stat_element;
`}
private fun file_mkdir: Bool is extern "string_NativeString_NativeString_file_mkdir_0"
- private fun rmdir: Bool `{ return rmdir(recv); `}
+ private fun rmdir: Bool `{ return !rmdir(recv); `}
private fun file_delete: Bool is extern "string_NativeString_NativeString_file_delete_0"
- private fun file_chdir is extern "string_NativeString_NativeString_file_chdir_0"
+ private fun file_chdir: Bool is extern "string_NativeString_NativeString_file_chdir_0"
private fun file_realpath: NativeString is extern "file_NativeString_realpath"
end
end
# Print `objects` on the standard output (`stdout`).
-protected fun printn(objects: Object...)
+fun printn(objects: Object...)
do
sys.stdout.write(objects.to_s)
end
# Print an `object` on the standard output (`stdout`) and add a newline.
-protected fun print(object: Object)
+fun print(object: Object)
do
sys.stdout.write(object.to_s)
sys.stdout.write("\n")
end
# Read a character from the standard input (`stdin`).
-protected fun getc: Char
+fun getc: Char
do
return sys.stdin.read_char.ascii
end
# Read a line from the standard input (`stdin`).
-protected fun gets: String
+fun gets: String
do
return sys.stdin.read_line
end
# Return the working (current) directory
-protected fun getcwd: String do return file_getcwd.to_s
+fun getcwd: String do return file_getcwd.to_s
private fun file_getcwd: NativeString is extern "string_NativeString_NativeString_file_getcwd_0"