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