X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=daemon%2Fsfdisk.c;h=a504141d91c8a2dc3a658ada7d0beacab2169d4e;hb=0dd6c8c8442d4ff588f6dac2efab24d3409b0dec;hp=295e35337fa75d14662a3212a9516f658ca0f939;hpb=887290e949d54c6ac4c9b787231e588f84f2367c;p=libguestfs.git diff --git a/daemon/sfdisk.c b/daemon/sfdisk.c index 295e353..a504141 100644 --- a/daemon/sfdisk.c +++ b/daemon/sfdisk.c @@ -1,5 +1,5 @@ /* libguestfs - the guestfsd daemon - * Copyright (C) 2009 Red Hat Inc. + * Copyright (C) 2009 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 @@ -30,16 +30,18 @@ #include "actions.h" static int -sfdisk (const char *device, int n, int cyls, int heads, int sectors, - char * const* const lines) +sfdisk (char *device, int n, int cyls, int heads, int sectors, + const char *extra_flag, + char * const* const lines) { FILE *fp; char buf[256]; int i; - IS_DEVICE (device, -1); + RESOLVE_DEVICE (device, return -1); strcpy (buf, "/sbin/sfdisk"); + if (n > 0) sprintf (buf + strlen (buf), " -N %d", n); if (cyls) @@ -48,7 +50,10 @@ sfdisk (const char *device, int n, int cyls, int heads, int sectors, sprintf (buf + strlen (buf), " -H %d", heads); if (sectors) sprintf (buf + strlen (buf), " -S %d", sectors); - /* Safe because of IS_DEVICE above: */ + if (extra_flag) + sprintf (buf + strlen (buf), " %s", extra_flag); + + /* Safe because of RESOLVE_DEVICE above: */ sprintf (buf + strlen (buf), " %s", device); if (verbose) @@ -63,43 +68,50 @@ sfdisk (const char *device, int n, int cyls, int heads, int sectors, for (i = 0; lines[i] != NULL; ++i) { if (fprintf (fp, "%s\n", lines[i]) < 0) { reply_with_perror (buf); - fclose (fp); + pclose (fp); return -1; } } - if (fclose (fp) == EOF) { - reply_with_perror (buf); - fclose (fp); + if (pclose (fp) != 0) { + reply_with_error ("%s: external command failed", buf); return -1; } + udev_settle (); + return 0; } int do_sfdisk (char *device, int cyls, int heads, int sectors, - char **lines) + char **lines) { - return sfdisk (device, 0, cyls, heads, sectors, lines); + return sfdisk (device, 0, cyls, heads, sectors, NULL, lines); } int do_sfdisk_N (char *device, int n, int cyls, int heads, int sectors, - char *line) + char *line) { const char *lines[2] = { line, NULL }; - return sfdisk (device, n, cyls, heads, sectors, lines); + return sfdisk (device, n, cyls, heads, sectors, NULL, lines); +} + +int +do_sfdiskM (char *device, char **lines) +{ + return sfdisk (device, 0, 0, 0, 0, "-uM", lines); } static char * -sfdisk_flag (const char *device, const char *flag) +sfdisk_flag (char *device, const char *flag) { char *out, *err; int r; - IS_DEVICE (device, NULL); + RESOLVE_DEVICE (device, return NULL); r = command (&out, &err, "/sbin/sfdisk", flag, device, NULL); if (r == -1) { @@ -111,6 +123,8 @@ sfdisk_flag (const char *device, const char *flag) free (err); + udev_settle (); + return out; /* caller frees */ }