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
|
/* libguestfs
* Copyright (C) 2011 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 <inttypes.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <libintl.h>
#include <sys/wait.h>
#include "guestfs.h"
#include "guestfs-internal.h"
#include "guestfs-internal-actions.h"
/* External tools are required for some icon types. Check we have them. */
#if defined(PBMTEXT) && defined (PNMTOPNG)
#define CAN_DO_CIRROS 1
#endif
#if defined(WRESTOOL) && defined(BMPTOPNM) && defined(PNMTOPNG) && \
defined(PAMCUT)
#define CAN_DO_WINDOWS 1
#endif
/* All these icon_*() functions return the same way. One of:
*
* ret == NULL:
* An error occurred. Error has been set in the handle. The caller
* should return NULL immediately.
*
* ret == NOT_FOUND:
* Not an error, but no icon was found. 'ret' is just a dummy value
* which should be ignored (do not free it!)
*
* ret == ordinary pointer:
* An icon was found. 'ret' points to the icon buffer, and *size_r
* is the size.
*/
static char *icon_favicon (guestfs_h *g, const char *type, size_t *size_r);
static char *icon_fedora (guestfs_h *g, size_t *size_r);
static char *icon_rhel (guestfs_h *g, int major, size_t *size_r);
static char *icon_debian (guestfs_h *g, size_t *size_r);
static char *icon_ubuntu (guestfs_h *g, size_t *size_r);
static char *icon_mageia (guestfs_h *g, size_t *size_r);
static char *icon_opensuse (guestfs_h *g, size_t *size_r);
#if CAN_DO_CIRROS
static char *icon_cirros (guestfs_h *g, size_t *size_r);
#endif
static char *icon_voidlinux (guestfs_h *g, size_t *size_r);
static char *icon_altlinux (guestfs_h *g, size_t *size_r);
static char *icon_gentoo (guestfs_h *g, size_t *size_r);
static char *icon_openmandriva (guestfs_h *g, size_t *size_r);
#if CAN_DO_WINDOWS
static char *icon_windows (guestfs_h *g, const char *root, size_t *size_r);
#endif
static char *case_sensitive_path_silently (guestfs_h *g, const char *path);
/* Dummy static object. */
static char *NOT_FOUND = (char *) "not_found";
/* For the unexpected legal consequences of this function, see:
* http://lists.fedoraproject.org/pipermail/legal/2011-April/001615.html
*
* Returns an RBufferOut, so the length of the returned buffer is
* returned in *size_r.
*
* Check optargs for the optional argument.
*/
char *
guestfs_impl_inspect_get_icon (guestfs_h *g, const char *root, size_t *size_r,
const struct guestfs_inspect_get_icon_argv *optargs)
{
char *r = NOT_FOUND;
int favicon, highquality;
size_t size;
CLEANUP_FREE char *type = NULL;
CLEANUP_FREE char *distro = NULL;
type = guestfs_inspect_get_type (g, root);
if (!type)
return NULL;
distro = guestfs_inspect_get_distro (g, root);
if (!distro)
return NULL;
/* Get optargs, or defaults. */
favicon =
optargs->bitmask & GUESTFS_INSPECT_GET_ICON_FAVICON_BITMASK ?
optargs->favicon : 1;
highquality =
optargs->bitmask & GUESTFS_INSPECT_GET_ICON_HIGHQUALITY_BITMASK ?
optargs->highquality : 0;
/* Favicons are never high quality, so ... */
if (highquality)
favicon = 0;
/* Try looking for a favicon first. */
if (favicon) {
r = icon_favicon (g, type, &size);
if (!r)
return NULL;
if (r != NOT_FOUND) {
/* try_favicon succeeded in finding a favicon. */
*size_r = size;
return r;
}
}
/* Favicon failed, so let's try a method based on the detected operating
* system.
*/
if (STREQ (type, "linux") || STREQ (type, "hurd")) {
if (STREQ (distro, "fedora")) {
r = icon_fedora (g, &size);
}
else if (STREQ (distro, "rhel") ||
STREQ (distro, "redhat-based") ||
STREQ (distro, "centos") ||
STREQ (distro, "scientificlinux") ||
STREQ (distro, "oraclelinux")) {
r = icon_rhel (g, guestfs_inspect_get_major_version (g, root), &size);
}
else if (STREQ (distro, "debian")) {
r = icon_debian (g, &size);
}
else if (STREQ (distro, "ubuntu")) {
if (!highquality)
r = icon_ubuntu (g, &size);
}
else if (STREQ (distro, "mageia")) {
r = icon_mageia (g, &size);
}
else if (STREQ (distro, "suse-based") ||
STREQ (distro, "opensuse") ||
STREQ (distro, "sles")) {
r = icon_opensuse (g, &size);
}
else if (STREQ (distro, "cirros")) {
#if CAN_DO_CIRROS
r = icon_cirros (g, &size);
#endif
}
else if (STREQ (distro, "voidlinux")) {
r = icon_voidlinux (g, &size);
}
else if (STREQ (distro, "altlinux")) {
r = icon_altlinux (g, &size);
}
else if (STREQ (distro, "gentoo")) {
r = icon_gentoo (g, &size);
}
else if (STREQ (distro, "openmandriva")) {
r = icon_openmandriva (g, &size);
}
}
else if (STREQ (type, "windows")) {
#if CAN_DO_WINDOWS
/* We don't know how to get high quality icons from a Windows guest,
* so disable this if high quality was specified.
*/
if (!highquality)
r = icon_windows (g, root, &size);
#endif
}
if (r == NOT_FOUND) {
/* Not found, but not an error. So return the special zero-length
* buffer. Use malloc(1) here to ensure that malloc won't return
* NULL.
*/
r = safe_malloc (g, 1);
size = 0;
}
*size_r = size;
return r;
}
/* Check that the named file 'filename' is a PNG file and is reasonable.
* If it is, download and return it.
*/
static char *
get_png (guestfs_h *g, const char *filename, size_t *size_r, size_t max_size)
{
char *ret;
CLEANUP_FREE char *real = NULL;
CLEANUP_FREE char *type = NULL;
CLEANUP_FREE char *local = NULL;
int r, w, h;
r = guestfs_is_file_opts (g, filename,
GUESTFS_IS_FILE_OPTS_FOLLOWSYMLINKS, 1, -1);
if (r == -1)
return NULL; /* a real error */
if (r == 0)
return NOT_FOUND;
/* Resolve the path, in case it's a symbolic link (as in RHEL 7). */
guestfs_push_error_handler (g, NULL, NULL);
real = guestfs_realpath (g, filename);
guestfs_pop_error_handler (g);
if (real == NULL)
return NOT_FOUND; /* could just be a broken link */
/* Check the file type and geometry. */
type = guestfs_file (g, real);
if (!type)
return NOT_FOUND;
if (!STRPREFIX (type, "PNG image data, "))
return NOT_FOUND;
if (sscanf (&type[16], "%d x %d", &w, &h) != 2)
return NOT_FOUND;
if (w < 16 || h < 16 || w > 1024 || h > 1024)
return NOT_FOUND;
/* Define a maximum reasonable size based on the geometry. This
* also limits the maximum we allocate below to around 4 MB.
*/
if (max_size == 0)
max_size = 4 * w * h;
local = guestfs_int_download_to_tmp (g, real, "png", max_size);
if (!local)
return NOT_FOUND;
/* Successfully passed checks and downloaded. Read it into memory. */
if (guestfs_int_read_whole_file (g, local, &ret, size_r) == -1)
return NULL;
return ret;
}
static char *
find_png (guestfs_h *g, const char **filenames, size_t *size_r, size_t max_size)
{
size_t i;
char *ret;
for (i = 0; filenames[i] != NULL; ++i) {
ret = get_png (g, filenames[i], size_r, max_size);
if (ret == NULL)
return NULL;
if (ret != NOT_FOUND)
return ret;
}
return NOT_FOUND;
}
/* Return /etc/favicon.png (or \etc\favicon.png) if it exists and if
* it has a reasonable size and format.
*/
static char *
icon_favicon (guestfs_h *g, const char *type, size_t *size_r)
{
char *ret;
char *filename = safe_strdup (g, "/etc/favicon.png");
if (STREQ (type, "windows")) {
char *f = case_sensitive_path_silently (g, filename);
if (f) {
free (filename);
filename = f;
}
}
ret = get_png (g, filename, size_r, 0);
free (filename);
return ret;
}
/* Return FEDORA_ICON. I checked that this exists on at least Fedora 6
* through 16.
*/
#define FEDORA_ICON "/usr/share/icons/hicolor/96x96/apps/fedora-logo-icon.png"
static char *
icon_fedora (guestfs_h *g, size_t *size_r)
{
return get_png (g, FEDORA_ICON, size_r, 0);
}
/* RHEL 3, 4:
* /usr/share/pixmaps/redhat/shadowman-transparent.png is a 517x515
* PNG with alpha channel, around 64K in size.
*
* RHEL 5, 6:
* As above, but the file has been optimized to about 16K.
*
* In RHEL 7 the logos were completely broken (RHBZ#1063300).
*
* Conveniently the RHEL clones also have the same file with the
* same name, but containing their own logos. Sense prevails!
*
* Use a generic 100K limit for all the images, as logos in the
* RHEL clones have different sizes.
*/
static char *
icon_rhel (guestfs_h *g, int major, size_t *size_r)
{
const char *shadowman;
if (major < 7)
shadowman = "/usr/share/pixmaps/redhat/shadowman-transparent.png";
else
shadowman = "/usr/share/icons/hicolor/96x96/apps/system-logo-icon.png";
return get_png (g, shadowman, size_r, 102400);
}
#define DEBIAN_ICON "/usr/share/pixmaps/debian-logo.png"
static char *
icon_debian (guestfs_h *g, size_t *size_r)
{
return get_png (g, DEBIAN_ICON, size_r, 2048);
}
static char *
icon_ubuntu (guestfs_h *g, size_t *size_r)
{
const char *icons[] = {
"/usr/share/icons/gnome/24x24/places/ubuntu-logo.png",
/* Very low quality and only present when ubuntu-desktop packages
* have been installed.
*/
"/usr/share/help/C/ubuntu-help/figures/ubuntu-logo.png",
NULL
};
return find_png (g, icons, size_r, 2048);
}
#define MAGEIA_ICON "/usr/share/icons/mageia.png"
static char *
icon_mageia (guestfs_h *g, size_t *size_r)
{
return get_png (g, MAGEIA_ICON, size_r, 10240);
}
static char *
icon_opensuse (guestfs_h *g, size_t *size_r)
{
const char *icons[] = {
"/usr/share/icons/hicolor/48x48/apps/distributor.png",
"/usr/share/icons/hicolor/24x24/apps/distributor.png",
NULL
};
return find_png (g, icons, size_r, 10240);
}
#if CAN_DO_CIRROS
/* Cirros's logo is a text file! */
#define CIRROS_LOGO "/usr/share/cirros/logo"
static char *
icon_cirros (guestfs_h *g, size_t *size_r)
{
char *ret;
CLEANUP_FREE char *type = NULL;
CLEANUP_FREE char *local = NULL;
CLEANUP_FREE char *pngfile = NULL;
CLEANUP_CMD_CLOSE struct command *cmd = guestfs_int_new_command (g);
int r;
r = guestfs_is_file_opts (g, CIRROS_LOGO,
GUESTFS_IS_FILE_OPTS_FOLLOWSYMLINKS, 1, -1);
if (r == -1)
return NULL; /* a real error */
if (r == 0)
return NOT_FOUND;
/* Check the file type and geometry. */
type = guestfs_file (g, CIRROS_LOGO);
if (!type)
return NOT_FOUND;
if (!STRPREFIX (type, "ASCII text"))
return NOT_FOUND;
local = guestfs_int_download_to_tmp (g, CIRROS_LOGO, "png", 1024);
if (!local)
return NOT_FOUND;
/* Use pbmtext to render it. */
pngfile = guestfs_int_make_temp_path (g, "cirros", "png");
if (!pngfile)
return NOT_FOUND;
guestfs_int_cmd_add_string_unquoted (cmd, PBMTEXT " < ");
guestfs_int_cmd_add_string_quoted (cmd, local);
guestfs_int_cmd_add_string_unquoted (cmd, " | " PNMTOPNG " > ");
guestfs_int_cmd_add_string_quoted (cmd, pngfile);
r = guestfs_int_cmd_run (cmd);
if (r == -1)
return NULL;
if (!WIFEXITED (r) || WEXITSTATUS (r) != 0)
return NOT_FOUND;
/* Read it into memory. */
if (guestfs_int_read_whole_file (g, pngfile, &ret, size_r) == -1)
return NULL;
return ret;
}
#endif /* CAN_DO_CIRROS */
#define VOIDLINUX_ICON "/usr/share/void-artwork/void-logo.png"
static char *
icon_voidlinux (guestfs_h *g, size_t *size_r)
{
return get_png (g, VOIDLINUX_ICON, size_r, 20480);
}
#define ALTLINUX_ICON "/usr/share/icons/hicolor/48x48/apps/altlinux.png"
static char *
icon_altlinux (guestfs_h *g, size_t *size_r)
{
return get_png (g, ALTLINUX_ICON, size_r, 20480);
}
/* Installed by x11-themes/gentoo-artwork. */
#define GENTOO_ICON "/usr/share/icons/gentoo/48x48/gentoo.png"
static char *
icon_gentoo (guestfs_h *g, size_t *size_r)
{
return get_png (g, GENTOO_ICON, size_r, 10240);
}
static char *
icon_openmandriva (guestfs_h *g, size_t *size_r)
{
const char *icons[] = {
"/usr/share/icons/large/mandriva.png",
"/usr/share/icons/mandriva.png",
NULL
};
return find_png (g, icons, size_r, 10240);
}
#if CAN_DO_WINDOWS
/* Windows, as usual, has to be much more complicated and stupid than
* anything else.
*
* We have to download %systemroot%\explorer.exe and use a special
* program called 'wrestool' to extract the icons from this file. For
* each version of Windows, the icon we want is in a different place.
* The icon is in a stupid format (BMP), and in some cases multiple
* icons are in a single BMP file so we have to do some manipulation
* on the file.
*
* XXX I've only bothered with this nonsense for a few versions of
* Windows that I have handy. Please send patches to support other
* versions.
*/
static char *
icon_windows_xp (guestfs_h *g, const char *systemroot, size_t *size_r)
{
CLEANUP_FREE char *filename = NULL;
CLEANUP_FREE char *filename_case = NULL;
CLEANUP_FREE char *filename_downloaded = NULL;
CLEANUP_FREE char *pngfile = NULL;
CLEANUP_CMD_CLOSE struct command *cmd = guestfs_int_new_command (g);
int r;
char *ret;
/* Download %systemroot%\explorer.exe */
filename = safe_asprintf (g, "%s/explorer.exe", systemroot);
filename_case = guestfs_case_sensitive_path (g, filename);
if (filename_case == NULL)
return NULL;
guestfs_push_error_handler (g, NULL, NULL);
r = guestfs_is_file (g, filename_case);
guestfs_pop_error_handler (g);
if (r == -1)
return NULL;
if (r == 0)
return NOT_FOUND;
filename_downloaded = guestfs_int_download_to_tmp (g, filename_case, "exe",
MAX_WINDOWS_EXPLORER_SIZE);
if (filename_downloaded == NULL)
return NOT_FOUND;
pngfile = guestfs_int_make_temp_path (g, "windows-xp-icon", "png");
if (!pngfile)
return NOT_FOUND;
guestfs_int_cmd_add_string_unquoted (cmd, WRESTOOL " -x --type=2 --name=143 ");
guestfs_int_cmd_add_string_quoted (cmd, filename_downloaded);
guestfs_int_cmd_add_string_unquoted (cmd,
" | " BMPTOPNM " | " PNMTOPNG " > ");
guestfs_int_cmd_add_string_quoted (cmd, pngfile);
r = guestfs_int_cmd_run (cmd);
if (r == -1)
return NULL;
if (!WIFEXITED (r) || WEXITSTATUS (r) != 0)
return NOT_FOUND;
if (guestfs_int_read_whole_file (g, pngfile, &ret, size_r) == -1)
return NULL;
return ret;
}
/* For Windows 7 we get the icon from explorer.exe. Prefer
* %systemroot%\SysWOW64\explorer.exe, a PE32 binary that usually
* contains the icons on 64 bit guests. Note the whole SysWOW64
* directory doesn't exist on 32 bit guests, so we have to be prepared
* for that.
*/
static const char *win7_explorer[] = {
"SysWOW64/explorer.exe",
"explorer.exe",
NULL
};
static char *
icon_windows_7 (guestfs_h *g, const char *systemroot, size_t *size_r)
{
size_t i;
CLEANUP_FREE char *filename_case = NULL;
CLEANUP_FREE char *filename_downloaded = NULL;
CLEANUP_FREE char *pngfile = NULL;
CLEANUP_CMD_CLOSE struct command *cmd = guestfs_int_new_command (g);
int r;
char *ret;
for (i = 0; win7_explorer[i] != NULL; ++i) {
CLEANUP_FREE char *filename = NULL;
filename = safe_asprintf (g, "%s/%s", systemroot, win7_explorer[i]);
free (filename_case);
filename_case = case_sensitive_path_silently (g, filename);
if (filename_case == NULL)
continue;
guestfs_push_error_handler (g, NULL, NULL);
r = guestfs_is_file (g, filename_case);
guestfs_pop_error_handler (g);
if (r == -1)
return NULL;
if (r)
break;
}
if (win7_explorer[i] == NULL)
return NOT_FOUND;
filename_downloaded = guestfs_int_download_to_tmp (g, filename_case, "exe",
MAX_WINDOWS_EXPLORER_SIZE);
if (filename_downloaded == NULL)
return NOT_FOUND;
pngfile = guestfs_int_make_temp_path (g, "windows-7-icon", "png");
if (!pngfile)
return NOT_FOUND;
guestfs_int_cmd_add_string_unquoted (cmd,
WRESTOOL " -x --type=2 --name=6801 ");
guestfs_int_cmd_add_string_quoted (cmd, filename_downloaded);
guestfs_int_cmd_add_string_unquoted (cmd,
" | " BMPTOPNM " | "
PAMCUT " -bottom 54 | "
PNMTOPNG " > ");
guestfs_int_cmd_add_string_quoted (cmd, pngfile);
r = guestfs_int_cmd_run (cmd);
if (r == -1)
return NULL;
if (!WIFEXITED (r) || WEXITSTATUS (r) != 0)
return NOT_FOUND;
if (guestfs_int_read_whole_file (g, pngfile, &ret, size_r) == -1)
return NULL;
return ret;
}
/* There are several sources we might use:
* - /ProgramData/Microsoft/Windows Live/WLive48x48.png
* - w-brand.png (in a very long directory name)
* - /Windows/System32/slui.exe --type=14 group icon #2
*/
static char *
icon_windows_8 (guestfs_h *g, size_t *size_r)
{
CLEANUP_FREE char *filename_case = NULL;
CLEANUP_FREE char *filename_downloaded = NULL;
int r;
char *ret;
filename_case = case_sensitive_path_silently
(g, "/ProgramData/Microsoft/Windows Live/WLive48x48.png");
if (filename_case == NULL)
return NOT_FOUND; /* Not an error since a parent dir might not exist. */
guestfs_push_error_handler (g, NULL, NULL);
r = guestfs_is_file (g, filename_case);
guestfs_pop_error_handler (g);
if (r == -1)
return NULL;
if (r == 0)
return NOT_FOUND;
filename_downloaded = guestfs_int_download_to_tmp (g, filename_case, "png",
8192);
if (filename_downloaded == NULL)
return NOT_FOUND;
if (guestfs_int_read_whole_file (g, filename_downloaded, &ret, size_r) == -1)
return NULL;
return ret;
}
static char *
icon_windows (guestfs_h *g, const char *root, size_t *size_r)
{
CLEANUP_FREE char *systemroot =
guestfs_inspect_get_windows_systemroot (g, root);
int major = guestfs_inspect_get_major_version (g, root);
int minor = guestfs_inspect_get_minor_version (g, root);
if (systemroot == NULL)
return NOT_FOUND;
/* Windows XP. */
if (major == 5 && minor == 1)
return icon_windows_xp (g, systemroot, size_r);
/* Windows 7. */
else if (major == 6 && minor == 1)
return icon_windows_7 (g, systemroot, size_r);
/* Windows 8. */
else if (major == 6 && minor == 2)
return icon_windows_8 (g, size_r);
/* Not (yet) a supported version of Windows. */
else return NOT_FOUND;
}
#endif /* CAN_DO_WINDOWS */
/* NB: This function DOES NOT test for the existence of the file. It
* will return non-NULL even if the file/directory does not exist.
* You have to call guestfs_is_file{,_opts} etc.
*/
static char *
case_sensitive_path_silently (guestfs_h *g, const char *path)
{
char *ret;
guestfs_push_error_handler (g, NULL, NULL);
ret = guestfs_case_sensitive_path (g, path);
guestfs_pop_error_handler (g);
return ret;
}
/**
* Download a guest file to a local temporary file.
*
* The name of the temporary (downloaded) file is returned. The
* caller must free the pointer, but does I<not> need to delete the
* temporary file. It will be deleted when the handle is closed.
*
* The name of the temporary file is randomly generated, but an
* extension can be specified using C<extension> (or pass C<NULL> for none).
*
* Refuse to download the guest file if it is larger than C<max_size>.
* On this and other errors, C<NULL> is returned.
*/
char *
guestfs_int_download_to_tmp (guestfs_h *g, const char *filename,
const char *extension,
uint64_t max_size)
{
char *r;
int fd;
char devfd[32];
int64_t size;
r = guestfs_int_make_temp_path (g, "download", extension);
if (r == NULL)
return NULL;
/* 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;
}
|