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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
|
/* libguestfs
* Copyright (C) 2010-2012 Red Hat Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <errno.h>
#ifdef HAVE_ENDIAN_H
#include <endian.h>
#endif
#include <pcre.h>
#ifdef HAVE_HIVEX
#include <hivex.h>
#endif
#include "c-ctype.h"
#include "ignore-value.h"
#include "xstrtol.h"
#include "guestfs.h"
#include "guestfs-internal.h"
#include "guestfs-internal-actions.h"
#include "guestfs_protocol.h"
#if defined(HAVE_HIVEX)
/* The main inspection code. */
char **
guestfs__inspect_os (guestfs_h *g)
{
/* Remove any information previously stored in the handle. */
guestfs___free_inspect_info (g);
if (guestfs_umount_all (g) == -1)
return NULL;
/* Iterate over all possible devices. Try to mount each
* (read-only). Examine ones which contain filesystems and add that
* information to the handle.
*/
/* Look to see if any devices directly contain filesystems (RHBZ#590167). */
char **devices = guestfs_list_devices (g);
if (devices == NULL)
return NULL;
size_t i;
for (i = 0; devices[i] != NULL; ++i) {
if (guestfs___check_for_filesystem_on (g, devices[i], 1, 0) == -1) {
guestfs___free_string_list (devices);
guestfs___free_inspect_info (g);
return NULL;
}
}
guestfs___free_string_list (devices);
/* Look at all partitions. */
char **partitions = guestfs_list_partitions (g);
if (partitions == NULL) {
guestfs___free_inspect_info (g);
return NULL;
}
for (i = 0; partitions[i] != NULL; ++i) {
if (guestfs___check_for_filesystem_on (g, partitions[i], 0, i+1) == -1) {
guestfs___free_string_list (partitions);
guestfs___free_inspect_info (g);
return NULL;
}
}
guestfs___free_string_list (partitions);
/* Look at MD devices. */
char **mds = guestfs_list_md_devices (g);
if (mds == NULL) {
guestfs___free_inspect_info (g);
return NULL;
}
for (i = 0; mds[i] != NULL; ++i) {
if (guestfs___check_for_filesystem_on (g, mds[i], 0, i+1) == -1) {
guestfs___free_string_list (mds);
guestfs___free_inspect_info (g);
return NULL;
}
}
guestfs___free_string_list (mds);
/* Look at all LVs. */
if (guestfs___feature_available (g, "lvm2")) {
char **lvs;
lvs = guestfs_lvs (g);
if (lvs == NULL) {
guestfs___free_inspect_info (g);
return NULL;
}
for (i = 0; lvs[i] != NULL; ++i) {
if (guestfs___check_for_filesystem_on (g, lvs[i], 0, 0) == -1) {
guestfs___free_string_list (lvs);
guestfs___free_inspect_info (g);
return NULL;
}
}
guestfs___free_string_list (lvs);
}
/* At this point we have, in the handle, a list of all filesystems
* found and data about each one. Now we assemble the list of
* filesystems which are root devices and return that to the user.
* Fall through to guestfs__inspect_get_roots to do that.
*/
char **ret = guestfs__inspect_get_roots (g);
if (ret == NULL)
guestfs___free_inspect_info (g);
return ret;
}
static int
compare_strings (const void *vp1, const void *vp2)
{
const char *s1 = * (char * const *) vp1;
const char *s2 = * (char * const *) vp2;
return strcmp (s1, s2);
}
char **
guestfs__inspect_get_roots (guestfs_h *g)
{
/* NB. Doesn't matter if g->nr_fses == 0. We just return an empty
* list in this case.
*/
size_t i;
size_t count = 0;
for (i = 0; i < g->nr_fses; ++i)
if (g->fses[i].is_root)
count++;
char **ret = calloc (count+1, sizeof (char *));
if (ret == NULL) {
perrorf (g, "calloc");
return NULL;
}
count = 0;
for (i = 0; i < g->nr_fses; ++i) {
if (g->fses[i].is_root) {
ret[count] = safe_strdup (g, g->fses[i].device);
count++;
}
}
ret[count] = NULL;
qsort (ret, count, sizeof (char *), compare_strings);
return ret;
}
char *
guestfs__inspect_get_type (guestfs_h *g, const char *root)
{
struct inspect_fs *fs = guestfs___search_for_root (g, root);
if (!fs)
return NULL;
char *ret;
switch (fs->type) {
case OS_TYPE_DOS: ret = safe_strdup (g, "dos"); break;
case OS_TYPE_FREEBSD: ret = safe_strdup (g, "freebsd"); break;
case OS_TYPE_HURD: ret = safe_strdup (g, "hurd"); break;
case OS_TYPE_LINUX: ret = safe_strdup (g, "linux"); break;
case OS_TYPE_NETBSD: ret = safe_strdup (g, "netbsd"); break;
case OS_TYPE_WINDOWS: ret = safe_strdup (g, "windows"); break;
case OS_TYPE_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
}
return ret;
}
char *
guestfs__inspect_get_arch (guestfs_h *g, const char *root)
{
struct inspect_fs *fs = guestfs___search_for_root (g, root);
if (!fs)
return NULL;
return safe_strdup (g, fs->arch ? : "unknown");
}
char *
guestfs__inspect_get_distro (guestfs_h *g, const char *root)
{
struct inspect_fs *fs = guestfs___search_for_root (g, root);
if (!fs)
return NULL;
char *ret;
switch (fs->distro) {
case OS_DISTRO_ARCHLINUX: ret = safe_strdup (g, "archlinux"); break;
case OS_DISTRO_BUILDROOT: ret = safe_strdup (g, "buildroot"); break;
case OS_DISTRO_CENTOS: ret = safe_strdup (g, "centos"); break;
case OS_DISTRO_CIRROS: ret = safe_strdup (g, "cirros"); break;
case OS_DISTRO_DEBIAN: ret = safe_strdup (g, "debian"); break;
case OS_DISTRO_FEDORA: ret = safe_strdup (g, "fedora"); break;
case OS_DISTRO_FREEDOS: ret = safe_strdup (g, "freedos"); break;
case OS_DISTRO_GENTOO: ret = safe_strdup (g, "gentoo"); break;
case OS_DISTRO_LINUX_MINT: ret = safe_strdup (g, "linuxmint"); break;
case OS_DISTRO_MAGEIA: ret = safe_strdup (g, "mageia"); break;
case OS_DISTRO_MANDRIVA: ret = safe_strdup (g, "mandriva"); break;
case OS_DISTRO_MEEGO: ret = safe_strdup (g, "meego"); break;
case OS_DISTRO_OPENSUSE: ret = safe_strdup (g, "opensuse"); break;
case OS_DISTRO_PARDUS: ret = safe_strdup (g, "pardus"); break;
case OS_DISTRO_REDHAT_BASED: ret = safe_strdup (g, "redhat-based"); break;
case OS_DISTRO_RHEL: ret = safe_strdup (g, "rhel"); break;
case OS_DISTRO_SCIENTIFIC_LINUX: ret = safe_strdup (g, "scientificlinux"); break;
case OS_DISTRO_SLACKWARE: ret = safe_strdup (g, "slackware"); break;
case OS_DISTRO_TTYLINUX: ret = safe_strdup (g, "ttylinux"); break;
case OS_DISTRO_WINDOWS: ret = safe_strdup (g, "windows"); break;
case OS_DISTRO_UBUNTU: ret = safe_strdup (g, "ubuntu"); break;
case OS_DISTRO_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
}
return ret;
}
int
guestfs__inspect_get_major_version (guestfs_h *g, const char *root)
{
struct inspect_fs *fs = guestfs___search_for_root (g, root);
if (!fs)
return -1;
return fs->major_version;
}
int
guestfs__inspect_get_minor_version (guestfs_h *g, const char *root)
{
struct inspect_fs *fs = guestfs___search_for_root (g, root);
if (!fs)
return -1;
return fs->minor_version;
}
char *
guestfs__inspect_get_product_name (guestfs_h *g, const char *root)
{
struct inspect_fs *fs = guestfs___search_for_root (g, root);
if (!fs)
return NULL;
return safe_strdup (g, fs->product_name ? : "unknown");
}
char *
guestfs__inspect_get_product_variant (guestfs_h *g, const char *root)
{
struct inspect_fs *fs = guestfs___search_for_root (g, root);
if (!fs)
return NULL;
return safe_strdup (g, fs->product_variant ? : "unknown");
}
char *
guestfs__inspect_get_windows_systemroot (guestfs_h *g, const char *root)
{
struct inspect_fs *fs = guestfs___search_for_root (g, root);
if (!fs)
return NULL;
if (!fs->windows_systemroot) {
error (g, _("not a Windows guest, or systemroot could not be determined"));
return NULL;
}
return safe_strdup (g, fs->windows_systemroot);
}
char *
guestfs__inspect_get_windows_current_control_set (guestfs_h *g,
const char *root)
{
struct inspect_fs *fs = guestfs___search_for_root (g, root);
if (!fs)
return NULL;
if (!fs->windows_current_control_set) {
error (g, _("not a Windows guest, or CurrentControlSet could not be determined"));
return NULL;
}
return safe_strdup (g, fs->windows_current_control_set);
}
char *
guestfs__inspect_get_format (guestfs_h *g, const char *root)
{
struct inspect_fs *fs = guestfs___search_for_root (g, root);
if (!fs)
return NULL;
char *ret;
switch (fs->format) {
case OS_FORMAT_INSTALLED: ret = safe_strdup (g, "installed"); break;
case OS_FORMAT_INSTALLER: ret = safe_strdup (g, "installer"); break;
case OS_FORMAT_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
}
return ret;
}
int
guestfs__inspect_is_live (guestfs_h *g, const char *root)
{
struct inspect_fs *fs = guestfs___search_for_root (g, root);
if (!fs)
return -1;
return fs->is_live_disk;
}
int
guestfs__inspect_is_netinst (guestfs_h *g, const char *root)
{
struct inspect_fs *fs = guestfs___search_for_root (g, root);
if (!fs)
return -1;
return fs->is_netinst_disk;
}
int
guestfs__inspect_is_multipart (guestfs_h *g, const char *root)
{
struct inspect_fs *fs = guestfs___search_for_root (g, root);
if (!fs)
return -1;
return fs->is_multipart_disk;
}
char **
guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root)
{
struct inspect_fs *fs = guestfs___search_for_root (g, root);
if (!fs)
return NULL;
#define CRITERION(fs, i) fs->fstab[i].mountpoint[0] == '/'
char **ret;
size_t i, count, nr = fs->nr_fstab;
if (nr == 0)
count = 1;
else {
count = 0;
for (i = 0; i < nr; ++i)
if (CRITERION (fs, i))
count++;
}
/* Hashtables have 2N+1 entries. */
ret = calloc (2*count+1, sizeof (char *));
if (ret == NULL) {
perrorf (g, "calloc");
return NULL;
}
/* If no fstab information (Windows) return just the root. */
if (nr == 0) {
ret[0] = safe_strdup (g, "/");
ret[1] = safe_strdup (g, root);
ret[2] = NULL;
return ret;
}
count = 0;
for (i = 0; i < nr; ++i)
if (CRITERION (fs, i)) {
ret[2*count] = safe_strdup (g, fs->fstab[i].mountpoint);
ret[2*count+1] = safe_strdup (g, fs->fstab[i].device);
count++;
}
#undef CRITERION
return ret;
}
char **
guestfs__inspect_get_filesystems (guestfs_h *g, const char *root)
{
struct inspect_fs *fs = guestfs___search_for_root (g, root);
if (!fs)
return NULL;
char **ret;
size_t i, nr = fs->nr_fstab;
ret = calloc (nr == 0 ? 2 : nr+1, sizeof (char *));
if (ret == NULL) {
perrorf (g, "calloc");
return NULL;
}
/* If no fstab information (Windows) return just the root. */
if (nr == 0) {
ret[0] = safe_strdup (g, root);
ret[1] = NULL;
return ret;
}
for (i = 0; i < nr; ++i)
ret[i] = safe_strdup (g, fs->fstab[i].device);
return ret;
}
char **
guestfs__inspect_get_drive_mappings (guestfs_h *g, const char *root)
{
char **ret;
size_t i, count;
struct inspect_fs *fs;
fs = guestfs___search_for_root (g, root);
if (!fs)
return NULL;
/* If no drive mappings, return an empty hashtable. */
if (!fs->drive_mappings)
count = 0;
else {
for (count = 0; fs->drive_mappings[count] != NULL; count++)
;
}
ret = calloc (count+1, sizeof (char *));
if (ret == NULL) {
perrorf (g, "calloc");
return NULL;
}
/* We need to make a deep copy of the hashtable since the caller
* will free it.
*/
for (i = 0; i < count; ++i)
ret[i] = safe_strdup (g, fs->drive_mappings[i]);
ret[count] = NULL;
return ret;
}
char *
guestfs__inspect_get_package_format (guestfs_h *g, const char *root)
{
struct inspect_fs *fs = guestfs___search_for_root (g, root);
if (!fs)
return NULL;
char *ret;
switch (fs->package_format) {
case OS_PACKAGE_FORMAT_RPM: ret = safe_strdup (g, "rpm"); break;
case OS_PACKAGE_FORMAT_DEB: ret = safe_strdup (g, "deb"); break;
case OS_PACKAGE_FORMAT_PACMAN: ret = safe_strdup (g, "pacman"); break;
case OS_PACKAGE_FORMAT_EBUILD: ret = safe_strdup (g, "ebuild"); break;
case OS_PACKAGE_FORMAT_PISI: ret = safe_strdup (g, "pisi"); break;
case OS_PACKAGE_FORMAT_PKGSRC: ret = safe_strdup (g, "pkgsrc"); break;
case OS_PACKAGE_FORMAT_UNKNOWN:
default:
ret = safe_strdup (g, "unknown");
break;
}
return ret;
}
char *
guestfs__inspect_get_package_management (guestfs_h *g, const char *root)
{
struct inspect_fs *fs = guestfs___search_for_root (g, root);
if (!fs)
return NULL;
char *ret;
switch (fs->package_management) {
case OS_PACKAGE_MANAGEMENT_YUM: ret = safe_strdup (g, "yum"); break;
case OS_PACKAGE_MANAGEMENT_UP2DATE: ret = safe_strdup (g, "up2date"); break;
case OS_PACKAGE_MANAGEMENT_APT: ret = safe_strdup (g, "apt"); break;
case OS_PACKAGE_MANAGEMENT_PACMAN: ret = safe_strdup (g, "pacman"); break;
case OS_PACKAGE_MANAGEMENT_PORTAGE: ret = safe_strdup (g, "portage"); break;
case OS_PACKAGE_MANAGEMENT_PISI: ret = safe_strdup (g, "pisi"); break;
case OS_PACKAGE_MANAGEMENT_URPMI: ret = safe_strdup (g, "urpmi"); break;
case OS_PACKAGE_MANAGEMENT_ZYPPER: ret = safe_strdup (g, "zypper"); break;
case OS_PACKAGE_MANAGEMENT_UNKNOWN:
default:
ret = safe_strdup (g, "unknown");
break;
}
return ret;
}
char *
guestfs__inspect_get_hostname (guestfs_h *g, const char *root)
{
struct inspect_fs *fs = guestfs___search_for_root (g, root);
if (!fs)
return NULL;
return safe_strdup (g, fs->hostname ? : "unknown");
}
#else /* no hivex at compile time */
/* XXX These functions should be in an optgroup. */
#define NOT_IMPL(r) \
guestfs_error_errno (g, ENOTSUP, _("inspection API not available since this version of libguestfs was compiled without the hivex library")); \
return r
char **
guestfs__inspect_os (guestfs_h *g)
{
NOT_IMPL(NULL);
}
char **
guestfs__inspect_get_roots (guestfs_h *g)
{
NOT_IMPL(NULL);
}
char *
guestfs__inspect_get_type (guestfs_h *g, const char *root)
{
NOT_IMPL(NULL);
}
char *
guestfs__inspect_get_arch (guestfs_h *g, const char *root)
{
NOT_IMPL(NULL);
}
char *
guestfs__inspect_get_distro (guestfs_h *g, const char *root)
{
NOT_IMPL(NULL);
}
int
guestfs__inspect_get_major_version (guestfs_h *g, const char *root)
{
NOT_IMPL(-1);
}
int
guestfs__inspect_get_minor_version (guestfs_h *g, const char *root)
{
NOT_IMPL(-1);
}
char *
guestfs__inspect_get_product_name (guestfs_h *g, const char *root)
{
NOT_IMPL(NULL);
}
char *
guestfs__inspect_get_product_variant (guestfs_h *g, const char *root)
{
NOT_IMPL(NULL);
}
char *
guestfs__inspect_get_windows_systemroot (guestfs_h *g, const char *root)
{
NOT_IMPL(NULL);
}
char *
guestfs__inspect_get_windows_current_control_set (guestfs_h *g,
const char *root)
{
NOT_IMPL(NULL);
}
char **
guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root)
{
NOT_IMPL(NULL);
}
char **
guestfs__inspect_get_filesystems (guestfs_h *g, const char *root)
{
NOT_IMPL(NULL);
}
char **
guestfs__inspect_get_drive_mappings (guestfs_h *g, const char *root)
{
NOT_IMPL(NULL);
}
char *
guestfs__inspect_get_package_format (guestfs_h *g, const char *root)
{
NOT_IMPL(NULL);
}
char *
guestfs__inspect_get_package_management (guestfs_h *g, const char *root)
{
NOT_IMPL(NULL);
}
char *
guestfs__inspect_get_hostname (guestfs_h *g, const char *root)
{
NOT_IMPL(NULL);
}
char *
guestfs__inspect_get_format (guestfs_h *g, const char *root)
{
NOT_IMPL(NULL);
}
int
guestfs__inspect_is_live (guestfs_h *g, const char *root)
{
NOT_IMPL(-1);
}
int
guestfs__inspect_is_netinst (guestfs_h *g, const char *root)
{
NOT_IMPL(-1);
}
int
guestfs__inspect_is_multipart (guestfs_h *g, const char *root)
{
NOT_IMPL(-1);
}
#endif /* no hivex at compile time */
void
guestfs___free_inspect_info (guestfs_h *g)
{
size_t i;
for (i = 0; i < g->nr_fses; ++i) {
free (g->fses[i].device);
free (g->fses[i].product_name);
free (g->fses[i].product_variant);
free (g->fses[i].arch);
free (g->fses[i].hostname);
free (g->fses[i].windows_systemroot);
free (g->fses[i].windows_current_control_set);
size_t j;
for (j = 0; j < g->fses[i].nr_fstab; ++j) {
free (g->fses[i].fstab[j].device);
free (g->fses[i].fstab[j].mountpoint);
}
free (g->fses[i].fstab);
if (g->fses[i].drive_mappings)
guestfs___free_string_list (g->fses[i].drive_mappings);
}
free (g->fses);
g->nr_fses = 0;
g->fses = NULL;
}
/* In the Perl code this is a public function. */
int
guestfs___feature_available (guestfs_h *g, const char *feature)
{
/* If there's an error we should ignore it, so to do that we have to
* temporarily replace the error handler with a null one.
*/
guestfs_error_handler_cb old_error_cb = g->error_cb;
g->error_cb = NULL;
const char *groups[] = { feature, NULL };
int r = guestfs_available (g, (char * const *) groups);
g->error_cb = old_error_cb;
return r == 0 ? 1 : 0;
}
/* Download a guest file to a local temporary file. The file is
* cached in the temporary directory, and is not downloaded again.
*
* The name of the temporary (downloaded) file is returned. The
* caller must free the pointer, but does *not* need to delete the
* temporary file. It will be deleted when the handle is closed.
*
* Refuse to download the guest file if it is larger than max_size.
* On this and other errors, NULL is returned.
*
* There is actually one cache per 'struct inspect_fs *' in order
* to handle the case of multiple roots.
*/
char *
guestfs___download_to_tmp (guestfs_h *g, struct inspect_fs *fs,
const char *filename,
const char *basename, uint64_t max_size)
{
char *r;
int fd;
char devfd[32];
int64_t size;
/* Make the basename unique by prefixing it with the fs number. */
if (asprintf (&r, "%s/%td-%s", g->tmpdir, fs - g->fses, basename) == -1) {
perrorf (g, "asprintf");
return NULL;
}
/* If the file has already been downloaded, return. */
if (access (r, R_OK) == 0)
return r;
/* Check size of remote file. */
size = guestfs_filesize (g, filename);
if (size == -1)
/* guestfs_filesize failed and has already set error in handle */
goto error;
if ((uint64_t) size > max_size) {
error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
filename, size);
goto error;
}
fd = open (r, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY|O_CLOEXEC, 0600);
if (fd == -1) {
perrorf (g, "open: %s", r);
goto error;
}
snprintf (devfd, sizeof devfd, "/dev/fd/%d", fd);
if (guestfs_download (g, filename, devfd) == -1) {
unlink (r);
close (fd);
goto error;
}
if (close (fd) == -1) {
perrorf (g, "close: %s", r);
unlink (r);
goto error;
}
return r;
error:
free (r);
return NULL;
}
struct inspect_fs *
guestfs___search_for_root (guestfs_h *g, const char *root)
{
if (g->nr_fses == 0) {
error (g, _("no inspection data: call guestfs_inspect_os first"));
return NULL;
}
size_t i;
struct inspect_fs *fs;
for (i = 0; i < g->nr_fses; ++i) {
fs = &g->fses[i];
if (fs->is_root && STREQ (root, fs->device))
return fs;
}
error (g, _("%s: root device not found: only call this function with a root device previously returned by guestfs_inspect_os"),
root);
return NULL;
}
|