On RHEL 5, 'file' command prints 'AMD x86-64' for 'x86-64' arch.
[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 (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   NEED_ROOT (NULL);
40   ABS_PATH (path, NULL);
41
42   ret = malloc (sizeof *ret);
43   if (ret == NULL) {
44     reply_with_perror ("malloc");
45     return NULL;
46   }
47
48   ret->guestfs_int_dirent_list_len = 0;
49   ret->guestfs_int_dirent_list_val = NULL;
50
51   CHROOT_IN;
52   dir = opendir (path);
53   CHROOT_OUT;
54
55   if (dir == NULL) {
56     reply_with_perror ("opendir: %s", path);
57     free (ret);
58     return NULL;
59   }
60
61   i = 0;
62   while ((d = readdir (dir)) != NULL) {
63     guestfs_int_dirent *p;
64
65     p = realloc (ret->guestfs_int_dirent_list_val,
66                  sizeof (guestfs_int_dirent) * (i+1));
67     v.name = strdup (d->d_name);
68     if (!p || !v.name) {
69       reply_with_perror ("allocate");
70       free (ret->guestfs_int_dirent_list_val);
71       free (p);
72       free (v.name);
73       free (ret);
74       closedir (dir);
75       return NULL;
76     }
77     ret->guestfs_int_dirent_list_val = p;
78
79     v.ino = d->d_ino;
80     switch (d->d_type) {
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;
90     }
91
92     ret->guestfs_int_dirent_list_val[i] = v;
93
94     i++;
95   }
96
97   ret->guestfs_int_dirent_list_len = i;
98
99   if (closedir (dir) == -1) {
100     reply_with_perror ("closedir");
101     free (ret->guestfs_int_dirent_list_val);
102     free (ret);
103     return NULL;
104   }
105
106   return ret;
107 }