nit: Added link to `CONTRIBUTING.md` from the README
[nit.git] / lib / core / file.nit
index 66b81aa..7f1d481 100644 (file)
@@ -498,8 +498,8 @@ class Path
                var output = dest.open_wo
 
                while not input.eof do
-                       var buffer = input.read(1024)
-                       output.write buffer
+                       var buffer = input.read_bytes(1024)
+                       output.write_bytes buffer
                end
 
                input.close
@@ -663,6 +663,19 @@ class Path
                return res
        end
 
+       # Is `self` the path to an existing directory ?
+       #
+       # ~~~nit
+       # assert ".".to_path.is_dir
+       # assert not "/etc/issue".to_path.is_dir
+       # assert not "/should/not/exist".to_path.is_dir
+       # ~~~
+       fun is_dir: Bool do
+               var st = stat
+               if st == null then return false
+               return st.is_dir
+       end
+
        # Delete a directory and all of its content
        #
        # Does not go through symbolic links and may get stuck in a cycle if there
@@ -1145,11 +1158,16 @@ redef class String
 
        # Create a directory (and all intermediate directories if needed)
        #
+       # The optional `mode` parameter specifies the permissions of the directory,
+       # the default value is `0o777`.
+       #
        # Return an error object in case of error.
        #
        #    assert "/etc/".mkdir != null
-       fun mkdir: nullable Error
+       fun mkdir(mode: nullable Int): nullable Error
        do
+               mode = mode or else 0o777
+
                var dirs = self.split_with("/")
                var path = new FlatBuffer
                if dirs.is_empty then return null
@@ -1162,7 +1180,7 @@ redef class String
                        if d.is_empty then continue
                        path.append(d)
                        path.add('/')
-                       var res = path.to_s.to_cstring.file_mkdir
+                       var res = path.to_s.to_cstring.file_mkdir(mode)
                        if not res and error == null then
                                error = new IOError("Cannot create directory `{path}`: {sys.errno.strerror}")
                        end
@@ -1326,7 +1344,7 @@ redef class NativeString
                return stat_element;
        `}
 
-       private fun file_mkdir: Bool `{ return !mkdir(self, 0777); `}
+       private fun file_mkdir(mode: Int): Bool `{ return !mkdir(self, mode); `}
 
        private fun rmdir: Bool `{ return !rmdir(self); `}