7ceeafafcd35dec922f6d5e96dd6e7044158cc02
[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[PATH_MAX];
60   char str[PATH_MAX];
61
62   NEED_ROOT (NULL);
63   ABS_PATH (dir, NULL);
64
65   snprintf (sysrootdir, sizeof sysrootdir, "/sysroot%s", dir);
66
67   r = stat (sysrootdir, &statbuf);
68   if (r == -1) {
69     reply_with_perror ("%s", dir);
70     return NULL;
71   }
72   if (!S_ISDIR (statbuf.st_mode)) {
73     reply_with_error ("%s: not a directory", dir);
74     return NULL;
75   }
76
77   sysrootdirlen = strlen (sysrootdir);
78
79   /* Assemble the external find command. */
80   len = 2 * sysrootdirlen + 32;
81   cmd = malloc (len);
82   if (!cmd) {
83     reply_with_perror ("malloc");
84     return NULL;
85   }
86
87   strcpy (cmd, "find ");
88   shell_quote (cmd+5, len-5, sysrootdir);
89   strcat (cmd, " -print0");
90
91   if (verbose)
92     printf ("%s\n", cmd);
93
94   fp = popen (cmd, "r");
95   if (fp == NULL) {
96     reply_with_perror ("%s", cmd);
97     free (cmd);
98     return NULL;
99   }
100   free (cmd);
101
102   while ((r = input_to_nul (fp, str, PATH_MAX)) > 0) {
103     if (verbose)
104       printf ("find string: %s\n", str);
105
106     len = strlen (str);
107     if (len <= sysrootdirlen)
108       continue;
109
110     /* Remove the directory part of the path when adding it. */
111     if (add_string (&res, &size, &alloc, str + sysrootdirlen) == -1) {
112       pclose (fp);
113       return NULL;
114     }
115   }
116   if (pclose (fp) != 0) {
117     reply_with_perror ("pclose: find");
118     free_stringslen (res, size);
119     return NULL;
120   }
121
122   if (r == -1) {
123     free_stringslen (res, size);
124     return NULL;
125   }
126
127   if (add_string (&res, &size, &alloc, NULL) == -1)
128     return NULL;
129
130   sort_strings (res, size-1);
131
132   return res;                   /* caller frees */
133 }