From 4c8fb45a201cd15aa038d84bd792679a31bec5a8 Mon Sep 17 00:00:00 2001
From: Hannes Mehnert <hannes@mehnert.org>
Date: Thu, 27 Jan 2022 16:24:42 +0100
Subject: [PATCH] avoid deprecated Cstruct.len, use Cstruct.length instead

---
 lib/eraseBlock.ml      |  4 ++--
 lib/ring.ml            | 22 +++++++++++-----------
 lib_test/test.ml       |  2 +-
 shared-block-ring.opam |  2 +-
 4 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/lib/eraseBlock.ml b/lib/eraseBlock.ml
index 5264b20..d073c36 100644
--- a/lib/eraseBlock.ml
+++ b/lib/eraseBlock.ml
@@ -23,14 +23,14 @@ module Make(B: S.BLOCK) = struct
     >>= fun info ->
     let pages = Io_page.get block_size_pages in
     let buffer = Io_page.to_cstruct pages in
-    for i = 0 to Cstruct.len buffer - 1 do
+    for i = 0 to Cstruct.length buffer - 1 do
       Cstruct.set_char buffer i (pattern.[i mod (String.length pattern)])
     done;
     let rec loop n =
       if n = info.Mirage_block.size_sectors
       then return (Ok ())
       else
-        let buffer_in_sectors = Cstruct.len buffer / info.Mirage_block.sector_size in
+        let buffer_in_sectors = Cstruct.length buffer / info.Mirage_block.sector_size in
         let needed = Int64.to_int (min (Int64.sub info.Mirage_block.size_sectors n) (Int64.of_int buffer_in_sectors)) in
         let towrite = Cstruct.sub buffer 0 (needed * info.Mirage_block.sector_size) in
         B.write t n [ towrite ]
diff --git a/lib/ring.ml b/lib/ring.ml
index 58e98c4..23db9f1 100644
--- a/lib/ring.ml
+++ b/lib/ring.ml
@@ -34,7 +34,7 @@ let minimum_size_sectors = Int64.add sector_data 1L
 let magic = Printf.sprintf "mirage shared-block-device 1.0"
 
 let zero buf =
-  for i = 0 to Cstruct.len buf - 1 do
+  for i = 0 to Cstruct.length buf - 1 do
     Cstruct.set_uint8 buf i 0
   done
 
@@ -357,7 +357,7 @@ module Producer = struct
     let open ResultM in
     let _sector = alloc t.info.Mirage_block.sector_size in
     (* add a 4 byte header of size, and round up to the next 4-byte offset *)
-    let needed_bytes = Int64.(logand (lognot 3L) (add 7L (of_int (Cstruct.len item)))) in
+    let needed_bytes = Int64.(logand (lognot 3L) (add 7L (of_int (Cstruct.length item)))) in
     let first_sector = Int64.(div t.producer.producer (of_int t.info.Mirage_block.sector_size)) in
     let first_offset = Int64.(to_int (rem t.producer.producer (of_int t.info.Mirage_block.sector_size))) in
 
@@ -365,23 +365,23 @@ module Producer = struct
        the first page as it's only a 4-byte integer and we're padding to 4-byte offsets. *)
     read_modify_write t first_sector (fun sector ->
       (* Write the header and anything else we can *)
-      Cstruct.LE.set_uint32 sector first_offset (Int32.of_int (Cstruct.len item));
+      Cstruct.LE.set_uint32 sector first_offset (Int32.of_int (Cstruct.length item));
       if first_offset + 4 = t.info.Mirage_block.sector_size
       then item (* We can't write anything else, so just return the item *)
       else begin
-        let this = min (t.info.Mirage_block.sector_size - first_offset - 4) (Cstruct.len item) in
+        let this = min (t.info.Mirage_block.sector_size - first_offset - 4) (Cstruct.length item) in
         Cstruct.blit item 0 sector (first_offset + 4) this;
         Cstruct.shift item this
       end) >>= fun remaining ->
 
     let rec loop offset remaining =
-      if Cstruct.len remaining = 0
+      if Cstruct.length remaining = 0
       then return ()
       else begin
         read_modify_write t offset (fun sector ->
-          let this = min t.info.Mirage_block.sector_size (Cstruct.len remaining) in
+          let this = min t.info.Mirage_block.sector_size (Cstruct.length remaining) in
           let frag = Cstruct.sub sector 0 this in
-          Cstruct.blit remaining 0 frag 0 (Cstruct.len frag);
+          Cstruct.blit remaining 0 frag 0 (Cstruct.length frag);
           Cstruct.shift remaining this) >>= fun remaining ->
         loop (Int64.succ offset) remaining
       end in
@@ -408,7 +408,7 @@ module Producer = struct
       (fun () ->
         let item = Item.to_cstruct item in
         (* every item has a 4 byte header *)
-        let needed_bytes = Int64.(add 4L (of_int (Cstruct.len item))) in
+        let needed_bytes = Int64.(add 4L (of_int (Cstruct.length item))) in
         let open C in
         let open ResultM in
         ok_to_write t needed_bytes
@@ -535,10 +535,10 @@ module Consumer = struct
       let frag = Cstruct.sub sector (4 + first_offset) this in
       Cstruct.blit frag 0 result 0 this;
       let rec loop consumer remaining =
-        if Cstruct.len remaining = 0
+        if Cstruct.length remaining = 0
         then return ()
         else
-          let this = min t.info.Mirage_block.sector_size (Cstruct.len remaining) in
+          let this = min t.info.Mirage_block.sector_size (Cstruct.length remaining) in
           let frag = Cstruct.sub remaining 0 this in
           read Int64.(add sector_data (rem consumer total_sectors)) t.disk sector >>= fun () ->
           Cstruct.blit sector 0 frag 0 this;
@@ -547,7 +547,7 @@ module Consumer = struct
       (* Read the payload before updating the consumer pointer *)
       let needed_bytes = Int64.(logand (lognot 3L) (add 7L (of_int (len)))) in
       match Item.of_cstruct result with
-      | None -> Lwt.return (Error (`Msg (Printf.sprintf "Failed to parse queue item: (%d)[%s]" (Cstruct.len result) (String.escaped (Cstruct.to_string result)))))
+      | None -> Lwt.return (Error (`Msg (Printf.sprintf "Failed to parse queue item: (%d)[%s]" (Cstruct.length result) (String.escaped (Cstruct.to_string result)))))
       | Some result ->
         return (Int64.(add t.consumer.consumer needed_bytes),result)
     end
diff --git a/lib_test/test.ml b/lib_test/test.ml
index 3769137..d9ff865 100644
--- a/lib_test/test.ml
+++ b/lib_test/test.ml
@@ -47,7 +47,7 @@ let alloc sector_size =
   sector
 
 let fill_with_message buffer message =
-  for i = 0 to Cstruct.len buffer - 1 do
+  for i = 0 to Cstruct.length buffer - 1 do
     Cstruct.set_char buffer i (message.[i mod (String.length message)])
   done
 
diff --git a/shared-block-ring.opam b/shared-block-ring.opam
index d9a95d1..1c703fa 100644
--- a/shared-block-ring.opam
+++ b/shared-block-ring.opam
@@ -13,7 +13,7 @@ depends: [
   "ocaml" {>= "4.06.0"}
   "bisect_ppx" {dev & >= "2.5.0"}
   "cmdliner"
-  "cstruct" {>= "3.0.0"}
+  "cstruct" {>= "6.0.0"}
   "dune" {>= "2.7.0"}
   "duration"
   "io-page"
