* src/generator.ml: Change all `String "device"' to `Device "device"'.
[libguestfs.git] / daemon / find.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 #include <sys/stat.h>
27
28 #include "../src/guestfs_protocol.h"
29 #include "daemon.h"
30 #include "actions.h"
31
32 static int
33 input_to_nul (FILE *fp, char *buf, int maxlen)
34 {
35   int i = 0, c;
36
37   while (i < maxlen) {
38     c = fgetc (fp);
39     if (c == EOF)
40       return 0;
41     buf[i++] = c;
42     if (c == '\0')
43       return i;
44   }
45
46   reply_with_error ("input_to_nul: input string too long");
47   return -1;
48 }
49
50 char **
51 do_find (char *dir)
52 {
53   struct stat statbuf;
54   int r, len, sysrootdirlen;
55   char *cmd;
56   FILE *fp;
57   char **res = NULL;
58   int size = 0, alloc = 0;
59   char *sysrootdir;
60   char str[PATH_MAX];
61
62   NEED_ROOT (NULL);
63   ABS_PATH (dir, return NULL);
64
65   sysrootdir = sysroot_path (dir);
66   if (!sysrootdir) {
67     reply_with_perror ("malloc");
68     return NULL;
69   }
70
71   r = stat (sysrootdir, &statbuf);
72   if (r == -1) {
73     reply_with_perror ("%s", dir);
74     free (sysrootdir);
75     return NULL;
76   }
77   if (!S_ISDIR (statbuf.st_mode)) {
78     reply_with_error ("%s: not a directory", dir);
79     free (sysrootdir);
80     return NULL;
81   }
82
83   sysrootdirlen = strlen (sysrootdir);
84
85   /* Assemble the external find command. */
86   if (asprintf_nowarn (&cmd, "find %Q -print0", sysrootdir) == -1) {
87     reply_with_perror ("malloc");
88     free (sysrootdir);
89     return NULL;
90   }
91
92   if (verbose)
93     fprintf (stderr, "%s\n", cmd);
94
95   fp = popen (cmd, "r");
96   if (fp == NULL) {
97     reply_with_perror ("%s", cmd);
98     free (cmd);
99     return NULL;
100   }
101   free (cmd);
102
103   while ((r = input_to_nul (fp, str, PATH_MAX)) > 0) {
104     if (verbose)
105       printf ("find string: %s\n", str);
106
107     len = strlen (str);
108     if (len <= sysrootdirlen)
109       continue;
110
111     /* Remove the directory part of the path when adding it. */
112     if (add_string (&res, &size, &alloc, str + sysrootdirlen) == -1) {
113       pclose (fp);
114       return NULL;
115     }
116   }
117   if (pclose (fp) != 0) {
118     reply_with_perror ("pclose: find");
119     free_stringslen (res, size);
120     return NULL;
121   }
122
123   if (r == -1) {
124     free_stringslen (res, size);
125     return NULL;
126   }
127
128   if (add_string (&res, &size, &alloc, NULL) == -1)
129     return NULL;
130
131   sort_strings (res, size-1);
132
133   return res;                   /* caller frees */
134 }