-
Notifications
You must be signed in to change notification settings - Fork 296
libs: add Stat module in Unixext to handle special device IDs #6812
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
psafont
wants to merge
3
commits into
xapi-project:master
Choose a base branch
from
psafont:dev/pau/majmin
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
141 changes: 141 additions & 0 deletions
141
ocaml/libs/xapi-stdext/lib/xapi-stdext-unix/test/test_stat.ml
This file contains hidden or 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,141 @@ | ||
| let major_nbd = 43 | ||
|
|
||
| (** This module type helps us to implement alternative modules for [Stat]. | ||
| In particular one that uses the previous ad-hoc functions that where | ||
| incorrect, and one that we can use as reference in case the behaviour | ||
| changes and possibly change the users of [Stat]. | ||
| *) | ||
| module type S = sig | ||
| type device | ||
|
|
||
| val device : major:int -> minor:int -> device option | ||
|
|
||
| val encode_st_dev : device -> int | ||
|
|
||
| val decode_st_dev : int -> device | ||
|
|
||
| val major : device -> int | ||
|
|
||
| val minor : device -> int | ||
|
|
||
| val pp : Format.formatter -> device -> unit | ||
| end | ||
|
|
||
| module Stat : S = struct | ||
| include Xapi_stdext_unix.Unixext.Stat | ||
|
|
||
| let major {major; _} = major | ||
|
|
||
| let minor {minor; _} = minor | ||
|
|
||
| let pp = | ||
| Fmt.( | ||
| record ~sep:(any ", ") | ||
| [ | ||
| field "major" (fun d -> d.major) int | ||
| ; field "minor" (fun d -> d.minor) int | ||
| ] | ||
| ) | ||
| end | ||
|
|
||
| module Stat_reference : S = struct | ||
| type device = {major: int; minor: int} | ||
|
|
||
| let ( << ) = Stdlib.( lsl ) | ||
|
|
||
| let ( >> ) = Stdlib.( lsr ) | ||
|
|
||
| let ( &^ ) = Stdlib.( land ) | ||
|
|
||
| let ( |^ ) = Stdlib.( lor ) | ||
|
|
||
| let device ~major ~minor = | ||
| (* Linux's devids are 32-bit wide and the major and minor ones are 16-bit | ||
| wide, but we can support well up to 32-bit-wide minors *) | ||
| let minor_max = (1 lsl 32) - 1 in | ||
| let major_max = (1 lsl 16) - 1 in | ||
| if major < 0 || major_max < major || minor < 0 || minor_max < minor then | ||
| None | ||
| else | ||
| Some {major; minor} | ||
|
|
||
| let encode_st_dev {major; minor} = | ||
| 0 | ||
| |^ (major &^ 0x00000fff << 8) | ||
| |^ (major &^ 0x7ffff000 << 32) | ||
| |^ (minor &^ 0x000000ff << 0) | ||
| |^ (minor &^ 0xffffff00 << 12) | ||
|
|
||
| let decode_st_dev dev = | ||
| (* follow glibc's implementation, with an exception: the higher-most bit is | ||
| ignored because ints are 63 bits in ocaml. In any case, [Unix.stat] | ||
| returns a 63-bit int, so we can't do much in this code to avoid this. *) | ||
| let major = | ||
| 0 |^ (dev &^ 0x7ffff00000000000 >> 32) |^ (dev &^ 0x00000000000fff00 >> 8) | ||
| in | ||
| let minor = | ||
| 0 |^ (dev &^ 0x00000ffffff00000 >> 12) |^ (dev &^ 0x00000000000000ff >> 0) | ||
| in | ||
| {major; minor} | ||
|
|
||
| let major {major; _} = major | ||
|
|
||
| let minor {minor; _} = minor | ||
|
|
||
| let pp = | ||
| Fmt.( | ||
| record ~sep:(any ", ") | ||
| [ | ||
| field "major" (fun d -> d.major) int | ||
| ; field "minor" (fun d -> d.minor) int | ||
| ] | ||
| ) | ||
| end | ||
|
|
||
| let hex = Alcotest.testable (Fmt.of_to_string (Format.sprintf "0x%x")) ( = ) | ||
|
|
||
| let current_t = Alcotest.testable Stat.pp ( = ) | ||
|
|
||
| let test_combinations f ~major:lst_a ~minor:lst_b = | ||
| let test a b = (Printf.sprintf "major %i, minor %i" a b, `Quick, f a b) in | ||
| List.concat_map (fun a -> List.map (test a) lst_b) lst_a | ||
|
|
||
| let spec_minor = [0; 31; 65; 256; 1025; 4098; (1 lsl 32) - 1] | ||
|
|
||
| let spec_major = [0; major_nbd; (1 lsl 16) - 1] | ||
|
|
||
| let test_reference = | ||
| let test major minor () = | ||
| let current = Stat.device ~major ~minor |> Option.get in | ||
| let reference = Stat_reference.device ~major ~minor |> Option.get in | ||
| let encoded_cur = Stat.encode_st_dev current in | ||
| let encoded_ref = Stat_reference.encode_st_dev reference in | ||
|
|
||
| Alcotest.check hex "Encode must match reference implementation" encoded_ref | ||
| encoded_cur ; | ||
|
|
||
| let decoded_cur = Stat.decode_st_dev encoded_ref in | ||
| let decoded_ref = Stat_reference.decode_st_dev encoded_ref in | ||
|
|
||
| Alcotest.(check @@ pair int int) | ||
| "Decode must match reference implementation" | ||
| Stat_reference.(major decoded_ref, minor decoded_ref) | ||
| Stat.(major decoded_cur, minor decoded_cur) | ||
| in | ||
| let tests = test_combinations test ~major:spec_major ~minor:spec_minor in | ||
| ("Compare with reference", tests) | ||
|
|
||
| let test_roundtrip = | ||
| let test major minor () = | ||
| let current = Stat.device ~major ~minor |> Option.get in | ||
| let encoded_cur = Stat.encode_st_dev current in | ||
|
|
||
| let decoded_cur = Stat.decode_st_dev encoded_cur in | ||
| Alcotest.check current_t "Roundtripped current" current decoded_cur | ||
| in | ||
| let tests = test_combinations test ~major:spec_major ~minor:spec_minor in | ||
| ("Roundtrip", tests) | ||
|
|
||
| let tests = [test_reference; test_roundtrip] | ||
|
|
||
| let () = Alcotest.run "Uniext.Stat suite" tests | ||
Empty file.
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |
| vhd-format-lwt | ||
| xapi-idl | ||
| xapi-log | ||
| xapi-stdext-unix | ||
| xenstore_transport.unix | ||
| ezxenstore | ||
| ) | ||
|
|
||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the
0 |^is added to make ocamlformat behave and keep the firstmajorterm parenthesised and aligned. The same has been done in indecode