Skip to content

Add binary xapi_gzip for testing Xapi_compression #4685

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

Merged
merged 1 commit into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions ocaml/libs/xapi-compression/dune
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(library
(name xapi_compression)
(public_name xapi-compression)
(modules xapi_compression)
(libraries
forkexec
threads
Expand All @@ -11,3 +12,10 @@
)
)

(executable
(name xapi_gzip)
(modules xapi_gzip)
; don't install this
; (public_name xapi-gzip)
; (package xapi-compression)
(libraries xapi_compression cmdliner))
67 changes: 67 additions & 0 deletions ocaml/libs/xapi-compression/xapi_gzip.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
(*
* Copyright (C) Citrix Systems Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; version 2.1 only. with the special
* exception on linking described in file LICENSE.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*)

(* This is a minimal test CLI to exercise gzip for compression and
uncompression using the Gzip module; which in turn spawns a process via
Forkexecd *)

module C = Cmdliner

module Gzip = Xapi_compression.Make (struct let executable = "/bin/gzip" end)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already a gzip module that does this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know, but I wanted to avoid the dependency given that it depends on this opam package - that would be circular.


let help =
[
`P "These options are common to all commands."
; `S "MORE HELP"
; `P "Use `$(mname) $(i,COMMAND) --help' for help on a single command."
]

(** copy bytes from file descriptor [src] to [dst] *)
let copy ~src ~dst =
let size = 1024 * 64 in
let buffer = Bytes.create size in
let rec loop () =
match Unix.read src buffer 0 size with
| 0 ->
()
| n ->
ignore (Unix.write dst buffer 0 n) ;
loop ()
in
loop ()

let gzip ~src ~dst = Gzip.compress dst (fun dst -> copy ~src ~dst)

let gunzip ~src ~dst = Gzip.decompress_passive src (fun src -> copy ~src ~dst)

let compress uncomp =
match uncomp with
| false ->
gzip ~src:Unix.stdin ~dst:Unix.stdout
| true ->
gunzip ~src:Unix.stdin ~dst:Unix.stdout

let decompress =
let doc = "Decompress." in
C.Arg.(
value
& flag
& info ["decompress"; "uncompress"; "d"] ~docv:"DECOMPRESS" ~doc
)

let main =
let doc = "Test compression and uncompression" in
C.Term.(const compress $ decompress, info "xapi-gzip" ~doc ~man:help)

let () = if !Sys.interactive then () else C.Term.(exit @@ eval main)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an interesting way of making the main module testable from utop, I'll keep this in mind

1 change: 1 addition & 0 deletions ocaml/libs/xapi-compression/xapi_gzip.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(* empty *)