Aurel Bílý 6 years ago
parent
commit
d68cb64aa0
3 changed files with 63 additions and 18 deletions
  1. 33 0
      libs/libuv/libuv.ml
  2. 27 17
      libs/libuv/libuv_stubs.c
  3. 3 1
      libs/libuv/test.ml

+ 33 - 0
libs/libuv/libuv.ml

@@ -44,6 +44,32 @@ type uv_file
 (* type uv_stat_t *)
 type uv_buf_t
 
+(* Non-abstract type definitions  *)
+
+type uv_stat_t = {
+  dev: int;
+  kind: int;
+  perm: int;
+  nlink: int;
+  uid: int;
+  gid: int;
+  rdev: int;
+  ino: int;
+  size: int64;
+  blksize: int;
+  blocks: int;
+  flags: int;
+  gen: int;
+  atime: int64;
+  atime_nsec: int;
+  mtime: int64;
+  mtime_nsec: int;
+  ctime: int64;
+  ctime_nsec: int;
+  birthtime: int64;
+  birthtime_nsec: int;
+}
+
 (* ------------- LOOP ----------------------------------------------- *)
 
 external loop_init : unit -> uv_loop_t = "w_loop_init"
@@ -58,6 +84,7 @@ type fs_cb_bytes = string -> unit
 type fs_cb_path = string -> unit
 type fs_cb_file = uv_file -> unit
 type fs_cb_int = int -> unit
+type fs_cb_stat= uv_stat_t -> unit
 type fs_cb_scandir = (string * int) list -> unit
 
 external fs_close : uv_loop_t -> uv_file -> fs_cb -> unit = "w_fs_close"
@@ -67,6 +94,9 @@ external fs_mkdir : uv_loop_t -> string -> int -> fs_cb -> unit = "w_fs_mkdir"
 external fs_mkdtemp : uv_loop_t -> string -> fs_cb_path -> unit = "w_fs_mkdtemp"
 external fs_rmdir : uv_loop_t -> string -> fs_cb -> unit = "w_fs_rmdir"
 external fs_scandir : uv_loop_t -> string -> int -> fs_cb_scandir -> unit = "w_fs_scandir"
+external fs_stat : uv_loop_t -> string -> fs_cb_stat -> unit = "w_fs_stat"
+external fs_fstat : uv_loop_t -> uv_file -> fs_cb_stat -> unit = "w_fs_fstat"
+external fs_lstat : uv_loop_t -> string -> fs_cb_stat -> unit = "w_fs_lstat"
 external fs_rename : uv_loop_t -> string -> string -> fs_cb -> unit = "w_fs_rename"
 external fs_fsync : uv_loop_t -> uv_file -> fs_cb -> unit = "w_fs_fsync"
 external fs_fdatasync : uv_loop_t -> uv_file -> fs_cb -> unit = "w_fs_fdatasync"
@@ -90,6 +120,9 @@ external fs_mkdir_sync : uv_loop_t -> string -> int -> unit = "w_fs_mkdir_sync"
 external fs_mkdtemp_sync : uv_loop_t -> string -> string = "w_fs_mkdtemp_sync"
 external fs_rmdir_sync : uv_loop_t -> string -> unit = "w_fs_rmdir_sync"
 external fs_scandir_sync : uv_loop_t -> string -> int -> (string * int) list = "w_fs_scandir_sync"
+external fs_stat_sync : uv_loop_t -> string -> uv_stat_t = "w_fs_stat_sync"
+external fs_fstat_sync : uv_loop_t -> uv_file -> uv_stat_t = "w_fs_fstat_sync"
+external fs_lstat_sync : uv_loop_t -> string -> uv_stat_t = "w_fs_lstat_sync"
 external fs_rename_sync : uv_loop_t -> string -> string -> unit = "w_fs_rename_sync"
 external fs_fsync_sync : uv_loop_t -> uv_file -> unit = "w_fs_fsync_sync"
 external fs_fdatasync_sync : uv_loop_t -> uv_file -> unit = "w_fs_fdatasync_sync"

+ 27 - 17
libs/libuv/libuv_stubs.c

