daemon: Print failed path in stat command errors.
[libguestfs.git] / daemon / grep.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 #include <fcntl.h>
26
27 #include "../src/guestfs_protocol.h"
28 #include "daemon.h"
29 #include "actions.h"
30
31 static char **
32 grep (const char *prog, const char *flag, const char *regex, const char *path)
33 {
34   char *out, *err;
35   int fd, flags, r;
36   char **lines;
37
38   CHROOT_IN;
39   fd = open (path, O_RDONLY);
40   CHROOT_OUT;
41
42   if (fd == -1) {
43     reply_with_perror ("%s", path);
44     return NULL;
45   }
46
47   /* Note that grep returns an error if no match.  We want to
48    * suppress this error and return an empty list.
49    */
50   flags = COMMAND_FLAG_CHROOT_COPY_FILE_TO_STDIN | fd;
51   r = commandrf (&out, &err, flags, prog, flag, regex, NULL);
52   if (r == -1 || r > 1) {
53     reply_with_error ("%s %s %s: %s", prog, flag, regex, err);
54     free (out);
55     free (err);
56     return NULL;
57   }
58
59   free (err);
60
61   lines = split_lines (out);
62   free (out);
63   if (lines == NULL) return NULL;
64
65   return lines;
66 }
67
68 char **
69 do_grep (const char *regex, const char *path)
70 {
71   /* The "--" is not really needed, but it helps when we don't need a flag. */
72   return grep ("grep", "--", regex, path);
73 }
74
75 char **
76 do_egrep (const char *regex, const char *path)
77 {
78   return grep ("egrep", "--", regex, path);
79 }
80
81 char **
82 do_fgrep (const char *regex, const char *path)
83 {
84   return grep ("fgrep", "--", regex, path);
85 }
86
87 char **
88 do_grepi (const char *regex, const char *path)
89 {
90   return grep ("grep", "-i", regex, path);
91 }
92
93 char **
94 do_egrepi (const char *regex, const char *path)
95 {
96   return grep ("egrep", "-i", regex, path);
97 }
98
99 char **
100 do_fgrepi (const char *regex, const char *path)
101 {
102   return grep ("fgrep", "-i", regex, path);
103 }
104
105 char **
106 do_zgrep (const char *regex, const char *path)
107 {
108   return grep ("zgrep", "--", regex, path);
109 }
110
111 char **
112 do_zegrep (const char *regex, const char *path)
113 {
114   return grep ("zegrep", "--", regex, path);
115 }
116
117 char **
118 do_zfgrep (const char *regex, const char *path)
119 {
120   return grep ("zfgrep", "--", regex, path);
121 }
122
123 char **
124 do_zgrepi (const char *regex, const char *path)
125 {
126   return grep ("zgrep", "-i", regex, path);
127 }
128
129 char **
130 do_zegrepi (const char *regex, const char *path)
131 {
132   return grep ("zegrep", "-i", regex, path);
133 }
134
135 char **
136 do_zfgrepi (const char *regex, const char *path)
137 {
138   return grep ("zfgrep", "-i", regex, path);
139 }