Skip to content

Commit

Permalink
Merge pull request #144 from djs55/cstruct.length
Browse files Browse the repository at this point in the history
Use Cstruct.length (6.0.0+) and Fmt.str rather than Fmt.strf
  • Loading branch information
djs55 authored Feb 6, 2022
2 parents b72ccf0 + 33937d4 commit 778765e
Show file tree
Hide file tree
Showing 17 changed files with 68 additions and 61 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## v2.0.2 (2021-01-16)

* Update to Cstruct.length in cstruct.6.0.0 and use Fmt.str
rather than Fmt.strf (@djs55)

## v2.0.1 (2019-03-26)

* Use cstruct-sexp introduced in cstruct.4.0.0 (@avsm)
Expand Down
12 changes: 7 additions & 5 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ platform:
- x86

environment:
FORK_USER: ocaml
FORK_BRANCH: master
PINS: "protocol-9p:. protocol-9p-tool:. protocol-9p-unix:."
PACKAGE: protocol-9p-unix
CYG_ROOT: C:\cygwin64
global:
FORK_USER: ocaml
FORK_BRANCH: master
CYG_ROOT: C:\cygwin64
PINS: "protocol-9p:. protocol-9p-tool:. protocol-9p-unix:."
PACKAGE: protocol-9p-unix
OPAM_SWITCH: 4.08.0+mingw64c

