From ebe254fa72e37b4a0a39b31c301c2a2b8815ce0c Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Mon, 6 Apr 2015 22:09:55 +0700 Subject: [PATCH] lib/file: some methods return a nullable Error on error Signed-off-by: Jean Privat --- lib/standard/file.nit | 47 ++++++++++++++++++++++++++++++++++++++--------- lib/standard/file_nit.h | 4 ++-- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/lib/standard/file.nit b/lib/standard/file.nit index 545f59b..42eca14 100644 --- a/lib/standard/file.nit +++ b/lib/standard/file.nit @@ -495,7 +495,7 @@ class Path 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 @@ -903,28 +903,47 @@ redef class String 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 # @@ -933,8 +952,18 @@ redef class String # "..".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) # @@ -1022,9 +1051,9 @@ redef class NativeString 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 diff --git a/lib/standard/file_nit.h b/lib/standard/file_nit.h index d8d9b4b..d86b2df 100644 --- a/lib/standard/file_nit.h +++ b/lib/standard/file_nit.h @@ -46,9 +46,9 @@ int file_NativeFile_NativeFile_set_buffering_type_0(FILE* f, int buf_sz, int mod #define file_Sys_Sys_buffer_mode_line_0(self) _IOLBF #define file_Sys_Sys_buffer_mode_none_0(self) _IONBF -#define string_NativeString_NativeString_file_mkdir_0(p) (mkdir(p, 0777)) +#define string_NativeString_NativeString_file_mkdir_0(p) (!mkdir(p, 0777)) #define string_NativeString_NativeString_file_getcwd_0(p) (getcwd(NULL, 0)) -#define string_NativeString_NativeString_file_chdir_0(p) (chdir(p)?-1:0) /* hack to avoid warn_unused_result */ +#define string_NativeString_NativeString_file_chdir_0(p) (!chdir(p)) #define file_NativeString_realpath(p) (realpath(p, NULL)) #define file_stdin_poll_in(self) file_stdin_poll_in_() -- 1.7.9.5