Source file file.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
(* This file is part of Luv, released under the MIT license. See LICENSE.md for
   details, or visit https://github.com/aantron/luv/blob/master/LICENSE.md. *)



(* This is given the internal name Request_ to avoid shadowing request.mli in
   for code in this file. *)
module Request_ =
struct
  type t = [ `File ] Request.t

  let make () =
    Request.allocate C.Types.File.Request.t

  let or_make maybe_request =
    match maybe_request with
    | Some request -> request
    | None -> make ()

  let cleanup =
    C.Blocking.File.req_cleanup

  let int_result request =
    request
    |> C.Blocking.File.get_result
    |> PosixTypes.Ssize.to_int

  let result request =
    int_result request
    |> Error.clamp

  let file request =
    let file_or_error = int_result request in
    Error.to_result file_or_error file_or_error

  let byte_count request =
    let count_or_error = C.Blocking.File.get_result request in
    if PosixTypes.Ssize.(compare count_or_error zero) >= 0 then
      count_or_error
      |> PosixTypes.Ssize.to_int64
      |> Unsigned.Size_t.of_int64
      |> fun n -> Result.Ok n
    else
      count_or_error
      |> PosixTypes.Ssize.to_int
      |> fun n -> Error.result_from_c n

  let path =
    C.Blocking.File.get_path
end

module Open_flag =
struct
  type t = [
    | `RDONLY
    | `WRONLY
    | `RDWR

    | `CREAT
    | `EXCL
    | `EXLOCK
    | `NOCTTY
    | `NOFOLLOW
    | `TEMPORARY
    | `TRUNC

    | `APPEND
    | `DIRECT
    | `DSYNC
    | `FILEMAP
    | `NOATIME
    | `NONBLOCK
    | `RANDOM
    | `SEQUENTIAL
    | `SHORT_LIVED
    | `SYMLINK
    | `SYNC
  ]

  let to_c = let open C.Types.File.Open_flag in function
    | `RDONLY -> rdonly
    | `WRONLY -> wronly
    | `RDWR -> rdwr

    | `CREAT -> creat
    | `EXCL -> excl
    | `EXLOCK -> exlock
    | `NOCTTY -> noctty
    | `NOFOLLOW -> nofollow
    | `TEMPORARY -> temporary
    | `TRUNC -> trunc

    | `APPEND -> append
    | `DIRECT -> direct
    | `DSYNC -> dsync
    | `FILEMAP -> filemap
    | `NOATIME -> noatime
    | `NONBLOCK -> nonblock
    | `RANDOM -> random
    | `SEQUENTIAL -> sequential
    | `SHORT_LIVED -> short_lived
    | `SYMLINK -> symlink
    | `SYNC -> sync
end

module Mode =
struct
  type t = [
    | `IRWXU
    | `IRUSR
    | `IWUSR
    | `IXUSR

    | `IRWXG
    | `IRGRP
    | `IWGRP
    | `IXGRP

    | `IRWXO
    | `IROTH
    | `IWOTH
    | `IXOTH

    | `ISUID
    | `ISGID
    | `ISVTX

    | `IFMT
    | `IFDIR
    | `IFBLK
    | `IFCHR
    | `IFLNK
    | `IFIFO

    | `NUMERIC of int
  ]

  let to_c = let open C.Types.File.Mode in function
    | `IRWXU -> irwxu
    | `IRUSR -> irusr
    | `IWUSR -> iwusr
    | `IXUSR -> ixusr

    | `IRWXG -> irwxg
    | `IRGRP -> irgrp
    | `IWGRP -> iwgrp
    | `IXGRP -> ixgrp

    | `IRWXO -> irwxo
    | `IROTH -> iroth
    | `IWOTH -> iwoth
    | `IXOTH -> ixoth

    | `ISUID -> isuid
    | `ISGID -> isgid
    | `ISVTX -> isvtx

    | `IFMT -> ifmt
    | `IFDIR -> ifdir
    | `IFBLK -> ifblk
    | `IFCHR -> ifchr
    | `IFLNK -> iflnk
    | `IFIFO -> ififo

    | `NUMERIC i -> i

  let file_default = [`NUMERIC 0o644]
  let directory_default = [`NUMERIC 0o755]

  type numeric = int

  let list_to_c =
    Helpers.Bit_field.list_to_c to_c

  let test =
    Helpers.Bit_field.test to_c
end

module Dirent =
struct
  module Kind = C.Types.File.Dirent.Kind

  type t = {
    kind : Kind.t;
    name : string;
  }

  let from_c c_dirent =
    {
      kind = Ctypes.getf c_dirent C.Types.File.Dirent.type_;
      name = Ctypes.getf c_dirent C.Types.File.Dirent.name
    }
end

module Dir =
struct
  type t = C.Types.File.Dir.t Ctypes.ptr

  let from_request request =
    C.Blocking.File.get_ptr request
    |> Ctypes.from_voidp C.Types.File.Dir.t
end

module Directory_scan =
struct
  type t = {
    request : Request_.t;
    dirent : C.Types.File.Dirent.t;
  }

  let stop scan =
    Request_.cleanup scan.request

  let next scan =
    let result =
      C.Blocking.File.scandir_next scan.request (Ctypes.addr scan.dirent) in
    if result < 0 then begin
      stop scan;
      None
    end
    else
      Some (Dirent.from_c scan.dirent)

  let start request =
    let dirent = Ctypes.make C.Types.File.Dirent.t in
    {request; dirent}
end

let scandir_next =
  Directory_scan.next

let scandir_end =
  Directory_scan.stop

module Stat =
struct
  type timespec = {
    sec : Signed.Long.t;
    nsec : Signed.Long.t;
  }

  type t = {
    dev : Unsigned.UInt64.t;
    mode : Mode.numeric;
    nlink : Unsigned.UInt64.t;
    uid : Unsigned.UInt64.t;
    gid : Unsigned.UInt64.t;
    rdev : Unsigned.UInt64.t;
    ino : Unsigned.UInt64.t;
    size : Unsigned.UInt64.t;
    blksize : Unsigned.UInt64.t;
    blocks : Unsigned.UInt64.t;
    flags : Unsigned.UInt64.t;
    gen : Unsigned.UInt64.t;
    atim : timespec;
    mtim : timespec;
    ctim : timespec;
    birthtim : timespec;
  }

  let load_timespec c_timespec =
    {
      sec = Ctypes.getf c_timespec C.Types.File.Timespec.tv_sec;
      nsec = Ctypes.getf c_timespec C.Types.File.Timespec.tv_nsec;
    }

  let load stat =
    let field f = Ctypes.getf stat f in
    let module C_stat = C.Types.File.Stat in
    {
      dev = field C_stat.st_dev;
      mode = field C_stat.st_mode |> Unsigned.UInt64.to_int;
      nlink = field C_stat.st_nlink;
      uid = field C_stat.st_uid;
      gid = field C_stat.st_gid;
      rdev = field C_stat.st_rdev;
      ino = field C_stat.st_ino;
      size = field C_stat.st_size;
      blksize = field C_stat.st_blksize;
      blocks = field C_stat.st_blocks;
      flags = field C_stat.st_flags;
      gen = field C_stat.st_gen;
      atim = load_timespec (field C_stat.st_atim);
      mtim = load_timespec (field C_stat.st_mtim);
      ctim = load_timespec (field C_stat.st_ctim);
      birthtim = load_timespec (field C_stat.st_birthtim);
    }

  let from_request request =
    load (Ctypes.(!@) (C.Blocking.File.get_statbuf request))
end

module Statfs =
struct
  type t = {
    type_ : Unsigned.UInt64.t;
    bsize : Unsigned.UInt64.t;
    blocks : Unsigned.UInt64.t;
    bfree : Unsigned.UInt64.t;
    bavail : Unsigned.UInt64.t;
    files : Unsigned.UInt64.t;
    ffree : Unsigned.UInt64.t;
    f_spare :
      Unsigned.UInt64.t
      * Unsigned.UInt64.t
      * Unsigned.UInt64.t
      * Unsigned.UInt64.t;
  }

  let from_request request =
    let module C_statfs = C.Types.File.Statfs in
    let c_statfs =
      Ctypes.(!@ (from_voidp C_statfs.t (C.Blocking.File.get_ptr request))) in
    let field f = Ctypes.getf c_statfs f in
    {
      type_ = field C_statfs.f_type;
      bsize = field C_statfs.f_bsize;
      blocks = field C_statfs.f_blocks;
      bfree = field C_statfs.f_bfree;
      bavail = field C_statfs.f_bavail;
      files = field C_statfs.f_files;
      ffree = field C_statfs.f_ffree;
      f_spare =
        let array = field C_statfs.f_spare in
        Ctypes.CArray.get array 0,
        Ctypes.CArray.get array 1,
        Ctypes.CArray.get array 2,
        Ctypes.CArray.get array 3
    }
end

module Access_flag =
struct
  type t = [
    | `F_OK
    | `R_OK
    | `W_OK
    | `X_OK
  ]

  let to_c = let open C.Types.File.Access_flag in function
    | `F_OK -> f
    | `R_OK -> r
    | `W_OK -> w
    | `X_OK -> x
end

module Returns =
struct
  type 'a t = {
    from_request : Request_.t -> 'a;
    immediate_error : int -> 'a;
    clean_up_request_on_success : bool;
  }

  let returns_error = {
    from_request = (fun request ->
      Request_.result request |> Error.to_result ());
    immediate_error = Error.to_result ();
    clean_up_request_on_success = true;
  }

  let returns_file = {
    from_request = Request_.file;
    immediate_error = Error.result_from_c;
    clean_up_request_on_success = true;
  }

  let returns_byte_count = {
    from_request = Request_.byte_count;
    immediate_error = Error.result_from_c;
    clean_up_request_on_success = true;
  }

  let returns_path = {
    from_request = (fun request ->
      Error.to_result_lazy
        (fun () -> Request_.path request) (Request_.result request));
    immediate_error = Error.result_from_c;
    clean_up_request_on_success = true;
  }

  let returns_path_and_file = {
    from_request = (fun request ->
      Error.to_result_lazy
        (fun () -> Request_.path request, (Request_.int_result request))
        (Request_.result request));
    immediate_error = Error.result_from_c;
    clean_up_request_on_success = true;
  }

  let returns_directory_handle = {
    from_request = (fun request ->
      Error.to_result_lazy
        (fun () -> Dir.from_request request)
        (Request_.result request));
    immediate_error = Error.result_from_c;
    clean_up_request_on_success = true;
  }

  let returns_directory_entries = {
    from_request = (fun request ->
      Error.to_result_lazy
        (fun () ->
          let dirents =
            Dir.from_request request
            |> Ctypes.(!@)
            |> fun dir -> Ctypes.getf dir C.Types.File.Dir.dirents
          in
          Array.init
            (Request_.int_result request)
            (fun index ->
              Dirent.from_c Ctypes.(!@ (dirents +@ index))))
        (Request_.result request));
      immediate_error = Error.result_from_c;
      clean_up_request_on_success = true;
  }

  let returns_directory_scan = {
    from_request = (fun request ->
      Error.to_result_lazy
        (fun () -> Directory_scan.start request) (Request_.result request));
    immediate_error = Error.result_from_c;
    clean_up_request_on_success = false;
  }

  let returns_stat = {
    from_request = (fun request ->
      Error.to_result_lazy
        (fun () -> Stat.from_request request) (Request_.result request));
    immediate_error = Error.result_from_c;
    clean_up_request_on_success = true;
  }

  let returns_statfs = {
    from_request = (fun request ->
      Error.to_result_lazy
        (fun () -> Statfs.from_request request) (Request_.result request));
    immediate_error = Error.result_from_c;
    clean_up_request_on_success = true;
  }

  let returns_string = {
    from_request = (fun request ->
      Error.to_result_lazy
        (fun () -> C.Blocking.File.get_ptr_as_string request)
        (Request_.result request));
    immediate_error = Error.result_from_c;
    clean_up_request_on_success = true;
  }
end

module Args =
struct
  let uint i = fun f -> f (Unsigned.UInt.of_int i)

  let (!) v = fun f -> f v
  let (@@) a b = fun f -> b (a f)
end

type t = C.Types.File.t

let stdin = 0
let stdout = 1
let stderr = 2

module type ASYNC_OR_SYNC =
sig
  type 'value cps_or_normal_return
  type 'fn maybe_with_loop_and_request_arguments

  val async_or_sync :
    (Loop.t -> Request_.t -> 'c_signature) ->
    'result Returns.t ->
    ((('c_signature -> C.Blocking.File.trampoline -> int) ->
      (unit -> unit) ->
        'result cps_or_normal_return) ->
       'ocaml_signature) ->
      'ocaml_signature maybe_with_loop_and_request_arguments
end

module Make_functions (Async_or_sync : ASYNC_OR_SYNC) =
struct
  open Returns
  open Args

  let async_or_sync = Async_or_sync.async_or_sync
  let no_cleanup = ignore

  let open_ =
    async_or_sync
      C.Blocking.File.open_
      returns_file
      (fun run ?(mode = Mode.file_default) path flags ->
        let mode = Mode.list_to_c mode in
        let flags = Helpers.Bit_field.list_to_c Open_flag.to_c flags in
        run (!path @@ !flags @@ !mode) no_cleanup)

  let close =
    async_or_sync
      C.Blocking.File.close
      returns_error
      (fun run file -> run !file no_cleanup)

  let read_or_write c_function =
    async_or_sync
      c_function
      returns_byte_count
      (fun run ?(file_offset = -1L) file buffers ->
        let count = List.length buffers in
        let iovecs = Helpers.Buf.bigstrings_to_iovecs buffers count in
        run
          (!file @@ !(Ctypes.CArray.start iovecs) @@ uint count @@ !file_offset)
          (fun () ->
            let module Sys = Compatibility.Sys in
            ignore (Sys.opaque_identity buffers);
            ignore (Sys.opaque_identity iovecs)))

  let read = read_or_write C.Blocking.File.read
  let write = read_or_write C.Blocking.File.write

  let unlink =
    async_or_sync
      C.Blocking.File.unlink
      returns_error
      (fun run path -> run !path no_cleanup)

  let mkdir =
    async_or_sync
      C.Blocking.File.mkdir
      returns_error
      (fun run ?(mode = Mode.directory_default) path ->
        let mode = Mode.list_to_c mode in
        run (!path @@ !mode) no_cleanup)

  let mkdtemp =
    async_or_sync
      C.Blocking.File.mkdtemp
      returns_path
      (fun run path -> run !path no_cleanup)

  let mkstemp =
    async_or_sync
      C.Blocking.File.mkstemp
      returns_path_and_file
      (fun run path -> run !path no_cleanup)

  let rmdir =
    async_or_sync
      C.Blocking.File.rmdir
      returns_error
      (fun run path -> run !path no_cleanup)

  let opendir =
    async_or_sync
      C.Blocking.File.opendir
      returns_directory_handle
      (fun run path -> run !path no_cleanup)

  let closedir =
    async_or_sync
      C.Blocking.File.closedir
      returns_error
      (fun run dir -> run !dir no_cleanup)

  let readdir =
    async_or_sync
      C.Blocking.File.readdir
      returns_directory_entries
      (fun run ?(number_of_entries = 1024) dir ->
        let dirents =
          Ctypes.allocate_n C.Types.File.Dirent.t ~count:number_of_entries in
        let dir' = Ctypes.(!@) dir in
        Ctypes.setf dir' C.Types.File.Dir.dirents dirents;
        Ctypes.setf
          dir'
          C.Types.File.Dir.nentries
          (Unsigned.Size_t.of_int number_of_entries);
        run !dir (fun () ->
          ignore (Compatibility.Sys.opaque_identity dirents)))

  let scandir =
    async_or_sync
      C.Blocking.File.scandir
      returns_directory_scan
      (fun run path -> run (!path @@ !0) no_cleanup)

  let generic_stat c_function =
    async_or_sync
      c_function
      returns_stat
      (fun run argument -> run !argument no_cleanup)

  let stat = generic_stat C.Blocking.File.stat
  let lstat = generic_stat C.Blocking.File.lstat
  let fstat = generic_stat C.Blocking.File.fstat

  let statfs =
    async_or_sync
      C.Blocking.File.statfs
      returns_statfs
      (fun run path -> run !path no_cleanup)

  let rename =
    async_or_sync
      C.Blocking.File.rename
      returns_error
      (fun run from ~to_ -> run (!from @@ !to_) no_cleanup)

  let generic_fsync c_function =
    async_or_sync
      c_function
      returns_error
      (fun run file -> run !file no_cleanup)

  let fsync = generic_fsync C.Blocking.File.fsync
  let fdatasync = generic_fsync C.Blocking.File.fdatasync

  let ftruncate =
    async_or_sync
      C.Blocking.File.ftruncate
      returns_error
      (fun run file length -> run (!file @@ !length) no_cleanup)

  let copyfile =
    async_or_sync
      C.Blocking.File.copyfile
      returns_error
      (fun run
          ?(excl = false)
          ?(ficlone = false)
          ?(ficlone_force = false)
          from
          ~to_ ->
        let flags =
          let accumulate = Helpers.Bit_field.accumulate in
          0
          |> accumulate C.Types.File.Copy_flag.excl excl
          |> accumulate C.Types.File.Copy_flag.ficlone ficlone
          |> accumulate C.Types.File.Copy_flag.ficlone_force ficlone_force
        in
        run (!from @@ !to_ @@ !flags) no_cleanup)

  let sendfile =
    async_or_sync
      C.Blocking.File.sendfile
      returns_byte_count
      (fun run from ~to_ ~offset length ->
        run (!to_ @@ !from @@ !offset @@ !length) no_cleanup)

  let access =
    async_or_sync
      C.Blocking.File.access
      returns_error
      (fun run path mode ->
        let mode = Helpers.Bit_field.list_to_c Access_flag.to_c mode in
        run (!path @@ !mode) no_cleanup)

  let generic_chmod c_function =
    async_or_sync
      c_function
      returns_error
      (fun run argument mode ->
        let mode = Mode.list_to_c mode in
        run (!argument @@ !mode) no_cleanup)

  let chmod = generic_chmod C.Blocking.File.chmod
  let fchmod = generic_chmod C.Blocking.File.fchmod

  let generic_utime c_function =
    async_or_sync
      c_function
      returns_error
      (fun run argument ~atime ~mtime ->
        run (!argument @@ !atime @@ !mtime) no_cleanup)

  let utime = generic_utime C.Blocking.File.utime
  let futime = generic_utime C.Blocking.File.futime
  let lutime = generic_utime C.Blocking.File.lutime

  let link =
    async_or_sync
      C.Blocking.File.link
      returns_error
      (fun run target ~link -> run (!target @@ !link) no_cleanup)

  let symlink =
    async_or_sync
      C.Blocking.File.symlink
      returns_error
      (fun run ?(dir = false) ?(junction = false) target ~link ->
        let flags =
          let accumulate = Helpers.Bit_field.accumulate in
          0
          |> accumulate C.Types.File.Symlink_flag.dir dir
          |> accumulate C.Types.File.Symlink_flag.junction junction
        in
        run (!target @@ !link @@ !flags) no_cleanup)

  let generic_readpath c_function =
    async_or_sync
      c_function
      returns_string
      (fun run path -> run !path no_cleanup)

  let readlink = generic_readpath C.Blocking.File.readlink
  let realpath = generic_readpath C.Blocking.File.realpath

  let generic_chown c_function =
    async_or_sync
      c_function
      returns_error
      (fun run argument ~uid ~gid -> run (!argument @@ !uid @@ !gid) no_cleanup)

  let chown = generic_chown C.Blocking.File.chown
  let fchown = generic_chown C.Blocking.File.fchown
  let lchown = generic_chown C.Blocking.File.lchown
end

module Async =
struct
  let trampoline =
    C.Blocking.File.get_trampoline ()

  let async c_function returns get_args =
    fun ?loop ?request ->
      get_args begin fun args cleanup callback ->
        let loop = Loop.or_default loop in
        let request = Request_.or_make request in

        let callback = Error.catch_exceptions callback in
        Request.set_callback request begin fun () ->
          let result = Returns.(returns.from_request request) in
          if Returns.(returns.clean_up_request_on_success)
            || Request_.result request < 0 then begin
            Request_.cleanup request
          end;
          cleanup ();
          callback result
        end;

        let immediate_result =
          ((c_function loop request) |> args) trampoline in

        if immediate_result < 0 then begin
          Request.release request;
          Request_.cleanup request;
          cleanup ();
          callback (Returns.(returns.immediate_error) immediate_result)
        end
      end

  include Make_functions
    (struct
      type 'value cps_or_normal_return = ('value -> unit) -> unit
      type 'fn maybe_with_loop_and_request_arguments =
        ?loop:Loop.t -> ?request:Request_.t -> 'fn
      let async_or_sync = async
    end)
end

include Async

module Sync =
struct
  let null_callback =
    C.Blocking.File.get_null_callback ()

  let sync c_function returns get_args =
    get_args begin fun args cleanup ->
      let request = Request_.make () in
      Request.release request;
      let immediate_result =
        ((c_function (Loop.default ()) request) |> args)
          null_callback
      in

      cleanup ();
      let result =
        if immediate_result < 0 then
          Returns.(returns.immediate_error) immediate_result
        else
          Returns.(returns.from_request) request
      in
      if Returns.(returns.clean_up_request_on_success)
        || immediate_result < 0 then begin
        Request_.cleanup request;
      end;
      result
    end

  include Make_functions
    (struct
      type 'value cps_or_normal_return = 'value
      type 'fn maybe_with_loop_and_request_arguments = 'fn
      let async_or_sync = sync
    end)
end

module Request = Request_

let get_osfhandle file =
  let handle = C.Functions.Os_fd.get_osfhandle file in
  Result.Ok handle

let open_osfhandle handle =
  let file = C.Functions.Os_fd.open_osfhandle handle in
  if file = -1 then
    Result.Error `EBADF
  else
    Result.Ok file

let to_int file =
  file