File: parse_vmx.ml

package info (click to toggle)
virt-v2v 2.6.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,132 kB
  • sloc: ml: 19,674; sh: 7,631; ansic: 6,897; makefile: 3,261; python: 1,114; perl: 852; xml: 114
file content (392 lines) | stat: -rw-r--r-- 11,401 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
(* virt-v2v
 * Copyright (C) 2017 Red Hat Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *)

open Printf

open Std_utils
open Tools_utils
open Common_gettext.Gettext

(* As far as I can tell the VMX format is totally unspecified.
 * However libvirt has a useful selection of .vmx files in the
 * sources which explore some of the darker regions of this
 * format.
 *
 * So here are some facts about VMX derived from libvirt and
 * other places:
 *
 * - Keys are compared case insensitively.  We assume here
 *   that keys are 7-bit ASCII.
 *
 * - Multiple keys with the same name are not allowed.
 *
 * - Escaping in the value string is possible using a very weird
 *   escape format: "|22" means the character '\x22'.  To write
 *   a pipe character you must use "|7C".
 *
 * - Boolean values are written "TRUE", "FALSE", "True", "true", etc.
 *   Because of the quotes they cannot be distinguished from strings.
 *
 * - Comments (#...) and blank lines are ignored.  Some files start
 *   with a hash-bang path, but we ignore those as comments.  This
 *   parser also ignores any other line which it doesn't understand,
 *   but will print a warning.
 *
 * - Multi-line values are not permitted.
 *
 * - Keys are namespaced using dots, eg. scsi0:0.deviceType has
 *   the namespace "scsi0:0" and the key name "deviceType".
 *
 * - Using namespace.present = "FALSE" means that all other keys
 *   in and under the namespace are ignored.
 *
 * - You cannot have a namespace and a key with the same name, eg.
 *   this is not allowed:
 *     namespace = "some value"
 *     namespace.foo = "another value"
 *
 * - The Hashicorp packer VMX writer considers some special keys
 *   as not requiring any quotes around their values, but I'm
 *   ignoring that for now.
 *)

(* This VMX file:
 *
 *   foo.a = "abc"
 *   foo.b = "def"
 *   foo.bar.c = "abc"
 *   foo.bar.d = "def"
 *
 * would be represented by this structure:
 *
 *   "foo" => Namespace (             # "foo" is a namespace
 *              "a" => Key "abc";     # "foo.a" is a key with value "abc"
 *              "b" => Key "def";
 *              "bar" => Namespace (  # "foo.bar" is another namespace
 *                         "c" => Key "abc";
 *                         "d" => Key "def";
 *                       )
 *            )
 *   ‘( => )’s represent the StringMap type.
 *)
type t = key StringMap.t

and key =
  | Key of string
  | Namespace of t

let empty = StringMap.empty

(* Compare two trees for equality. *)
let rec equal vmx1 vmx2 =
  let cmp k1 k2 =
    match k1, k2 with
    | Key v1, Key v2 -> v1 = v2
    | Key _, Namespace _ -> false
    | Namespace _, Key _ -> false
    | Namespace vmx1, Namespace vmx2 -> equal vmx1 vmx2
  in
  StringMap.equal cmp vmx1 vmx2

(* Higher-order functions. *)
let rec select_namespaces pred vmx =
  _select_namespaces [] pred vmx

and _select_namespaces path pred vmx =
  StringMap.fold (
    fun k v new_vmx ->
      let path = path @ [k] in
      match v with
      | Key _ -> new_vmx
      | Namespace _ when pred path ->
         StringMap.add k v new_vmx
      | Namespace t ->
         let t = _select_namespaces path pred t in
         if not (equal t empty) then
           StringMap.add k (Namespace t) new_vmx
         else
           new_vmx
  ) vmx empty

let rec map f vmx =
  _map [] f vmx

and _map path f vmx =
  StringMap.fold (
    fun k v r ->
      let path = path @ [k] in
      match v with
      | Key v -> r @ [ f path (Some v) ]
      | Namespace t -> r @ [ f path None ] @ _map path f t
  ) vmx []

let rec namespace_present vmx = function
  | [] -> false
  | [ns] ->
     let ns = String.lowercase_ascii ns in
     (try
        let v = StringMap.find ns vmx in
        match v with
        | Key _ -> false
        | Namespace _ -> true
      with
        Not_found -> false
     )
  | ns :: path ->
     let ns = String.lowercase_ascii ns in
     (try
        let v = StringMap.find ns vmx in
        match v with
        | Key _ -> false
        | Namespace vmx -> namespace_present vmx path
      with
        Not_found -> false
     )

(* Dump the vmx structure to [chan].  Used for debugging. *)
let rec print chan indent vmx =
  StringMap.iter (print_key chan indent) vmx

and print_key chan indent k = function
  | Key v ->
     output_spaces chan indent;
     fprintf chan "%s = \"%s\"\n" k v
  | Namespace vmx ->
     output_spaces chan indent;
     fprintf chan "namespace '%s':\n" k;
     print chan (indent+4) vmx

(* As above, but creates a string instead. *)
let rec to_string indent vmx =
  StringMap.fold (fun k v str -> str ^ to_string_key indent k v) vmx ""

and to_string_key indent k = function
  | Key v ->
     String.spaces indent ^ sprintf "%s = \"%s\"\n" k v
  | Namespace vmx ->
     String.spaces indent ^ sprintf "namespace '%s':\n" k ^
       to_string (indent+4) vmx

(* Access keys in the tree. *)
let rec get_string vmx = function
  | [] -> None
  | [k] ->
     let k = String.lowercase_ascii k in
     (try
        let v = StringMap.find k vmx in
        match v with
        | Key v -> Some v
        | Namespace _ -> None
      with Not_found -> None
     )
  | ns :: path ->
     let ns = String.lowercase_ascii ns in
     (try
        let v = StringMap.find ns vmx in
        match v with
        | Key v -> None
        | Namespace vmx -> get_string vmx path
      with
        Not_found -> None
     )

let get_int64 vmx path =
  match get_string vmx path with
  | None -> None
  | Some i -> Some (Int64.of_string i)

let get_int vmx path =
  match get_string vmx path with
  | None -> None
  | Some i -> Some (int_of_string i)

let rec get_bool vmx path =
  match get_string vmx path with
  | None -> None
  | Some t -> Some (vmx_bool_of_string t)

and vmx_bool_of_string t =
  if String.lowercase_ascii t = "true" then true
  else if String.lowercase_ascii t = "false" then false
  else failwith "bool_of_string"

(* Regular expression used to match key = "value" in VMX file. *)
let rex = PCRE.compile "^([^ \t=]+)\\s*=\\s*\"(.*)\"$"

(* Remove the weird escapes used in value strings.  See description above. *)
let remove_vmx_escapes str =
  let len = String.length str in
  let out = Bytes.make len '\000' in
  let j = ref 0 in

  let rec loop i =
    if i >= len then ()
    else (
      let c = String.unsafe_get str i in
      if i <= len-3 && c = '|' then (
        let c1 = str.[i+1] and c2 = str.[i+2] in
        if Char.isxdigit c1 && Char.isxdigit c2 then (
          let x = Char.hexdigit c1 * 0x10 + Char.hexdigit c2 in
          Bytes.set out !j (Char.chr x);
          incr j;
          loop (i+3)
        )
        else (
          Bytes.set out !j c;
          incr j;
          loop (i+1)
        )
      )
      else (
        Bytes.set out !j c;
        incr j;
        loop (i+1)
      )
    )
  in
  loop 0;

  (* Truncate the output string to its real size and return it
   * as an immutable string.
   *)
  Bytes.sub_string out 0 !j

(* Parsing. *)
let rec parse_file vmx_filename =
  (* Read the whole file as a list of lines. *)
  let str = read_whole_file vmx_filename in
  if verbose () then eprintf "VMX file:\n%s\n" str;
  parse_string str

and parse_string str =
  (* Reject VMDK files early, since in virt-v2v we have seen users try
   * to pass a .vmdk file as a .vmx file.  Luckily this is easy to
   * do with the whole file as a string.
   *)
  if String.is_prefix str "VMDK" ||
     String.is_prefix str "KDMV" ||
     String.is_prefix str "# Disk DescriptorFile\n" then
    error "input file is a VMDK (disk image), but we are expecting a \
           VMX (VMware metadata)";

  (* Split the input into lines. *)
  let lines = String.nsplit "\n" str in

  (* I've never seen any VMX file with CR-LF endings, and VMware
   * itself is Linux-based, but to be on the safe side ...
   *)
  let lines = List.map (String.trimr ~test:((=) '\r')) lines in

  (* Ignore blank lines and comments. *)
  let lines = List.filter (
    fun line ->
      let line = String.triml line in
      let len = String.length line in
      len > 0 && line.[0] != '#'
  ) lines in

  (* Parse the lines into key = "value". *)
  let lines = List.filter_map (
    fun line ->
      if PCRE.matches rex line then (
        let key = PCRE.sub 1 and value = PCRE.sub 2 in
        let key = String.lowercase_ascii key in
        let value = remove_vmx_escapes value in
        Some (key, value)
      )
      else (
        warning (f_"vmx parser: cannot parse this line, ignoring: %s") line;
        None
      )
  ) lines in

  (* Split the keys into namespace paths. *)
  let lines =
    List.map (fun (key, value) -> String.nsplit "." key, value) lines in

  (* Build a tree from the flat list and return it.  This is horribly
   * inefficient, at least O(n²), possibly even O(n².log n).  Hope
   * there are no large VMX files!  (XXX)
   *)
  let vmx =
    List.fold_left (
      fun vmx (path, value) -> insert vmx value path
    ) empty lines in

  (* If we're verbose, dump the parsed VMX for debugging purposes. *)
  if verbose () then (
    eprintf "parsed VMX tree:\n";
    print stderr 0 vmx
  );

  (* Drop all present = "FALSE" namespaces. *)
  let vmx = drop_not_present vmx in

  vmx

and insert vmx value = function
  | [] -> assert false
  | [k] ->
     if StringMap.mem k vmx then (
       warning (f_"vmx parser: duplicate key '%s' ignored") k;
       vmx
     ) else
       StringMap.add k (Key value) vmx
  | ns :: path ->
     let v =
       try
         (match StringMap.find ns vmx with
          | Namespace vmx -> Some vmx
          | Key _ -> None
         )
       with Not_found -> None in
     let v =
       match v with
       | None ->
          (* Completely new namespace. *)
          insert empty value path
       | Some v ->
          (* Insert the subkey into the previously created namespace. *)
          insert v value path in
     StringMap.add ns (Namespace v) vmx

(* Find any "present" keys.  If we find present = "FALSE", then
 * drop the containing namespace and all subkeys and subnamespaces.
 *)
and drop_not_present vmx =
  StringMap.fold (
    fun k v new_vmx ->
      match v with
      | Key _ ->
         StringMap.add k v new_vmx
      | Namespace vmx when contains_key_present_false vmx ->
         (* drop this namespace and all sub-spaces *)
         new_vmx
      | Namespace v ->
         (* recurse into sub-namespace and do the same check *)
         let v = drop_not_present v in
         StringMap.add k (Namespace v) new_vmx
  ) vmx empty

and contains_key_present_false vmx =
  try
    match StringMap.find "present" vmx with
    | Key v when vmx_bool_of_string v = false -> true
    | Key _ | Namespace _ -> false
  with
    Failure _ | Not_found -> false