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 = Filename.chop_extension name in (* remove .opt or .exe *)
34 let name = String.lowercase name in
40 (f_"diskzip: unknown executable name '%s', assuming 'diskzip'\n")
42 let compressing = ref compressing in
44 (* Command line argument parsing. *)
46 printf "diskzip\n"; (* XXX version XXX *)
50 let output = ref None in
52 if !output <> None then (
53 prerr_endline (s_"diskzip: '-o' option cannot appear more than once");
57 let statbuf = stat path in
58 if statbuf.st_kind = S_DIR then
59 output := Some (Dir path)
61 output := Some (File path)
63 (* No such file or directory, assume it's a file output. *)
64 | Unix_error (ENOENT, _, _) -> output := Some (File path)
67 (* By default we don't use any external compression program. *)
68 let extcompress = ref None in
69 let set_extcompress t () =
70 if !extcompress <> None then (
71 prerr_endline (s_"diskzip: '-z' or '-j' cannot appear more than once");
77 let force = ref false in
79 let argspec = Arg.align [
80 "-d", Arg.Clear compressing,
81 " " ^ s_ "Uncompress (default: depends on executable name)";
82 "--debug", Arg.Set Diskimage.debug,
83 " " ^ s_ "Debug mode (default: false)";
85 " " ^ s_"Force compress even if stdout looks like a tty";
86 "-j", Arg.Unit (set_extcompress BZip2),
87 " " ^ s_"Pipe the output/input through bzip2";
88 "-o", Arg.String set_output,
89 "path " ^ s_"Set the output filename or directory name";
90 "-p", Arg.String (fun prog -> set_extcompress (External prog) ()),
91 "prog " ^ s_"Pipe the output/input through external program";
92 "--version", Arg.Unit version,
93 " " ^ s_"Display version and exit";
94 "-z", Arg.Unit (set_extcompress GZip),
95 " " ^ s_"Pipe the output/input through gzip";
99 let anon_fun str = args := str :: !args in
100 let usage_msg = s_"diskzip: Intelligently compress disk images
103 diskzip [-options] disk.img [disk.img ...] > output.dz
104 diskzcat [-options] output.dz > disk.img
108 Arg.parse argspec anon_fun usage_msg;
110 (* Turn refs back into normal values. *)
111 let compressing = !compressing in
112 let extcompress = !extcompress in
113 let output = !output in
114 let force = !force in
117 (* Check the arguments make sense. *)
118 if compressing && output <> None then (
119 prerr_endline (s_"diskzip: '-o' option cannot be used when compressing");
122 if compressing && args = [] then (
123 prerr_endline (s_"diskzip: no input");
126 if compressing && not force && isatty stdout then (
127 prerr_endline (s_"diskzip: compressed data not written to a terminal, use '-f' to force");
131 (* Run the compression or decompression functions. *)
133 go_compress extcompress args
135 go_decompress ?output extcompress args
137 (* Do compression. *)
138 and go_compress extcompress images =
139 (* Create a Diskimage machine description from the requested images. This
140 * also checks that everything we need is readable.
143 Diskimage.open_machine "diskzip" (List.map (fun n -> (n,n)) images) in
145 (* Scan the images for filesystems. *)
146 let machine = Diskimage.scan_machine machine in
148 (* Redirect output through external pipe if asked. *)
149 (match extcompress with
154 | BZip2 -> "bzip2", [|"bzip2"; "-c"|]
155 | GZip -> "gzip", [|"gzip"; "-c"|]
156 | External prog -> "sh", [|"sh"; "-c"; prog |] in
157 let rfd, wfd = pipe () in
159 if pid = 0 then ( (* child *)
164 ) else ( (* parent *)
180 and go_decompress ?output extcompress args =
181 (* Read the input, which may be a single named file, or a series of
182 * files (we just concatenate them). We may have to feed the input
183 * through an external program.
187 | [] -> () (* Reading from stdin. *)
188 | [file] -> (* Read the named file. *)
189 let fd = openfile file [O_RDONLY] 0 in
192 | files -> (* Concatenate files. *)
193 let rfd, wfd = pipe () in
195 if pid = 0 then ( (* child *)
199 execvp "cat" (Array.of_list ("cat" :: "--" :: files))
200 ) else ( (* parent *)
206 (match extcompress with
211 | BZip2 -> "bzip2", [|"bzip2"; "-cd"|]
212 | GZip -> "gzip", [|"gzip"; "-cd"|]
213 | External prog -> "sh", [|"sh"; "-c"; prog |] in
214 let rfd, wfd = pipe () in
216 if pid = 0 then ( (* child *)
221 ) else ( (* parent *)
229 let header = read_header () in
242 (* Since we have the wonderful pa_bitmatch, might as well use it to
243 * define a robust binary format for the compressed files.
245 and write_header ... =
247 0xD152 : 16; 0x01 : 8; 0x00 : 8; (* file magic, version 1.0 *)
248 nr_disks : 8; (* number of disks being packed *)
256 (* Diskzip headers are limited to overall max size of 1024 bytes. *)
257 let bs = Bitmatch.bitstring_of_file_descr_max stdin 1024 in
260 | { 0xD152 : 16; (* file magic *)
261 0x01 : 8; (_ as minor) : 8; (* major, minor versions *)
264 (* Is this a later version (major != 1)? *)
265 | { 0xD152 : 16; (* file magic *)
266 (_ as major) : 8; (_ as minor) : 8 } when major <> 1 ->
267 eprintf (f_"diskzip: archive version %d.%d, this program only understands version 1.x")
271 (* If it looks like gzip or bzip2, exit with an informative error. *)
272 | { 0o37 : 8; 0o213 : 8 } -> (* gzip *)
273 prerr_endline (s_"diskzip: This looks like a gzip archive. Did you mean to pass the '-z' option?");
275 | { "BZh" : 24 : string } -> (* bzip2 *)
276 prerr_endline (s_"diskzip: This looks like a bzip2 archive. Did you mean to pass the '-j' option?");
279 (* If it looks like a disk image (MBR), give an error. *)
280 | { _ : 4080 : bitstring; 0x55 : 8; 0xAA : 8 } ->
281 prerr_endline (s_"diskzip: This looks like a disk image. Did you mean to compress it?");
285 prerr_endline (s_"diskzip: Not a diskzip archive.");