Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added support, bindings for readdir() using libuv.

  • Loading branch information...
commit 08523e47ba7d7f2b74f932f33cfd9224ee4c1fcd 1 parent e519c91
@kmsquire kmsquire authored Keno committed
Showing with 56 additions and 0 deletions.
  1. +1 −0  base/export.jl
  2. +34 −0 base/file.jl
  3. +5 −0 src/julia.expmap
  4. +16 −0 src/sys.c
View
1  base/export.jl
@@ -1023,6 +1023,7 @@ export
read,
readall,
readchomp,
+ readdir,
readline,
readlines,
readuntil,
View
34 base/file.jl
@@ -237,3 +237,37 @@ function download_file(url::String)
run(`curl -o $filename $url`)
filename
end
+
+function readdir(path::String)
+ # Allocate space for uv_fs_t struct
+ uv_readdir_req = zeros(Uint8, ccall(:jl_sizeof_uv_fs_t, Int, ()))
+
+ # defined in sys.c, to call uv_fs_readdir
+ file_count = ccall(:jl_readdir, Int, (Ptr{Uint8}, Ptr{Uint8}),
+ bytestring(path), uv_readdir_req)
+
+ if file_count < 0
+ error("Unable to read directory $path.")
+ end
+
+ # The list of dir entries is returned as a contiguous sequence of null-terminated
+ # strings, the first of which is pointed to by ptr in uv_readdir_req.
+ # The following lines extracts those strings into dirent
+ entries = String[]
+ offset = 0
+
+ for i = 1:file_count
+ entry = bytestring(ccall(:jl_uv_fs_t_ptr_offset, Ptr{Uint8},
+ (Ptr{Uint8}, Int), uv_readdir_req, offset))
+ push(entries, entry)
+ offset += length(entry) + 1 # offset to the next entry
+ end
+
+ # Clean up the request string
+ ccall(:jl_uv_fs_req_cleanup, Void, (Ptr{Uint8},), uv_readdir_req)
+
+ entries
+end
+
+readdir(cmd::Cmd) = readdir(string(cmd)[2:end-1])
+readdir() = readdir(".")
View
5 src/julia.expmap
@@ -70,6 +70,11 @@
jl_sizeof_off_t;
jl_sizeof_ios_t;
jl_stdout_stream;
+ jl_sizeof_uv_fs_t;
+ jl_uv_fs_req_cleanup;
+ jl_readdir;
+ jl_uv_fs_t_ptr;
+ jl_uv_fs_t_ptr_offset;
jl_sizeof_stat;
jl_stat;
jl_lstat;
View
16 src/sys.c
@@ -106,6 +106,22 @@ DLLEXPORT jl_value_t *jl_stdout_stream(void)
return (jl_value_t*)a;
}
+// --- dir/file stuff ---
+
+DLLEXPORT int jl_sizeof_uv_fs_t(void) { return sizeof(uv_fs_t); }
+DLLEXPORT void jl_uv_fs_req_cleanup(uv_fs_t* req) {
+ uv_fs_req_cleanup(req);
+}
+
+DLLEXPORT int jl_readdir(const char* path, uv_fs_t* readdir_req)
+{
+ // Note that the flags field is mostly ignored by libuv
+ return uv_fs_readdir(uv_default_loop(), readdir_req, path, 0 /*flags*/, NULL);
+}
+
+DLLEXPORT char* jl_uv_fs_t_ptr(uv_fs_t* req) {return req->ptr; }
+DLLEXPORT char* jl_uv_fs_t_ptr_offset(uv_fs_t* req, int offset) {return req->ptr + offset; }
+
// --- stat ---
DLLEXPORT int jl_sizeof_stat(void) { return sizeof(struct stat); }
Please sign in to comment.
Something went wrong with that request. Please try again.