install:
- ps: iex ((new-object net.webclient).DownloadString("https://raw.githubusercontent.com/$env:FORK_USER/ocaml-ci-scripts/$env:FORK_BRANCH/appveyor-install.ps1"))
Expand Down
2 changes: 1 addition & 1 deletion lib/dune
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
(name protocol_9p)
(public_name protocol-9p)
(libraries rresult cstruct cstruct-sexp sexplib lwt mirage-flow mirage-channel
astring fmt)
astring fmt logs)
(preprocess
(pps ppx_sexp_conv)))
2 changes: 1 addition & 1 deletion lib/protocol_9p_buffered9PReader.ml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module Make(Log: Protocol_9p_s.LOG)(FLOW: Mirage_flow.S) = struct
C.read_exactly ~len c >>= function
| Ok (`Data bufs) -> Lwt.return (Ok (Cstruct.concat bufs))
| Ok `Eof -> Lwt.return (Error `Eof)
| Error e -> Lwt.return (Error (`Msg (Fmt.strf "%a" C.pp_error e)))
| Error e -> Lwt.return (Error (`Msg (Fmt.str "%a" C.pp_error e)))

let read_must_have_lock t =
let len_size = 4 in
Expand Down
10 changes: 5 additions & 5 deletions lib/protocol_9p_client.ml
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ module Make(Log: Protocol_9p_s.LOG)(FLOW: Mirage_flow.S) = struct
openfid t newfid Types.OpenMode.write_only
>>*= fun _ ->
let rec loop offset remaining =
let len = Cstruct.len remaining in
let len = Cstruct.length remaining in
if len = 0
then Lwt.return (Ok ())
else begin
Expand All @@ -416,7 +416,7 @@ module Make(Log: Protocol_9p_s.LOG)(FLOW: Mirage_flow.S) = struct
let to_request = min remaining t.maximum_payload in
read t newfid offset to_request
>>*= fun { Response.Read.data } ->
let n = Cstruct.len data in
let n = Cstruct.length data in
if n = 0
then Lwt.return (Ok (List.rev acc))
else
Expand Down Expand Up @@ -495,15 +495,15 @@ module Make(Log: Protocol_9p_s.LOG)(FLOW: Mirage_flow.S) = struct
let rec loop acc offset =
read t newfid offset t.maximum_payload
>>*= fun { Response.Read.data } ->
if Cstruct.len data = 0
if Cstruct.length data = 0
then Lwt.return (Ok acc)
else
(* Data should be an integral number of marshalled Stat.ts *)
let module StatArray = Types.Arr(Types.Stat) in
(Lwt.return (StatArray.read data))
>>*= fun (stats, rest) ->
assert (Cstruct.len rest = 0);
loop (acc @ stats) Int64.(add offset (of_int (Cstruct.len data))) in
assert (Cstruct.length rest = 0);
loop (acc @ stats) Int64.(add offset (of_int (Cstruct.length data))) in
loop [] 0L
)

Expand Down
2 changes: 1 addition & 1 deletion lib/protocol_9p_error.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type 'a t = ('a, error) result

let return x = Ok x

let error_msg fmt = Fmt.kstrf (fun s -> Error (`Msg s)) fmt
let error_msg fmt = Fmt.kstr (fun s -> Error (`Msg s)) fmt

let ( >>= ) m f = match m with
| Error x -> Error x
Expand Down
12 changes: 6 additions & 6 deletions lib/protocol_9p_request.ml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ module Auth = struct
>>= fun (aname, rest) ->
let uname = Data.to_string uname in
let aname = Data.to_string aname in
if Cstruct.len rest = 0
if Cstruct.length rest = 0
then return ({ afid; uname; aname; n_uname = None }, rest)
else
Int32.read rest
Expand Down Expand Up @@ -143,7 +143,7 @@ module Attach = struct
>>= fun (aname, rest) ->
let uname = Data.to_string uname in
let aname = Data.to_string aname in
if Cstruct.len rest = 0
if Cstruct.length rest = 0
then return ({ fid; afid; uname; aname; n_uname = None }, rest)
else
Int32.read rest
Expand Down Expand Up @@ -259,7 +259,7 @@ module Create = struct
OpenMode.read rest
>>= fun (mode, rest) ->
let name = Data.to_string name in
if Cstruct.len rest = 0
if Cstruct.length rest = 0
then return ({ fid; name; perm; mode; extension = None}, rest)
else
Data.read rest
Expand Down Expand Up @@ -306,14 +306,14 @@ module Write = struct

let sizeof_header = 4 + 8 + 4

let sizeof t = (Fid.sizeof t.fid) + 8 + 4 + (Cstruct.len t.data)
let sizeof t = (Fid.sizeof t.fid) + 8 + 4 + (Cstruct.length t.data)

let write t rest =
Fid.write t.fid rest
>>= fun rest ->
Int64.write t.offset rest
>>= fun rest ->
let len = Cstruct.len t.data in
let len = Cstruct.length t.data in
Int32.write (Int32.of_int len) rest
>>= fun rest ->
big_enough_for "Write.data" rest len
Expand Down Expand Up @@ -501,7 +501,7 @@ let pp ppf = function
| { tag; payload = Write { Write.fid; offset; data } } ->
let tag = Types.Tag.to_int tag in
let fid = Types.Fid.to_int32 fid in
let len = Cstruct.len data in
let len = Cstruct.length data in
Format.fprintf ppf "tag %d Write(fid: %ld, offset: %Ld, len(data): %d)"
tag fid offset len
| t -> Sexplib.Sexp.pp_hum ppf (sexp_of_t t)
8 changes: 4 additions & 4 deletions lib/protocol_9p_response.ml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ module Err = struct
Data.read buf
>>= fun (ename, rest) ->
let ename = Data.to_string ename in
if Cstruct.len rest = 0
if Cstruct.length rest = 0
then return ({ ename; errno = None }, rest)
else
Int32.read rest
Expand Down Expand Up @@ -183,10 +183,10 @@ module Read = struct

let sizeof_header = 4

let sizeof t = sizeof_header + (Cstruct.len t.data)
let sizeof t = sizeof_header + (Cstruct.length t.data)

let write t rest =
let len = Cstruct.len t.data in
let len = Cstruct.length t.data in
Int32.write (Int32.of_int len) rest
>>= fun rest ->
big_enough_for "Read.data" rest len
Expand Down Expand Up @@ -368,7 +368,7 @@ let read rest =
let pp ppf = function
| { tag; payload = Read { Read.data } } ->
let tag = Types.Tag.to_int tag in
let len = Cstruct.len data in
let len = Cstruct.length data in
Format.fprintf ppf "tag %d Read(len(data): %d)" tag len
| t -> Sexplib.Sexp.pp_hum ppf (sexp_of_t t)

Expand Down
20 changes: 10 additions & 10 deletions lib/protocol_9p_types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ open Sexplib.Std
open Protocol_9p_error

let big_enough_for name buf needed =
let length = Cstruct.len buf in
let length = Cstruct.length buf in
if length < needed
then error_msg "%s: buffer too small (%d < %d)" name length needed
else return ()
Expand Down Expand Up @@ -490,17 +490,17 @@ module Data = struct

let to_string x = Cstruct.to_string x

let sizeof t = 2 + (Cstruct.len t)
let sizeof t = 2 + (Cstruct.length t)

let read buf =
let length = Cstruct.len buf in
let length = Cstruct.length buf in
( if length < 2
then error_msg "Buffer is too short to contain a string length"
else return ()
) >>= fun () ->
let required = Cstruct.LE.get_uint16 buf 0 in
let rest = Cstruct.shift buf 2 in
let remaining = Cstruct.len rest in
let remaining = Cstruct.length rest in
( if remaining < required
then error_msg "Buffer is too short to contain string payload"
else return ()
Expand All @@ -510,14 +510,14 @@ module Data = struct
return (data, rest)

let write t buf =
let length = Cstruct.len buf in
let length = Cstruct.length buf in
let needed = sizeof t in
( if length < needed
then error_msg "Buffer is too small for Data.t (%d < %d)" needed length
else return ()
) >>= fun () ->
Cstruct.LE.set_uint16 buf 0 (Cstruct.len t);
Cstruct.blit t 0 buf 2 (Cstruct.len t);
Cstruct.LE.set_uint16 buf 0 (Cstruct.length t);
Cstruct.blit t 0 buf 2 (Cstruct.length t);
return (Cstruct.shift buf needed)
end

Expand Down Expand Up @@ -630,7 +630,7 @@ module Stat = struct
let t = { ty; dev; qid; mode; atime; mtime; length; name; uid; gid; muid; u = None } in
(* We are often decoding contiguous arrays of Stat structures,
so we must use the sz field to discover where the data ends. *)
let consumed = Cstruct.len buf - (Cstruct.len rest) in
let consumed = Cstruct.length buf - (Cstruct.length rest) in
if consumed = sz + 2 (* Size of the sz field itself *)
then return (t, rest)
else
Expand All @@ -645,7 +645,7 @@ module Stat = struct
>>= fun (n_muid, rest) ->
let u = Some { extension; n_uid; n_gid; n_muid } in
(* In case of future extensions, remove trailing garbage *)
let consumed = Cstruct.len buf - (Cstruct.len rest) in
let consumed = Cstruct.length buf - (Cstruct.length rest) in
let trailing_garbage = consumed - sz - 2 in
let rest = Cstruct.shift rest trailing_garbage in
return ({ t with u }, rest)
Expand Down Expand Up @@ -702,7 +702,7 @@ module Arr(T: Protocol_9p_s.SERIALISABLE) = struct

let read rest =
let rec loop acc rest =
if Cstruct.len rest = 0
if Cstruct.length rest = 0
then return (List.rev acc, rest)
else
T.read rest
Expand Down
2 changes: 1 addition & 1 deletion lib_test/lofs_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ let check_directory_boundary_read () =
| Error (`Msg err) ->
Alcotest.fail ("client1: read: " ^ err)
| Ok { Response.Read.data } ->
let n = Cstruct.len data in
let n = Cstruct.length data in
if n = 0
then Lwt.return (Ok ())
else Alcotest.fail ("client1: read non-zero: " ^ (string_of_int n))
Expand Down
12 changes: 6 additions & 6 deletions lib_test/tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ let () =

let example_data =
let data = Cstruct.create 1 in
for i = 0 to Cstruct.len data - 1 do
for i = 0 to Cstruct.length data - 1 do
Cstruct.set_char data i '\000'
done;
data
Expand Down Expand Up @@ -129,10 +129,10 @@ let print_parse_request r () =
Request.write r buf
>>= fun remaining ->
Cstruct.hexdump buf;
Alcotest.(check int) "write request" 0 (Cstruct.len remaining);
Alcotest.(check int) "write request" 0 (Cstruct.length remaining);
Request.read (Cstruct.shift buf len_size)
>>= fun (r', remaining) ->
Alcotest.(check int) "read request" 0 (Cstruct.len remaining);
Alcotest.(check int) "read request" 0 (Cstruct.length remaining);
Alcotest.(check request) "request" r r';
return ()
)
Expand All @@ -144,10 +144,10 @@ let print_parse_response r () =
let buf = Cstruct.create needed in
Response.write r buf
>>= fun remaining ->
Alcotest.(check int) "write response" 0 (Cstruct.len remaining);
Alcotest.(check int) "write response" 0 (Cstruct.length remaining);
Response.read (Cstruct.shift buf len_size)
>>= fun (r', remaining) ->
Alcotest.(check int) "read respsonse" 0 (Cstruct.len remaining);
Alcotest.(check int) "read respsonse" 0 (Cstruct.length remaining);
Alcotest.(check response) "response" r r';
return ()
)
Expand All @@ -157,7 +157,7 @@ let test_requests = List.map (fun r ->
) requests

let test_responses = List.map (fun r ->
Fmt.strf "print then parser random response", `Quick, print_parse_response r
Fmt.str "print then parser random response", `Quick, print_parse_response r
) responses

let tests = [
Expand Down
4 changes: 2 additions & 2 deletions protocol-9p-tool.opam
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ depends: [
"rresult"
"logs" {>= "0.5.0"}
"fmt"
"lambda-term" {>= "2.0.0"}
"lambda-term"
"win-error"
"cmdliner"
]
build: [
["dune" "subst"] {pinned}
["dune" "subst"] {dev}
["dune" "build" "-p" name "-j" jobs]
]
dev-repo: "git+https://github.com/mirage/ocaml-9p.git"
Expand Down
16 changes: 8 additions & 8 deletions protocol-9p-unix.opam
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@ depends: [
"dune" {>= "1.0"}
"protocol-9p" {>="1.0.1"}
"base-bytes"
"cstruct" {>= "3.0.0"}
"cstruct-lwt" {>= "3.0.0"}
"sexplib" {> "113.00.00"}
"cstruct" {>= "6.0.0"}
"cstruct-lwt" {>= "6.0.0"}
"sexplib" {> "113.00.00" & < "v0.15"}
"prometheus"
"rresult"
"mirage-flow" {>= "2.0.0"}
"mirage-channel" {>= "4.0.0"}
"mirage-flow" {>= "3.0.0"}
"mirage-channel" {with-test & >= "4.0.0"}
"lwt" {>= "3.0.0"}
"base-unix"
"astring"
"fmt"
"logs" {>= "0.5.0"}
"logs" {with-test & >= "0.5.0"}
"win-error"
"io-page-unix" {>= "2.0.0"}
"ppx_sexp_conv"
"ppx_sexp_conv" {< "v0.15"}
"alcotest" {with-test & >= "0.4.0"}
]
build: [
["dune" "subst"] {pinned}
["dune" "subst"] {dev}
["dune" "build" "-p" name "-j" jobs]
["dune" "runtest" "-p" name "-j" jobs] {with-test}
]
Expand Down
10 changes: 5 additions & 5 deletions protocol-9p.opam
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ depends: [
"ocaml" {>= "4.03.0"}
"dune" {>= "1.0"}
"base-bytes"
"cstruct" {>= "4.0.0"}
"cstruct" {>= "6.0.0"}
"cstruct-sexp"
"sexplib" {> "113.00.00"}
"sexplib" {> "113.00.00" & < "v0.15"}
"rresult"
"mirage-flow" {>= "2.0.0"}
"mirage-flow" {>= "3.0.0"}
"mirage-channel" {>= "4.0.0"}
"lwt" {>= "3.0.0"}
"astring"
"fmt"
"logs" {>= "0.5.0"}
"win-error"
"ppx_sexp_conv" {>="v0.9.0"}
"ppx_sexp_conv" {>= "v0.9.0" & < "v0.15"}
"alcotest" {with-test & >= "0.4.0"}
]
build: [
["dune" "subst"] {pinned}
["dune" "subst"] {dev}
["dune" "build" "-p" name "-j" jobs]
]
dev-repo: "git+https://github.com/mirage/ocaml-9p.git"
Expand Down
Loading

0 comments on commit 778765e

Please sign in to comment.