1 (* 'diskzip' command for intelligently compressing disk images.
2 (C) Copyright 2007 Richard W.M. Jones, Red Hat Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 open Diskzip_gettext.Gettext
25 type output = File of string | Dir of string
26 type extcompress = BZip2 | GZip | External of string
29 (* Program name changes behaviour. *)
31 let name = Sys.argv.(0) in
32 let name = Filename.basename name in (* just the executable name *)
33 let name = (* remove .opt or .exe *)
34 try Filename.chop_extension name
35 with Invalid_argument("Filename.chop_extension") -> name in
36 let name = String.lowercase name in
42 (f_"diskzip: unknown executable name '%s', assuming 'diskzip'\n")
44 let compressing = ref compressing in
46 (* Command line argument parsing. *)
48 printf "diskzip\n"; (* XXX version XXX *)
52 let output = ref None in
54 if !output <> None then (
55 prerr_endline (s_"diskzip: '-o' option cannot appear more than once");
59 let statbuf = stat path in
60 if statbuf.st_kind = S_DIR then
61 output := Some (Dir path)
63 output := Some (File path)
65 (* No such file or directory, assume it's a file output. *)
66 | Unix_error (ENOENT, _, _) -> output := Some (File path)
69 (* By default we don't use any external compression program. *)
70 let extcompress = ref None in
71 let set_extcompress t () =
72 if !extcompress <> None then (
73 prerr_endline (s_"diskzip: '-z' or '-j' cannot appear more than once");
79 let force = ref false in
81 let argspec = Arg.align [
82 "-d", Arg.Clear compressing,
83 " " ^ s_ "Uncompress (default: depends on executable name)";
84 "--debug", Arg.Set Diskimage.debug,
85 " " ^ s_ "Debug mode (default: false)";
87 " " ^ s_"Force compress even if stdout looks like a tty";
88 "-j", Arg.Unit (set_extcompress BZip2),
89 " " ^ s_"Pipe the output/input through bzip2";
90 "-o", Arg.String set_output,
91 "path " ^ s_"Set the output filename or directory name";
92 "-p", Arg.String (fun prog -> set_extcompress (External prog) ()),
93 "prog " ^ s_"Pipe the output/input through external program";
94 "--version", Arg.Unit version,
95 " " ^ s_"Display version and exit";
96 "-z", Arg.Unit (set_extcompress GZip),
97 " " ^ s_"Pipe the output/input through gzip";
101 let anon_fun str = args := str :: !args in
102 let usage_msg = s_"diskzip: Intelligently compress disk images
105 diskzip [-options] disk.img [disk.img ...] > output.dz
106 diskzcat [-options] output.dz > disk.img
110 Arg.parse argspec anon_fun usage_msg;
112 (* Turn refs back into normal values. *)
113 let compressing = !compressing in
114 let extcompress = !extcompress in
115 let output = !output in
116 let force = !force in
119 (* Check the arguments make sense. *)
120 if compressing && output <> None then (
121 prerr_endline (s_"diskzip: '-o' option cannot be used when compressing");
124 if compressing && args = [] then (
125 prerr_endline (s_"diskzip: no input");
128 if compressing && not force && isatty stdout then (
129 prerr_endline (s_"diskzip: compressed data not written to a terminal, use '-f' to force");
133 (* Run the compression or decompression functions. *)
135 go_compress extcompress args
137 go_decompress ?output extcompress args
139 (* Do compression. *)
140 and go_compress extcompress images =
141 (* Create a Diskimage machine description from the requested images. This
142 * also checks that everything we need is readable.
145 Diskimage.open_machine "diskzip" (List.map (fun n -> (n,n)) images) in
147 (* Scan the images for filesystems. *)
148 let machine = Diskimage.scan_machine machine in
150 (* Create ownership tables. *)
151 let ownership = Diskimage.create_ownership machine in
153 (* Create ownership bitmap for each disk. *)
156 (* Redirect output through external pipe if asked. *)
157 (match extcompress with
162 | BZip2 -> "bzip2", [|"bzip2"; "-c"|]
163 | GZip -> "gzip", [|"gzip"; "-c"|]
164 | External prog -> "sh", [|"sh"; "-c"; prog |] in
165 let rfd, wfd = pipe () in
167 if pid = 0 then ( (* child *)
172 ) else ( (* parent *)
188 and go_decompress ?output extcompress args =
189 (* Read the input, which may be a single named file, or a series of
190 * files (we just concatenate them). We may have to feed the input
191 * through an external program.
195 | [] -> () (* Reading from stdin. *)
196 | [file] -> (* Read the named file. *)
197 let fd = openfile file [O_RDONLY] 0 in
200 | files -> (* Concatenate files. *)
201 let rfd, wfd = pipe () in
203 if pid = 0 then ( (* child *)
207 execvp "cat" (Array.of_list ("cat" :: "--" :: files))
208 ) else ( (* parent *)
214 (match extcompress with
219 | BZip2 -> "bzip2", [|"bzip2"; "-cd"|]
220 | GZip -> "gzip", [|"gzip"; "-cd"|]
221 | External prog -> "sh", [|"sh"; "-c"; prog |] in
222 let rfd, wfd = pipe () in
224 if pid = 0 then ( (* child *)
229 ) else ( (* parent *)
237 let header = read_header () in
250 (* Since we have the wonderful pa_bitmatch, might as well use it to
251 * define a robust binary format for the compressed files.
253 and write_header ... =
255 0xD152 : 16; 0x01 : 8; 0x00 : 8; (* file magic, version 1.0 *)
256 nr_disks : 8; (* number of disks being packed *)
264 (* Diskzip headers are limited to overall max size of 1024 bytes. *)
265 let bs = Bitmatch.bitstring_of_file_descr_max stdin 1024 in
268 | { 0xD152 : 16; (* file magic *)
269 0x01 : 8; (_ as minor) : 8; (* major, minor versions *)
272 (* Is this a later version (major != 1)? *)
273 | { 0xD152 : 16; (* file magic *)
274 (_ as major) : 8; (_ as minor) : 8 } when major <> 1 ->
275 eprintf (f_"diskzip: archive version %d.%d, this program only understands version 1.x")
279 (* If it looks like gzip or bzip2, exit with an informative error. *)
280 | { 0o37 : 8; 0o213 : 8 } -> (* gzip *)
281 prerr_endline (s_"diskzip: This looks like a gzip archive. Did you mean to pass the '-z' option?");
283 | { "BZh" : 24 : string } -> (* bzip2 *)
284 prerr_endline (s_"diskzip: This looks like a bzip2 archive. Did you mean to pass the '-j' option?");
287 (* If it looks like a disk image (MBR), give an error. *)
288 | { _ : 4080 : bitstring; 0x55 : 8; 0xAA : 8 } ->
289 prerr_endline (s_"diskzip: This looks like a disk image. Did you mean to compress it?");
293 prerr_endline (s_"diskzip: Not a diskzip archive.");