Generated code for lvremove, vgremove, pvremove.
[libguestfs.git] / daemon / checksum.c
1 /* libguestfs - the guestfsd daemon
2  * Copyright (C) 2009 Red Hat Inc. 
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include "../src/guestfs_protocol.h"
27 #include "daemon.h"
28 #include "actions.h"
29
30 char *
31 do_checksum (const char *csumtype, const char *path)
32 {
33   const char *program;
34   char *buf;
35   char *out, *err;
36   int r, len;
37
38   NEED_ROOT (NULL);
39   ABS_PATH (path, NULL);
40
41   if (strcasecmp (csumtype, "crc") == 0)
42     program = "cksum";
43   else if (strcasecmp (csumtype, "md5") == 0)
44     program = "md5sum";
45   else if (strcasecmp (csumtype, "sha1") == 0)
46     program = "sha1sum";
47   else if (strcasecmp (csumtype, "sha224") == 0)
48     program = "sha224sum";
49   else if (strcasecmp (csumtype, "sha256") == 0)
50     program = "sha256sum";
51   else if (strcasecmp (csumtype, "sha384") == 0)
52     program = "sha384sum";
53   else if (strcasecmp (csumtype, "sha512") == 0)
54     program = "sha512sum";
55   else {
56     reply_with_error ("unknown checksum type, expecting crc|md5|sha1|sha224|sha256|sha384|sha512");
57     return NULL;
58   }
59
60   /* Make the path relative to /sysroot. */
61   len = strlen (path) + 9;
62   buf = malloc (len);
63   if (!buf) {
64     reply_with_perror ("malloc");
65     return NULL;
66   }
67   snprintf (buf, len, "/sysroot%s", path);
68
69   r = command (&out, &err, program, buf, NULL);
70   free (buf);
71   if (r == -1) {
72     reply_with_error ("%s: %s", program, err);
73     free (out);
74     free (err);
75     return NULL;
76   }
77
78   free (err);
79
80   /* Split it at the first whitespace. */
81   len = strcspn (out, " \t\n");
82   out[len] = '\0';
83
84   return out;                   /* Caller frees. */
85 }