Make /sysroot path configurable.
[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, 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   len = 2 * sysrootdirlen + 32;
87   cmd = malloc (len);
88   if (!cmd) {
89     reply_with_perror ("malloc");
90     free (sysrootdir);
91     return NULL;
92   }
93
94   strcpy (cmd, "find ");
95   shell_quote (cmd+5, len-5, sysrootdir);
96   free (sysrootdir);
97   strcat (cmd, " -print0");
98
99   if (verbose)
100     printf ("%s\n", cmd);
101
102   fp = popen (cmd, "r");
103   if (fp == NULL) {
104     reply_with_perror ("%s", cmd);
105     free (cmd);
106     return NULL;
107   }
108   free (cmd);
109
110   while ((r = input_to_nul (fp, str, PATH_MAX)) > 0) {
111     if (verbose)
112       printf ("find string: %s\n", str);
113
114     len = strlen (str);
115     if (len <= sysrootdirlen)
116       continue;
117
118     /* Remove the directory part of the path when adding it. */
119     if (add_string (&res, &size, &alloc, str + sysrootdirlen) == -1) {
120       pclose (fp);
121       return NULL;
122     }
123   }
124   if (pclose (fp) != 0) {
125     reply_with_perror ("pclose: find");
126     free_stringslen (res, size);
127     return NULL;
128   }
129
130   if (r == -1) {
131     free_stringslen (res, size);
132     return NULL;
133   }
134
135   if (add_string (&res, &size, &alloc, NULL) == -1)
136     return NULL;
137
138   sort_strings (res, size-1);
139
140   return res;                   /* caller frees */
141 }