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
155 (* Redirect output through external pipe if asked. *)
156 (match extcompress with
161 | BZip2 -> "bzip2", [|"bzip2"; "-c"|]
162 | GZip -> "gzip", [|"gzip"; "-c"|]
163 | External prog -> "sh", [|"sh"; "-c"; prog |] in
164 let rfd, wfd = pipe () in
166 if pid = 0 then ( (* child *)
171 ) else ( (* parent *)
187 and go_decompress ?output extcompress args =
188 (* Read the input, which may be a single named file, or a series of
189 * files (we just concatenate them). We may have to feed the input
190 * through an external program.
194 | [] -> () (* Reading from stdin. *)
195 | [file] -> (* Read the named file. *)
196 let fd = openfile file [O_RDONLY] 0 in
199 | files -> (* Concatenate files. *)
200 let rfd, wfd = pipe () in
202 if pid = 0 then ( (* child *)
206 execvp "cat" (Array.of_list ("cat" :: "--" :: files))
207 ) else ( (* parent *)
213 (match extcompress with
218 | BZip2 -> "bzip2", [|"bzip2"; "-cd"|]
219 | GZip -> "gzip", [|"gzip"; "-cd"|]
220 | External prog -> "sh", [|"sh"; "-c"; prog |] in
221 let rfd, wfd = pipe () in
223 if pid = 0 then ( (* child *)
228 ) else ( (* parent *)
236 let header = read_header () in
249 (* Since we have the wonderful pa_bitmatch, might as well use it to
250 * define a robust binary format for the compressed files.
252 and write_header ... =
254 0xD152 : 16; 0x01 : 8; 0x00 : 8; (* file magic, version 1.0 *)
255 nr_disks : 8; (* number of disks being packed *)
263 (* Diskzip headers are limited to overall max size of 1024 bytes. *)
264 let bs = Bitmatch.bitstring_of_file_descr_max stdin 1024 in
267 | { 0xD152 : 16; (* file magic *)
268 0x01 : 8; (_ as minor) : 8; (* major, minor versions *)
271 (* Is this a later version (major != 1)? *)
272 | { 0xD152 : 16; (* file magic *)
273 (_ as major) : 8; (_ as minor) : 8 } when major <> 1 ->
274 eprintf (f_"diskzip: archive version %d.%d, this program only understands version 1.x")
278 (* If it looks like gzip or bzip2, exit with an informative error. *)
279 | { 0o37 : 8; 0o213 : 8 } -> (* gzip *)
280 prerr_endline (s_"diskzip: This looks like a gzip archive. Did you mean to pass the '-z' option?");
282 | { "BZh" : 24 : string } -> (* bzip2 *)
283 prerr_endline (s_"diskzip: This looks like a bzip2 archive. Did you mean to pass the '-j' option?");
286 (* If it looks like a disk image (MBR), give an error. *)
287 | { _ : 4080 : bitstring; 0x55 : 8; 0xAA : 8 } ->
288 prerr_endline (s_"diskzip: This looks like a disk image. Did you mean to compress it?");
292 prerr_endline (s_"diskzip: Not a diskzip archive.");