Just code motion. --- daemon/Makefile.am | 1 + daemon/daemon.h | 5 ++- daemon/device-name-translation.c | 95 ++++++++++++++++++++++++++++++++++++++++ daemon/guestfsd.c | 64 --------------------------- 4 files changed, 99 insertions(+), 66 deletions(-) create mode 100644 daemon/device-name-translation.c diff --git a/daemon/Makefile.am b/daemon/Makefile.am index 68f863bcd..0d3dde516 100644 --- a/daemon/Makefile.am +++ b/daemon/Makefile.am @@ -74,6 +74,7 @@ guestfsd_SOURCES = \ dd.c \ debug.c \ debug-bmap.c \ + device-name-translation.c \ devsparts.c \ df.c \ dir.c \ diff --git a/daemon/daemon.h b/daemon/daemon.h index 75af5246e..3a7f6795e 100644 --- a/daemon/daemon.h +++ b/daemon/daemon.h @@ -136,8 +136,6 @@ extern int is_power_of_2 (unsigned long v); extern void trim (char *str); -extern char *device_name_translation (const char *device); - extern int parse_btrfsvol (const char *desc, mountable_t *mountable); extern int prog_exists (const char *prog); @@ -167,6 +165,9 @@ extern uint64_t optargs_bitmask; extern int is_root_mounted (void); extern int is_device_mounted (const char *device); +/*-- in device-name-translation.c --*/ +extern char *device_name_translation (const char *device); + /*-- in stubs.c (auto-generated) --*/ extern void dispatch_incoming_message (XDR *); extern guestfs_int_lvm_pv_list *parse_command_line_pvs (void); diff --git a/daemon/device-name-translation.c b/daemon/device-name-translation.c new file mode 100644 index 000000000..0c4387f54 --- /dev/null +++ b/daemon/device-name-translation.c @@ -0,0 +1,95 @@ +/* libguestfs - the guestfsd daemon + * Copyright (C) 2009-2017 Red Hat Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include <config.h> + +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <string.h> +#include <unistd.h> +#include <fcntl.h> +#include <dirent.h> +#include <limits.h> +#include <sys/stat.h> + +#include "daemon.h" + +/** + * Perform device name translation. + * + * It returns a newly allocated string which the caller must free. + * + * It returns C<NULL> on error. B<Note> it does I<not> call + * C<reply_with_*>. + * + * We have to open the device and test for C<ENXIO>, because the + * device nodes may exist in the appliance. + */ +char * +device_name_translation (const char *device) +{ + int fd; + char *ret; + + fd = open (device, O_RDONLY|O_CLOEXEC); + if (fd >= 0) { + close (fd); + return strdup (device); + } + + if (errno != ENXIO && errno != ENOENT) + return NULL; + + /* If the name begins with "/dev/sd" then try the alternatives. */ + if (!STRPREFIX (device, "/dev/sd")) + return NULL; + device += 7; /* device == "a1" etc. */ + + /* /dev/vd (virtio-blk) */ + if (asprintf (&ret, "/dev/vd%s", device) == -1) + return NULL; + fd = open (ret, O_RDONLY|O_CLOEXEC); + if (fd >= 0) { + close (fd); + return ret; + } + free (ret); + + /* /dev/hd (old IDE driver) */ + if (asprintf (&ret, "/dev/hd%s", device) == -1) + return NULL; + fd = open (ret, O_RDONLY|O_CLOEXEC); + if (fd >= 0) { + close (fd); + return ret; + } + free (ret); + + /* User-Mode Linux */ + if (asprintf (&ret, "/dev/ubd%s", device) == -1) + return NULL; + fd = open (ret, O_RDONLY|O_CLOEXEC); + if (fd >= 0) { + close (fd); + return ret; + } + free (ret); + + return NULL; +} diff --git a/daemon/guestfsd.c b/daemon/guestfsd.c index 8e9fa9d63..db2bb702f 100644 --- a/daemon/guestfsd.c +++ b/daemon/guestfsd.c @@ -948,70 +948,6 @@ print_arginfo (const struct printf_info *info, size_t n, int *argtypes) #endif /** - * Perform device name translation. - * - * It returns a newly allocated string which the caller must free. - * - * It returns C<NULL> on error. B<Note> it does I<not> call - * C<reply_with_*>. - * - * We have to open the device and test for C<ENXIO>, because the - * device nodes may exist in the appliance. - */ -char * -device_name_translation (const char *device) -{ - int fd; - char *ret; - - fd = open (device, O_RDONLY|O_CLOEXEC); - if (fd >= 0) { - close (fd); - return strdup (device); - } - - if (errno != ENXIO && errno != ENOENT) - return NULL; - - /* If the name begins with "/dev/sd" then try the alternatives. */ - if (!STRPREFIX (device, "/dev/sd")) - return NULL; - device += 7; /* device == "a1" etc. */ - - /* /dev/vd (virtio-blk) */ - if (asprintf (&ret, "/dev/vd%s", device) == -1) - return NULL; - fd = open (ret, O_RDONLY|O_CLOEXEC); - if (fd >= 0) { - close (fd); - return ret; - } - free (ret); - - /* /dev/hd (old IDE driver) */ - if (asprintf (&ret, "/dev/hd%s", device) == -1) - return NULL; - fd = open (ret, O_RDONLY|O_CLOEXEC); - if (fd >= 0) { - close (fd); - return ret; - } - free (ret); - - /* User-Mode Linux */ - if (asprintf (&ret, "/dev/ubd%s", device) == -1) - return NULL; - fd = open (ret, O_RDONLY|O_CLOEXEC); - if (fd >= 0) { - close (fd); - return ret; - } - free (ret); - - return NULL; -} - -/** * Parse the mountable descriptor for a btrfs subvolume. Don't call * this directly; it is only used from the stubs. * -- 2.12.0