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 (char *path)
33 guestfs_int_dirent_list *ret;
40 ABS_PATH (path, NULL);
42 ret = malloc (sizeof *ret);
44 reply_with_perror ("malloc");
48 ret->guestfs_int_dirent_list_len = 0;
49 ret->guestfs_int_dirent_list_val = NULL;
56 reply_with_perror ("opendir: %s", path);
62 while ((d = readdir (dir)) != NULL) {
63 guestfs_int_dirent *p;
65 p = realloc (ret->guestfs_int_dirent_list_val,
66 sizeof (guestfs_int_dirent) * (i+1));
67 v.name = strdup (d->d_name);
69 reply_with_perror ("allocate");
70 free (ret->guestfs_int_dirent_list_val);
77 ret->guestfs_int_dirent_list_val = p;
81 case DT_BLK: v.ftyp = 'b'; break;
82 case DT_CHR: v.ftyp = 'c'; break;
83 case DT_DIR: v.ftyp = 'd'; break;
84 case DT_FIFO: v.ftyp = 'f'; break;
85 case DT_LNK: v.ftyp = 'l'; break;
86 case DT_REG: v.ftyp = 'r'; break;
87 case DT_SOCK: v.ftyp = 's'; break;
88 case DT_UNKNOWN: v.ftyp = 'u'; break;
89 default: v.ftyp = '?'; break;
92 ret->guestfs_int_dirent_list_val[i] = v;
97 ret->guestfs_int_dirent_list_len = i;
99 if (closedir (dir) == -1) {
100 reply_with_perror ("closedir");
101 free (ret->guestfs_int_dirent_list_val);