lib/file: some methods return a nullable Error on error
authorJean Privat <jean@pryen.org>
Mon, 6 Apr 2015 15:09:55 +0000 (22:09 +0700)
committerJean Privat <jean@pryen.org>
Thu, 9 Apr 2015 13:47:27 +0000 (20:47 +0700)
Signed-off-by: Jean Privat <jean@pryen.org>

lib/standard/file.nit
lib/standard/file_nit.h

index 545f59b..42eca14 100644 (file)
@@ -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
 
index d8d9b4b..d86b2df 100644 (file)
@@ -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_()