3b2720c7c94e03fb8a4056973f8d6bc9a1c6154a
[libguestfs.git] / src / inspect.c
1 /* libguestfs
2  * Copyright (C) 2010 Red Hat Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdint.h>
24 #include <inttypes.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <sys/stat.h>
28
29 #ifdef HAVE_PCRE
30 #include <pcre.h>
31 #endif
32
33 #ifdef HAVE_HIVEX
34 #include <hivex.h>
35 #endif
36
37 #include "c-ctype.h"
38 #include "ignore-value.h"
39 #include "xstrtol.h"
40
41 #include "guestfs.h"
42 #include "guestfs-internal.h"
43 #include "guestfs-internal-actions.h"
44 #include "guestfs_protocol.h"
45
46 #if defined(HAVE_PCRE) && defined(HAVE_HIVEX)
47
48 /* Compile all the regular expressions once when the shared library is
49  * loaded.  PCRE is thread safe so we're supposedly OK here if
50  * multiple threads call into the libguestfs API functions below
51  * simultaneously.
52  */
53 static pcre *re_fedora;
54 static pcre *re_rhel_old;
55 static pcre *re_rhel;
56 static pcre *re_rhel_no_minor;
57 static pcre *re_major_minor;
58 static pcre *re_aug_seq;
59 static pcre *re_xdev;
60 static pcre *re_windows_version;
61
62 static void compile_regexps (void) __attribute__((constructor));
63 static void free_regexps (void) __attribute__((destructor));
64
65 static void
66 compile_regexps (void)
67 {
68   const char *err;
69   int offset;
70
71 #define COMPILE(re,pattern,options)                                     \
72   do {                                                                  \
73     re = pcre_compile ((pattern), (options), &err, &offset, NULL);      \
74     if (re == NULL) {                                                   \
75       ignore_value (write (2, err, strlen (err)));                      \
76       abort ();                                                         \
77     }                                                                   \
78   } while (0)
79
80   COMPILE (re_fedora, "Fedora release (\\d+)", 0);
81   COMPILE (re_rhel_old,
82            "(?:Red Hat Enterprise Linux|CentOS|Scientific Linux).*release (\\d+).*Update (\\d+)", 0);
83   COMPILE (re_rhel,
84            "(?:Red Hat Enterprise Linux|CentOS|Scientific Linux).*release (\\d+)\\.(\\d+)", 0);
85   COMPILE (re_rhel_no_minor,
86            "(?:Red Hat Enterprise Linux|CentOS|Scientific Linux).*release (\\d+)", 0);
87   COMPILE (re_major_minor, "(\\d+)\\.(\\d+)", 0);
88   COMPILE (re_aug_seq, "/\\d+$", 0);
89   COMPILE (re_xdev, "^/dev/(?:h|s|v|xv)d([a-z]\\d*)$", 0);
90   COMPILE (re_windows_version, "^(\\d+)\\.(\\d+)", 0);
91 }
92
93 static void
94 free_regexps (void)
95 {
96   pcre_free (re_fedora);
97   pcre_free (re_rhel_old);
98   pcre_free (re_rhel);
99   pcre_free (re_rhel_no_minor);
100   pcre_free (re_major_minor);
101   pcre_free (re_aug_seq);
102   pcre_free (re_xdev);
103   pcre_free (re_windows_version);
104 }
105
106 /* The main inspection code. */
107 static int check_for_filesystem_on (guestfs_h *g, const char *device);
108
109 char **
110 guestfs__inspect_os (guestfs_h *g)
111 {
112   /* Remove any information previously stored in the handle. */
113   guestfs___free_inspect_info (g);
114
115   if (guestfs_umount_all (g) == -1)
116     return NULL;
117
118   /* Iterate over all possible devices.  Try to mount each
119    * (read-only).  Examine ones which contain filesystems and add that
120    * information to the handle.
121    */
122   /* Look to see if any devices directly contain filesystems (RHBZ#590167). */
123   char **devices;
124   devices = guestfs_list_devices (g);
125   if (devices == NULL)
126     return NULL;
127
128   size_t i;
129   for (i = 0; devices[i] != NULL; ++i) {
130     if (check_for_filesystem_on (g, devices[i]) == -1) {
131       guestfs___free_string_list (devices);
132       guestfs___free_inspect_info (g);
133       return NULL;
134     }
135   }
136   guestfs___free_string_list (devices);
137
138   /* Look at all partitions. */
139   char **partitions;
140   partitions = guestfs_list_partitions (g);
141   if (partitions == NULL) {
142     guestfs___free_inspect_info (g);
143     return NULL;
144   }
145
146   for (i = 0; partitions[i] != NULL; ++i) {
147     if (check_for_filesystem_on (g, partitions[i]) == -1) {
148       guestfs___free_string_list (partitions);
149       guestfs___free_inspect_info (g);
150       return NULL;
151     }
152   }
153   guestfs___free_string_list (partitions);
154
155   /* Look at all LVs. */
156   if (guestfs___feature_available (g, "lvm2")) {
157     char **lvs;
158     lvs = guestfs_lvs (g);
159     if (lvs == NULL) {
160       guestfs___free_inspect_info (g);
161       return NULL;
162     }
163
164     for (i = 0; lvs[i] != NULL; ++i) {
165       if (check_for_filesystem_on (g, lvs[i]) == -1) {
166         guestfs___free_string_list (lvs);
167         guestfs___free_inspect_info (g);
168         return NULL;
169       }
170     }
171     guestfs___free_string_list (lvs);
172   }
173
174   /* At this point we have, in the handle, a list of all filesystems
175    * found and data about each one.  Now we assemble the list of
176    * filesystems which are root devices and return that to the user.
177    * Fall through to guestfs__inspect_get_roots to do that.
178    */
179   char **ret = guestfs__inspect_get_roots (g);
180   if (ret == NULL)
181     guestfs___free_inspect_info (g);
182   return ret;
183 }
184
185 /* Find out if 'device' contains a filesystem.  If it does, add
186  * another entry in g->fses.
187  */
188 static int check_filesystem (guestfs_h *g, const char *device);
189 static int check_linux_root (guestfs_h *g, struct inspect_fs *fs);
190 static int check_fstab (guestfs_h *g, struct inspect_fs *fs);
191 static int check_windows_root (guestfs_h *g, struct inspect_fs *fs);
192 static int check_windows_arch (guestfs_h *g, struct inspect_fs *fs);
193 static int check_windows_registry (guestfs_h *g, struct inspect_fs *fs);
194 static char *resolve_windows_path_silently (guestfs_h *g, const char *);
195 static int extend_fses (guestfs_h *g);
196 static int parse_unsigned_int (guestfs_h *g, const char *str);
197 static int add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
198                             const char *spec, const char *mp);
199 static char *resolve_fstab_device (guestfs_h *g, const char *spec);
200 static void check_package_format (guestfs_h *g, struct inspect_fs *fs);
201 static void check_package_management (guestfs_h *g, struct inspect_fs *fs);
202
203 static int
204 check_for_filesystem_on (guestfs_h *g, const char *device)
205 {
206   /* Get vfs-type in order to check if it's a Linux(?) swap device.
207    * If there's an error we should ignore it, so to do that we have to
208    * temporarily replace the error handler with a null one.
209    */
210   guestfs_error_handler_cb old_error_cb = g->error_cb;
211   g->error_cb = NULL;
212   char *vfs_type = guestfs_vfs_type (g, device);
213   g->error_cb = old_error_cb;
214
215   int is_swap = vfs_type && STREQ (vfs_type, "swap");
216
217   if (g->verbose)
218     fprintf (stderr, "check_for_filesystem_on: %s (%s)\n",
219              device, vfs_type ? vfs_type : "failed to get vfs type");
220
221   if (is_swap) {
222     free (vfs_type);
223     if (extend_fses (g) == -1)
224       return -1;
225     g->fses[g->nr_fses-1].is_swap = 1;
226     return 0;
227   }
228
229   /* Try mounting the device.  As above, ignore errors. */
230   g->error_cb = NULL;
231   int r = guestfs_mount_ro (g, device, "/");
232   if (r == -1 && vfs_type && STREQ (vfs_type, "ufs")) /* Hack for the *BSDs. */
233     r = guestfs_mount_vfs (g, "ro,ufstype=ufs2", "ufs", device, "/");
234   free (vfs_type);
235   g->error_cb = old_error_cb;
236   if (r == -1)
237     return 0;
238
239   /* Do the rest of the checks. */
240   r = check_filesystem (g, device);
241
242   /* Unmount the filesystem. */
243   if (guestfs_umount_all (g) == -1)
244     return -1;
245
246   return r;
247 }
248
249 static int
250 check_filesystem (guestfs_h *g, const char *device)
251 {
252   if (extend_fses (g) == -1)
253     return -1;
254
255   struct inspect_fs *fs = &g->fses[g->nr_fses-1];
256
257   fs->device = safe_strdup (g, device);
258   fs->is_mountable = 1;
259
260   /* Grub /boot? */
261   if (guestfs_is_file (g, "/grub/menu.lst") > 0 ||
262       guestfs_is_file (g, "/grub/grub.conf") > 0)
263     fs->content = FS_CONTENT_LINUX_BOOT;
264   /* Linux root? */
265   else if (guestfs_is_dir (g, "/etc") > 0 &&
266            guestfs_is_dir (g, "/bin") > 0 &&
267            guestfs_is_file (g, "/etc/fstab") > 0) {
268     fs->is_root = 1;
269     fs->content = FS_CONTENT_LINUX_ROOT;
270     if (check_linux_root (g, fs) == -1)
271       return -1;
272   }
273   /* Linux /usr/local? */
274   else if (guestfs_is_dir (g, "/etc") > 0 &&
275            guestfs_is_dir (g, "/bin") > 0 &&
276            guestfs_is_dir (g, "/share") > 0 &&
277            guestfs_exists (g, "/local") == 0 &&
278            guestfs_is_file (g, "/etc/fstab") == 0)
279     fs->content = FS_CONTENT_LINUX_USR_LOCAL;
280   /* Linux /usr? */
281   else if (guestfs_is_dir (g, "/etc") > 0 &&
282            guestfs_is_dir (g, "/bin") > 0 &&
283            guestfs_is_dir (g, "/share") > 0 &&
284            guestfs_exists (g, "/local") > 0 &&
285            guestfs_is_file (g, "/etc/fstab") == 0)
286     fs->content = FS_CONTENT_LINUX_USR;
287   /* Linux /var? */
288   else if (guestfs_is_dir (g, "/log") > 0 &&
289            guestfs_is_dir (g, "/run") > 0 &&
290            guestfs_is_dir (g, "/spool") > 0)
291     fs->content = FS_CONTENT_LINUX_VAR;
292   /* Windows root? */
293   else if (guestfs_is_file (g, "/AUTOEXEC.BAT") > 0 ||
294            guestfs_is_file (g, "/autoexec.bat") > 0 ||
295            guestfs_is_dir (g, "/Program Files") > 0 ||
296            guestfs_is_dir (g, "/WINDOWS") > 0 ||
297            guestfs_is_dir (g, "/Windows") > 0 ||
298            guestfs_is_dir (g, "/windows") > 0 ||
299            guestfs_is_dir (g, "/WIN32") > 0 ||
300            guestfs_is_dir (g, "/Win32") > 0 ||
301            guestfs_is_dir (g, "/WINNT") > 0 ||
302            guestfs_is_file (g, "/boot.ini") > 0 ||
303            guestfs_is_file (g, "/ntldr") > 0) {
304     fs->is_root = 1;
305     fs->content = FS_CONTENT_WINDOWS_ROOT;
306     if (check_windows_root (g, fs) == -1)
307       return -1;
308   }
309
310   return 0;
311 }
312
313 /* Set fs->product_name to the first line of the release file. */
314 static int
315 parse_release_file (guestfs_h *g, struct inspect_fs *fs,
316                     const char *release_filename)
317 {
318   char **product_name = guestfs_head_n (g, 1, release_filename);
319   if (product_name == NULL)
320     return -1;
321   if (product_name[0] == NULL) {
322     error (g, "%s: file is empty", release_filename);
323     guestfs___free_string_list (product_name);
324     return -1;
325   }
326
327   /* Note that this string becomes owned by the handle and will
328    * be freed by guestfs___free_inspect_info.
329    */
330   fs->product_name = product_name[0];
331   free (product_name);
332
333   return 0;
334 }
335
336 /* Parse generic MAJOR.MINOR from the fs->product_name string. */
337 static int
338 parse_major_minor (guestfs_h *g, struct inspect_fs *fs)
339 {
340   char *major, *minor;
341
342   if (match2 (g, fs->product_name, re_major_minor, &major, &minor)) {
343     fs->major_version = parse_unsigned_int (g, major);
344     free (major);
345     if (fs->major_version == -1) {
346       free (minor);
347       return -1;
348     }
349     fs->minor_version = parse_unsigned_int (g, minor);
350     free (minor);
351     if (fs->minor_version == -1)
352       return -1;
353   }
354   return 0;
355 }
356
357 /* Ubuntu has /etc/lsb-release containing:
358  *   DISTRIB_ID=Ubuntu                                # Distro
359  *   DISTRIB_RELEASE=10.04                            # Version
360  *   DISTRIB_CODENAME=lucid
361  *   DISTRIB_DESCRIPTION="Ubuntu 10.04.1 LTS"         # Product name
362  * In theory other distros could have this LSB file, but none do.
363  */
364 static int
365 parse_lsb_release (guestfs_h *g, struct inspect_fs *fs)
366 {
367   char **lines;
368   size_t i;
369   int r = 0;
370
371   lines = guestfs_head_n (g, 10, "/etc/lsb-release");
372   if (lines == NULL)
373     return -1;
374
375   for (i = 0; lines[i] != NULL; ++i) {
376     if (fs->distro == 0 &&
377         STREQ (lines[i], "DISTRIB_ID=Ubuntu")) {
378       fs->distro = OS_DISTRO_UBUNTU;
379       r = 1;
380     }
381     else if (STRPREFIX (lines[i], "DISTRIB_RELEASE=")) {
382       char *major, *minor;
383       if (match2 (g, &lines[i][16], re_major_minor, &major, &minor)) {
384         fs->major_version = parse_unsigned_int (g, major);
385         free (major);
386         if (fs->major_version == -1) {
387           free (minor);
388           guestfs___free_string_list (lines);
389           return -1;
390         }
391         fs->minor_version = parse_unsigned_int (g, minor);
392         free (minor);
393         if (fs->minor_version == -1) {
394           guestfs___free_string_list (lines);
395           return -1;
396         }
397       }
398     }
399     else if (fs->product_name == NULL &&
400              (STRPREFIX (lines[i], "DISTRIB_DESCRIPTION=\"") ||
401               STRPREFIX (lines[i], "DISTRIB_DESCRIPTION='"))) {
402       size_t len = strlen (lines[i]) - 21 - 1;
403       fs->product_name = safe_strndup (g, &lines[i][21], len);
404       r = 1;
405     }
406     else if (fs->product_name == NULL &&
407              STRPREFIX (lines[i], "DISTRIB_DESCRIPTION=")) {
408       size_t len = strlen (lines[i]) - 20;
409       fs->product_name = safe_strndup (g, &lines[i][20], len);
410       r = 1;
411     }
412   }
413
414   guestfs___free_string_list (lines);
415   return r;
416 }
417
418 /* The currently mounted device is known to be a Linux root.  Try to
419  * determine from this the distro, version, etc.  Also parse
420  * /etc/fstab to determine the arrangement of mountpoints and
421  * associated devices.
422  */
423 static int
424 check_linux_root (guestfs_h *g, struct inspect_fs *fs)
425 {
426   int r;
427
428   fs->type = OS_TYPE_LINUX;
429
430   if (guestfs_exists (g, "/etc/lsb-release") > 0) {
431     r = parse_lsb_release (g, fs);
432     if (r == -1)        /* error */
433       return -1;
434     if (r == 1)         /* ok - detected the release from this file */
435       goto skip_release_checks;
436   }
437
438   if (guestfs_exists (g, "/etc/redhat-release") > 0) {
439     fs->distro = OS_DISTRO_REDHAT_BASED; /* Something generic Red Hat-like. */
440
441     if (parse_release_file (g, fs, "/etc/redhat-release") == -1)
442       return -1;
443
444     char *major, *minor;
445     if ((major = match1 (g, fs->product_name, re_fedora)) != NULL) {
446       fs->distro = OS_DISTRO_FEDORA;
447       fs->major_version = parse_unsigned_int (g, major);
448       free (major);
449       if (fs->major_version == -1)
450         return -1;
451     }
452     else if (match2 (g, fs->product_name, re_rhel_old, &major, &minor) ||
453              match2 (g, fs->product_name, re_rhel, &major, &minor)) {
454       fs->distro = OS_DISTRO_RHEL;
455       fs->major_version = parse_unsigned_int (g, major);
456       free (major);
457       if (fs->major_version == -1) {
458         free (minor);
459         return -1;
460       }
461       fs->minor_version = parse_unsigned_int (g, minor);
462       free (minor);
463       if (fs->minor_version == -1)
464         return -1;
465     }
466     else if ((major = match1 (g, fs->product_name, re_rhel_no_minor)) != NULL) {
467       fs->distro = OS_DISTRO_RHEL;
468       fs->major_version = parse_unsigned_int (g, major);
469       free (major);
470       if (fs->major_version == -1)
471         return -1;
472       fs->minor_version = 0;
473     }
474   }
475   else if (guestfs_exists (g, "/etc/debian_version") > 0) {
476     fs->distro = OS_DISTRO_DEBIAN;
477
478     if (parse_release_file (g, fs, "/etc/debian_version") == -1)
479       return -1;
480
481     if (parse_major_minor (g, fs) == -1)
482       return -1;
483   }
484   else if (guestfs_exists (g, "/etc/pardus-release") > 0) {
485     fs->distro = OS_DISTRO_PARDUS;
486
487     if (parse_release_file (g, fs, "/etc/pardus-release") == -1)
488       return -1;
489
490     if (parse_major_minor (g, fs) == -1)
491       return -1;
492   }
493   else if (guestfs_exists (g, "/etc/arch-release") > 0) {
494     fs->distro = OS_DISTRO_ARCHLINUX;
495
496     /* /etc/arch-release file is empty and I can't see a way to
497      * determine the actual release or product string.
498      */
499   }
500   else if (guestfs_exists (g, "/etc/gentoo-release") > 0) {
501     fs->distro = OS_DISTRO_GENTOO;
502
503     if (parse_release_file (g, fs, "/etc/gentoo-release") == -1)
504       return -1;
505
506     if (parse_major_minor (g, fs) == -1)
507       return -1;
508   }
509   else if (guestfs_exists (g, "/etc/meego-release") > 0) {
510     fs->distro = OS_DISTRO_MEEGO;
511
512     if (parse_release_file (g, fs, "/etc/meego-release") == -1)
513       return -1;
514
515     if (parse_major_minor (g, fs) == -1)
516       return -1;
517   }
518
519  skip_release_checks:;
520
521   /* If distro test above was successful, work out the package format. */
522   check_package_format (g, fs);
523   check_package_management (g, fs);
524
525   /* Determine the architecture. */
526   const char *binaries[] =
527     { "/bin/bash", "/bin/ls", "/bin/echo", "/bin/rm", "/bin/sh" };
528   size_t i;
529   for (i = 0; i < sizeof binaries / sizeof binaries[0]; ++i) {
530     if (guestfs_is_file (g, binaries[i]) > 0) {
531       /* Ignore errors from file_architecture call. */
532       guestfs_error_handler_cb old_error_cb = g->error_cb;
533       g->error_cb = NULL;
534       char *arch = guestfs_file_architecture (g, binaries[i]);
535       g->error_cb = old_error_cb;
536
537       if (arch) {
538         /* String will be owned by handle, freed by
539          * guestfs___free_inspect_info.
540          */
541         fs->arch = arch;
542         break;
543       }
544     }
545   }
546
547   /* We already know /etc/fstab exists because it's part of the test
548    * for Linux root above.  We must now parse this file to determine
549    * which filesystems are used by the operating system and how they
550    * are mounted.
551    * XXX What if !feature_available (g, "augeas")?
552    */
553   if (guestfs_aug_init (g, "/", 16|32) == -1)
554     return -1;
555
556   /* Tell Augeas to only load /etc/fstab (thanks Raphaël Pinson). */
557   guestfs_aug_rm (g, "/augeas/load//incl[. != \"/etc/fstab\"]");
558   guestfs_aug_load (g);
559
560   r = check_fstab (g, fs);
561   guestfs_aug_close (g);
562   if (r == -1)
563     return -1;
564
565   return 0;
566 }
567
568 static int
569 check_fstab (guestfs_h *g, struct inspect_fs *fs)
570 {
571   char **lines = guestfs_aug_ls (g, "/files/etc/fstab");
572   if (lines == NULL)
573     return -1;
574
575   if (lines[0] == NULL) {
576     error (g, "could not parse /etc/fstab or empty file");
577     guestfs___free_string_list (lines);
578     return -1;
579   }
580
581   size_t i;
582   char augpath[256];
583   for (i = 0; lines[i] != NULL; ++i) {
584     /* Ignore comments.  Only care about sequence lines which
585      * match m{/\d+$}.
586      */
587     if (match (g, lines[i], re_aug_seq)) {
588       snprintf (augpath, sizeof augpath, "%s/spec", lines[i]);
589       char *spec = guestfs_aug_get (g, augpath);
590       if (spec == NULL) {
591         guestfs___free_string_list (lines);
592         return -1;
593       }
594
595       snprintf (augpath, sizeof augpath, "%s/file", lines[i]);
596       char *mp = guestfs_aug_get (g, augpath);
597       if (mp == NULL) {
598         guestfs___free_string_list (lines);
599         free (spec);
600         return -1;
601       }
602
603       int r = add_fstab_entry (g, fs, spec, mp);
604       free (spec);
605       free (mp);
606
607       if (r == -1) {
608         guestfs___free_string_list (lines);
609         return -1;
610       }
611     }
612   }
613
614   guestfs___free_string_list (lines);
615   return 0;
616 }
617
618 /* Add a filesystem and possibly a mountpoint entry for
619  * the root filesystem 'fs'.
620  *
621  * 'spec' is the fstab spec field, which might be a device name or a
622  * pseudodevice or 'UUID=...' or 'LABEL=...'.
623  *
624  * 'mp' is the mount point, which could also be 'swap' or 'none'.
625  */
626 static int
627 add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
628                  const char *spec, const char *mp)
629 {
630   /* Ignore certain mountpoints. */
631   if (STRPREFIX (mp, "/dev/") ||
632       STREQ (mp, "/dev") ||
633       STRPREFIX (mp, "/media/") ||
634       STRPREFIX (mp, "/proc/") ||
635       STREQ (mp, "/proc") ||
636       STRPREFIX (mp, "/selinux/") ||
637       STREQ (mp, "/selinux") ||
638       STRPREFIX (mp, "/sys/") ||
639       STREQ (mp, "/sys"))
640     return 0;
641
642   /* Ignore /dev/fd (floppy disks) (RHBZ#642929) and CD-ROM drives. */
643   if ((STRPREFIX (spec, "/dev/fd") && c_isdigit (spec[7])) ||
644       STREQ (spec, "/dev/floppy") ||
645       STREQ (spec, "/dev/cdrom"))
646     return 0;
647
648   /* Resolve UUID= and LABEL= to the actual device. */
649   char *device = NULL;
650   if (STRPREFIX (spec, "UUID="))
651     device = guestfs_findfs_uuid (g, &spec[5]);
652   else if (STRPREFIX (spec, "LABEL="))
653     device = guestfs_findfs_label (g, &spec[6]);
654   /* Ignore "/.swap" (Pardus) and pseudo-devices like "tmpfs". */
655   else if (STRPREFIX (spec, "/dev/"))
656     /* Resolve guest block device names. */
657     device = resolve_fstab_device (g, spec);
658
659   /* If we haven't resolved the device successfully by this point,
660    * we don't care, just ignore it.
661    */
662   if (device == NULL)
663     return 0;
664
665   char *mountpoint = safe_strdup (g, mp);
666
667   /* Add this to the fstab entry in 'fs'.
668    * Note these are further filtered by guestfs_inspect_get_mountpoints
669    * and guestfs_inspect_get_filesystems.
670    */
671   size_t n = fs->nr_fstab + 1;
672   struct inspect_fstab_entry *p;
673
674   p = realloc (fs->fstab, n * sizeof (struct inspect_fstab_entry));
675   if (p == NULL) {
676     perrorf (g, "realloc");
677     free (device);
678     free (mountpoint);
679     return -1;
680   }
681
682   fs->fstab = p;
683   fs->nr_fstab = n;
684
685   /* These are owned by the handle and freed by guestfs___free_inspect_info. */
686   fs->fstab[n-1].device = device;
687   fs->fstab[n-1].mountpoint = mountpoint;
688
689   if (g->verbose)
690     fprintf (stderr, "fstab: device=%s mountpoint=%s\n", device, mountpoint);
691
692   return 0;
693 }
694
695 /* Resolve block device name to the libguestfs device name, eg.
696  * /dev/xvdb1 => /dev/vdb1; and /dev/mapper/VG-LV => /dev/VG/LV.  This
697  * assumes that disks were added in the same order as they appear to
698  * the real VM, which is a reasonable assumption to make.  Return
699  * anything we don't recognize unchanged.
700  */
701 static char *
702 resolve_fstab_device (guestfs_h *g, const char *spec)
703 {
704   char *a1;
705   char *device = NULL;
706
707   if (STRPREFIX (spec, "/dev/mapper/")) {
708     /* LVM2 does some strange munging on /dev/mapper paths for VGs and
709      * LVs which contain '-' character:
710      *
711      * ><fs> lvcreate LV--test VG--test 32
712      * ><fs> debug ls /dev/mapper
713      * VG----test-LV----test
714      *
715      * This makes it impossible to reverse those paths directly, so
716      * we have implemented lvm_canonical_lv_name in the daemon.
717      */
718     device = guestfs_lvm_canonical_lv_name (g, spec);
719   }
720   else if ((a1 = match1 (g, spec, re_xdev)) != NULL) {
721     char **devices = guestfs_list_devices (g);
722     if (devices == NULL)
723       return NULL;
724
725     size_t count;
726     for (count = 0; devices[count] != NULL; count++)
727       ;
728
729     size_t i = a1[0] - 'a'; /* a1[0] is always [a-z] because of regex. */
730     if (i < count) {
731       size_t len = strlen (devices[i]) + strlen (a1) + 16;
732       device = safe_malloc (g, len);
733       snprintf (device, len, "%s%s", devices[i], &a1[1]);
734     }
735
736     free (a1);
737     guestfs___free_string_list (devices);
738   }
739   else {
740     /* Didn't match device pattern, return original spec unchanged. */
741     device = safe_strdup (g, spec);
742   }
743
744   return device;
745 }
746
747 /* XXX Handling of boot.ini in the Perl version was pretty broken.  It
748  * essentially didn't do anything for modern Windows guests.
749  * Therefore I've omitted all that code.
750  */
751 static int
752 check_windows_root (guestfs_h *g, struct inspect_fs *fs)
753 {
754   fs->type = OS_TYPE_WINDOWS;
755   fs->distro = OS_DISTRO_WINDOWS;
756
757   /* Try to find Windows systemroot using some common locations. */
758   const char *systemroots[] =
759     { "/windows", "/winnt", "/win32", "/win" };
760   size_t i;
761   char *systemroot = NULL;
762   for (i = 0;
763        systemroot == NULL && i < sizeof systemroots / sizeof systemroots[0];
764        ++i) {
765     systemroot = resolve_windows_path_silently (g, systemroots[i]);
766   }
767
768   if (!systemroot) {
769     error (g, _("cannot resolve Windows %%SYSTEMROOT%%"));
770     return -1;
771   }
772
773   if (g->verbose)
774     fprintf (stderr, "windows %%SYSTEMROOT%% = %s", systemroot);
775
776   /* Freed by guestfs___free_inspect_info. */
777   fs->windows_systemroot = systemroot;
778
779   if (check_windows_arch (g, fs) == -1)
780     return -1;
781
782   if (check_windows_registry (g, fs) == -1)
783     return -1;
784
785   check_package_format (g, fs);
786   check_package_management (g, fs);
787
788   return 0;
789 }
790
791 static int
792 check_windows_arch (guestfs_h *g, struct inspect_fs *fs)
793 {
794   size_t len = strlen (fs->windows_systemroot) + 32;
795   char cmd_exe[len];
796   snprintf (cmd_exe, len, "%s/system32/cmd.exe", fs->windows_systemroot);
797
798   char *cmd_exe_path = resolve_windows_path_silently (g, cmd_exe);
799   if (!cmd_exe_path)
800     return 0;
801
802   char *arch = guestfs_file_architecture (g, cmd_exe_path);
803   free (cmd_exe_path);
804
805   if (arch)
806     fs->arch = arch;        /* freed by guestfs___free_inspect_info */
807
808   return 0;
809 }
810
811 /* At the moment, pull just the ProductName and version numbers from
812  * the registry.  In future there is a case for making many more
813  * registry fields available to callers.
814  */
815 static int
816 check_windows_registry (guestfs_h *g, struct inspect_fs *fs)
817 {
818   TMP_TEMPLATE_ON_STACK (dir);
819 #define dir_len (strlen (dir))
820 #define software_hive_len (dir_len + 16)
821   char software_hive[software_hive_len];
822 #define cmd_len (dir_len + 16)
823   char cmd[cmd_len];
824
825   size_t len = strlen (fs->windows_systemroot) + 64;
826   char software[len];
827   snprintf (software, len, "%s/system32/config/software",
828             fs->windows_systemroot);
829
830   char *software_path = resolve_windows_path_silently (g, software);
831   if (!software_path)
832     /* If the software hive doesn't exist, just accept that we cannot
833      * find product_name etc.
834      */
835     return 0;
836
837   int ret = -1;
838   hive_h *h = NULL;
839   hive_value_h *values = NULL;
840
841   if (mkdtemp (dir) == NULL) {
842     perrorf (g, "mkdtemp");
843     goto out;
844   }
845
846   snprintf (software_hive, software_hive_len, "%s/software", dir);
847
848   if (guestfs_download (g, software_path, software_hive) == -1)
849     goto out;
850
851   h = hivex_open (software_hive, g->verbose ? HIVEX_OPEN_VERBOSE : 0);
852   if (h == NULL) {
853     perrorf (g, "hivex_open");
854     goto out;
855   }
856
857   hive_node_h node = hivex_root (h);
858   const char *hivepath[] =
859     { "Microsoft", "Windows NT", "CurrentVersion" };
860   size_t i;
861   for (i = 0;
862        node != 0 && i < sizeof hivepath / sizeof hivepath[0];
863        ++i) {
864     node = hivex_node_get_child (h, node, hivepath[i]);
865   }
866
867   if (node == 0) {
868     perrorf (g, "hivex: cannot locate HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");
869     goto out;
870   }
871
872   values = hivex_node_values (h, node);
873
874   for (i = 0; values[i] != 0; ++i) {
875     char *key = hivex_value_key (h, values[i]);
876     if (key == NULL) {
877       perrorf (g, "hivex_value_key");
878       goto out;
879     }
880
881     if (STRCASEEQ (key, "ProductName")) {
882       fs->product_name = hivex_value_string (h, values[i]);
883       if (!fs->product_name) {
884         perrorf (g, "hivex_value_string");
885         free (key);
886         goto out;
887       }
888     }
889     else if (STRCASEEQ (key, "CurrentVersion")) {
890       char *version = hivex_value_string (h, values[i]);
891       if (!version) {
892         perrorf (g, "hivex_value_string");
893         free (key);
894         goto out;
895       }
896       char *major, *minor;
897       if (match2 (g, version, re_windows_version, &major, &minor)) {
898         fs->major_version = parse_unsigned_int (g, major);
899         free (major);
900         if (fs->major_version == -1) {
901           free (minor);
902           free (key);
903           free (version);
904           goto out;
905         }
906         fs->minor_version = parse_unsigned_int (g, minor);
907         free (minor);
908         if (fs->minor_version == -1) {
909           free (key);
910           free (version);
911           return -1;
912         }
913       }
914
915       free (version);
916     }
917
918     free (key);
919   }
920
921   ret = 0;
922
923  out:
924   if (h) hivex_close (h);
925   free (values);
926   free (software_path);
927
928   /* Free up the temporary directory.  Note the directory name cannot
929    * contain shell meta-characters because of the way it was
930    * constructed above.
931    */
932   snprintf (cmd, cmd_len, "rm -rf %s", dir);
933   ignore_value (system (cmd));
934 #undef dir_len
935 #undef software_hive_len
936 #undef cmd_len
937
938   return ret;
939 }
940
941 static char *
942 resolve_windows_path_silently (guestfs_h *g, const char *path)
943 {
944   guestfs_error_handler_cb old_error_cb = g->error_cb;
945   g->error_cb = NULL;
946   char *ret = guestfs_case_sensitive_path (g, path);
947   g->error_cb = old_error_cb;
948   return ret;
949 }
950
951 static int
952 extend_fses (guestfs_h *g)
953 {
954   size_t n = g->nr_fses + 1;
955   struct inspect_fs *p;
956
957   p = realloc (g->fses, n * sizeof (struct inspect_fs));
958   if (p == NULL) {
959     perrorf (g, "realloc");
960     return -1;
961   }
962
963   g->fses = p;
964   g->nr_fses = n;
965
966   memset (&g->fses[n-1], 0, sizeof (struct inspect_fs));
967
968   return 0;
969 }
970
971 /* Parse small, unsigned ints, as used in version numbers. */
972 static int
973 parse_unsigned_int (guestfs_h *g, const char *str)
974 {
975   long ret;
976   int r = xstrtol (str, NULL, 10, &ret, "");
977   if (r != LONGINT_OK) {
978     error (g, "could not parse integer in version number: %s", str);
979     return -1;
980   }
981   return ret;
982 }
983
984 /* At the moment, package format and package management is just a
985  * simple function of the distro and major_version fields, so these
986  * can never return an error.  We might be cleverer in future.
987  */
988 static void
989 check_package_format (guestfs_h *g, struct inspect_fs *fs)
990 {
991   switch (fs->distro) {
992   case OS_DISTRO_FEDORA:
993   case OS_DISTRO_MEEGO:
994   case OS_DISTRO_REDHAT_BASED:
995   case OS_DISTRO_RHEL:
996     fs->package_format = OS_PACKAGE_FORMAT_RPM;
997     break;
998
999   case OS_DISTRO_DEBIAN:
1000   case OS_DISTRO_UBUNTU:
1001     fs->package_format = OS_PACKAGE_FORMAT_DEB;
1002     break;
1003
1004   case OS_DISTRO_ARCHLINUX:
1005     fs->package_format = OS_PACKAGE_FORMAT_PACMAN;
1006     break;
1007   case OS_DISTRO_GENTOO:
1008     fs->package_format = OS_PACKAGE_FORMAT_EBUILD;
1009     break;
1010   case OS_DISTRO_PARDUS:
1011     fs->package_format = OS_PACKAGE_FORMAT_PISI;
1012     break;
1013
1014   case OS_DISTRO_WINDOWS:
1015   case OS_DISTRO_UNKNOWN:
1016   default:
1017     fs->package_format = OS_PACKAGE_FORMAT_UNKNOWN;
1018     break;
1019   }
1020 }
1021
1022 static void
1023 check_package_management (guestfs_h *g, struct inspect_fs *fs)
1024 {
1025   switch (fs->distro) {
1026   case OS_DISTRO_FEDORA:
1027   case OS_DISTRO_MEEGO:
1028     fs->package_management = OS_PACKAGE_MANAGEMENT_YUM;
1029     break;
1030
1031   case OS_DISTRO_REDHAT_BASED:
1032   case OS_DISTRO_RHEL:
1033     if (fs->major_version >= 5)
1034       fs->package_management = OS_PACKAGE_MANAGEMENT_YUM;
1035     else
1036       fs->package_management = OS_PACKAGE_MANAGEMENT_UP2DATE;
1037     break;
1038
1039   case OS_DISTRO_DEBIAN:
1040   case OS_DISTRO_UBUNTU:
1041     fs->package_management = OS_PACKAGE_MANAGEMENT_APT;
1042     break;
1043
1044   case OS_DISTRO_ARCHLINUX:
1045     fs->package_management = OS_PACKAGE_MANAGEMENT_PACMAN;
1046     break;
1047   case OS_DISTRO_GENTOO:
1048     fs->package_management = OS_PACKAGE_MANAGEMENT_PORTAGE;
1049     break;
1050   case OS_DISTRO_PARDUS:
1051     fs->package_management = OS_PACKAGE_MANAGEMENT_PISI;
1052     break;
1053
1054   case OS_DISTRO_WINDOWS:
1055   case OS_DISTRO_UNKNOWN:
1056   default:
1057     fs->package_management = OS_PACKAGE_MANAGEMENT_UNKNOWN;
1058     break;
1059   }
1060 }
1061
1062 static struct inspect_fs *
1063 search_for_root (guestfs_h *g, const char *root)
1064 {
1065   if (g->nr_fses == 0) {
1066     error (g, _("no inspection data: call guestfs_inspect_os first"));
1067     return NULL;
1068   }
1069
1070   size_t i;
1071   struct inspect_fs *fs;
1072   for (i = 0; i < g->nr_fses; ++i) {
1073     fs = &g->fses[i];
1074     if (fs->is_root && STREQ (root, fs->device))
1075       return fs;
1076   }
1077
1078   error (g, _("%s: root device not found: only call this function with a root device previously returned by guestfs_inspect_os"),
1079          root);
1080   return NULL;
1081 }
1082
1083 char **
1084 guestfs__inspect_get_roots (guestfs_h *g)
1085 {
1086   /* NB. Doesn't matter if g->nr_fses == 0.  We just return an empty
1087    * list in this case.
1088    */
1089
1090   size_t i;
1091   size_t count = 0;
1092   for (i = 0; i < g->nr_fses; ++i)
1093     if (g->fses[i].is_root)
1094       count++;
1095
1096   char **ret = calloc (count+1, sizeof (char *));
1097   if (ret == NULL) {
1098     perrorf (g, "calloc");
1099     return NULL;
1100   }
1101
1102   count = 0;
1103   for (i = 0; i < g->nr_fses; ++i) {
1104     if (g->fses[i].is_root) {
1105       ret[count] = safe_strdup (g, g->fses[i].device);
1106       count++;
1107     }
1108   }
1109   ret[count] = NULL;
1110
1111   return ret;
1112 }
1113
1114 char *
1115 guestfs__inspect_get_type (guestfs_h *g, const char *root)
1116 {
1117   struct inspect_fs *fs = search_for_root (g, root);
1118   if (!fs)
1119     return NULL;
1120
1121   char *ret;
1122   switch (fs->type) {
1123   case OS_TYPE_LINUX: ret = safe_strdup (g, "linux"); break;
1124   case OS_TYPE_WINDOWS: ret = safe_strdup (g, "windows"); break;
1125   case OS_TYPE_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
1126   }
1127
1128   return ret;
1129 }
1130
1131 char *
1132 guestfs__inspect_get_arch (guestfs_h *g, const char *root)
1133 {
1134   struct inspect_fs *fs = search_for_root (g, root);
1135   if (!fs)
1136     return NULL;
1137
1138   return safe_strdup (g, fs->arch ? : "unknown");
1139 }
1140
1141 char *
1142 guestfs__inspect_get_distro (guestfs_h *g, const char *root)
1143 {
1144   struct inspect_fs *fs = search_for_root (g, root);
1145   if (!fs)
1146     return NULL;
1147
1148   char *ret;
1149   switch (fs->distro) {
1150   case OS_DISTRO_ARCHLINUX: ret = safe_strdup (g, "archlinux"); break;
1151   case OS_DISTRO_DEBIAN: ret = safe_strdup (g, "debian"); break;
1152   case OS_DISTRO_FEDORA: ret = safe_strdup (g, "fedora"); break;
1153   case OS_DISTRO_GENTOO: ret = safe_strdup (g, "gentoo"); break;
1154   case OS_DISTRO_MEEGO: ret = safe_strdup (g, "meego"); break;
1155   case OS_DISTRO_PARDUS: ret = safe_strdup (g, "pardus"); break;
1156   case OS_DISTRO_REDHAT_BASED: ret = safe_strdup (g, "redhat-based"); break;
1157   case OS_DISTRO_RHEL: ret = safe_strdup (g, "rhel"); break;
1158   case OS_DISTRO_WINDOWS: ret = safe_strdup (g, "windows"); break;
1159   case OS_DISTRO_UBUNTU: ret = safe_strdup (g, "ubuntu"); break;
1160   case OS_DISTRO_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
1161   }
1162
1163   return ret;
1164 }
1165
1166 int
1167 guestfs__inspect_get_major_version (guestfs_h *g, const char *root)
1168 {
1169   struct inspect_fs *fs = search_for_root (g, root);
1170   if (!fs)
1171     return -1;
1172
1173   return fs->major_version;
1174 }
1175
1176 int
1177 guestfs__inspect_get_minor_version (guestfs_h *g, const char *root)
1178 {
1179   struct inspect_fs *fs = search_for_root (g, root);
1180   if (!fs)
1181     return -1;
1182
1183   return fs->minor_version;
1184 }
1185
1186 char *
1187 guestfs__inspect_get_product_name (guestfs_h *g, const char *root)
1188 {
1189   struct inspect_fs *fs = search_for_root (g, root);
1190   if (!fs)
1191     return NULL;
1192
1193   return safe_strdup (g, fs->product_name ? : "unknown");
1194 }
1195
1196 char *
1197 guestfs__inspect_get_windows_systemroot (guestfs_h *g, const char *root)
1198 {
1199   struct inspect_fs *fs = search_for_root (g, root);
1200   if (!fs)
1201     return NULL;
1202
1203   if (!fs->windows_systemroot) {
1204     error (g, _("not a Windows guest, or systemroot could not be determined"));
1205     return NULL;
1206   }
1207
1208   return safe_strdup (g, fs->windows_systemroot);
1209 }
1210
1211 char **
1212 guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root)
1213 {
1214   struct inspect_fs *fs = search_for_root (g, root);
1215   if (!fs)
1216     return NULL;
1217
1218   char **ret;
1219
1220   /* If no fstab information (Windows) return just the root. */
1221   if (fs->nr_fstab == 0) {
1222     ret = calloc (3, sizeof (char *));
1223     ret[0] = safe_strdup (g, "/");
1224     ret[1] = safe_strdup (g, root);
1225     ret[2] = NULL;
1226     return ret;
1227   }
1228
1229 #define CRITERION fs->fstab[i].mountpoint[0] == '/'
1230   size_t i, count = 0;
1231   for (i = 0; i < fs->nr_fstab; ++i)
1232     if (CRITERION)
1233       count++;
1234
1235   /* Hashtables have 2N+1 entries. */
1236   ret = calloc (2*count+1, sizeof (char *));
1237   if (ret == NULL) {
1238     perrorf (g, "calloc");
1239     return NULL;
1240   }
1241
1242   count = 0;
1243   for (i = 0; i < fs->nr_fstab; ++i)
1244     if (CRITERION) {
1245       ret[2*count] = safe_strdup (g, fs->fstab[i].mountpoint);
1246       ret[2*count+1] = safe_strdup (g, fs->fstab[i].device);
1247       count++;
1248     }
1249 #undef CRITERION
1250
1251   return ret;
1252 }
1253
1254 char **
1255 guestfs__inspect_get_filesystems (guestfs_h *g, const char *root)
1256 {
1257   struct inspect_fs *fs = search_for_root (g, root);
1258   if (!fs)
1259     return NULL;
1260
1261   char **ret;
1262
1263   /* If no fstab information (Windows) return just the root. */
1264   if (fs->nr_fstab == 0) {
1265     ret = calloc (2, sizeof (char *));
1266     ret[0] = safe_strdup (g, root);
1267     ret[1] = NULL;
1268     return ret;
1269   }
1270
1271   ret = calloc (fs->nr_fstab + 1, sizeof (char *));
1272   if (ret == NULL) {
1273     perrorf (g, "calloc");
1274     return NULL;
1275   }
1276
1277   size_t i;
1278   for (i = 0; i < fs->nr_fstab; ++i)
1279     ret[i] = safe_strdup (g, fs->fstab[i].device);
1280
1281   return ret;
1282 }
1283
1284 char *
1285 guestfs__inspect_get_package_format (guestfs_h *g, const char *root)
1286 {
1287   struct inspect_fs *fs = search_for_root (g, root);
1288   if (!fs)
1289     return NULL;
1290
1291   char *ret;
1292   switch (fs->package_format) {
1293   case OS_PACKAGE_FORMAT_RPM: ret = safe_strdup (g, "rpm"); break;
1294   case OS_PACKAGE_FORMAT_DEB: ret = safe_strdup (g, "deb"); break;
1295   case OS_PACKAGE_FORMAT_PACMAN: ret = safe_strdup (g, "pacman"); break;
1296   case OS_PACKAGE_FORMAT_EBUILD: ret = safe_strdup (g, "ebuild"); break;
1297   case OS_PACKAGE_FORMAT_PISI: ret = safe_strdup (g, "pisi"); break;
1298   case OS_PACKAGE_FORMAT_UNKNOWN:
1299   default:
1300     ret = safe_strdup (g, "unknown");
1301     break;
1302   }
1303
1304   return ret;
1305 }
1306
1307 char *
1308 guestfs__inspect_get_package_management (guestfs_h *g, const char *root)
1309 {
1310   struct inspect_fs *fs = search_for_root (g, root);
1311   if (!fs)
1312     return NULL;
1313
1314   char *ret;
1315   switch (fs->package_management) {
1316   case OS_PACKAGE_MANAGEMENT_YUM: ret = safe_strdup (g, "yum"); break;
1317   case OS_PACKAGE_MANAGEMENT_UP2DATE: ret = safe_strdup (g, "up2date"); break;
1318   case OS_PACKAGE_MANAGEMENT_APT: ret = safe_strdup (g, "apt"); break;
1319   case OS_PACKAGE_MANAGEMENT_PACMAN: ret = safe_strdup (g, "pacman"); break;
1320   case OS_PACKAGE_MANAGEMENT_PORTAGE: ret = safe_strdup (g, "portage"); break;
1321   case OS_PACKAGE_MANAGEMENT_PISI: ret = safe_strdup (g, "pisi"); break;
1322   case OS_PACKAGE_MANAGEMENT_UNKNOWN:
1323   default:
1324     ret = safe_strdup (g, "unknown");
1325     break;
1326   }
1327
1328   return ret;
1329 }
1330
1331 #else /* no PCRE or hivex at compile time */
1332
1333 /* XXX These functions should be in an optgroup. */
1334
1335 #define NOT_IMPL(r)                                                     \
1336   error (g, _("inspection API not available since this version of libguestfs was compiled without PCRE or hivex libraries")); \
1337   return r
1338
1339 char **
1340 guestfs__inspect_os (guestfs_h *g)
1341 {
1342   NOT_IMPL(NULL);
1343 }
1344
1345 char **
1346 guestfs__inspect_get_roots (guestfs_h *g)
1347 {
1348   NOT_IMPL(NULL);
1349 }
1350
1351 char *
1352 guestfs__inspect_get_type (guestfs_h *g, const char *root)
1353 {
1354   NOT_IMPL(NULL);
1355 }
1356
1357 char *
1358 guestfs__inspect_get_arch (guestfs_h *g, const char *root)
1359 {
1360   NOT_IMPL(NULL);
1361 }
1362
1363 char *
1364 guestfs__inspect_get_distro (guestfs_h *g, const char *root)
1365 {
1366   NOT_IMPL(NULL);
1367 }
1368
1369 int
1370 guestfs__inspect_get_major_version (guestfs_h *g, const char *root)
1371 {
1372   NOT_IMPL(-1);
1373 }
1374
1375 int
1376 guestfs__inspect_get_minor_version (guestfs_h *g, const char *root)
1377 {
1378   NOT_IMPL(-1);
1379 }
1380
1381 char *
1382 guestfs__inspect_get_product_name (guestfs_h *g, const char *root)
1383 {
1384   NOT_IMPL(NULL);
1385 }
1386
1387 char *
1388 guestfs__inspect_get_windows_systemroot (guestfs_h *g, const char *root)
1389 {
1390   NOT_IMPL(NULL);
1391 }
1392
1393 char **
1394 guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root)
1395 {
1396   NOT_IMPL(NULL);
1397 }
1398
1399 char **
1400 guestfs__inspect_get_filesystems (guestfs_h *g, const char *root)
1401 {
1402   NOT_IMPL(NULL);
1403 }
1404
1405 char *
1406 guestfs__inspect_get_package_format (guestfs_h *g, const char *root)
1407 {
1408   NOT_IMPL(NULL);
1409 }
1410
1411 char *
1412 guestfs__inspect_get_package_management (guestfs_h *g, const char *root)
1413 {
1414   NOT_IMPL(NULL);
1415 }
1416
1417 #endif /* no PCRE or hivex at compile time */
1418
1419 void
1420 guestfs___free_inspect_info (guestfs_h *g)
1421 {
1422   size_t i;
1423   for (i = 0; i < g->nr_fses; ++i) {
1424     free (g->fses[i].device);
1425     free (g->fses[i].product_name);
1426     free (g->fses[i].arch);
1427     free (g->fses[i].windows_systemroot);
1428     size_t j;
1429     for (j = 0; j < g->fses[i].nr_fstab; ++j) {
1430       free (g->fses[i].fstab[j].device);
1431       free (g->fses[i].fstab[j].mountpoint);
1432     }
1433     free (g->fses[i].fstab);
1434   }
1435   free (g->fses);
1436   g->nr_fses = 0;
1437   g->fses = NULL;
1438 }
1439
1440 /* In the Perl code this is a public function. */
1441 int
1442 guestfs___feature_available (guestfs_h *g, const char *feature)
1443 {
1444   /* If there's an error we should ignore it, so to do that we have to
1445    * temporarily replace the error handler with a null one.
1446    */
1447   guestfs_error_handler_cb old_error_cb = g->error_cb;
1448   g->error_cb = NULL;
1449
1450   const char *groups[] = { feature, NULL };
1451   int r = guestfs_available (g, (char * const *) groups);
1452
1453   g->error_cb = old_error_cb;
1454
1455   return r == 0 ? 1 : 0;
1456 }
1457
1458 #ifdef HAVE_PCRE
1459
1460 /* Match a regular expression which contains no captures.  Returns
1461  * true if it matches or false if it doesn't.
1462  */
1463 int
1464 guestfs___match (guestfs_h *g, const char *str, const pcre *re)
1465 {
1466   size_t len = strlen (str);
1467   int vec[30], r;
1468
1469   r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
1470   if (r == PCRE_ERROR_NOMATCH)
1471     return 0;
1472   if (r != 1) {
1473     /* Internal error -- should not happen. */
1474     fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
1475              __FILE__, __func__, r, str);
1476     return 0;
1477   }
1478
1479   return 1;
1480 }
1481
1482 /* Match a regular expression which contains exactly one capture.  If
1483  * the string matches, return the capture, otherwise return NULL.  The
1484  * caller must free the result.
1485  */
1486 char *
1487 guestfs___match1 (guestfs_h *g, const char *str, const pcre *re)
1488 {
1489   size_t len = strlen (str);
1490   int vec[30], r;
1491
1492   r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
1493   if (r == PCRE_ERROR_NOMATCH)
1494     return NULL;
1495   if (r != 2) {
1496     /* Internal error -- should not happen. */
1497     fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
1498              __FILE__, __func__, r, str);
1499     return NULL;
1500   }
1501
1502   return safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
1503 }
1504
1505 /* Match a regular expression which contains exactly two captures. */
1506 int
1507 guestfs___match2 (guestfs_h *g, const char *str, const pcre *re,
1508                   char **ret1, char **ret2)
1509 {
1510   size_t len = strlen (str);
1511   int vec[30], r;
1512
1513   r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30);
1514   if (r == PCRE_ERROR_NOMATCH)
1515     return 0;
1516   if (r != 3) {
1517     /* Internal error -- should not happen. */
1518     fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
1519              __FILE__, __func__, r, str);
1520     return 0;
1521   }
1522
1523   *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
1524   *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]);
1525
1526   return 1;
1527 }
1528
1529 #endif /* HAVE_PCRE */