daemon: Move device_name_translation function to its own file.

Just code motion.

(cherry picked from commit c75d873568e95fb7845c6e3913347c4bf267f4e9)
This commit is contained in:
Richard W.M. Jones
2017-04-25 13:45:30 +01:00
parent 9013a93d0e
commit 8a545c0b1e
4 changed files with 99 additions and 66 deletions

View File

@@ -74,6 +74,7 @@ guestfsd_SOURCES = \
dd.c \
debug.c \
debug-bmap.c \
device-name-translation.c \
devsparts.c \
df.c \
dir.c \

View File

@@ -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);

View File

@@ -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;
}

View File

@@ -947,70 +947,6 @@ print_arginfo (const struct printf_info *info, size_t n, int *argtypes)
#endif
#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.