lib: Move guestfs_int_download_to_tmp and remove inspect.c.

Move the last remaining function ‘guestfs_int_download_to_tmp’ to
lib/inspect-icon.c (the main, but not only user of this function).
Then remove lib/inspect.c entirely.

This is not quite code motion because I updated the comment for the
function to reflect what it does in reality.
This commit is contained in:
Richard W.M. Jones
2017-09-19 11:35:41 +01:00
parent 26a5916a24
commit 41df9aaf90
5 changed files with 73 additions and 110 deletions

View File

@@ -313,7 +313,6 @@ lib/handle.c
lib/info.c
lib/inspect-apps.c
lib/inspect-icon.c
lib/inspect.c
lib/journal.c
lib/launch-direct.c
lib/launch-libvirt.c

View File

@@ -93,7 +93,6 @@ libguestfs_la_SOURCES = \
guid.c \
handle.c \
info.c \
inspect.c \
inspect-apps.c \
inspect-icon.c \
journal.c \

View File

@@ -20,8 +20,11 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <inttypes.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <libintl.h>
#include <sys/wait.h>
#include "guestfs.h"
@@ -644,3 +647,72 @@ case_sensitive_path_silently (guestfs_h *g, const char *path)
return ret;
}
/**
* Download a guest file to a local temporary file. The file is
* cached in the temporary directory using C<basename>, and is not
* downloaded again.
*
* 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.
*
* Refuse to download the guest file if it is larger than C<max_size>.
* On this and other errors, C<NULL> is returned.
*
* XXX Prior to commit 65cfecb0f5344ec92d3f0d3c2ec0538b6b2726e2 this
* function used different basenames for each inspection root. After
* this commit icons probably won't work properly. Needs fixing.
*/
char *
guestfs_int_download_to_tmp (guestfs_h *g,
const char *filename,
const char *basename, uint64_t max_size)
{
char *r;
int fd;
char devfd[32];
int64_t size;
if (asprintf (&r, "%s/%s", g->tmpdir, basename) == -1) {
perrorf (g, "asprintf");
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;
}

View File

@@ -1,106 +0,0 @@
/* 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 <inttypes.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <libintl.h>
#include <assert.h>
#ifdef HAVE_ENDIAN_H
#include <endian.h>
#endif
#include "ignore-value.h"
#include "guestfs.h"
#include "guestfs-internal.h"
#include "guestfs-internal-actions.h"
/**
* 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 I<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 C<max_size>.
* On this and other errors, C<NULL> is returned.
*
* There is actually one cache per C<struct inspect_fs *> in order to
* handle the case of multiple roots.
*/
char *
guestfs_int_download_to_tmp (guestfs_h *g,
const char *filename,
const char *basename, uint64_t max_size)
{
char *r;
int fd;
char devfd[32];
int64_t size;
if (asprintf (&r, "%s/%s", g->tmpdir, basename) == -1) {
perrorf (g, "asprintf");
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;
}

View File

@@ -376,7 +376,6 @@ lib/handle.c
lib/info.c
lib/inspect-apps.c
lib/inspect-icon.c
lib/inspect.c
lib/journal.c
lib/launch-direct.c
lib/launch-libvirt.c