daemon: Move 'exists', 'is-file' and 'is-dir' to separate file.
[libguestfs.git] / daemon / readdir.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 <dirent.h>
26
27 #include "daemon.h"
28 #include "actions.h"
29
30 guestfs_int_dirent_list *
31 do_readdir (const char *path)
32 {
33   guestfs_int_dirent_list *ret;
34   guestfs_int_dirent v;
35   DIR *dir;
36   struct dirent *d;
37   int i;
38
39   ret = malloc (sizeof *ret);
40   if (ret == NULL) {
41     reply_with_perror ("malloc");
42     return NULL;
43   }
44
45   ret->guestfs_int_dirent_list_len = 0;
46   ret->guestfs_int_dirent_list_val = NULL;
47
48   CHROOT_IN;
49   dir = opendir (path);
50   CHROOT_OUT;
51
52   if (dir == NULL) {
53     reply_with_perror ("opendir: %s", path);
54     free (ret);
55     return NULL;
56   }
57
58   i = 0;
59   while ((d = readdir (dir)) != NULL) {
60     guestfs_int_dirent *p;
61
62     p = realloc (ret->guestfs_int_dirent_list_val,
63                  sizeof (guestfs_int_dirent) * (i+1));
64     v.name = strdup (d->d_name);
65     if (!p || !v.name) {
66       reply_with_perror ("allocate");
67       free (ret->guestfs_int_dirent_list_val);
68       free (p);
69       free (v.name);
70       free (ret);
71       closedir (dir);
72       return NULL;
73     }
74     ret->guestfs_int_dirent_list_val = p;
75
76     v.ino = d->d_ino;
77 #ifdef HAVE_STRUCT_DIRENT_D_TYPE
78     switch (d->d_type) {
79     case DT_BLK: v.ftyp = 'b'; break;
80     case DT_CHR: v.ftyp = 'c'; break;
81     case DT_DIR: v.ftyp = 'd'; break;
82     case DT_FIFO: v.ftyp = 'f'; break;
83     case DT_LNK: v.ftyp = 'l'; break;
84     case DT_REG: v.ftyp = 'r'; break;
85     case DT_SOCK: v.ftyp = 's'; break;
86     case DT_UNKNOWN: v.ftyp = 'u'; break;
87     default: v.ftyp = '?'; break;
88     }
89 #else
90     v.ftyp = 'u';
91 #endif
92
93     ret->guestfs_int_dirent_list_val[i] = v;
94
95     i++;
96   }
97
98   ret->guestfs_int_dirent_list_len = i;
99
100   if (closedir (dir) == -1) {
101     reply_with_perror ("closedir");
102     free (ret->guestfs_int_dirent_list_val);
103     free (ret);
104     return NULL;
105   }
106
107   return ret;
108 }