@@ -132,20 +132,30 @@ UV_FS_HANDLER(handle_fs_cb_bytes, value2 = caml_copy_string((const char *)req->p
 UV_FS_HANDLER(handle_fs_cb_path, value2 = caml_copy_string((const char *)req->path););
 UV_FS_HANDLER(handle_fs_cb_int, value2 = (value)req->result;);
 UV_FS_HANDLER(handle_fs_cb_file, value2 = (value)req->result;);
-/*UV_FS_HANDLER(handle_fs_cb_stat, value2 = construct_fs_stat(
-		req->statbuf.st_dev,
-		req->statbuf.st_mode,
-		req->statbuf.st_nlink,
-		req->statbuf.st_uid,
-		req->statbuf.st_gid,
-		req->statbuf.st_rdev,
-		req->statbuf.st_ino,
-		req->statbuf.st_size,
-		req->statbuf.st_blksize,
-		req->statbuf.st_blocks,
-		req->statbuf.st_flags,
-		req->statbuf.st_gen
-	));*/
+UV_FS_HANDLER(handle_fs_cb_stat, {
+		value2 = caml_alloc(21, 0);
+		Field(value2, 0) = Val_long(req->statbuf.st_dev);
+		Field(value2, 1) = Val_long(req->statbuf.st_mode & S_IFMT);
+		Field(value2, 2) = Val_long(req->statbuf.st_mode & 07777);
+		Field(value2, 3) = Val_long(req->statbuf.st_nlink);
+		Field(value2, 4) = Val_long(req->statbuf.st_uid);
+		Field(value2, 5) = Val_long(req->statbuf.st_gid);
+		Field(value2, 6) = Val_long(req->statbuf.st_rdev);
+		Field(value2, 7) = Val_long(req->statbuf.st_ino);
+		Field(value2, 8) = caml_copy_int64(req->statbuf.st_size);
+		Field(value2, 9) = Val_long(req->statbuf.st_blksize);
+		Field(value2, 10) = Val_long(req->statbuf.st_blocks);
+		Field(value2, 11) = Val_long(req->statbuf.st_flags);
+		Field(value2, 12) = Val_long(req->statbuf.st_gen);
+		Field(value2, 13) = caml_copy_int64(req->statbuf.st_atim.tv_sec);
+		Field(value2, 14) = Val_long(req->statbuf.st_atim.tv_nsec);
+		Field(value2, 15) = caml_copy_int64(req->statbuf.st_mtim.tv_sec);
+		Field(value2, 16) = Val_long(req->statbuf.st_mtim.tv_nsec);
+		Field(value2, 17) = caml_copy_int64(req->statbuf.st_ctim.tv_sec);
+		Field(value2, 18) = Val_long(req->statbuf.st_ctim.tv_nsec);
+		Field(value2, 19) = caml_copy_int64(req->statbuf.st_birthtim.tv_sec);
+		Field(value2, 20) = Val_long(req->statbuf.st_birthtim.tv_nsec);
+	});
 UV_FS_HANDLER(handle_fs_cb_scandir, {
 		uv_dirent_t ent;
 		value2 = Val_int(0);
@@ -304,9 +314,9 @@ FS_WRAP2(fs_mkdir, String_val, (int), handle_fs_cb);
 FS_WRAP1(fs_mkdtemp, String_val, handle_fs_cb_path);
 FS_WRAP1(fs_rmdir, String_val, handle_fs_cb);
 FS_WRAP2(fs_scandir, String_val, (int), handle_fs_cb_scandir);
-//FS_WRAP1(fs_stat, vdynamic *, const char*, _STAT, _BYTES, _CB_STAT, handle_fs_cb_stat, return);
-//FS_WRAP1(fs_fstat, vdynamic *, uv_file, _STAT, _FILE, _CB_STAT, handle_fs_cb_stat, return);
-//FS_WRAP1(fs_lstat, vdynamic *, const char*, _STAT, _BYTES, _CB_STAT, handle_fs_cb_stat, return);
+FS_WRAP1(fs_stat, String_val, handle_fs_cb_stat);
+FS_WRAP1(fs_fstat, (uv_file), handle_fs_cb_stat);
+FS_WRAP1(fs_lstat, String_val, handle_fs_cb_stat);
 FS_WRAP2(fs_rename, String_val, String_val, handle_fs_cb);
 FS_WRAP1(fs_fsync, (uv_file), handle_fs_cb);
 FS_WRAP1(fs_fdatasync, (uv_file), handle_fs_cb);

+ 3 - 1
libs/libuv/test.ml

@@ -7,6 +7,8 @@ let loop = Libuv.loop_init () in
 let cb file = print_string "hey I got a file I guess\n"; flush_all (); Libuv.fs_close loop file cb_c in*)
 let cb file =
 	print_string "hey I got a file I guess\n"; flush_all ();
+	let stat = Libuv.fs_fstat_sync loop file in
+	print_string ("length: " ^ (Int64.to_string stat.size) ^ "\n"); flush_all ();
 	Libuv.fs_close_sync loop file;
 	print_string "closed\n"; flush_all ();
 in
@@ -14,7 +16,7 @@ print_string "open files...\n"; flush_all ();
 Libuv.fs_open loop "libuv.ml" 0 511 cb;
 Libuv.fs_open loop "Makefile" 0 511 cb;
 print_string "sync open...\n"; flush_all ();
-let other_file = Libuv.fs_open_sync loop "dummy2.txt" 0 511 in
+let other_file = Libuv.fs_open_sync loop "Makefile" 0 511 in
 print_string "run gc...\n"; flush_all ();
 Gc.full_major ();
 Libuv.fs_close_sync loop other_file;