1 /* libguestfs - the guestfsd daemon
2 * Copyright (C) 2009 Red Hat Inc.
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.
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.
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.
30 guestfs_int_dirent_list *
31 do_readdir (const char *path)
33 guestfs_int_dirent_list *ret;
39 ret = malloc (sizeof *ret);
41 reply_with_perror ("malloc");
45 ret->guestfs_int_dirent_list_len = 0;
46 ret->guestfs_int_dirent_list_val = NULL;
53 reply_with_perror ("opendir: %s", path);
59 while ((d = readdir (dir)) != NULL) {
60 guestfs_int_dirent *p;
62 p = realloc (ret->guestfs_int_dirent_list_val,
63 sizeof (guestfs_int_dirent) * (i+1));
64 v.name = strdup (d->d_name);
66 reply_with_perror ("allocate");
67 free (ret->guestfs_int_dirent_list_val);
74 ret->guestfs_int_dirent_list_val = p;
78 case DT_BLK: v.ftyp = 'b'; break;
79 case DT_CHR: v.ftyp = 'c'; break;
80 case DT_DIR: v.ftyp = 'd'; break;
81 case DT_FIFO: v.ftyp = 'f'; break;
82 case DT_LNK: v.ftyp = 'l'; break;
83 case DT_REG: v.ftyp = 'r'; break;
84 case DT_SOCK: v.ftyp = 's'; break;
85 case DT_UNKNOWN: v.ftyp = 'u'; break;
86 default: v.ftyp = '?'; break;
89 ret->guestfs_int_dirent_list_val[i] = v;
94 ret->guestfs_int_dirent_list_len = i;
96 if (closedir (dir) == -1) {
97 reply_with_perror ("closedir");
98 free (ret->guestfs_int_dirent_list_val);