forked from mor1/mirage-tutorial
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a solved version of devices/crunch
- Loading branch information
Showing
5 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
(* This file has been autogenerated by mir-crunch *) | ||
module Internal = struct | ||
let file_chunks = function | ||
| "bar" | "/bar" -> Some ["67890\n" ] | ||
| "foo" | "/foo" -> Some ["12345\n" ] | ||
| _ -> None | ||
|
||
let file_list = ["foo"; "bar"; ] | ||
let size = function | ||
|"foo" |"/foo" -> Some 6L | ||
|"bar" |"/bar" -> Some 6L | ||
|_ -> None | ||
|
||
end | ||
|
||
let name="myblock" | ||
|
||
open Lwt | ||
|
||
exception Error of string | ||
|
||
let iter_s fn = Lwt_list.iter_s fn Internal.file_list | ||
|
||
let size name = return (Internal.size name) | ||
|
||
let read name = | ||
match Internal.file_chunks name with | ||
|None -> return None | ||
|Some c -> | ||
let chunks = ref c in | ||
return (Some (Lwt_stream.from (fun () -> | ||
match !chunks with | ||
|hd :: tl -> | ||
chunks := tl; | ||
return (Some (Bitstring.bitstring_of_string hd)) | ||
|[] -> return None | ||
))) | ||
|
||
let create vbd : OS.Devices.kv_ro Lwt.t = | ||
return (object | ||
method iter_s fn = iter_s fn | ||
method read name = read name | ||
method size name = size name | ||
end) | ||
|
||
let _ = | ||
let plug = Lwt_mvar.create_empty () in | ||
let unplug = Lwt_mvar.create_empty () in | ||
let provider = object(self) | ||
method id = name | ||
method plug = plug | ||
method unplug = unplug | ||
method create ~deps ~cfg id = | ||
Lwt.bind (create id) (fun kv -> | ||
let entry = OS.Devices.({ | ||
provider=self; | ||
id=self#id; | ||
depends=[]; | ||
node=KV_RO kv }) in | ||
return entry | ||
) | ||
end in | ||
OS.Devices.new_provider provider; | ||
OS.Main.at_enter (fun () -> Lwt_mvar.put plug {OS.Devices.p_id=name; p_dep_ids=[]; p_cfg=[]}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Server.main | ||
Block.SimpleKV |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
open Lwt | ||
open Printf | ||
|
||
let main () = | ||
printf "Plugging device\n%!"; | ||
lwt kv_ro = OS.Devices.with_kv_ro "myblock" return in | ||
printf "Reading file foo\n%!"; | ||
match_lwt kv_ro#read "foo" with | ||
|Some s -> | ||
printf "File contents:\n%!"; | ||
Lwt_stream.iter (fun b -> | ||
printf "%s%!" (Bitstring.string_of_bitstring b); | ||
) s | ||
|None -> | ||
printf "File not found\n%!"; | ||
return () | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
67890 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
12345 |