return 1;
}
+static int
+check_path (guestfs_h *g, const char *filename)
+{
+ if (strchr (filename, ',') != NULL) {
+ error (g, _("filename cannot contain ',' (comma) character"));
+ return -1;
+ }
+ return 0;
+}
+
int
guestfs__add_drive_opts (guestfs_h *g, const char *filename,
const struct guestfs_add_drive_opts_argv *optargs)
char *format;
char *iface;
char *name;
+ char *abs_path = NULL;
int use_cache_off;
- if (strchr (filename, ',') != NULL) {
- error (g, _("filename cannot contain ',' (comma) character"));
+ if (check_path(g, filename) == -1)
return -1;
- }
readonly = optargs->bitmask & GUESTFS_ADD_DRIVE_OPTS_READONLY_BITMASK
? optargs->readonly : 0;
if (format && !valid_format_iface (format)) {
error (g, _("%s parameter is empty or contains disallowed characters"),
"format");
- free (format);
- free (iface);
- free (name);
- return -1;
+ goto err_out;
}
if (!valid_format_iface (iface)) {
error (g, _("%s parameter is empty or contains disallowed characters"),
"iface");
- free (format);
- free (iface);
- free (name);
- return -1;
+ goto err_out;
}
/* For writable files, see if we can use cache=off. This also
* to do the check explicitly.
*/
use_cache_off = readonly ? 0 : test_cache_off (g, filename);
- if (use_cache_off == -1) {
- free (format);
- free (iface);
- free (name);
- return -1;
- }
+ if (use_cache_off == -1)
+ goto err_out;
if (readonly) {
if (access (filename, R_OK) == -1) {
perrorf (g, "%s", filename);
- free (format);
- free (iface);
- free (name);
- return -1;
+ goto err_out;
}
}
+ abs_path = realpath (filename, NULL);
struct drive **i = &(g->drives);
- while (*i != NULL) i = &((*i)->next);
+ while (*i != NULL) {
+ if (STREQ((*i)->path, abs_path)) {
+ error (g, _("drive %s can't be added twice"), abs_path);
+ goto err_out;
+ }
+ i = &((*i)->next);
+ }
*i = safe_malloc (g, sizeof (struct drive));
(*i)->next = NULL;
- (*i)->path = safe_strdup (g, filename);
+ (*i)->path = safe_strdup (g, abs_path);
(*i)->readonly = readonly;
(*i)->format = format;
(*i)->iface = iface;
(*i)->name = name;
(*i)->use_cache_off = use_cache_off;
+ free (abs_path);
return 0;
+
+err_out:
+ free (format);
+ free (iface);
+ free (name);
+ free (abs_path);
+ return -1;
}
int
int
guestfs__add_cdrom (guestfs_h *g, const char *filename)
{
- if (strchr (filename, ',') != NULL) {
- error (g, _("filename cannot contain ',' (comma) character"));
+ if (check_path(g, filename) == -1)
return -1;
- }
if (access (filename, F_OK) == -1) {
perrorf (g, "%s", filename);